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.

Nock returning statusCode 200 instead of 404

See original GitHub issue

I am trying an example that matches the following code.

nock('https://api.orchestrate.io:443')
  .get('/v0/addInSetTest/0')
  .reply(404, {"message":"The requested items could not be found.","details":{"items":[{"collection":"addInSetTest","key":"0"}]},"code":"items_not_found"}, { 'content-type': 'application/json',
  date: 'Fri, 13 Jun 2014 18:51:05 GMT',
  'x-orchestrate-req-id': 'ac8d94f0-f32b-11e3-82c9-12313d2f7cdc',
  'content-length': '140',
  connection: 'Close' });

In my case, nock is returning status code 200 instead 404. Any ideas?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
vrinekcommented, Nov 23, 2016

@lordakshaya as far as I understand, Code.expect(response.statusCode).to.equal(404); passes while Code.expect(response.result.error).to.equal('Not Found'); fails.

If this is the case, I think response.result only exists on('data') or on('end') (see: https://nodejs.org/dist/latest-v6.x/docs/api/http.html#http_http_request_options_callback).

Could you please verify if this is the case?

1reaction
akshayakrshcommented, Nov 11, 2016

@vrinek apologies for the delayed response, here’s the full code:

lab.test('returns error for non-existent user', function (done) {

    var errRes = {
      'statusCode': 404,
      'error': 'Not Found',
      'message': 'The user does not exist.',
      'errorCode': 'inexistent_user'
    };

    scope = Nock('http://www.abc.com')
      .get('/test')
      .reply(404, errRes);

    var http = require('http');
    http.get('http://www.abc.com/test', function(response) {
      console.log('[Status Code]', response.statusCode);
      console.log('[Status Result]', response.result);
      console.log('[Status Error]', response.error);

      Code.expect(response.statusCode).to.equal(404);
      Code.expect(response.result.error).to.equal('Not Found');
      Code.expect(response.result.message).to.equal('The user does not exist.');
      Code.expect(response.result.errorCode).to.equal('inexistent_user');

      done();

    });
  });

And here’s the output of the test: image

Please re-open the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ASP.NET Custom 404 Returning 200 OK Instead of 404 Not ...
A 404 status code will require a Server.Transfer instead of a Request.Redirect, along with the appropriate status code written in the header.
Read more >
Solved: Getting error 404 Not Found but expecting 200 when...
Solved: Hi, I have a GET step which uses the this URL " https://inter.comp.se/v1/state/${Properties1#agreement}/tree ". Now when I run this.
Read more >
To err or not to err: 404 vs 200 - GSiteCrawler
Even custom error pages must return result-code 404 instead of 200. A search engine thinks that pages with 200 are good URLs and...
Read more >
Creating Responses - Requests-Mock - Read the Docs
status_code: The HTTP status response to return. Defaults to 200. reason: The reason text that accompanies the Status (e.g. 'OK' in '200 OK')....
Read more >
nock - npm
You can specify the return status code for a path on the first argument of ... nock('http://myapp.iriscouch.com').get('/users/1').reply(404).
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