question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add statusMessage to response

See original GitHub issue

Context

This isn’t really a feature request, as what I need is possible with nock as-is. But I spent several hours searching, reading old issues and searching through the source code to find the solution, so I thought this might help others.

I am testing code that accesses a service that sets the statusMessage of the response, as well as the statusCode. I am using nock to mock the server/service in tests of the client code. Because the service does and the client responds to it, I want to set the statusMessage of the responses provided by nock, as well as the statusCode.

Alternatives

It is possible to set the statusMessage of the response with nock 12.0.3 (and possibly older, but I have only done it with 12.0.3), without any change required. To set the statusMessage of the response, one can use a callback to provide the body of the reply and set the statusMessage via this.req.response. For example:

const scope = nock('http://www.google.com')
  .post('/echo')
  .reply(200, function (uri, requestBody) {
    this.req.response.statusMessage = 'This is the custom status message'
    return {
      status: 200,
      message: 'This is the reply body',
      ...
    }
  })

I have only tried this one form of callback, but I expect the other forms would work as well. As noted in the docs, a function, rather than an arrow function, is necessary to get the correct ‘this’.

Has the feature been requested before?

This has been discussed before in #469, #558 and #587 but these are all closed now.

If the feature request is accepted, would you be willing to submit a PR?

No need, as noted above.

Thanks all for a very nice tool!

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:5
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jsumnerscommented, Nov 30, 2020

So if an issue is labeled as “good first issue”, and hasn’t been resolved, how will new contributors know that the good starting point still needs work?

0reactions
mastermattcommented, Nov 30, 2020

Nock has the “pinned” label for that functionality. But after 100 days without any activity or a PR, there probably isn’t a point in leaving this issue open. PRs are still always welcome.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Response.statusMessage - Javascript
Executes a GET request to the given URL and calls a callback function with the request's response * @param {String} url the URL...
Read more >
Response.statusText - Web APIs | MDN
The statusText read-only property of the Response interface contains the status message corresponding to the HTTP status code in ...
Read more >
node.js - How to send a custom http status message in ...
Response object in Node.js 0.11+. You can use it like this (tested in Express 4.x): function(req, res) { res.statusMessage = "Current password does...
Read more >
How to add status_message in REST API response on
Hello, I have a requirement to include the 'status_message' in REST API response body when the third-party web service calls to ServiceNow.
Read more >
Set your status message in Teams
Note: If you have a status message set in Teams, it will not show your automatic out-of-office reply from Microsoft Outlook. DesktopMobile.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found