Repository: koajs/koa
Pull request #438 (2015-04-28 17:50:27)

make sure helpers return strict string
not sure if this is look good to everyone, but it reduce lots of code like:

```
(this.charset || '').toLowerCase();
(this.get('ua') || '').indexOf('chrome');
(this.type || '').match(/^text/);
```

and this won't break your app if your are using `!this.get('referer')` etc.


Patch:
diff --git a/docs/api/context.md b/docs/api/context.md
index 2e6647b..912afc8 100644
--- a/docs/api/context.md
+++ b/docs/api/context.md
@@ -126,7 +126,7 @@ koa uses [http-errors](https://github.com/jshttp/http-errors) to create errors.
   method.
 
 ```js
-this.assert(this.state.user, 401, 'User not found. Please login!');
+this.assert(this.user, 401, 'User not found. Please login!');
 ```
 
 koa uses [http-assert](https://github.com/jshttp/http-assert) for assertions.
diff --git a/lib/request.js b/lib/request.js
index 84f8cfc..f8d75e4 100644
--- a/lib/request.js
+++ b/lib/request.js
@@ -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 || '';
   },
 
   /**
@@ -315,7 +315,7 @@ module.exports = {
 
   get length() {
     var len = this.get('Content-Length');
-    if (null == len) return;
+    if (len == '') return;
     return ~~len;
   },
 
@@ -362,7 +362,7 @@ module.exports = {
    */
 
   get ip() {
-    return this.ips[0] || this.socket.remoteAddress;
+    return this.ips[0] || this.socket.remoteAddress || '';
   },
 
   /**
@@ -546,7 +546,7 @@ module.exports = {
 
   get type() {
     var type = this.get('Content-Type');
-    if (!type) return;
+    if (!type) return '';
     return type.split(';')[0];
   },
 
@@ -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] || '';
     }
   },
 
@@ -607,6 +607,6 @@ module.exports = {
       method: this.method,
       url: this.url,
       header: this.header
-    }
+    };
   }
 };
diff --git a/lib/response.js b/lib/response.js
index 4a8a6e3..9359fa4 100644
--- a/lib/response.js
+++ b/lib/response.js
@@ -367,7 +367,7 @@ module.exports = {
 
   get type() {
     var type = this.get('Content-Type');
-    if (!type) return;
+    if (!type) return '';
     return type.split(';')[0];
   },
 
@@ -404,7 +404,7 @@ module.exports = {
    */
 
   get: function(field){
-    return this.header[field.toLowerCase()];
+    return this.header[field.toLowerCase()] || '';
   },
 
   /**
@@ -512,6 +512,6 @@ module.exports = {
       status: this.status,
       message: this.message,
       header: this.header
-    }
+    };
   }
 };


Test patch:
diff --git a/test/request/charset.js b/test/request/charset.js
index 87ded1e..ddbfb75 100644
--- a/test/request/charset.js
+++ b/test/request/charset.js
@@ -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);
     })
   })
 
diff --git a/test/request/type.js b/test/request/type.js
index 124d9b1..f3b31ef 100644
--- a/test/request/type.js
+++ b/test/request/type.js
@@ -11,6 +11,6 @@ describe('req.type', function(){
 
   describe('with no host present', function(){
     var req = request();
-    assert(null == req.type);
+    assert('' === req.type);
   })
 })
diff --git a/test/response/type.js b/test/response/type.js
index 6fc65b4..2aba95c 100644
--- a/test/response/type.js
+++ b/test/response/type.js
@@ -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);
     })
   })