Skip to content

Commit

Permalink
test: Koa.js handling of node.js set error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
vijaykrishnavanshi committed May 17, 2020
1 parent 4995156 commit ce9133a
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions test/context/onerror.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,48 @@ describe('ctx.onerror(err)', () => {
.expect(200, () => {});
});

it('should set status specified in the error using statusCode', () => {
const app = new Koa();

app.use((ctx, next) => {
ctx.body = 'something else';
const err = new Error('Not found');
err.statusCode = 404;
throw err;
});

const server = app.listen();

return request(server)
.get('/')
.expect(404)
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect('Not Found');
});

describe('when invalid err.statusCode', () => {
describe('not number', () => {
it('should respond 500', () => {
const app = new Koa();

app.use((ctx, next) => {
ctx.body = 'something else';
const err = new Error('some error');
err.statusCode = 'notnumber';
throw err;
});

const server = app.listen();

return request(server)
.get('/')
.expect(500)
.expect('Content-Type', 'text/plain; charset=utf-8')
.expect('Internal Server Error');
});
});
});

describe('when invalid err.status', () => {
describe('not number', () => {
it('should respond 500', () => {
Expand Down

0 comments on commit ce9133a

Please sign in to comment.