add GeneratorFunction assertion for app.use(). Closes #120

breaks old old shit but thats ok, super early in the game
This commit is contained in:
TJ Holowaychuk 2013-12-19 10:03:08 -08:00
parent fefb3c59ae
commit 70971dcb53
3 changed files with 23 additions and 12 deletions

View file

@ -10,6 +10,7 @@ var request = require('./request');
var response = require('./response'); var response = require('./response');
var Cookies = require('cookies'); var Cookies = require('cookies');
var Keygrip = require('keygrip'); var Keygrip = require('keygrip');
var assert = require('assert');
var Stream = require('stream'); var Stream = require('stream');
var http = require('http'); var http = require('http');
var co = require('co'); var co = require('co');
@ -71,12 +72,13 @@ app.listen = function(){
/** /**
* Use the given middleware `fn`. * Use the given middleware `fn`.
* *
* @param {Function} fn * @param {GeneratorFunction} fn
* @return {Application} self * @return {Application} self
* @api public * @api public
*/ */
app.use = function(fn){ app.use = function(fn){
assert('GeneratorFunction' == fn.constructor.name, 'app.use() requires a generator function');
debug('use %s', fn.name || '-'); debug('use %s', fn.name || '-');
this.middleware.push(fn); this.middleware.push(fn);
return this; return this;

View file

@ -58,6 +58,17 @@ describe('app.use(fn)', function(){
done(); 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(){ describe('app.respond', function(){

View file

@ -40,8 +40,7 @@ describe('res.status=', function(){
it('should strip content related header fields', function(done){ it('should strip content related header fields', function(done){
var app = koa(); var app = koa();
app.use(function(next){ app.use(function *(){
return function *(){
this.body = { foo: 'bar' }; this.body = { foo: 'bar' };
this.set('Content-Type', 'application/json'); this.set('Content-Type', 'application/json');
this.set('Content-Length', '15'); this.set('Content-Length', '15');
@ -50,7 +49,6 @@ describe('res.status=', function(){
assert(null == this.response.header['content-type']); assert(null == this.response.header['content-type']);
assert(null == this.response.header['content-length']); assert(null == this.response.header['content-length']);
assert(null == this.response.header['transfer-encoding']); assert(null == this.response.header['transfer-encoding']);
}
}); });
request(app.listen()) request(app.listen())