Skip to content

Commit

Permalink
test: fix download package version tar controller.
Browse files Browse the repository at this point in the history
  • Loading branch information
hezhengxu2018 committed Jul 14, 2024
1 parent 176d336 commit 8e64b36
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
18 changes: 9 additions & 9 deletions app/port/controller/package/ShowPackageVersionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { ProxyCacheService } from '../../../core/service/ProxyCacheService';
import { Spec } from '../../../port/typebox';
import { ABBREVIATED_META_TYPE, SyncMode } from '../../../common/constants';
import { DIST_NAMES } from '../../../core/entity/Package';
import { NotFoundError } from 'egg-errors';

@HTTPController()
export class ShowPackageVersionController extends AbstractController {
Expand Down Expand Up @@ -53,6 +54,11 @@ export class ShowPackageVersionController extends AbstractController {
);
const allowSync = this.getAllowSync(ctx);

if (blockReason) {
this.setCDNHeaders(ctx);
throw this.createPackageBlockError(blockReason, fullname, versionSpec);
}

if (!pkg || !manifest) {
if (this.config.cnpmcore.syncMode === SyncMode.proxy) {
const fileType = isFullManifests
Expand All @@ -65,18 +71,12 @@ export class ShowPackageVersionController extends AbstractController {
);
}

if (!manifest) {
throw this.createPackageNotFoundErrorWithRedirect(fullname, versionSpec, allowSync);
}

if (!pkg) {
throw this.createPackageNotFoundErrorWithRedirect(fullname, undefined, allowSync);
}
}

if (blockReason) {
this.setCDNHeaders(ctx);
throw this.createPackageBlockError(blockReason, fullname, versionSpec);
if (!manifest) {
throw new NotFoundError(`${fullname}@${versionSpec} not found`);
}
}

this.setCDNHeaders(ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ describe('test/port/controller/package/DownloadPackageVersionTarController.test.
it('should not create sync task when package version tgz not exists and syncNotFound=false', async () => {
mock(app.config.cnpmcore, 'syncMode', 'exist');
mock(app.config.cnpmcore, 'syncNotFound', false);
mock(app.config.cnpmcore, 'redirectNotFound', false);
const res = await app.httpRequest()
.get('/lodash/-/lodash-1.404.404.tgz')
.set('user-agent', publisher.ua + ' node/16.0.0')
Expand All @@ -302,13 +303,14 @@ describe('test/port/controller/package/DownloadPackageVersionTarController.test.

it('should create sync specific version task when package version tgz not found in proxy mode ', async () => {
mock(app.config.cnpmcore, 'syncMode', SyncMode.proxy);
mock(app.config.cnpmcore, 'redirectNotFound', false);
const res = await app.httpRequest()
.get('/foobar/-/foobar-1.0.0.tgz')
.set('user-agent', publisher.ua + ' node/16.0.0')
.set('Accept', 'application/vnd.npm.install-v1+json');
assert(res.status === 200);
// run in background
await setTimeout(1000);
await setTimeout(500);
app.expectLog('[DownloadPackageVersionTarController.createSyncTask:success]');
});

Expand Down

0 comments on commit 8e64b36

Please sign in to comment.