7

When I stub request with nock it returns String result instead of Object even with 'Content-Type': 'application/json':

var response = {
  success: true,
  statusCode: 200,
  body: {
    "status": "OK",
    "id": "05056b27b82",
  }
};

Test.BuildRequest();
Test.SendRequest(done);

nock('https://someapi.com')
  // also tried
  // .defaultReplyHeaders({
  //   'Content-Type': 'application/json',
  //   'Accept': 'application/json'
  // })

  .post('/order')
  .reply(200, response.body, 
    'Content-Type': 'application/json',
    'Accept': 'application/json');

checking:

console.log(put.response.body);
console.log(put.response.body.id);

output:

{"status":"OK","id":"05056b27b82"}
undefined

In code I use request module that returns Object with the same data. I tried also sinon (doesn't work for me) and fakeweb but got the same issue.

My code, which i'm trying to test:

var request = require('request');
// ...

request(section.request, function (err, response, body) {
  if (err || _.isEmpty(response))
    return result(err, curSyndication);

  //if (_.isString(body))
  //    body = JSON.parse(body);

  section.response.body = body;
  console.log(body.id); // => undefined (if uncomment previous code - 05056b27b82)

  _this.handleResponse(section, response, body, result);
});

And it returns an object in real requests.

PS. I could add next code in my response handler:

if (_.isString(body))
  body = JSON.parse(body);

But some of queries returns xml string, and i'm not responsible for such changes.

Fakeweb:

fakeweb.registerUri({
  uri: 'https://someapi.com/order',
  body: JSON.stringify({
    status: "OK",
    id: "05056b27b82",
  }),
  statusCode: 200,
  headers: {
    'User-Agent': 'My requestor',
    'Content-Type': 'application/json',
    'Accept': 'application/json'
  }
});
Test.SendRequest(done);

Same results.

Updated:

I read a couple of articles, that uses JSON Object, without parsing it (with nock), so it should returns JSON object, same way how request library do that.

zishe
  • 10,055
  • 12
  • 60
  • 101
  • nock returns JSON. To convert to Object you need to convert the JSON to Object using `JSON.parse` --> http://jsfiddle.net/5qaxtfz6/ – Karthik Chintala Dec 31 '14 at 06:09
  • I updated my answer. [Request](https://github.com/request/request) module returns an Object – zishe Dec 31 '14 at 06:17
  • In [this question](http://stackoverflow.com/questions/14689252/how-can-superagent-and-nock-work-together) it returns an Object. – zishe Dec 31 '14 at 06:24
  • 1
    Have you specified the `json` option on your `section.request` object? So do you have `section.request.json = true` somewhere in your code? Because it is need for `request` to "additionally, parses the response body as JSON." – nemesv Jan 02 '15 at 13:55
  • That solves my problem, thanks! You can post the answer for awards. – zishe Jan 02 '15 at 14:03

1 Answers1

8

There is nothing wrong with your nock configuration however you haven't told request to parse the response as JSON.

From the request method documentation (emphasis on me):

json - sets body but to JSON representation of value and adds Content-type: application/json header. Additionally, parses the response body as JSON.

The callback argument gets 3 arguments:

  • An error when applicable (usually from http.ClientRequest object)
  • An http.IncomingMessage object
  • The third is the response body (String or Buffer, or JSON object if the json option is supplied)

So you need to set the json property to true on your section.request object:

var request = require('request');
// ...

section.request.json = true;
request(section.request, function (err, response, body) {
  //..
});
Community
  • 1
  • 1
nemesv
  • 133,215
  • 15
  • 395
  • 348