Skip to content

Commit

Permalink
Merge pull request #1396 from simov/fix-json-rfc3986
Browse files Browse the repository at this point in the history
Do not rfc3986 escape JSON bodies
  • Loading branch information
nylen committed Feb 2, 2015
2 parents 1fd0752 + 29f98f3 commit 4475734
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 27 deletions.
4 changes: 2 additions & 2 deletions request.js
Expand Up @@ -1428,15 +1428,15 @@ Request.prototype.json = function (val) {
if (self.body !== undefined) {
if (!/^application\/x-www-form-urlencoded\b/.test(self.getHeader('content-type'))) {
self.body = safeStringify(self.body)
} else {
self.body = rfc3986(self.body)
}
self.body = rfc3986(self.body)
if (!self.hasHeader('content-type')) {
self.setHeader('content-type', 'application/json')
}
}
} else {
self.body = safeStringify(val)
self.body = rfc3986(self.body)
if (!self.hasHeader('content-type')) {
self.setHeader('content-type', 'application/json')
}
Expand Down
76 changes: 51 additions & 25 deletions tests/test-rfc3986.js
Expand Up @@ -20,20 +20,7 @@ function runTest (t, options) {
if (options.qs) {
t.equal(req.url, '/?rfc3986=%21%2A%28%29%27')
}
if (options.form) {
t.equal(data, 'rfc3986=%21%2A%28%29%27')
}
if (options.body) {
if (options.headers) {
t.equal(data, 'rfc3986=%21%2A%28%29%27')
}
else {
t.equal(data, '{"rfc3986":"%21%2A%28%29%27"}')
}
}
if (typeof options.json === 'object') {
t.equal(data, '{"rfc3986":"%21%2A%28%29%27"}')
}
t.equal(data, options._expectBody)

res.writeHead(200)
res.end('done')
Expand All @@ -51,28 +38,67 @@ function runTest (t, options) {
})
}

var bodyEscaped = 'rfc3986=%21%2A%28%29%27'
, bodyJson = '{"rfc3986":"!*()\'"}'

var cases = [
{qs: {rfc3986: '!*()\''}},
{qs: {rfc3986: '!*()\''}, json: true},
{form: {rfc3986: '!*()\''}},
{form: {rfc3986: '!*()\''}, json: true},
{qs: {rfc3986: '!*()\''}, form: {rfc3986: '!*()\''}},
{qs: {rfc3986: '!*()\''}, form: {rfc3986: '!*()\''}, json: true},
{
_name: 'qs',
qs: {rfc3986: '!*()\''},
_expectBody: ''
},
{
_name: 'qs + json',
qs: {rfc3986: '!*()\''},
json: true,
_expectBody: ''
},
{
_name: 'form',
form: {rfc3986: '!*()\''},
_expectBody: bodyEscaped
},
{
_name: 'form + json',
form: {rfc3986: '!*()\''},
json: true,
_expectBody: bodyEscaped
},
{
_name: 'qs + form',
qs: {rfc3986: '!*()\''},
form: {rfc3986: '!*()\''},
_expectBody: bodyEscaped
},
{
_name: 'qs + form + json',
qs: {rfc3986: '!*()\''},
form: {rfc3986: '!*()\''},
json: true,
_expectBody: bodyEscaped
},
{
_name: 'body + header + json',
headers: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'},
body: 'rfc3986=!*()\'',
json: true
json: true,
_expectBody: bodyEscaped
},
{
body: {rfc3986: '!*()\''}, json: true
_name: 'body + json',
body: {rfc3986: '!*()\''},
json: true,
_expectBody: bodyJson
},
{
json: {rfc3986: '!*()\''}
_name: 'json object',
json: {rfc3986: '!*()\''},
_expectBody: bodyJson
}
]

cases.forEach(function (options, index) {
tape('rfc3986 ' + index, function(t) {
cases.forEach(function (options) {
tape('rfc3986 ' + options._name, function(t) {
runTest(t, options)
})
})

0 comments on commit 4475734

Please sign in to comment.