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

Implement rfc3986 option #1315

Merged
merged 5 commits into from Dec 23, 2014
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
20 changes: 17 additions & 3 deletions request.js
Expand Up @@ -253,6 +253,13 @@ function responseToJSON() {
}
}

// encode rfc3986 characters
function rfc3986 (str) {
return str.replace(/[!'()*]/g, function(c) {
return '%' + c.charCodeAt(0).toString(16).toUpperCase()
})
}

function Request (options) {
// if tunnel property of options was not given default to false
// if given the method property in options, set property explicitMethod to true
Expand Down Expand Up @@ -1402,7 +1409,9 @@ Request.prototype.qs = function (q, clobber) {
return self
}

self.uri = url.parse(self.uri.href.split('?')[0] + '?' + self.qsLib.stringify(base))
var qs = self.qsLib.stringify(base)

self.uri = url.parse(self.uri.href.split('?')[0] + '?' + rfc3986(qs))
self.url = self.uri
self.path = self.uri.path

Expand All @@ -1413,6 +1422,7 @@ Request.prototype.form = function (form) {
if (form) {
self.setHeader('content-type', 'application/x-www-form-urlencoded')
self.body = (typeof form === 'string') ? form.toString('utf8') : self.qsLib.stringify(form).toString('utf8')
self.body = rfc3986(self.body)
return self
}
// create form-data object
Expand Down Expand Up @@ -1482,14 +1492,18 @@ Request.prototype.json = function (val) {

self._json = true
if (typeof val === 'boolean') {
if (self.body !== undefined && !/^application\/x-www-form-urlencoded\b/.test(self.getHeader('content-type'))) {
self.body = safeStringify(self.body)
if (self.body !== undefined) {
if (!/^application\/x-www-form-urlencoded\b/.test(self.getHeader('content-type'))) {
self.body = safeStringify(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
77 changes: 77 additions & 0 deletions tests/test-rfc3986.js
@@ -0,0 +1,77 @@
'use strict'

var http = require('http')
, request = require('../index')
, tape = require('tape')


function runTest (t, options) {

var server = http.createServer(function(req, res) {

var data = ''
req.setEncoding('utf8')

req.on('data', function(d) {
data += d
})

req.on('end', function() {
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"}')
}

res.writeHead(200)
res.end('done')
})
})

server.listen(8080, function() {

request.post('http://localhost:8080', options, function(err, res, body) {
t.equal(err, null)
server.close()
t.end()
})
})
}

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

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