test: improve test coverage for application and response

master
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) {
this.type = 'text';
body = this.message || String(code);
/* istanbul ignore else */
if (body) this.length = Buffer.byteLength(body);
return res.end(body);
}

View File

@ -48,6 +48,14 @@ describe('app', function(){
.get('/')
.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(){
@ -180,6 +188,22 @@ describe('app.onerror(err)', function(){
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(){

View File

@ -13,7 +13,8 @@ describe('ctx.append(name, val)', function(){
var ctx = context();
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 (){