You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
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 bodyWhat 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 be1
andresponseBody
will be2
, instead ofstatusCode
being200
andresponseBody
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:reply(function (url) { })
- function MUST return array of [statusCode, responseBody]reply(200, function (url) { })
- returned value from the function is used as the responseBody regardless of its typeHow 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
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
The text was updated successfully, but these errors were encountered: