From 202718d9bbc6422c2374b96e8eeaf1e08cda7e2b Mon Sep 17 00:00:00 2001 From: simov Date: Fri, 12 Dec 2014 12:05:50 +0200 Subject: [PATCH 1/2] Detect urlencoded form data header via regex. Fixes https://github.com/request/request/issues/1313 --- request.js | 2 +- tests/test-form-urlencoded.js | 30 ++++++++++++++++++++++-------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/request.js b/request.js index 90220d2ba..143623860 100644 --- a/request.js +++ b/request.js @@ -1482,7 +1482,7 @@ Request.prototype.json = function (val) { self._json = true if (typeof val === 'boolean') { - if (self.body !== undefined && self.getHeader('content-type') !== 'application/x-www-form-urlencoded') { + if (self.body !== undefined && !/application\/x-www-form-urlencoded/.test(self.getHeader('content-type'))) { self.body = safeStringify(self.body) if (!self.hasHeader('content-type')) { self.setHeader('content-type', 'application/json') diff --git a/tests/test-form-urlencoded.js b/tests/test-form-urlencoded.js index eef384192..97534c225 100644 --- a/tests/test-form-urlencoded.js +++ b/tests/test-form-urlencoded.js @@ -5,11 +5,11 @@ var http = require('http') , tape = require('tape') -tape('application/x-www-form-urlencoded', function(t) { +function runTest (t, options) { var server = http.createServer(function(req, res) { - t.equal(req.headers['content-type'], 'application/x-www-form-urlencoded') + t.ok(req.headers['content-type'].match(/application\/x-www-form-urlencoded/)) t.equal(req.headers.accept, 'application/json') var data = '' @@ -24,21 +24,35 @@ tape('application/x-www-form-urlencoded', function(t) { res.writeHead(200) res.end('done') - - t.end() }) }) server.listen(8080, function() { - request.post('http://localhost:8080', { - form: {some: 'url', encoded: 'data'}, - json: true - }, function(err, res, body) { + request.post('http://localhost:8080', options, function(err, res, body) { t.equal(err, null) t.equal(res.statusCode, 200) t.equal(body, 'done') server.close() + t.end() }) }) +} + +var cases = [ + { + form: {some: 'url', encoded: 'data'}, + json: true + }, + { + headers: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'}, + body: 'some=url&encoded=data', + json: true + } +] + +cases.forEach(function (options, index) { + tape('application/x-www-form-urlencoded ' + index, function(t) { + runTest(t, options) + }) }) From f5a07a648b08a765e5b04cf950c1ae6ce04e7ed2 Mon Sep 17 00:00:00 2001 From: simov Date: Fri, 12 Dec 2014 19:44:58 +0200 Subject: [PATCH 2/2] Improve urlencoded header detection regex --- request.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/request.js b/request.js index 143623860..6857890d8 100644 --- a/request.js +++ b/request.js @@ -1482,7 +1482,7 @@ Request.prototype.json = function (val) { self._json = true if (typeof val === 'boolean') { - if (self.body !== undefined && !/application\/x-www-form-urlencoded/.test(self.getHeader('content-type'))) { + if (self.body !== undefined && !/^application\/x-www-form-urlencoded\b/.test(self.getHeader('content-type'))) { self.body = safeStringify(self.body) if (!self.hasHeader('content-type')) { self.setHeader('content-type', 'application/json')