From 86571c794b75bb637b39eb7574e825e461647151 Mon Sep 17 00:00:00 2001 From: simov Date: Sun, 21 Jun 2015 21:29:32 +0300 Subject: [PATCH] Allow content-type overriding when using the `form` option --- request.js | 4 +++- tests/test-form-urlencoded.js | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/request.js b/request.js index c032ea8f6..7384cf6bb 100644 --- a/request.js +++ b/request.js @@ -1145,7 +1145,9 @@ Request.prototype.qs = function (q, clobber) { Request.prototype.form = function (form) { var self = this if (form) { - self.setHeader('content-type', 'application/x-www-form-urlencoded') + if (!/^application\/x-www-form-urlencoded\b/.test(self.getHeader('content-type'))) { + self.setHeader('content-type', 'application/x-www-form-urlencoded') + } self.body = (typeof form === 'string') ? self._qs.rfc3986(form.toString('utf8')) : self._qs.stringify(form).toString('utf8') diff --git a/tests/test-form-urlencoded.js b/tests/test-form-urlencoded.js index ae2f17182..2d069933d 100644 --- a/tests/test-form-urlencoded.js +++ b/tests/test-form-urlencoded.js @@ -5,11 +5,15 @@ var http = require('http') , tape = require('tape') -function runTest (t, options) { +function runTest (t, options, index) { var server = http.createServer(function(req, res) { - t.ok(req.headers['content-type'].match(/application\/x-www-form-urlencoded/)) + if (index === 0) { + 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.accept, 'application/json') var data = '' @@ -45,6 +49,11 @@ var cases = [ form: {some: 'url', encoded: 'data'}, json: true }, + { + headers: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'}, + form: {some: 'url', encoded: 'data'}, + json: true + }, { headers: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'}, body: 'some=url&encoded=data', @@ -54,6 +63,6 @@ var cases = [ cases.forEach(function (options, index) { tape('application/x-www-form-urlencoded ' + index, function(t) { - runTest(t, options) + runTest(t, options, index) }) })