Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in compress detecting content-type set via writeHead #524

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 18 additions & 2 deletions lib/patch.js
Expand Up @@ -69,10 +69,26 @@ if (!res._hasConnectPatch) {
return _renderHeaders.call(this);
};

res.writeHead = function(){
res.writeHead = function(statusCode){
var reasonPhrase, headers, headerIndex;

if (typeof arguments[1] == 'string') {
reasonPhrase = arguments[1];
headerIndex = 2;
} else {
headerIndex = 1;
}
headers = arguments[headerIndex];

if (headers) {
for (var name in headers) {
this.setHeader(name, headers[name]);
}
}

if (!this._emittedHeader) this.emit('header');
this._emittedHeader = true;
return writeHead.apply(this, arguments);
return writeHead.call(this, statusCode, reasonPhrase);
};

res._hasConnectPatch = true;
Expand Down
62 changes: 62 additions & 0 deletions test/compress.js
Expand Up @@ -7,6 +7,38 @@ var app = connect();
app.use(connect.compress());
app.use(connect.static(fixtures));

var dynamicApp = connect();
dynamicApp.use(connect.compress());

dynamicApp.use(function(req, res){
var body = '- groceries';
Copy link
Member

Choose a reason for hiding this comment

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

I think the tests should be in test/patch.js since that's what we're changing


if (req.url == '/setHeaderEnd') {
res.setHeader('Content-Type', 'text/plain')
res.end(body);
return
}

if (req.url == '/writeHeadEnd') {
res.writeHead(200, {
'Content-Length': body.length,
'Content-Type': 'text/plain'
});
res.end(body);
return
}

if (req.url == '/writeHeadWriteEnd') {
res.writeHead(200, {
'Content-Length': body.length,
'Content-Type': 'text/plain'
});
res.write(body)
res.end();
return
}
});

describe('connect.compress()', function(){
it('should gzip files', function(done){
app.request()
Expand All @@ -18,6 +50,36 @@ describe('connect.compress()', function(){
});
})

it('should gzip after setHeader(), end()', function(done){
dynamicApp.request()
.get('/setHeaderEnd')
.set('Accept-Encoding', 'gzip')
.end(function(res){
res.body.should.not.equal('- groceries');
done();
});
})

it('should gzip after writeHead(), end()', function(done){
dynamicApp.request()
.get('/writeHeadEnd')
.set('Accept-Encoding', 'gzip')
.end(function(res){
res.body.should.not.equal('- groceries');
done();
});
})

it('should gzip after writeHead(), write(), end()', function(done){
dynamicApp.request()
.get('/writeHeadWriteEnd')
.set('Accept-Encoding', 'gzip')
.end(function(res){
res.body.should.not.equal('- groceries');
done();
});
})

it('should set Content-Encoding', function(done){
app.request()
.get('/todo.txt')
Expand Down