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

Unable to return an array as valid JSON response #1228

Closed
motss opened this issue Sep 18, 2018 · 11 comments
Closed

Unable to return an array as valid JSON response #1228

motss opened this issue Sep 18, 2018 · 11 comments
Labels
stale support General questions or support.

Comments

@motss
Copy link

motss commented Sep 18, 2018

What is the expected behavior?

// Reduced test case
const data = ['haha', 'lol', 'wow'];

nock(url)
    .persist(true)
    .log(console.log)
    .defaultReplyHeaders({
      'content-type': 'application/json',
    })
    .get(uri => /^\/response\//i.test(uri))
    .reply(200, () => {
      return data; // This returns ['haha', 'lol', 'wow']
    });

What is the actual behavior?

// Reduced test case
const data = ['haha', 'lol', 'wow'];

nock(url)
    .persist(true)
    .log(console.log)
    .defaultReplyHeaders({
      'content-type': 'application/json',
    })
    .get(uri => /^\/response\//i.test(uri))
    .reply(200, () => {
      return data; // This does not return ['haha', 'lol', 'wow']
    });

Possible solution

// Reduced test case
const data = ['haha', 'lol', 'wow'];

nock(url)
    .persist(true)
    .log(console.log)
    .defaultReplyHeaders({
      'content-type': 'application/json',
    })
    .get(uri => /^\/response\//i.test(uri))
    .reply(200, () => {
-     return data;
+     return JSON.stringify(data);
    });

How to reproduce the issue

Please refer to the reduced test case provided above.

Does the bug have a test case?

No

Versions

Software Version(s)
Nock 10.0.0
Node 10.10.0
@RichardLitt
Copy link
Member

What does data return, instead of the array?

@RichardLitt RichardLitt added the support General questions or support. label Sep 20, 2018
@nguymin4
Copy link

nguymin4 commented Sep 24, 2018

Hi, @RichardLitt, I think there is a breaking change when upgrading from 9.6.1 to 10.0.0 here is the result and a simplified test - tested with Node.js v8.12.0 and v10.11.0

In nock@9.6.1 the result is ['haha', 'lol', 'wow']
In nock@10.0.0 the result is lol

If this is intentional then should we update the change log and the doc?

const request = require('request-promise');
const nock = require('nock');

async function main() {
  const data = ['haha', 'lol', 'wow'];
  nock('http://localhost')
    .get('/')
    .reply(200, () => data);

  const result = await request('http://localhost');
  console.log(result);
}

main();

@RichardLitt
Copy link
Member

I don't think that this was intentional.

I think the best thing to do at this point would be to start a PR with tests for this, and then we can implement your suggested fix and see if that solves the problem. What do you think about that? Would you like to help?

@MarshallRJ
Copy link

What does data return, instead of the array?

I note the same issue with version 10.0.0 in my instance it returns the last element in the array.

@nguymin4
Copy link

nguymin4 commented Sep 26, 2018

@RichardLitt Yes, I can take a look at it

Update: I think this issue was introduced when we change the logic in PR https://github.com/nock/nock/pull/1203/files

I honestly don't know what is desired behavior for it. But in my opinion, the first argument for reply should always be the statusCode and the second argument can be a responseBody or a function which will return the responseBody.

@matthewheck
Copy link

Agreed with @nguymin4. I believe the fact that this test failed and had to be commented out is somewhat indicative too.

@gr2m
Copy link
Member

gr2m commented Oct 12, 2018

We could really use some help with getting that test passing

@gr2m
Copy link
Member

gr2m commented Oct 12, 2018

I honestly don't know what is desired behavior for it. But in my opinion, the first argument for reply should always be the statusCode and the second argument can be a responseBody or a function which will return the responseBody.

that sounds reasonable to me

@modestfake
Copy link

Seems like issue #1208 is related to this one.

@stale
Copy link

stale bot commented Mar 23, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. We try to do our best, but nock is maintained by volunteers and there is only so much we can do at a time. Thank you for your contributions.

@stale stale bot added the stale label Mar 23, 2019
@stale stale bot closed this as completed Mar 30, 2019
@paulmelnikow
Copy link
Member

See also #1222 (comment) which is proposing a change to this behavior which will be made in #1520.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale support General questions or support.
Projects
None yet
Development

No branches or pull requests

8 participants