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

Send Content-Length: 0 and empty chunk for HTTP 205 #4596

Merged
merged 1 commit into from
Feb 21, 2022
Merged
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
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