Skip to content

Commit

Permalink
Merge pull request #438 from koajs/strict
Browse files Browse the repository at this point in the history
make sure helpers return strict string
  • Loading branch information
dead-horse committed Apr 29, 2015
2 parents 34c36d0 + c8eb5ee commit e21fc8a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
16 changes: 8 additions & 8 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,9 @@ module.exports = {

get charset() {
var type = this.get('Content-Type');
if (!type) return;
if (!type) return '';

return contentType.parse(type).parameters.charset;
return contentType.parse(type).parameters.charset || '';
},

/**
Expand All @@ -315,7 +315,7 @@ module.exports = {

get length() {
var len = this.get('Content-Length');
if (null == len) return;
if (len == '') return;
return ~~len;
},

Expand Down Expand Up @@ -362,7 +362,7 @@ module.exports = {
*/

get ip() {
return this.ips[0] || this.socket.remoteAddress;
return this.ips[0] || this.socket.remoteAddress || '';
},

/**
Expand Down Expand Up @@ -546,7 +546,7 @@ module.exports = {

get type() {
var type = this.get('Content-Type');
if (!type) return;
if (!type) return '';
return type.split(';')[0];
},

Expand Down Expand Up @@ -577,9 +577,9 @@ module.exports = {
switch (field = field.toLowerCase()) {
case 'referer':
case 'referrer':
return req.headers.referrer || req.headers.referer;
return req.headers.referrer || req.headers.referer || '';
default:
return req.headers[field];
return req.headers[field] || '';
}
},

Expand Down Expand Up @@ -607,6 +607,6 @@ module.exports = {
method: this.method,
url: this.url,
header: this.header
}
};
}
};
6 changes: 3 additions & 3 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ module.exports = {

get type() {
var type = this.get('Content-Type');
if (!type) return;
if (!type) return '';
return type.split(';')[0];
},

Expand Down Expand Up @@ -404,7 +404,7 @@ module.exports = {
*/

get: function(field){
return this.header[field.toLowerCase()];
return this.header[field.toLowerCase()] || '';
},

/**
Expand Down Expand Up @@ -512,6 +512,6 @@ module.exports = {
status: this.status,
message: this.message,
header: this.header
}
};
}
};
8 changes: 4 additions & 4 deletions test/request/charset.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ var assert = require('assert');

describe('req.charset', function(){
describe('with no content-type present', function(){
it('should return null', function(){
it('should return ""', function(){
var req = request();
assert(null == req.charset);
assert('' === req.charset);
})
})

describe('with charset present', function(){
it('should return null', function(){
it('should return ""', function(){
var req = request();
req.header['content-type'] = 'text/plain';
assert(null == req.charset);
assert('' === req.charset);
})
})

Expand Down
2 changes: 1 addition & 1 deletion test/request/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ describe('req.type', function(){

describe('with no host present', function(){
var req = request();
assert(null == req.type);
assert('' === req.type);
})
})
4 changes: 2 additions & 2 deletions test/response/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ describe('ctx.type=', function(){

describe('ctx.type', function(){
describe('with no Content-Type', function(){
it('should return null', function(){
it('should return ""', function(){
var ctx = context();
// TODO: this is lame
assert(null == ctx.type);
assert('' === ctx.type);
})
})

Expand Down

0 comments on commit e21fc8a

Please sign in to comment.