Skip to content

Commit

Permalink
fixed "header" event
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Dec 16, 2011
1 parent b74a338 commit b6d76b7
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 14 deletions.
4 changes: 2 additions & 2 deletions lib/middleware/compress.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ module.exports = function compress(options) {
// proxy // proxy


res.write = function(chunk, encoding){ res.write = function(chunk, encoding){
if (!res.headerSent) this._implicitHeader(); if (!this.headerSent) this._implicitHeader();
return stream.write(chunk, encoding); return stream.write(chunk, encoding);
}; };


res.end = function(chunk, encoding){ res.end = function(chunk, encoding){
if (!res.headerSent) this._implicitHeader(); if (!this.headerSent) this._implicitHeader();
if (chunk) this.write(chunk, encoding); if (chunk) this.write(chunk, encoding);
return stream.end(); return stream.end();
}; };
Expand Down
18 changes: 9 additions & 9 deletions lib/patch.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,15 +10,10 @@
*/ */


var http = require('http') var http = require('http')
, res = http.OutgoingMessage.prototype; , res = http.ServerResponse.prototype

, setHeader = res.setHeader
// original setHeader() , _renderHeaders = res._renderHeaders

, writeHead = res.writeHead;
var setHeader = res.setHeader;

// original _renderHeaders()

var _renderHeaders = res._renderHeaders;


// apply only once // apply only once


Expand Down Expand Up @@ -73,4 +68,9 @@ res._renderHeaders = function(){
return _renderHeaders.call(this); return _renderHeaders.call(this);
}; };


res.writeHead = function(){
this.emit('header');
return writeHead.apply(this, arguments);
};

res._hasConnectPatch = true; res._hasConnectPatch = true;
3 changes: 1 addition & 2 deletions lib/proto.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ app.use = function(route, fn){
*/ */


app.handle = function(req, res, out) { app.handle = function(req, res, out) {
var writeHead = res.writeHead var stack = this.stack
, stack = this.stack
, removed = '' , removed = ''
, index = 0; , index = 0;


Expand Down
2 changes: 2 additions & 0 deletions test/mocha.opts
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,2 @@
--require should
--growl
86 changes: 86 additions & 0 deletions test/patch.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,86 @@

var connect = require('../');

describe('patch', function(){
describe('"header" event', function(){
describe('with .setHeader()', function(){
it('should be emitted', function(done){
var app = connect();

app.use(function(req, res, next){
res.on('header', function(){
res.setHeader('bar', 'baz');
});

next();
});

app.use(function(req, res){
res.setHeader('foo', 'bar');
res.end();
})

app.request()
.get('/')
.end(function(res){
res.should.have.header('foo', 'bar');
res.should.have.header('bar', 'baz');
done();
});
})
})

describe('with .writeHead()', function(){
it('should be emitted', function(done){
var app = connect();

app.use(function(req, res, next){
res.on('header', function(){
res.setHeader('bar', 'baz');
});

next();
});

app.use(function(req, res){
res.writeHead(200, { foo: 'bar' });
res.end();
})

app.request()
.get('/')
.end(function(res){
res.should.have.header('foo', 'bar');
res.should.have.header('bar', 'baz');
done();
});
})
})

describe('with .end() only', function(){
it('should be emitted', function(done){
var app = connect();

app.use(function(req, res, next){
res.on('header', function(){
res.setHeader('bar', 'baz');
});

next();
});

app.use(function(req, res){
res.end();
})

app.request()
.get('/')
.end(function(res){
res.should.have.header('bar', 'baz');
done();
});
})
})

})
})
1 change: 0 additions & 1 deletion test/responseTime.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ app.use(connect.responseTime());


app.use(function(req, res){ app.use(function(req, res){
setTimeout(function(){ setTimeout(function(){
res.setHeader('foo', 'bar');
res.end(); res.end();
}, 30); }, 30);
}); });
Expand Down

0 comments on commit b6d76b7

Please sign in to comment.