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 + query string = interception fail #1421

Closed
NatalieWolfe opened this issue Feb 1, 2019 · 4 comments
Closed

allowUnmocked + query string = interception fail #1421

NatalieWolfe opened this issue Feb 1, 2019 · 4 comments

Comments

@NatalieWolfe
Copy link

What is the expected behavior?

When allowUnmocked is set to true there should be no change in how nock matches requests.

What is the actual behavior?

When allowUnmocked is set to true, query strings must be specified using the .query function, not in the path string.

Possible solution

This is sort of the opposite problem as #490, which was fixed in #955. That fix may have introduced this problem.

How to reproduce the issue

'use strict'

const nock = require('nock')
const http = require('http')

nock('http://localhost', {allowUnmocked: true})
  .get('/foo?bar=baz')
  .reply(418, 'Teapots')

const req = http.get('/foo?bar=baz', (res) => {
  console.log(res.statusCode)
})

req.on('error', console.error)

Does the bug have a test case?

Versions

Software Version(s)
Nock 9.1.9, 10.0.6
Node 8.12.0, 10.15.1
@stale
Copy link

stale bot commented May 2, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. We try to do our best, but nock is maintained by volunteers and there is only so much we can do at a time. Thank you for your contributions.

@stale stale bot added the stale label May 2, 2019
@paulmelnikow
Copy link
Member

paulmelnikow commented May 2, 2019

Thanks for the test case!

Confirmed in 86c5c4f that the above prints:

{ Error: getaddrinfo ENOTFOUND localhost:80 localhost:80:80
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:57:26)
  errno: 'ENOTFOUND',
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'localhost:80',
  host: 'localhost:80',
  port: 80 }

While removing { allowUnmocked: true } causes it to print:

418

Both versions work correctly if you use .query({ bar: 'baz' }) instead of the literal query string.

We don't outright say that literals are supported, though there is one example that uses them, and some tests which sort of imply as much.

Here are some todos:

  • Fix the bug
  • Add tests for query string literals, both with and without allowUnmocked
  • Say explicitly in the readme that query string literals are allowed

@stale stale bot removed the stale label May 2, 2019
mastermatt added a commit to mastermatt/nock that referenced this issue Jul 10, 2019
fixes: nock#1421

There was inconsistent behavior around how search params were handled
when they were provided as part of a URI string to new Interceptors.
The issue happen to come to light when `allowUnmocked` was set.

The fix normalizes the behavior by refactoring the constructor of
Interceptor to look for literal search params. If present, they're
stripped from the `path` attribute and set via the `query` method.

As part of the work, `Interceptor.query` was modified to accept
`URLSearchParams` instances as valid input.
mastermatt added a commit to mastermatt/nock that referenced this issue Jul 10, 2019
fixes: nock#1421

There was inconsistent behavior around how search params were handled
when they were provided as part of a URI string to new Interceptors.
The issue happen to come to light when `allowUnmocked` was set.

The fix normalizes the behavior by refactoring the constructor of
Interceptor to look for literal search params. If present, they're
stripped from the `path` attribute and set via the `query` method.

As part of the work, `Interceptor.query` was modified to accept
`URLSearchParams` instances as valid input.
mastermatt added a commit to mastermatt/nock that referenced this issue Jul 13, 2019
fixes: nock#1421

There was inconsistent behavior around how search params were handled
when they were provided as part of a URI string to new Interceptors.
The issue happen to come to light when `allowUnmocked` was set.

The fix normalizes the behavior by refactoring the constructor of
Interceptor to look for literal search params. If present, they're
stripped from the `path` attribute and set via the `query` method.

As part of the work, `Interceptor.query` was modified to accept
`URLSearchParams` instances as valid input.
mastermatt added a commit to mastermatt/nock that referenced this issue Jul 15, 2019
fixes: nock#1421

There was inconsistent behavior around how search params were handled
when they were provided as part of a URI string to new Interceptors.
The issue happen to come to light when `allowUnmocked` was set.

The fix normalizes the behavior by refactoring the constructor of
Interceptor to look for literal search params. If present, they're
stripped from the `path` attribute and set via the `query` method.

As part of the work, `Interceptor.query` was modified to accept
`URLSearchParams` instances as valid input.
mastermatt added a commit that referenced this issue Jul 15, 2019
fixes: #1421

There was inconsistent behavior around how search params were handled
when they were provided as part of a URI string to new Interceptors.
The issue happen to come to light when `allowUnmocked` was set.

The fix normalizes the behavior by refactoring the constructor of
Interceptor to look for literal search params. If present, they're
stripped from the `path` attribute and set via the `query` method.

As part of the work, `Interceptor.query` was modified to accept
`URLSearchParams` instances as valid input.
@nockbot
Copy link
Collaborator

nockbot commented Jul 15, 2019

🎉 This issue has been resolved in version 11.0.0-beta.25 🎉

The release is available on:

Your semantic-release bot 📦🚀

@nockbot
Copy link
Collaborator

nockbot commented Aug 13, 2019

🎉 This issue has been resolved in version 11.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

gr2m pushed a commit that referenced this issue Sep 4, 2019
fixes: #1421

There was inconsistent behavior around how search params were handled
when they were provided as part of a URI string to new Interceptors.
The issue happen to come to light when `allowUnmocked` was set.

The fix normalizes the behavior by refactoring the constructor of
Interceptor to look for literal search params. If present, they're
stripped from the `path` attribute and set via the `query` method.

As part of the work, `Interceptor.query` was modified to accept
`URLSearchParams` instances as valid input.
gr2m pushed a commit that referenced this issue Sep 4, 2019
fixes: #1421

There was inconsistent behavior around how search params were handled
when they were provided as part of a URI string to new Interceptors.
The issue happen to come to light when `allowUnmocked` was set.

The fix normalizes the behavior by refactoring the constructor of
Interceptor to look for literal search params. If present, they're
stripped from the `path` attribute and set via the `query` method.

As part of the work, `Interceptor.query` was modified to accept
`URLSearchParams` instances as valid input.
gr2m pushed a commit that referenced this issue Sep 5, 2019
fixes: #1421

There was inconsistent behavior around how search params were handled
when they were provided as part of a URI string to new Interceptors.
The issue happen to come to light when `allowUnmocked` was set.

The fix normalizes the behavior by refactoring the constructor of
Interceptor to look for literal search params. If present, they're
stripped from the `path` attribute and set via the `query` method.

As part of the work, `Interceptor.query` was modified to accept
`URLSearchParams` instances as valid input.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants