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!
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
allowUnmockedoption 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!
Most helpful comment
Having the exact same issue too, and @robinbobbitt's
persistsuggestion 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:
Which is not what's happening. Are we missing/misusing something, or is it a bug? Thanks :)