koa-lite/test/context/throw.js

170 lines
3.7 KiB
JavaScript
Raw Normal View History

'use strict';
const context = require('../helpers/context');
const assert = require('assert');
describe('ctx.throw(msg)', () => {
2017-05-11 03:30:32 +00:00
it('should set .status to 500', () => {
const ctx = context();
try {
ctx.throw('boom');
} catch (err) {
2017-05-11 03:30:32 +00:00
assert.equal(err.status, 500);
assert.equal(err.expose, false);
}
2015-10-12 20:36:41 +00:00
});
});
describe('ctx.throw(err)', () => {
2017-05-11 03:30:32 +00:00
it('should set .status to 500', () => {
const ctx = context();
const err = new Error('test');
2014-01-04 08:23:39 +00:00
try {
ctx.throw(err);
} catch (err) {
2017-05-11 03:30:32 +00:00
assert.equal(err.status, 500);
assert.equal(err.message, 'test');
assert.equal(err.expose, false);
2014-01-04 08:23:39 +00:00
}
2015-10-12 20:36:41 +00:00
});
});
2014-01-04 08:23:39 +00:00
describe('ctx.throw(status, err)', () => {
2017-05-11 03:30:32 +00:00
it('should throw the error and set .status', () => {
const ctx = context();
const error = new Error('test');
2014-01-04 08:23:39 +00:00
try {
ctx.throw(422, error);
} catch (err) {
2017-05-11 03:30:32 +00:00
assert.equal(err.status, 422);
assert.equal(err.message, 'test');
assert.equal(err.expose, true);
2014-01-04 08:23:39 +00:00
}
2015-10-12 20:36:41 +00:00
});
});
2014-01-04 08:23:39 +00:00
describe('ctx.throw(status, msg)', () => {
2017-05-11 03:30:32 +00:00
it('should throw an error', () => {
const ctx = context();
try {
ctx.throw(400, 'name required');
} catch (err) {
2017-05-11 03:30:32 +00:00
assert.equal(err.message, 'name required');
assert.equal(400, err.status);
assert.equal(true, err.expose);
}
2015-10-12 20:36:41 +00:00
});
});
describe('ctx.throw(status)', () => {
2017-05-11 03:30:32 +00:00
it('should throw an error', () => {
const ctx = context();
try {
ctx.throw(400);
} catch (err) {
2017-05-11 03:30:32 +00:00
assert.equal(err.message, 'Bad Request');
assert.equal(err.status, 400);
assert.equal(err.expose, true);
}
2015-10-12 20:36:41 +00:00
});
describe('when not valid status', () => {
2017-05-11 03:30:32 +00:00
it('should not expose', () => {
const ctx = context();
try {
const err = new Error('some error');
err.status = -1;
ctx.throw(err);
2015-10-12 20:36:41 +00:00
} catch (err) {
2017-05-11 03:30:32 +00:00
assert.equal(err.message, 'some error');
assert.equal(err.expose, false);
}
2015-10-12 20:36:41 +00:00
});
});
});
describe('ctx.throw(status, msg, props)', () => {
2017-05-11 03:30:32 +00:00
it('should mixin props', () => {
const ctx = context();
try {
ctx.throw(400, 'msg', { prop: true });
} catch (err) {
2017-05-11 03:30:32 +00:00
assert.equal(err.message, 'msg');
assert.equal(err.status, 400);
assert.equal(err.expose, true);
assert.equal(err.prop, true);
}
2015-10-12 20:36:41 +00:00
});
2014-08-09 05:38:54 +00:00
describe('when props include status', () => {
2017-05-11 03:30:32 +00:00
it('should be ignored', () => {
const ctx = context();
2014-08-09 05:38:54 +00:00
try {
ctx.throw(400, 'msg', {
prop: true,
status: -1
});
} catch (err) {
2017-05-11 03:30:32 +00:00
assert.equal(err.message, 'msg');
assert.equal(err.status, 400);
assert.equal(err.expose, true);
assert.equal(err.prop, true);
2014-08-09 05:38:54 +00:00
}
2015-10-12 20:36:41 +00:00
});
});
});
describe('ctx.throw(msg, props)', () => {
2017-05-11 03:30:32 +00:00
it('should mixin props', () => {
const ctx = context();
try {
ctx.throw('msg', { prop: true });
} catch (err) {
2017-05-11 03:30:32 +00:00
assert.equal(err.message, 'msg');
assert.equal(err.status, 500);
assert.equal(err.expose, false);
assert.equal(err.prop, true);
}
2015-10-12 20:36:41 +00:00
});
});
describe('ctx.throw(status, props)', () => {
2017-05-11 03:30:32 +00:00
it('should mixin props', () => {
const ctx = context();
try {
ctx.throw(400, { prop: true });
} catch (err) {
2017-05-11 03:30:32 +00:00
assert.equal(err.message, 'Bad Request');
assert.equal(err.status, 400);
assert.equal(err.expose, true);
assert.equal(err.prop, true);
}
2015-10-12 20:36:41 +00:00
});
});
2014-08-12 20:22:33 +00:00
describe('ctx.throw(err, props)', () => {
2017-05-11 03:30:32 +00:00
it('should mixin props', () => {
const ctx = context();
2014-08-12 20:22:33 +00:00
try {
ctx.throw(new Error('test'), { prop: true });
} catch (err) {
2017-05-11 03:30:32 +00:00
assert.equal(err.message, 'test');
assert.equal(err.status, 500);
assert.equal(err.expose, false);
assert.equal(err.prop, true);
2014-08-12 20:22:33 +00:00
}
2015-10-12 20:36:41 +00:00
});
});