test: improve test coverage for application and response

This commit is contained in:
fengmk2 2015-03-29 11:25:28 +08:00
parent 9b4e349c9b
commit 1edd6ec69a
3 changed files with 27 additions and 1 deletions

View file

@ -204,6 +204,7 @@ function *respond(next) {
if (null == body) { if (null == body) {
this.type = 'text'; this.type = 'text';
body = this.message || String(code); body = this.message || String(code);
/* istanbul ignore else */
if (body) this.length = Buffer.byteLength(body); if (body) this.length = Buffer.byteLength(body);
return res.end(body); return res.end(body);
} }

View file

@ -48,6 +48,14 @@ describe('app', function(){
.get('/') .get('/')
.end(function(){}); .end(function(){});
}) })
it('should set development env when NODE_ENV missing', function(){
var NODE_ENV = process.env.NODE_ENV;
process.env.NODE_ENV = '';
var app = koa();
process.env.NODE_ENV = NODE_ENV;
assert.equal(app.env, 'development');
})
}) })
describe('app.toJSON()', function(){ describe('app.toJSON()', function(){
@ -180,6 +188,22 @@ describe('app.onerror(err)', function(){
done(); done();
}) })
it('should use err.toString() instad of err.stack', function(done){
var app = koa();
app.env = 'dev';
var err = new Error('mock stack null');
err.stack = null;
var output = stderr.inspectSync(function() {
app.onerror(err);
});
output.should.eql(["\n", " Error: mock stack null\n", "\n"]);
done();
})
}) })
describe('app.respond', function(){ describe('app.respond', function(){

View file

@ -13,7 +13,8 @@ describe('ctx.append(name, val)', function(){
var ctx = context(); var ctx = context();
ctx.append('Set-Cookie', ['foo=bar', 'fizz=buzz']); ctx.append('Set-Cookie', ['foo=bar', 'fizz=buzz']);
ctx.response.header['set-cookie'].should.eql(['foo=bar', 'fizz=buzz']); ctx.append('Set-Cookie', 'hi=again');
ctx.response.header['set-cookie'].should.eql(['foo=bar', 'fizz=buzz', 'hi=again']);
}) })
it('should get reset by res.set(field, val)', function (){ it('should get reset by res.set(field, val)', function (){