From 70971dcb53db88c1801ff837223142478142782a Mon Sep 17 00:00:00 2001 From: TJ Holowaychuk Date: Thu, 19 Dec 2013 10:03:08 -0800 Subject: [PATCH] add GeneratorFunction assertion for app.use(). Closes #120 breaks old old shit but thats ok, super early in the game --- lib/application.js | 4 +++- test/application.js | 11 +++++++++++ test/response/status.js | 20 +++++++++----------- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/lib/application.js b/lib/application.js index 8a8b35d..c59a089 100644 --- a/lib/application.js +++ b/lib/application.js @@ -10,6 +10,7 @@ var request = require('./request'); var response = require('./response'); var Cookies = require('cookies'); var Keygrip = require('keygrip'); +var assert = require('assert'); var Stream = require('stream'); var http = require('http'); var co = require('co'); @@ -71,12 +72,13 @@ app.listen = function(){ /** * Use the given middleware `fn`. * - * @param {Function} fn + * @param {GeneratorFunction} fn * @return {Application} self * @api public */ app.use = function(fn){ + assert('GeneratorFunction' == fn.constructor.name, 'app.use() requires a generator function'); debug('use %s', fn.name || '-'); this.middleware.push(fn); return this; diff --git a/test/application.js b/test/application.js index 64f7c8d..29c433b 100644 --- a/test/application.js +++ b/test/application.js @@ -58,6 +58,17 @@ describe('app.use(fn)', function(){ done(); }); }) + + it('should error when a non-generator function is passed', function(done){ + var app = koa(); + + try { + app.use(function(){}); + } catch (err) { + err.message.should.equal('app.use() requires a generator function'); + done(); + } + }) }) describe('app.respond', function(){ diff --git a/test/response/status.js b/test/response/status.js index b81f8e3..c01aa51 100644 --- a/test/response/status.js +++ b/test/response/status.js @@ -40,17 +40,15 @@ describe('res.status=', function(){ it('should strip content related header fields', function(done){ var app = koa(); - app.use(function(next){ - return function *(){ - this.body = { foo: 'bar' }; - this.set('Content-Type', 'application/json'); - this.set('Content-Length', '15'); - this.set('Transfer-Encoding', 'chunked'); - this.status = status; - assert(null == this.response.header['content-type']); - assert(null == this.response.header['content-length']); - assert(null == this.response.header['transfer-encoding']); - } + app.use(function *(){ + this.body = { foo: 'bar' }; + this.set('Content-Type', 'application/json'); + this.set('Content-Length', '15'); + this.set('Transfer-Encoding', 'chunked'); + this.status = status; + assert(null == this.response.header['content-type']); + assert(null == this.response.header['content-length']); + assert(null == this.response.header['transfer-encoding']); }); request(app.listen())