From 65cb53cd9afb1a3c4963c90724f7352270cdfc02 Mon Sep 17 00:00:00 2001 From: simov Date: Thu, 24 Sep 2015 20:27:40 +0300 Subject: [PATCH 1/2] Revert "Set default application/json content-type when using json option" This reverts commit 5225289165a0fb060c30e5a71f69704c3450103f. --- request.js | 9 ++++++--- tests/test-defaults.js | 5 ++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/request.js b/request.js index 61e51fad3..af91a11d3 100644 --- a/request.js +++ b/request.js @@ -1198,9 +1198,6 @@ Request.prototype.json = function (val) { } self._json = true - if (!self.hasHeader('content-type')) { - self.setHeader('content-type', 'application/json') - } if (typeof val === 'boolean') { if (self.body !== undefined) { if (!/^application\/x-www-form-urlencoded\b/.test(self.getHeader('content-type'))) { @@ -1208,9 +1205,15 @@ Request.prototype.json = function (val) { } else { self.body = self._qs.rfc3986(self.body) } + if (!self.hasHeader('content-type')) { + self.setHeader('content-type', 'application/json') + } } } else { self.body = safeStringify(val) + if (!self.hasHeader('content-type')) { + self.setHeader('content-type', 'application/json') + } } if (typeof self.jsonReviver === 'function') { diff --git a/tests/test-defaults.js b/tests/test-defaults.js index 4f8e3c7fe..5b58304cf 100644 --- a/tests/test-defaults.js +++ b/tests/test-defaults.js @@ -73,7 +73,6 @@ tape('deep extend', function(t) { }, function (e, r, b) { delete b.headers.host delete b.headers.accept - delete b.headers['content-type'] delete b.headers.connection t.deepEqual(b.headers, { a: '1', b: '3', c: '4' }) t.deepEqual(b.qs, { a: '1', b: '3', c: '4' }) @@ -98,7 +97,7 @@ tape('post(string, object, function)', function(t) { }).post(s.url + '/', { json: true }, function (e, r, b) { t.equal(b.method, 'POST') t.equal(b.headers.foo, 'bar') - t.equal(b.headers['content-type'], 'application/json') + t.equal(b.headers['content-type'], undefined) t.end() }) }) @@ -109,7 +108,7 @@ tape('patch(string, object, function)', function(t) { }).patch(s.url + '/', { json: true }, function (e, r, b) { t.equal(b.method, 'PATCH') t.equal(b.headers.foo, 'bar') - t.equal(b.headers['content-type'], 'application/json') + t.equal(b.headers['content-type'], undefined) t.end() }) }) From f6b91b55a3b09f3fc2eb4bc9b1b2174016b2961a Mon Sep 17 00:00:00 2001 From: simov Date: Thu, 24 Sep 2015 20:56:55 +0300 Subject: [PATCH 2/2] Add test for missing request body when using json:true --- tests/test-json-request.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test-json-request.js b/tests/test-json-request.js index f45c753d7..e7f39556e 100644 --- a/tests/test-json-request.js +++ b/tests/test-json-request.js @@ -72,6 +72,16 @@ testJSONValueReviver('jsonReviver', -48269.592, function (k, v) { }, 48269.592) testJSONValueReviver('jsonReviverInvalid', -48269.592, 'invalid reviver', -48269.592) +tape('missing body', function (t) { + s.on('/missing-body', function (req, res) { + t.equal(req.headers['content-type'], undefined) + res.end() + }) + request({url:s.url + '/missing-body', json:true}, function () { + t.end() + }) +}) + tape('cleanup', function(t) { s.close(function() { t.end()