Skip to content

Commit

Permalink
Support proper 205 responses using res.send
Browse files Browse the repository at this point in the history
closes #4592
closes #4596
  • Loading branch information
tkesgar authored and dougwilson committed Feb 20, 2022
1 parent e693771 commit 628c524
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
unreleased
==========

* Support proper 205 responses using `res.send`

4.17.3 / 2022-02-16
===================

Expand Down
7 changes: 7 additions & 0 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ res.send = function send(body) {
chunk = '';
}

// alter headers for 205
if (this.statusCode === 205) {
this.set('Content-Length', '0')
this.removeHeader('Transfer-Encoding')
chunk = ''
}

if (req.method === 'HEAD') {
// skip body for HEAD
this.end();
Expand Down
16 changes: 16 additions & 0 deletions test/res.send.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,22 @@ describe('res', function(){
})
})

describe('when .statusCode is 205', function () {
it('should strip Transfer-Encoding field and body, set Content-Length', function (done) {
var app = express()

app.use(function (req, res) {
res.status(205).set('Transfer-Encoding', 'chunked').send('foo')
})

request(app)
.get('/')
.expect(utils.shouldNotHaveHeader('Transfer-Encoding'))
.expect('Content-Length', '0')
.expect(205, '', done)
})
})

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

0 comments on commit 628c524

Please sign in to comment.