From 9fe483ca767b64de3e9b9e2c78b7bfaf420861c2 Mon Sep 17 00:00:00 2001 From: mako-taco Date: Sun, 22 Dec 2013 12:26:21 -0500 Subject: [PATCH 1/4] handle manually written responses --- lib/application.js | 2 ++ test/application.js | 55 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/lib/application.js b/lib/application.js index be1a6b2..babf4c6 100644 --- a/lib/application.js +++ b/lib/application.js @@ -184,6 +184,8 @@ function *respond(next){ yield next; var res = this.res; + if (res.headersSent || !res.socket.writable) return; + var body = this.body; var head = 'HEAD' == this.method; var noContent = ~[204, 205, 304].indexOf(this.status); diff --git a/test/application.js b/test/application.js index 29c433b..073e484 100644 --- a/test/application.js +++ b/test/application.js @@ -134,6 +134,61 @@ describe('app.respond', function(){ }) }) + describe('when res has already been written to', function(){ + + it('should not cause an app error', function(done){ + var app = koa(); + + app.use(function *(next){ + var res = this.res; + res.setHeader("Content-Type", "text/html") + res.status = 200; + res.write('Hello'); + setTimeout(function () { + res.end("Goodbye") + }, 0); + }); + + var errorCaught = false; + + app.on('error', function (err) { + errorCaught = err; + }); + + var server = app.listen(); + + request(server) + .get('/') + .expect(200) + .end(function(err, res){ + if (err) return done(err); + if (errorCaught) return done(errorCaught); + done(); + }); + }) + + it('should send the right body', function(done){ + var app = koa(); + + app.use(function *(next){ + var res = this.res; + res.setHeader("Content-Type", "text/html") + res.status = 200; + res.write('Hello'); + setTimeout(function () { + res.end("Goodbye") + }, 0); + }); + + var server = app.listen(); + + request(server) + .get('/') + .expect(200) + .expect('HelloGoodbye', done); + }) + }) + describe('when .body is missing', function(){ describe('with status=400', function(){ it('should respond with the associated status message', function(done){ From 0d9336622c6e9d01abec01bc4a17ab9736d46252 Mon Sep 17 00:00:00 2001 From: Jonathan Ong Date: Sun, 22 Dec 2013 14:48:28 -0800 Subject: [PATCH 2/4] use yield *next internally --- lib/application.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/application.js b/lib/application.js index babf4c6..89e575c 100644 --- a/lib/application.js +++ b/lib/application.js @@ -181,7 +181,7 @@ function *respond(next){ this.status = 200; if (this.app.poweredBy) this.set('X-Powered-By', 'koa'); - yield next; + yield *next; var res = this.res; if (res.headersSent || !res.socket.writable) return; From b56531f75d558c3670ed6881031eca31f99acb41 Mon Sep 17 00:00:00 2001 From: Jonathan Ong Date: Sun, 22 Dec 2013 15:13:01 -0800 Subject: [PATCH 3/4] bump koa compose to 2.1.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 24aaba7..c87ca5e 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "mime": "~1.2.11", "fresh": "~0.2.0", "negotiator": "~0.3.0", - "koa-compose": "~2.0.1", + "koa-compose": "~2.1.0", "cookies": "~0.3.7", "keygrip": "~1.0.0" }, From b047805f9e93103733d1b88726339cf98e40c71c Mon Sep 17 00:00:00 2001 From: yosssi Date: Tue, 24 Dec 2013 21:13:42 +0900 Subject: [PATCH 4/4] Fixed the link url to the list of middlewares on docs/api/index.md. --- docs/api/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api/index.md b/docs/api/index.md index a0e787b..fa77426 100644 --- a/docs/api/index.md +++ b/docs/api/index.md @@ -126,7 +126,7 @@ http.createServer(app.callback()).listen(3001); ## app.use(function) - Add the given middleware function to this application. See [Middleware](#middleware) for + Add the given middleware function to this application. See [Middleware](https://github.com/koajs/koa/wiki#middleware) for more information. ## app.keys=