From f1b59e1c75885ab78fac68ae5e7a1345658a4f1a Mon Sep 17 00:00:00 2001 From: Jong Lee Date: Wed, 9 Dec 2015 13:50:31 -0800 Subject: [PATCH 1/2] check the value of content-length before setting the header when posting a form data. --- request.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/request.js b/request.js index 19c1b92f3..793388abf 100644 --- a/request.js +++ b/request.js @@ -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() From 53f5d13af4d5cd7389b255cdf560ca8d2aa1a109 Mon Sep 17 00:00:00 2001 From: Jong Lee Date: Thu, 10 Dec 2015 17:55:43 -0800 Subject: [PATCH 2/2] added a test case for setting content-length header value to NaN when using chunked HTTP response in form data. --- tests/test-form-data-error.js | 40 ++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/tests/test-form-data-error.js b/tests/test-form-data-error.js index 1f3178686..9b7642211 100644 --- a/tests/test-form-data-error.js +++ b/tests/test-form-data-error.js @@ -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() }) -}) +}) \ No newline at end of file