Merge pull request #58 from jonathanong/set-body-length
set length on body override
This commit is contained in:
commit
3b7a7b5047
2 changed files with 28 additions and 6 deletions
|
@ -163,31 +163,31 @@ module.exports = {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// content-type already set
|
// set the content-type only if not yet set
|
||||||
if (this.responseHeader['content-type']) return;
|
var setType = !this.responseHeader['content-type'];
|
||||||
|
|
||||||
// string
|
// string
|
||||||
if ('string' == typeof val) {
|
if ('string' == typeof val) {
|
||||||
this.type = ~val.indexOf('<') ? 'html' : 'text';
|
if (setType) this.type = ~val.indexOf('<') ? 'html' : 'text';
|
||||||
this.length = Buffer.byteLength(val);
|
this.length = Buffer.byteLength(val);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// buffer
|
// buffer
|
||||||
if (Buffer.isBuffer(val)) {
|
if (Buffer.isBuffer(val)) {
|
||||||
this.type = 'bin';
|
if (setType) this.type = 'bin';
|
||||||
this.length = val.length;
|
this.length = val.length;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// stream
|
// stream
|
||||||
if (val instanceof Stream) {
|
if (val instanceof Stream) {
|
||||||
this.type = 'bin';
|
if (setType) this.type = 'bin';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// json
|
// json
|
||||||
this.type = 'json';
|
if (setType) this.type = 'json';
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -29,6 +29,13 @@ describe('ctx.body=', function(){
|
||||||
ctx.body = new Buffer('something');
|
ctx.body = new Buffer('something');
|
||||||
assert('image/png' == ctx.responseHeader['content-type']);
|
assert('image/png' == ctx.responseHeader['content-type']);
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should override length', function(){
|
||||||
|
var ctx = context();
|
||||||
|
ctx.type = 'html';
|
||||||
|
ctx.body = 'something';
|
||||||
|
assert.equal(ctx.responseLength, 9);
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('when a string is given', function(){
|
describe('when a string is given', function(){
|
||||||
|
@ -51,6 +58,21 @@ describe('ctx.body=', function(){
|
||||||
ctx.body = '<h1>Tobi</h1>';
|
ctx.body = '<h1>Tobi</h1>';
|
||||||
assert('text/html; charset=utf-8' == ctx.responseHeader['content-type']);
|
assert('text/html; charset=utf-8' == ctx.responseHeader['content-type']);
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should set length', function(){
|
||||||
|
var string = '<h1>Tobi</h1>';
|
||||||
|
var ctx = context();
|
||||||
|
ctx.body = string;
|
||||||
|
assert.equal(ctx.responseLength, Buffer.byteLength(string));
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should set length when body is override', function(){
|
||||||
|
var string = '<h1>Tobi</h1>';
|
||||||
|
var ctx = context();
|
||||||
|
ctx.body = string;
|
||||||
|
ctx.body = string + string;
|
||||||
|
assert.equal(ctx.responseLength, 2 * Buffer.byteLength(string));
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('when a stream is given', function(){
|
describe('when a stream is given', function(){
|
||||||
|
|
Loading…
Reference in a new issue