Skip to content

Commit

Permalink
feat: misc router
Browse files Browse the repository at this point in the history
  • Loading branch information
elrrrrrrr committed Jul 17, 2023
1 parent 8a9412d commit f5eb7b2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
26 changes: 26 additions & 0 deletions app/port/controller/HomeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ import {
} from '@eggjs/tegg';
import { AbstractController } from './AbstractController';
import { CacheService, DownloadInfo, UpstreamRegistryInfo } from '../../core/service/CacheService';
import { NotFoundError, NotImplementedError } from 'egg-errors';

const startTime = new Date();

const NOT_IMPLEMENTED = ['/-/npm/v1/security/audits/quick', '/-/npm/v1/security/advisories/bulk'];

Check failure on line 16 in app/port/controller/HomeController.ts

View workflow job for this annotation

GitHub Actions / test-mysql57-fs-nfs (16, ubuntu-latest)

A space is required after '['

Check failure on line 16 in app/port/controller/HomeController.ts

View workflow job for this annotation

GitHub Actions / test-mysql57-fs-nfs (16, ubuntu-latest)

A space is required before ']'

Check failure on line 16 in app/port/controller/HomeController.ts

View workflow job for this annotation

GitHub Actions / test-mysql57-fs-nfs (18, ubuntu-latest)

A space is required after '['

Check failure on line 16 in app/port/controller/HomeController.ts

View workflow job for this annotation

GitHub Actions / test-mysql57-fs-nfs (18, ubuntu-latest)

A space is required before ']'

Check failure on line 16 in app/port/controller/HomeController.ts

View workflow job for this annotation

GitHub Actions / test-mysql57-fs-nfs (20, ubuntu-latest)

A space is required after '['

Check failure on line 16 in app/port/controller/HomeController.ts

View workflow job for this annotation

GitHub Actions / test-mysql57-fs-nfs (20, ubuntu-latest)

A space is required before ']'

// registry 站点信息数据 SiteTotalData
// SiteEnvInfo: 环境、运行时相关信息,实时查询
// UpstreamInfo: 上游信息,实时查询
Expand Down Expand Up @@ -97,4 +100,27 @@ export class HomeController extends AbstractController {
use: performance.now() - ctx.performanceStarttime!,
};
}

@HTTPMethod({
path: '/*',
method: HTTPMethodEnum.GET,
priority: -Infinity,
})
async notFound(@Context() ctx: EggContext) {
throw new NotFoundError(`${ctx.path} not found`);
}

@HTTPMethod({
path: '/*',
method: HTTPMethodEnum.POST,
priority: -Infinity,
})
async misc(@Context() ctx: EggContext) {
const {path} = ctx;

Check failure on line 119 in app/port/controller/HomeController.ts

View workflow job for this annotation

GitHub Actions / test-mysql57-fs-nfs (16, ubuntu-latest)

A space is required after '{'

Check failure on line 119 in app/port/controller/HomeController.ts

View workflow job for this annotation

GitHub Actions / test-mysql57-fs-nfs (16, ubuntu-latest)

A space is required before '}'

Check failure on line 119 in app/port/controller/HomeController.ts

View workflow job for this annotation

GitHub Actions / test-mysql57-fs-nfs (18, ubuntu-latest)

A space is required after '{'

Check failure on line 119 in app/port/controller/HomeController.ts

View workflow job for this annotation

GitHub Actions / test-mysql57-fs-nfs (18, ubuntu-latest)

A space is required before '}'

Check failure on line 119 in app/port/controller/HomeController.ts

View workflow job for this annotation

GitHub Actions / test-mysql57-fs-nfs (20, ubuntu-latest)

A space is required after '{'

Check failure on line 119 in app/port/controller/HomeController.ts

View workflow job for this annotation

GitHub Actions / test-mysql57-fs-nfs (20, ubuntu-latest)

A space is required before '}'
if (NOT_IMPLEMENTED.includes(path)) {
throw new NotImplementedError(`${ctx.path} not implemented yet`);
}

throw new NotFoundError(`${ctx.path} not found`);
}
}
29 changes: 29 additions & 0 deletions test/port/controller/HomeController/misc.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { app, assert } from 'egg-mock/bootstrap';

describe('test/port/controller/HomeController/misc.test.ts', () => {
describe('[POST /*] misc()', () => {
it('should 501', async () => {
const res = await app.httpRequest()
.post('/-/npm/v1/security/audits/quick')
.expect(501);
assert.equal(res.body.error, '[NOT_IMPLEMENTED] /-/npm/v1/security/audits/quick not implemented yet');
});

it('should 404', async () => {
const res = await app.httpRequest()
.post('/-/greed/is/good')
.expect(404);
assert.equal(res.body.error, '[NOT_FOUND] /-/greed/is/good not found');
});
});

describe('[GET /*] notFound()', () => {
it('should 404', async () => {
const res = await app.httpRequest()
.get('/-/greed/is/good')
.expect(404);
assert.equal(res.body.error, '[NOT_FOUND] /-/greed/is/good not found');
});
});

});

0 comments on commit f5eb7b2

Please sign in to comment.