From 2127aa3b4395f6b522f9530ac1f79d8343b7d05f Mon Sep 17 00:00:00 2001 From: simov Date: Mon, 22 Jun 2015 20:24:41 +0300 Subject: [PATCH 1/2] Add function for setting up the content-length --- request.js | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/request.js b/request.js index c032ea8f6..aa3ee4960 100644 --- a/request.js +++ b/request.js @@ -430,28 +430,24 @@ Request.prototype.init = function (options) { self.elapsedTime = self.elapsedTime || 0 } - if (self.body) { - var length = 0 - if (!Buffer.isBuffer(self.body)) { - if (Array.isArray(self.body)) { - for (var i = 0; i < self.body.length; i++) { - length += self.body[i].length - } - } else { - self.body = new Buffer(self.body) - length = self.body.length - } - } else { - length = self.body.length + function setContentLength () { + if (!Buffer.isBuffer(self.body) && !Array.isArray(self.body)) { + self.body = new Buffer(self.body) } - if (length) { - if (!self.hasHeader('content-length')) { + if (!self.hasHeader('content-length')) { + var length = (Array.isArray(self.body)) + ? self.body.reduce(function (a, b) {return a + b.length}, 0) + : self.body.length + if (length) { self.setHeader('content-length', length) + } else { + self.emit('error', new Error('Argument error, options.body.')) } - } else { - self.emit('error', new Error('Argument error, options.body.')) } } + if (self.body) { + setContentLength() + } if (options.oauth) { self.oauth(options.oauth) @@ -541,6 +537,7 @@ Request.prototype.init = function (options) { self._multipart.body.pipe(self) } if (self.body) { + setContentLength() if (Array.isArray(self.body)) { self.body.forEach(function (part) { self.write(part) From 986723e2992e8de83c92ceaa46224780542f3caf Mon Sep 17 00:00:00 2001 From: simov Date: Tue, 23 Jun 2015 11:32:16 +0300 Subject: [PATCH 2/2] Add test for setting up the body via the .form() method --- tests/test-form-urlencoded.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/test-form-urlencoded.js b/tests/test-form-urlencoded.js index 2d069933d..fdb283411 100644 --- a/tests/test-form-urlencoded.js +++ b/tests/test-form-urlencoded.js @@ -9,11 +9,12 @@ function runTest (t, options, index) { var server = http.createServer(function(req, res) { - if (index === 0) { + if (index === 0 || index === 3) { t.equal(req.headers['content-type'], 'application/x-www-form-urlencoded') } else { t.equal(req.headers['content-type'], 'application/x-www-form-urlencoded; charset=UTF-8') } + t.equal(req.headers['content-length'], '21') t.equal(req.headers.accept, 'application/json') var data = '' @@ -33,7 +34,7 @@ function runTest (t, options, index) { server.listen(6767, function() { - request.post('http://localhost:6767', options, function(err, res, body) { + var r = request.post('http://localhost:6767', options, function(err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.equal(body, 'done') @@ -41,6 +42,9 @@ function runTest (t, options, index) { t.end() }) }) + if (!options.form && !options.body) { + r.form({some: 'url', encoded: 'data'}) + } }) } @@ -58,6 +62,10 @@ var cases = [ headers: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'}, body: 'some=url&encoded=data', json: true + }, + { + // body set via .form() method + json: true } ]