Skip to content

Commit

Permalink
Merge pull request #1315 from simov/rfc3986
Browse files Browse the repository at this point in the history
Implement rfc3986 option
  • Loading branch information
nylen committed Dec 23, 2014
2 parents f48a577 + bfbba46 commit 37fbff4
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 3 deletions.
20 changes: 17 additions & 3 deletions request.js
Expand Up @@ -254,6 +254,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 @@ -1403,7 +1410,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 @@ -1414,6 +1423,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 @@ -1497,14 +1507,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)
})
})

0 comments on commit 37fbff4

Please sign in to comment.