Nock: allowUnmocked stops working once interceptor is removed

Created on 1 Mar 2016  ·  5Comments  ·  Source: nock/nock

var nock=require('nock'),
request=require('request');

nock('https://www.google.com', 
    { allowUnmocked: true })
    .log(console.log)
    .filteringPath(function(path) {
         return path.split("?")[0];
     })
    .get("/abc")
    .reply(200, "Match!")

function mockRequest(url) {
    request(url, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log(body);
        } else{
            console.log(error ? "error: " + error : "response.statusCode: "+ response.statusCode);
        }
    });
}

mockRequest('https://www.google.com/123?user=fred');
mockRequest('https://www.google.com/abc?user=fred');
mockRequest('https://www.google.com/123?user=fred');

In the code above, I would expect the first and third request to go through to google.com. However, the third request hits the "no match" error from Nock, as seen here:

$ node nockTest.js
matching https://www.google.com:443 to GET https://www.google.com:443/abc: true
error: Error: Nock: No match for request GET https://www.google.com/123?user=fred
Match!
response.statusCode: 404

I am able to get around this by using persist() but would not expect to have to do this. Is this a bug or as designed? Thanks!

help wanted

Most helpful comment

Having the exact same issue too, and @robinbobbitt's persist suggestion works for me too.

But like him, I'm wondering if this is a bug or working as designed; the Doc / Allow unmocked requests on a mocked hostname says:

If you need some request on the same host name to be mocked and some others to really go through the HTTP stack, you can use the allowUnmocked option like this:

options = {allowUnmocked: true};
var scope = nock('http://my.existing.service.com', options)
  .get('/my/url')
  .reply(200, 'OK!');

 // GET /my/url => goes through nock
 // GET /other/url => actually makes request to the server

Which is not what's happening. Are we missing/misusing something, or is it a bug? Thanks :)

All 5 comments

Having the exact same issue.
Thanks @robinbobbitt for the persist suggestion.

Having the exact same issue too, and @robinbobbitt's persist suggestion works for me too.

But like him, I'm wondering if this is a bug or working as designed; the Doc / Allow unmocked requests on a mocked hostname says:

If you need some request on the same host name to be mocked and some others to really go through the HTTP stack, you can use the allowUnmocked option like this:

options = {allowUnmocked: true};
var scope = nock('http://my.existing.service.com', options)
  .get('/my/url')
  .reply(200, 'OK!');

 // GET /my/url => goes through nock
 // GET /other/url => actually makes request to the server

Which is not what's happening. Are we missing/misusing something, or is it a bug? Thanks :)

Same issue here. I submitted a fix: #762.

Same issue here, had to use persist() or cleanAll() to get around it.
Wondering if something is wrong with the submitted PR.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue and add a reference to this one if it’s related. Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kevinburkeshyp picture kevinburkeshyp  ·  15Comments

paulmelnikow picture paulmelnikow  ·  25Comments

gabrielf picture gabrielf  ·  19Comments

itsovereasy picture itsovereasy  ·  22Comments

RichardLitt picture RichardLitt  ·  29Comments