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

Check form-data content-length value before setting up the header #1956

Merged
merged 2 commits into from Dec 12, 2015
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()
})
})
})