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

Enable non-object JSON bodies #1282

Merged
merged 5 commits into from Dec 2, 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
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
node_modules
.idea
2 changes: 1 addition & 1 deletion request.js
Expand Up @@ -1482,7 +1482,7 @@ Request.prototype.json = function (val) {

self._json = true
if (typeof val === 'boolean') {
if (typeof self.body === 'object') {
if (self.body !== undefined) {
self.body = safeStringify(self.body)
if (!self.hasHeader('content-type')) {
self.setHeader('content-type', 'application/json')
Expand Down
18 changes: 18 additions & 0 deletions tests/server.js
Expand Up @@ -77,6 +77,24 @@ exports.createPostValidator = function (text, reqContentType) {
}
return l
}
exports.createPostJSONValidator = function (value, reqContentType) {
var l = function (req, resp) {
var r = ''
req.on('data', function (chunk) {r += chunk})
req.on('end', function () {
var parsedValue = JSON.parse(r)
assert.deepEqual(parsedValue, value)
if (reqContentType) {
assert.ok(req.headers['content-type'])
assert.ok(~req.headers['content-type'].indexOf(reqContentType))
}
resp.writeHead(200, {'content-type':'application/json'})
resp.write(r)
resp.end()
})
}
return l
}
exports.createGetResponse = function (text, contentType) {
var l = function (req, resp) {
contentType = contentType || 'text/plain'
Expand Down
54 changes: 54 additions & 0 deletions tests/test-json-request.js
@@ -0,0 +1,54 @@
'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()
})
})

function testJSONValue(testId, value) {
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,
body: value
}
request(opts, function (err, resp, body) {
t.equal(err, null)
Copy link
Member

Choose a reason for hiding this comment

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

I wouldn't mind if we have t.deepEqual(value, body) here just to make sure (and clear) we're parsing back the value correctly. Currently that's not possible because createPostValidator is returning plain text, and in the case of this test it's checking only for the presence of the content-type header and if the value was stringified correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have updated the test to do this verification using a new createPostJSONValidator method that returns a JSON response.

t.equal(resp.statusCode, 200)
t.deepEqual(body, value)
t.end()
})
})
}

testJSONValue('jsonNull', null)
testJSONValue('jsonTrue', true)
testJSONValue('jsonFalse', false)
testJSONValue('jsonNumber', -289365.2938)
testJSONValue('jsonString', 'some string')
testJSONValue('jsonArray', ['value1', 2, null, 8925.53289, true, false, ['array'], { object: 'property' }])
testJSONValue('jsonObject', {
trueProperty: true,
falseProperty: false,
numberProperty: -98346.34698,
stringProperty: 'string',
nullProperty: null,
arrayProperty: ['array'],
objectProperty: { object: 'property' }
})

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