Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow content-type overriding when using the form option #1650

Merged
merged 1 commit into from Jun 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
})
})