Skip to content

Commit

Permalink
fix: not exists binary should return 404 (#377)
Browse files Browse the repository at this point in the history
closes #376
  • Loading branch information
fengmk2 authored Jan 18, 2023
1 parent 7952e33 commit 0cc348d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app/port/controller/BinarySyncController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ export class BinarySyncController extends AbstractController {
method: HTTPMethodEnum.GET,
})
async showBinary(@Context() ctx: EggContext, @HTTPParam() binaryName: string, @HTTPParam() subpath: string) {
if (!binaries[binaryName]) {
throw new NotFoundError(`Binary "${binaryName}" not found`);
}
subpath = subpath || '/';
if (subpath === '/') {
const items = await this.binarySyncerService.listRootBinaries(binaryName);
Expand Down
23 changes: 22 additions & 1 deletion test/port/controller/BinarySyncController/showBinary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,27 @@ describe('test/port/controller/BinarySyncController/showBinary.test.ts', () => {
url: 'http://localhost:7001/-/binary/nwjs/',
}]);
});

it('should 404 when binary not exists', async () => {
let res = await app.httpRequest()
.get('/-/binary/node-canvas-prebuilt-not-exists/');
assert.equal(res.status, 404);
assert.equal(res.headers['content-type'], 'application/json; charset=utf-8');
assert.deepEqual(res.body, { error: '[NOT_FOUND] Binary "node-canvas-prebuilt-not-exists" not found' });

res = await app.httpRequest()
.get('/-/binary/node-canvas-prebuilt-not-exists/v2.6.1/');
assert.equal(res.status, 404);
assert.equal(res.headers['content-type'], 'application/json; charset=utf-8');
assert.deepEqual(res.body, { error: '[NOT_FOUND] Binary "node-canvas-prebuilt-not-exists" not found' });

res = await app.httpRequest()
.get('/-/binary/node-canvas-prebuilt-not-exists/v2.6.1/foo.json');
assert.equal(res.status, 404);
assert.equal(res.headers['content-type'], 'application/json; charset=utf-8');
assert.deepEqual(res.body, { error: '[NOT_FOUND] Binary "node-canvas-prebuilt-not-exists" not found' });
});

it('should show valid sub dirs', async () => {
await binaryRepository.saveBinary(Binary.create({
category: 'node-canvas-prebuilt',
Expand All @@ -103,8 +124,8 @@ describe('test/port/controller/BinarySyncController/showBinary.test.ts', () => {
type: 'dir',
url: 'http://localhost:7001/-/binary/node-canvas-prebuilt/v2.6.1/',
}]);

});

it('should show valid files', async () => {
await binaryRepository.saveBinary(Binary.create({
category: 'node-canvas-prebuilt',
Expand Down

0 comments on commit 0cc348d

Please sign in to comment.