Skip to content

Commit

Permalink
Merge pull request #1650 from simov/fix-form-content-type
Browse files Browse the repository at this point in the history
Allow content-type overriding when using the `form` option
  • Loading branch information
simov committed Jun 23, 2015
2 parents 1506774 + 86571c7 commit c36d7ec
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
4 changes: 3 additions & 1 deletion request.js
Expand Up @@ -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')
Expand Down
15 changes: 12 additions & 3 deletions tests/test-form-urlencoded.js
Expand Up @@ -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 = ''
Expand Down Expand Up @@ -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',
Expand All @@ -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)
})
})

0 comments on commit c36d7ec

Please sign in to comment.