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

Do not rfc3986 escape JSON bodies #1396

Merged
merged 2 commits into from Feb 2, 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: 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you swap this conditional around? Also, I see we have a test for it (body + header + json) but in what situation is this needed?

edit - I think I see - this allows parsing a JSON response without sending the body as JSON.

}
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)
})
})