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){