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

allowUnmocked causes no requests to match when query is specified #490

Closed
robinbobbitt opened this issue Mar 1, 2016 · 3 comments
Closed

Comments

@robinbobbitt
Copy link

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

nock('https://www.google.com', 
    { allowUnmocked: true })
    .log(console.log)
    .get("/abc")
    .query({'user':'fred'})
    .reply(200, "Match!")

request('https://www.google.com/abc?user=fred', function (error, response, body) {
    if (!error && response.statusCode == 200) {
        console.log(body);
    } else{
        console.log(error ? "error: " + error : "response.statusCode: "+ response.statusCode);
    }
});

With the code above, my request does NOT match and goes to google.com.
$ node nockTest.js
response.statusCode: 404

By changing allowUnmocked to false, I get:

$ node nockTest.js
matching https://www.google.com:443?user=fred to GET https://www.google.com:443/abc with query({"user":"fred"}): true
Match!

I am able to work around this and keep allowUnmocked=true by using a filteringPath to remove the query. Is this a bug or am I missing something here? Thanks!

@pgte
Copy link
Member

pgte commented Mar 1, 2016

@robinbobbitt sounds like a bug around the behavior dependent on allowUnmocked being true.

@Elergy
Copy link
Contributor

Elergy commented Jun 19, 2016

Yes, when we have allowUnmocked:true, request is mocked when it exactly matches to url we passed to get.

For example, this variant gives us expected result.

let nock=require('nock');
let request=require('request');

nock('https://www.google.com',
    { allowUnmocked: true })
    .get('/abc?user=fred')
    .reply(200, 'Match!');

request('https://www.google.com/abc?user=fred', (error, response, body) => {
    if (!error && response.statusCode == 200) {
        console.log('response: ' + body);
    } else {
        console.log(error ? 'error: ' + error : 'response.statusCode: ' + response.statusCode);
    }
});

But if we pass to get something different, like RegEx, Function, string with path without parameters, we'll fail.

@lock
Copy link

lock bot commented Sep 13, 2018

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!

@lock lock bot locked as resolved and limited conversation to collaborators Sep 13, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants