Skip to content

Commit

Permalink
Merge branch 'master' of github.com:mikeal/request
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeal committed Aug 12, 2011
2 parents c63e6e9 + 4bf5861 commit ef767d1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
7 changes: 6 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,12 @@ Request.prototype.request = function () {
})
options.on("end", function () {
response.body = buffer
options.callback(null, response, buffer)
if (options.json) {
try {
response.body = JSON.parse(response.body)
} catch (e) {}
}
options.callback(null, response, response.body)
})
}
}
Expand Down
5 changes: 3 additions & 2 deletions tests/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ exports.createPostValidator = function (text) {
}
return l;
}
exports.createGetResponse = function (text) {
exports.createGetResponse = function (text, contentType) {
var l = function (req, resp) {
resp.writeHead(200, {'content-type':'text/plain'})
contentType = contentType || 'text/plain'
resp.writeHead(200, {'content-type':contentType})
resp.write(text)
resp.end()
}
Expand Down
8 changes: 6 additions & 2 deletions tests/test-body.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ var tests =
{ resp : server.createGetResponse("TESTING!")
, expectBody: "TESTING!"
}
, testGetJSON :
{ resp : server.createGetResponse('{"test":true}', 'application/json')
, json : true
, expectBody: {"test":true}
}
, testPutString :
{ resp : server.createPostValidator("PUTTINGDATA")
, method : "PUT"
Expand Down Expand Up @@ -60,8 +65,7 @@ for (i in tests) {
request(test, function (err, resp, body) {
if (err) throw err;
if (test.expectBody) {
if (test.expectBody !== body) console.log(test.expectBody, body);
assert.ok(test.expectBody === body)
assert.deepEqual(test.expectBody, body)
}
counter = counter - 1;
if (counter === 0) {
Expand Down

0 comments on commit ef767d1

Please sign in to comment.