9f27c1c414
change var to const for static require()'d modules make constant var references in app use const keyword refactor context to use es6 constants refactor request to use es6 constants, let block-scope coming next use const in response object for static refs make context tests use es6 constants experimental unit tests -> const use const for static references in unit test over req use const for static refs in res tests update app tests to use const for static refs make the context test use es6 constants for static refs use constants in the README es6 constants seem to work in --harmony on 0.12 too use const's for immutable refs in benchmarks ensure all JS files have blank newline at top add newline to bottom of file where missing add a webchat freenode link to irc channel no need to assign error in catch{}-able test app.silent option to turn off err logging keep test env logging for backwards-compat
211 lines
4.5 KiB
JavaScript
211 lines
4.5 KiB
JavaScript
|
|
'use strict';
|
|
|
|
const context = require('../context');
|
|
const assert = require('assert');
|
|
|
|
describe('ctx.throw(msg)', function(){
|
|
it('should set .status to 500', function(done){
|
|
const ctx = context();
|
|
|
|
try {
|
|
ctx.throw('boom');
|
|
} catch (err) {
|
|
assert(500 == err.status);
|
|
assert(!err.expose);
|
|
done();
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('ctx.throw(err)', function(){
|
|
it('should set .status to 500', function(done){
|
|
const ctx = context();
|
|
const err = new Error('test');
|
|
|
|
try {
|
|
ctx.throw(err);
|
|
} catch (err) {
|
|
assert(500 == err.status);
|
|
assert('test' == err.message);
|
|
assert(!err.expose);
|
|
done();
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('ctx.throw(err, status)', function(){
|
|
it('should throw the error and set .status', function(done){
|
|
const ctx = context();
|
|
const error = new Error('test');
|
|
|
|
try {
|
|
ctx.throw(error, 422);
|
|
} catch (err) {
|
|
assert(422 == err.status);
|
|
assert('test' == err.message);
|
|
assert(true === err.expose);
|
|
done();
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('ctx.throw(status, err)', function(){
|
|
it('should throw the error and set .status', function(done){
|
|
const ctx = context();
|
|
const error = new Error('test');
|
|
|
|
try {
|
|
ctx.throw(422, error);
|
|
} catch (err) {
|
|
assert(422 == err.status);
|
|
assert('test' == err.message);
|
|
assert(true === err.expose);
|
|
done();
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('ctx.throw(msg, status)', function(){
|
|
it('should throw an error', function(done){
|
|
const ctx = context();
|
|
|
|
try {
|
|
ctx.throw('name required', 400);
|
|
} catch (err) {
|
|
assert('name required' == err.message);
|
|
assert(400 == err.status);
|
|
assert(true === err.expose);
|
|
done();
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('ctx.throw(status, msg)', function(){
|
|
it('should throw an error', function(done){
|
|
const ctx = context();
|
|
|
|
try {
|
|
ctx.throw(400, 'name required');
|
|
} catch (err) {
|
|
assert('name required' == err.message);
|
|
assert(400 == err.status);
|
|
assert(true === err.expose);
|
|
done();
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('ctx.throw(status)', function(){
|
|
it('should throw an error', function(done){
|
|
const ctx = context();
|
|
|
|
try {
|
|
ctx.throw(400);
|
|
} catch (err) {
|
|
assert('Bad Request' == err.message);
|
|
assert(400 == err.status);
|
|
assert(true === err.expose);
|
|
done();
|
|
}
|
|
})
|
|
|
|
describe('when not valid status', function(){
|
|
it('should not expose', function(done){
|
|
const ctx = context();
|
|
|
|
try {
|
|
const err = new Error('some error');
|
|
err.status = -1;
|
|
ctx.throw(err);
|
|
} catch(err) {
|
|
assert('some error' == err.message);
|
|
assert(!err.expose);
|
|
done();
|
|
}
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('ctx.throw(status, msg, props)', function(){
|
|
it('should mixin props', function(done){
|
|
const ctx = context();
|
|
|
|
try {
|
|
ctx.throw(400, 'msg', { prop: true });
|
|
} catch (err) {
|
|
assert('msg' == err.message);
|
|
assert(400 == err.status);
|
|
assert(true === err.expose);
|
|
assert(true === err.prop);
|
|
done();
|
|
}
|
|
})
|
|
|
|
describe('when props include status', function(){
|
|
it('should be ignored', function(done){
|
|
const ctx = context();
|
|
|
|
try {
|
|
ctx.throw(400, 'msg', {
|
|
prop: true,
|
|
status: -1
|
|
});
|
|
} catch (err) {
|
|
assert('msg' == err.message);
|
|
assert(400 == err.status);
|
|
assert(true === err.expose);
|
|
assert(true === err.prop);
|
|
done();
|
|
}
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('ctx.throw(msg, props)', function(){
|
|
it('should mixin props', function(done){
|
|
const ctx = context();
|
|
|
|
try {
|
|
ctx.throw('msg', { prop: true });
|
|
} catch (err) {
|
|
assert('msg' == err.message);
|
|
assert(500 == err.status);
|
|
assert(false === err.expose);
|
|
assert(true === err.prop);
|
|
done();
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('ctx.throw(status, props)', function(){
|
|
it('should mixin props', function(done){
|
|
const ctx = context();
|
|
|
|
try {
|
|
ctx.throw(400, { prop: true });
|
|
} catch (err) {
|
|
assert('Bad Request' == err.message);
|
|
assert(400 == err.status);
|
|
assert(true === err.expose);
|
|
assert(true === err.prop);
|
|
done();
|
|
}
|
|
})
|
|
})
|
|
|
|
describe('ctx.throw(err, props)', function(){
|
|
it('should mixin props', function(done){
|
|
const ctx = context();
|
|
|
|
try {
|
|
ctx.throw(new Error('test'), { prop: true });
|
|
} catch (err) {
|
|
assert('test' == err.message);
|
|
assert(500 == err.status);
|
|
assert(false === err.expose);
|
|
assert(true === err.prop);
|
|
done();
|
|
}
|
|
})
|
|
})
|