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

Arrays returned from interceptor functions are parsed as [statusCode, body] even if status Code is given in the call #1584

Closed
henhal opened this issue Jun 13, 2019 · 2 comments

Comments

@henhal
Copy link

henhal commented Jun 13, 2019

What is the expected behavior?

An interceptor using statusCode and a function such as .reply(200, function () { return [1, 2, 3, 5, 8, 13];}) should return the array as the response body

What is the actual behavior?

If a interceptor reply function returns an array, it appears to be always assumed to be a [statusCode, responseBody] sort of array, meaning in the example above, statusCode will be 1 and responseBody will be 2, instead of statusCode being 200 and responseBody being [1, 2, 3, 5, 8, 13]
Possible solution

Do not treat arrays returned from the function as a [statusCode, responseBody] array if the statusCode is already given in the reply call:

  • Using reply(function (url) { }) - function MUST return array of [statusCode, responseBody]
  • Using reply(200, function (url) { }) - returned value from the function is used as the responseBody regardless of its type

How to reproduce the issue

nock('somehost').get('/foo').reply(200, function () { return [1, 2, 3]; })

This will return HTTP 1, and the second element of the array, 2 as the body, instead of HTTP 200 and the array itself as the body.

Funnily enough, if the first element is not statusCode-like, the status code is still sent as 200, but the array is still picked for element 1 as the responseBody. E.g. if returning an array of objects like this:

.reply(200, function () { return [{a:1}, {a:2}, {a:3}];})

Then this route will return

200 OK

{"a": 2}

This scenario is what kept me busy for an hour or so before understanding why my returned array was not an array on the client side. :/

Having problem producing a test case? Try and ask the community for help. If the test case cannot be reproduced, the Nock community might not be able to help you.

Does the bug have a test case?

No

Versions

Software Version(s)
Nock 10.0.6
Node 8.12.0
@mastermatt
Copy link
Member

@henhal this is expected behavior currently, but is changing with the next major release to exactly what you are suggesting.

Could you test the latest version of the beta branch and verify it works for your needs?

@paulmelnikow
Copy link
Member

Am I correct that #1520 is where this was fixed?

If so you can test this by using 11.0.0-beta.15 or later.

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

No branches or pull requests

3 participants