Skip to content

Commit

Permalink
Merge pull request #1956 from jongyoonlee/master
Browse files Browse the repository at this point in the history
Check form-data content-length value before setting up the header
  • Loading branch information
simov committed Dec 12, 2015
2 parents 29fca98 + 53f5d13 commit 288f814
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion request.js
Expand Up @@ -579,7 +579,7 @@ Request.prototype.init = function (options) {
// Before ending the request, we had to compute the length of the whole form, asyncly
self.setHeader(self._form.getHeaders(), true)
self._form.getLength(function (err, length) {
if (!err) {
if (!err && !isNaN(length)) {
self.setHeader('content-length', length)
}
end()
Expand Down
40 changes: 39 additions & 1 deletion tests/test-form-data-error.js
Expand Up @@ -27,8 +27,46 @@ tape('re-emit formData errors', function(t) {
}).form().append('field', ['value1', 'value2'])
})

tape('omit content-length header if the value is set to NaN', function(t) {

// returns chunked HTTP response which is streamed to the 2nd HTTP request in the form data
s.on('/chunky', server.createChunkResponse(
['some string',
'some other string'
]))

// accepts form data request
s.on('/stream', function(req, resp) {
req.on('data', function(chunk) {
// consume the request body
})
req.on('end', function() {
resp.writeHead(200)
resp.end()
})
})

var sendStreamRequest = function(stream) {
request.post({
uri: s.url + '/stream',
formData: {
param: stream
}
}, function(err, res) {
t.error(err, 'request failed')
t.end()
})
}

request.get({
uri: s.url + '/chunky',
}).on('response', function(res) {
sendStreamRequest(res)
})
})

tape('cleanup', function(t) {
s.close(function() {
t.end()
})
})
})

0 comments on commit 288f814

Please sign in to comment.