From 88b4f78d56d8e66f88c4dc7a21d2fde0b74893eb Mon Sep 17 00:00:00 2001 From: ZhengJin Date: Fri, 1 Nov 2024 20:32:34 +0800 Subject: [PATCH] test: add unit test --- ...ownloadPackageVersionTarController.test.ts | 51 ++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/test/port/controller/package/DownloadPackageVersionTarController.test.ts b/test/port/controller/package/DownloadPackageVersionTarController.test.ts index 4b0e182d..cece06b8 100644 --- a/test/port/controller/package/DownloadPackageVersionTarController.test.ts +++ b/test/port/controller/package/DownloadPackageVersionTarController.test.ts @@ -13,8 +13,9 @@ describe('test/port/controller/package/DownloadPackageVersionTarController.test. nfsClientAdapter = await app.getEggObject(NFSClientAdapter); }); - const scopedName = '@cnpm/testmodule-download-version-tar'; + const scope = '@cnpm'; const name = 'testmodule-download-version-tar'; + const scopedName = `${scope}/${name}`; beforeEach(async () => { mock(app.config.cnpmcore, 'allowPublishNonScopePackage', true); let pkg = await TestUtil.getFullPackage({ name, version: '1.0.0' }); @@ -367,4 +368,52 @@ describe('test/port/controller/package/DownloadPackageVersionTarController.test. assert(res.body.error === `[NOT_FOUND] "${name}-1.0.0.tgz" not found`); }); }); + + describe('[GET /:fullname/-/:scope/:name-:version.tgz] download()', () => { + it('should download a version tar redirect to mock cdn success', async () => { + mock(nfsClientAdapter, 'url', async (storeKey: string) => { + // console.log('call url: ', storeKey); + return `https://cdn.mock.com${storeKey}`; + }); + let res = await app.httpRequest() + .get(`/${name}/-/${scope}/${name}-1.0.0.tgz`); + assert(res.status === 302); + assert(res.headers.location === `https://cdn.mock.com/packages/${name}/1.0.0/${name}-1.0.0.tgz`); + res = await app.httpRequest() + .get(`/${scopedName}/-/${scope}/${scopedName}-1.0.0.tgz`); + assert(res.status === 302); + assert(res.headers.location === `https://cdn.mock.com/packages/${scopedName}/1.0.0/${name}-1.0.0.tgz`); + }); + + it('should download a version tar with streaming success', async () => { + mock(nfsClientAdapter, 'url', 'not-function'); + const res = await app.httpRequest() + .get(`/${name}/-/${scope}/${name}-1.0.0.tgz`); + assert(res.status === 200); + assert(res.headers['content-type'] === 'application/octet-stream'); + assert(res.headers['content-disposition'] === `attachment; filename="${name}-1.0.0.tgz"`); + + await app.httpRequest() + .get(`/${scopedName}/-/${scope}/${scopedName}-1.0.0.tgz`); + assert(res.status === 200); + assert(res.headers['content-type'] === 'application/octet-stream'); + assert(res.headers['content-disposition'] === `attachment; filename="${name}-1.0.0.tgz"`); + }); + + it('should mock getDownloadUrlOrStream return undefined', async () => { + mock(nfsClientAdapter, 'createDownloadStream', async () => { + return undefined; + }); + if (process.env.CNPMCORE_NFS_TYPE === 'oss') { + mock(nfsClientAdapter, 'url', async () => { + return undefined; + }); + } + const res = await app.httpRequest() + .get(`/${name}/-/${scope}/${name}-1.0.0.tgz`); + assert(res.status === 404); + assert(res.headers['content-type'] === 'application/json; charset=utf-8'); + assert(res.body.error === `[NOT_FOUND] "${name}-1.0.0.tgz" not found`); + }); + }); });