Skip to content

Commit

Permalink
handle manually written responses
Browse files Browse the repository at this point in the history
  • Loading branch information
mako-taco authored and jonathanong committed Dec 22, 2013
1 parent 33a5138 commit 9fe483c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
55 changes: 55 additions & 0 deletions test/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down

1 comment on commit 9fe483c

@kristianmandrup
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really a good idea? What about HTTP2 support? Wouldn't that conflict?

Please sign in to comment.