Skip to content
Browse files

fixed "header" event

  • Loading branch information...
1 parent b74a338 commit b6d76b7ba2722be2f16c6dac246db956832715ec @tj tj committed Dec 16, 2011
Showing with 100 additions and 14 deletions.
  1. +2 −2 lib/middleware/compress.js
  2. +9 −9 lib/patch.js
  3. +1 −2 lib/proto.js
  4. +2 −0 test/mocha.opts
  5. +86 −0 test/patch.js
  6. +0 −1 test/responseTime.js
View
4 lib/middleware/compress.js
@@ -77,12 +77,12 @@ module.exports = function compress(options) {
// proxy
res.write = function(chunk, encoding){
- if (!res.headerSent) this._implicitHeader();
+ if (!this.headerSent) this._implicitHeader();
return stream.write(chunk, encoding);
};
res.end = function(chunk, encoding){
- if (!res.headerSent) this._implicitHeader();
+ if (!this.headerSent) this._implicitHeader();
if (chunk) this.write(chunk, encoding);
return stream.end();
};
View
18 lib/patch.js
@@ -10,15 +10,10 @@
*/
var http = require('http')
- , res = http.OutgoingMessage.prototype;
-
-// original setHeader()
-
-var setHeader = res.setHeader;
-
-// original _renderHeaders()
-
-var _renderHeaders = res._renderHeaders;
+ , res = http.ServerResponse.prototype
+ , setHeader = res.setHeader
+ , _renderHeaders = res._renderHeaders
+ , writeHead = res.writeHead;
// apply only once
@@ -73,4 +68,9 @@ res._renderHeaders = function(){
return _renderHeaders.call(this);
};
+res.writeHead = function(){
+ this.emit('header');
+ return writeHead.apply(this, arguments);
+};
+
res._hasConnectPatch = true;
View
3 lib/proto.js
@@ -102,8 +102,7 @@ app.use = function(route, fn){
*/
app.handle = function(req, res, out) {
- var writeHead = res.writeHead
- , stack = this.stack
+ var stack = this.stack
, removed = ''
, index = 0;
View
2 test/mocha.opts
@@ -0,0 +1,2 @@
+--require should
+--growl
View
86 test/patch.js
@@ -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();
+ });
+ })
+ })
+
+ })
+})
View
1 test/responseTime.js
@@ -7,7 +7,6 @@ app.use(connect.responseTime());
app.use(function(req, res){
setTimeout(function(){
- res.setHeader('foo', 'bar');
res.end();
}, 30);
});

0 comments on commit b6d76b7

Please sign in to comment.
Something went wrong with that request. Please try again.