Skip to content

Commit

Permalink
Enable non-object JSON bodies
Browse files Browse the repository at this point in the history
Updated JSON requests so they can contain bodies that are not objects. For avoid unexpected surprises, JSON `null` values are not sent.
  • Loading branch information
jpage-godaddy committed Nov 21, 2014
1 parent e543acf commit 4f95428
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
node_modules
.idea
2 changes: 1 addition & 1 deletion request.js
Expand Up @@ -1481,7 +1481,7 @@ Request.prototype.json = function (val) {

self._json = true
if (typeof val === 'boolean') {
if (typeof self.body === 'object') {
if (self.body !== undefined && self.body !== null) {
self.body = safeStringify(self.body)
if (!self.hasHeader('content-type')) {
self.setHeader('content-type', 'application/json')
Expand Down
47 changes: 47 additions & 0 deletions tests/test-json-request.js
@@ -0,0 +1,47 @@
'use strict'

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

var s = server.createServer()

tape('setup', function(t) {
s.listen(s.port, function() {
t.end()
})
})

tape('test testPutBoolean', function(t) {
s.on('/testPutBoolean', server.createPostValidator('true', 'application/json'))
var opts = {
method: 'PUT',
uri: s.url + '/testPutBoolean',
json: true,
body: true
}
request(opts, function (err, resp, body) {
t.equal(err, null)
t.end()
})
})

tape('test testPutNull', function(t) {
s.on('/testPutNull', server.createPostValidator(''))
var opts = {
method: 'PUT',
uri: s.url + '/testPutNull',
json: true,
body: null
}
request(opts, function (err, resp, body) {
t.equal(err, null)
t.end()
})
})

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

0 comments on commit 4f95428

Please sign in to comment.