Skip to content

Commit

Permalink
test: fix proxy controller test.
Browse files Browse the repository at this point in the history
  • Loading branch information
hezhengxu2018 committed Jun 1, 2024
1 parent 8e7cd05 commit a849046
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 34 deletions.
9 changes: 5 additions & 4 deletions app/core/service/ProxyCacheService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export class ProxyCacheService extends AbstractService {
await this.taskService.finishTask(task, TaskState.Success, logs.join('\n'));
}

private async getSourceManifestAndCache<T extends DIST_NAMES>(fullname:string, fileType: T, versionOrTag?:string): Promise<GetSourceManifestAndCacheReturnType<T>> {
async getSourceManifestAndCache<T extends DIST_NAMES>(fullname:string, fileType: T, versionOrTag?:string): Promise<GetSourceManifestAndCacheReturnType<T>> {
let responseResult;
switch (fileType) {
case DIST_NAMES.FULL_MANIFESTS:
Expand Down Expand Up @@ -203,7 +203,8 @@ export class ProxyCacheService extends AbstractService {
const version = manifest.version;
storeKey = `/${PROXY_CACHE_DIR_NAME}/${fullname}/${version}/${fileType}`;
}
await this.nfsAdapter.uploadFile(storeKey, JSON.stringify(manifest));
const nfsBytes = Buffer.from(JSON.stringify(manifest));
await this.nfsAdapter.uploadBytes(storeKey, nfsBytes);
return manifest;
}

Expand Down Expand Up @@ -243,11 +244,11 @@ export class ProxyCacheService extends AbstractService {
return await this.getProxyResponse({ url, headers: { accept: ABBREVIATED_META_TYPE } }, { dataType: 'json' });
}
private async getUpstreamPackageVersionManifest(fullname: string, versionOrTag: string): Promise<HttpClientResponse> {
const url = `/${encodeURIComponent(fullname + '/' + versionOrTag)}?t=${Date.now()}&cache=0`;
const url = `/${encodeURIComponent(fullname)}/${encodeURIComponent(versionOrTag)}?t=${Date.now()}&cache=0`;
return await this.getProxyResponse({ url }, { dataType: 'json' });
}
private async getUpstreamAbbreviatedPackageVersionManifest(fullname: string, versionOrTag: string): Promise<HttpClientResponse> {
const url = `/${encodeURIComponent(fullname + '/' + versionOrTag)}?t=${Date.now()}&cache=0`;
const url = `/${encodeURIComponent(fullname)}/${encodeURIComponent(versionOrTag)}?t=${Date.now()}&cache=0`;
return await this.getProxyResponse({ url, headers: { accept: ABBREVIATED_META_TYPE } }, { dataType: 'json' });
}

Expand Down
50 changes: 22 additions & 28 deletions test/core/service/ProxyCacheService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,23 @@ import { TestUtil } from '../../TestUtil';
import { ProxyCacheService } from '../../../app/core/service/ProxyCacheService';
import { ProxyCacheRepository } from '../../../app/repository/ProxyCacheRepository';
import { DIST_NAMES } from '../../../app/core/entity/Package';
import { PROXY_CACHE_DIR_NAME } from '../../../app/common/constants';
import { NPMRegistry } from '../../../app/common/adapter/NPMRegistry';
import { NFSAdapter } from '../../../app/common/adapter/NFSAdapter';
import { ProxyCache } from '../../../app/core/entity/ProxyCache';
import { TaskService } from '../../../app/core/service/TaskService';

describe('test/core/service/ProxyCacheService/index.test.ts', () => {
let proxyCacheService: ProxyCacheService;
let npmRegistry: NPMRegistry;
let proxyCacheRepository: ProxyCacheRepository;

beforeEach(async () => {
proxyCacheService = await app.getEggObject(ProxyCacheService);
npmRegistry = await app.getEggObject(NPMRegistry);
proxyCacheRepository = await app.getEggObject(ProxyCacheRepository);
});

describe('getPackageManifest()', () => {
it('should invoke getSourceManifestAndCache first.', async () => {
mock(proxyCacheService, 'getSourceManifestAndCache', async () => {
return {
manifest: { name: 'mock info' },
};
return { name: 'mock info' };
});
const manifest = await proxyCacheService.getPackageManifest(
'foo',
Expand All @@ -38,10 +32,7 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => {
it('should read data from nfs when cached.', async () => {
const nfsAdapter = await app.getEggObject(NFSAdapter);
mock(proxyCacheService, 'getSourceManifestAndCache', async () => {
return {
storeKey: `/${PROXY_CACHE_DIR_NAME}/foo/${DIST_NAMES.FULL_MANIFESTS}`,
manifest: { name: 'foo remote mock info' },
};
return { name: 'foo remote mock info' };
});
await proxyCacheRepository.saveProxyCache(
ProxyCache.create({
Expand All @@ -63,10 +54,7 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => {
describe('getPackageVersionManifest()', () => {
it('should invoke getSourceManifestAndCache first.', async () => {
mock(proxyCacheService, 'getSourceManifestAndCache', async () => {
return {
storeKey: `/${PROXY_CACHE_DIR_NAME}/foobar/1.0.0/${DIST_NAMES.MANIFEST}`,
manifest: { name: 'mock package version info' },
};
return { name: 'mock package version info' };
});
const manifest = await proxyCacheService.getPackageVersionManifest(
'foo',
Expand All @@ -79,10 +67,7 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => {
it('should read data from nfs when cached.', async () => {
const nfsAdapter = await app.getEggObject(NFSAdapter);
mock(proxyCacheService, 'getSourceManifestAndCache', async () => {
return {
storeKey: `/${PROXY_CACHE_DIR_NAME}/foo/1.0.0/${DIST_NAMES.FULL_MANIFESTS}`,
manifest: { name: 'foo remote mock info' },
};
return { name: 'foo remote mock info' };
});
await proxyCacheRepository.saveProxyCache(
ProxyCache.create({
Expand All @@ -103,6 +88,15 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => {
});

it('should get correct verison via tag and cache the pkg manifest', async () => {
const data = await TestUtil.readJSONFile(
TestUtil.getFixtures('registry.npmjs.org/foobar/1.0.0/package.json'),
);
mock(proxyCacheService, 'getUpstreamPackageVersionManifest', async () => {
return {
status: 200,
data,
};
});
// get manifest by http
const pkgVersionManifest =
await proxyCacheService.getPackageVersionManifest(
Expand All @@ -111,7 +105,7 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => {
'latest',
);
assert(pkgVersionManifest);
assert.equal(pkgVersionManifest.version, '1.1.0');
assert.equal(pkgVersionManifest.version, '1.0.0');
const pkgManifest = proxyCacheRepository.findProxyCache(
'foobar',
DIST_NAMES.ABBREVIATED_MANIFESTS,
Expand All @@ -125,13 +119,13 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => {
const data = await TestUtil.readJSONFile(
TestUtil.getFixtures('registry.npmjs.org/foobar.json'),
);
mock(npmRegistry, 'getFullManifests', async () => {
mock(proxyCacheService, 'getUpstreamFullManifests', async () => {
return {
status: 200,
data,
};
});
const { manifest } = await proxyCacheService.getSourceManifestAndCache(
const manifest = await proxyCacheService.getSourceManifestAndCache(
'foobar',
DIST_NAMES.FULL_MANIFESTS,
);
Expand All @@ -145,13 +139,13 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => {
const data = await TestUtil.readJSONFile(
TestUtil.getFixtures('registry.npmjs.org/abbreviated_foobar.json'),
);
mock(npmRegistry, 'getAbbreviatedManifests', async () => {
mock(proxyCacheService, 'getUpstreamAbbreviatedManifests', async () => {
return {
status: 200,
data,
};
});
const { manifest } = await proxyCacheService.getSourceManifestAndCache(
const manifest = await proxyCacheService.getSourceManifestAndCache(
'foobar',
DIST_NAMES.ABBREVIATED_MANIFESTS,
);
Expand All @@ -165,13 +159,13 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => {
const data = await TestUtil.readJSONFile(
TestUtil.getFixtures('registry.npmjs.org/foobar/1.0.0/package.json'),
);
mock(npmRegistry, 'getPackageVersionManifest', async () => {
mock(proxyCacheService, 'getUpstreamPackageVersionManifest', async () => {
return {
status: 200,
data,
};
});
const { manifest } = await proxyCacheService.getSourceManifestAndCache(
const manifest = await proxyCacheService.getSourceManifestAndCache(
'foobar',
DIST_NAMES.MANIFEST,
'1.0.0',
Expand All @@ -186,13 +180,13 @@ describe('test/core/service/ProxyCacheService/index.test.ts', () => {
'registry.npmjs.org/foobar/1.0.0/abbreviated.json',
),
);
mock(npmRegistry, 'getAbbreviatedPackageVersionManifest', async () => {
mock(proxyCacheService, 'getUpstreamAbbreviatedPackageVersionManifest', async () => {
return {
status: 200,
data,
};
});
const { manifest } = await proxyCacheService.getSourceManifestAndCache(
const manifest = await proxyCacheService.getSourceManifestAndCache(
'foobar',
DIST_NAMES.ABBREVIATED,
'1.0.0',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ 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 @@ -303,11 +302,11 @@ 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');
console.log(res.status);
assert(res.status === 200);
// run in background
await setTimeout(1000);
Expand Down

0 comments on commit a849046

Please sign in to comment.