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

response body comes back with status code when a callback is passed to .reply() #2218

Open
iamwizzdom opened this issue Jul 23, 2021 · 7 comments
Labels
support General questions or support.

Comments

@iamwizzdom
Copy link

iamwizzdom commented Jul 23, 2021

What is the expected behavior?

The reply callback returns an array with index 0 as status code and index 1 as response body. I expect the my api call to respond with the request body specified in index 1 and not return the entire array with the status code.

What is the actual behavior?

The entire array is with status code is returned as request response.

Possible solution

Return just the body specified in index 1

How to reproduce the issue

nock('http://www.amazon.com')
  .post('/search')
  .reply((uri, requestBody) => {
    return [
      200,
      {"THIS IS THE REPLY BODY"},
      { header: 'value' }, // optional headers
    ]
  });

Runkit: Example link

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?

Versions

Software Version(s)
Nock 13.1.1
Node 15.4.0
TypeScript Using JavaScript
@paulmelnikow
Copy link
Member

Hi! can you fill out the bottom of the template?

@iamwizzdom
Copy link
Author

Done

@paulmelnikow
Copy link
Member

The example you provided isn't valid JavaScript:

{"THIS IS THE REPLY BODY"}

@paulmelnikow
Copy link
Member

This use case seems to work fine. I added a test case in #2219.

@paulmelnikow paulmelnikow added the support General questions or support. label Jul 23, 2021
@iamwizzdom
Copy link
Author

iamwizzdom commented Jul 23, 2021

The example you provided isn't valid JavaScript:

{"THIS IS THE REPLY BODY"}

Come on. I put that there just to show you that I understand that that's where the reply body should be. Of course it's an object.

@iamwizzdom
Copy link
Author

iamwizzdom commented Jul 23, 2021

The example you provided isn't valid JavaScript:

{"THIS IS THE REPLY BODY"}

Come on. I put that there just to show you that I understand that that's where the response body should be. Of course it's an object.

My exact use case looks something like this:

nock('https://amazon.com')
  .get('/search')
  .reply((uri, requestBody) => {
    
     let url new URL(`https://amazon.com${uri}`);
     let searchParams = url.searchParams;

     if (!searchParams.get('exactTerms')) {

            return [
                StatusCodes.INTERNAL_SERVER_ERROR,
                companyEventsResearchResponse
            ];

     }

     return [
         StatusCodes.OK,
         companyEventsResearchResponse
     ];

});

@paulmelnikow
Copy link
Member

Sorry, didn't mean to upset you. I was trying to determine if this is a bug in Nock, or if there was something strange about your use case that was causing this, which is why I wanted to try to reproduce the test case you reported. I added a test in #2219 which confirms that the behavior matches what you expect so I do not think this is a bug in Nock. For usage and support questions we primarily use StackOverflow. Please tag the questions with nock so the community can easily find it. https://stackoverflow.com/questions/tagged/nock

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

No branches or pull requests

2 participants