Skip to content

Commit

Permalink
Allow content-length of 0 (request#920)
Browse files Browse the repository at this point in the history
fix check for if length is set in order to allow passing a 0-length
buffer as body
  • Loading branch information
pretorh committed Dec 10, 2018
1 parent b3926a3 commit 6dcdce7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion request.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ Request.prototype.init = function (options) {
length = self.body.length
}

if (length) {
if (length !== undefined) {
self.setHeader('content-length', length)
} else {
self.emit('error', new Error('Argument error, options.body.'))
Expand Down
14 changes: 14 additions & 0 deletions tests/test-headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,20 @@ function addTests () {
function (t, req, res) {
t.equal(req.headers.cookie, undefined)
})

runTest(
'#920: allow content-length of 0',
'headers.json',
{
json: false,
method: 'POST',
headers: { 'content-type': 'text/plain; charset=UTF-8' },
body: Buffer.alloc(0)
},
function (t, req, res) {
t.equal(req.headers['content-length'], '0')
}
)
}

tape('setup', function (t) {
Expand Down

0 comments on commit 6dcdce7

Please sign in to comment.