Skip to content

Commit

Permalink
Merge pull request #1299 from rodati/master
Browse files Browse the repository at this point in the history
Add optional support for jsonReviver
  • Loading branch information
nylen committed Dec 9, 2014
2 parents 998831d + 98821ed commit 6f6e398
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -538,6 +538,7 @@ The first argument can be either a `url` or an `options` object. The only requir
body streams are not allowed.
* `auth` - A hash containing values `user` || `username`, `pass` || `password`, and `sendImmediately` (optional). See documentation above.
* `json` - sets `body` but to JSON representation of value and adds `Content-type: application/json` header. Additionally, parses the response body as JSON.
* `jsonReviver` - a [reviver function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse) that will be passed to `JSON.parse()` when parsing a JSON response body.
* `preambleCRLF` - append a newline/CRLF before the boundary of your `multipart/form-data` request.
* `postambleCRLF` - append a newline/CRLF at the end of the boundary of your `multipart/form-data` request.
* `followRedirect` - follow HTTP 3xx responses as redirects (default: `true`). This property can also be implemented as function which gets `response` object as a single argument and should return `true` if redirects should continue or `false` otherwise.
Expand Down
6 changes: 5 additions & 1 deletion request.js
Expand Up @@ -1308,7 +1308,7 @@ Request.prototype.onRequestResponse = function (response) {

if (self._json) {
try {
response.body = JSON.parse(response.body)
response.body = JSON.parse(response.body, self._jsonReviver)
} catch (e) {}
}
debug('emitting complete', self.uri.href)
Expand Down Expand Up @@ -1495,6 +1495,10 @@ Request.prototype.json = function (val) {
}
}

if (typeof self.jsonReviver === 'function') {
self._jsonReviver = self.jsonReviver
}

return self
}
Request.prototype.getHeader = function (name, headers) {
Expand Down
25 changes: 25 additions & 0 deletions tests/test-json-request.js
Expand Up @@ -32,6 +32,26 @@ function testJSONValue(testId, value) {
})
}

function testJSONValueReviver(testId, value, reviver, revivedValue) {
tape('test ' + testId, function(t) {
var testUrl = '/' + testId
s.on(testUrl, server.createPostJSONValidator(value, 'application/json'))
var opts = {
method: 'PUT',
uri: s.url + testUrl,
json: true,
jsonReviver: reviver,
body: value
}
request(opts, function (err, resp, body) {
t.equal(err, null)
t.equal(resp.statusCode, 200)
t.deepEqual(body, revivedValue)
t.end()
})
})
}

testJSONValue('jsonNull', null)
testJSONValue('jsonTrue', true)
testJSONValue('jsonFalse', false)
Expand All @@ -48,6 +68,11 @@ testJSONValue('jsonObject', {
objectProperty: { object: 'property' }
})

testJSONValueReviver('jsonReviver', -48269.592, function (k, v) {
return v * -1
}, 48269.592)
testJSONValueReviver('jsonReviverInvalid', -48269.592, 'invalid reviver', -48269.592)

tape('cleanup', function(t) {
s.close()
t.end()
Expand Down

0 comments on commit 6f6e398

Please sign in to comment.