Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,36 @@ res.send = function send(body) {
return this;
};

/**
* Ends the response process.
*
* Method comes from Node core, but we need to define it here
* so we can strip headers for 204 and 304 status codes.
*
* Examples:
*
* res.end();
*
* @public
*/

res.end = function end() {
if (204 === this.statusCode || 304 === this.statusCode) {
this.removeHeader('Content-Type');
this.removeHeader('Content-Length');
this.removeHeader('Transfer-Encoding');
}

// we need to get original .end()
Object.getPrototypeOf(
Object.getPrototypeOf(
Object.getPrototypeOf(
Object.getPrototypeOf(this)
)
)
).end.apply(this, arguments);
}

/**
* Send JSON response.
*
Expand Down
43 changes: 43 additions & 0 deletions test/res.end.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var assert = require('assert');
var express = require('..');
var methods = require('methods');
var request = require('supertest');
var utils = require('./support/utils');

describe('res', function(){
describe('.end()', function(){
describe('when .statusCode is 204', function(){
it('should strip Content-* fields, Transfer-Encoding field, and body', function(done){
var app = express();

app.use(function(req, res){
res.status(204).set('Transfer-Encoding', 'chunked').end();
});

request(app)
.get('/')
.expect(utils.shouldNotHaveHeader('Content-Type'))
.expect(utils.shouldNotHaveHeader('Content-Length'))
.expect(utils.shouldNotHaveHeader('Transfer-Encoding'))
.expect(204, '', done);
})
})

describe('when .statusCode is 304', function(){
it('should strip Content-* fields, Transfer-Encoding field, and body)', function(done){
var app = express();

app.use(function(req, res){
res.status(304).set('Transfer-Encoding', 'chunked').end();
});

request(app)
.get('/')
.expect(utils.shouldNotHaveHeader('Content-Type'))
.expect(utils.shouldNotHaveHeader('Content-Length'))
.expect(utils.shouldNotHaveHeader('Transfer-Encoding'))
.expect(304, '', done);
})
})
})
})