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

nock does not support the new Node.js 10.9 signature http.request(url[, options][, callback]) #1227

Closed
gkalpak opened this issue Sep 16, 2018 · 8 comments

Comments

@gkalpak
Copy link

gkalpak commented Sep 16, 2018

What is the expected behavior?
Using the new signature for http.request(url, options[, callback])/http.get(url, options[, callback]) (introduced in Node.js v10.9, should work with nock.

What is the actual behavior?
With nock (by merely requireing nock), it throws:

events.js:288
    throw new errors.ERR_INVALID_ARG_TYPE('listener', 'Function', listener);
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type Function. Received type object
    at ClientRequest.once (events.js:288:11)
    at new ClientRequest (_http_client.js:150:10)
    at Object.request (http.js:41:10)
    at ...\node_modules\nock\lib\intercept.js:405:16
    at Object.module.request (...\node_modules\nock\lib\common.js:140:14)
    at requestWithUrlAndOptions (...\script.js:27:5)
    at Object.<anonymous> (...\script.js:42:1)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)

Possible solution
I haven't looked at the code, but I suspect nock assumes the second argument is always the callback and uses it as such, whereas in Node.js 10.9+, the second argument can be an object (with the third being the callback). It should be trivial to detect that situation and pass the correct arguments to http.request() (or something like that 😁).

How to reproduce the issue

Runkit: Example link

Does the bug have a test case?
(Not sure wht this means.)

Versions

Software Version(s)
Nock All (at least up to 10.0.0)
Node 10.9+
@gr2m gr2m added the bug label Sep 16, 2018
@gr2m
Copy link
Member

gr2m commented Sep 16, 2018

I’m not familiar enough with the code base myself to tell what the problem could be. The runkit notebook is much appreciated, thank you!

Would you like to get a PR going? A failing test would be good enough, you can adjust the .travis.yml file to require node 10.9

@gkalpak
Copy link
Author

gkalpak commented Sep 17, 2018

I had a brief look at the source code and tests, but I couldn't figure out where the appropriate place would be (neither for the fix nor for the test) 😕
I am sure that it is a super simple and quick fix for someone familiar with the code base.

AFAICT, tests are already being run on Node.js 10 on Travis, so that is taken care of.

And as for a failing testcase, it is as simple as http.request('some url', {some: 'options'}, someCallback) (you can also see my runkit example). Again, I am not sure about the format of the tests, but it will be very straight forward for someone familiar with the code base.

@leonrinkel
Copy link

You are correct ✅

I used the following hack in common.js:138 to quickly get it running with the new signature:

module.request = function(url, options, callback) {
  url = new URL(url);

  options.protocol = url.protocol;
  options.hostname = url.hostname;
  options.port = url.port;
  options.path = url.pathname;

  return newRequest(proto, overriddenRequest.bind(module), options, callback);
};

Of course this is not a production-ready solution.

@stale
Copy link

stale bot commented Dec 17, 2018

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 Dec 17, 2018
@gr2m gr2m added the pinned label Dec 18, 2018
@stale stale bot removed the stale label Dec 18, 2018
@gr2m
Copy link
Member

gr2m commented Dec 18, 2018

I fear nobody is super familiar with the code base right now, but we are working on cleaning that up. We just dropped support for Node < 10, come join the fun :)

@nijynot
Copy link

nijynot commented Feb 2, 2019

@gr2m I'll join in on the fun :-), just submitted a PR (#1428) that fixes this bug. Would be cool if someone could review it.

mastermatt added a commit to mastermatt/nock that referenced this issue Jun 17, 2019
Fixes nock#1227

Beginning with v10.9.0, the `url` parameter can be passed along with a
separate options object to `http[s].request`, `http[s].get`, or to the
constructor of `ClientRequest`.

https://nodejs.org/api/http.html#http_http_request_url_options_callback

The logic that does the juggling was mostly copied from Node's source.
mastermatt added a commit to mastermatt/nock that referenced this issue Jun 17, 2019
Fixes nock#1227

Beginning with v10.9.0, the `url` parameter can be passed along with a
separate options object to `http[s].request`, `http[s].get`, or to the
constructor of `ClientRequest`.

https://nodejs.org/api/http.html#http_http_request_url_options_callback

The logic that does the juggling was mostly copied from Node's source.
mastermatt added a commit to mastermatt/nock that referenced this issue Jun 18, 2019
Fixes nock#1227

Beginning with v10.9.0, the `url` parameter can be passed along with a
separate options object to `http[s].request`, `http[s].get`, or to the
constructor of `ClientRequest`.

https://nodejs.org/api/http.html#http_http_request_url_options_callback

The logic that does the juggling was mostly copied from Node's source.
mastermatt added a commit to mastermatt/nock that referenced this issue Jun 23, 2019
Fixes nock#1227

Beginning with v10.9.0, the `url` parameter can be passed along with a
separate options object to `http[s].request`, `http[s].get`, or to the
constructor of `ClientRequest`.

https://nodejs.org/api/http.html#http_http_request_url_options_callback

The logic that does the juggling was mostly copied from Node's source.
mastermatt added a commit that referenced this issue Jul 9, 2019
* Allow three arg form of `request` and `ClientRequest`.

Fixes #1227

Beginning with v10.9.0, the `url` parameter can be passed along with a
separate options object to `http[s].request`, `http[s].get`, or to the
constructor of `ClientRequest`.

https://nodejs.org/api/http.html#http_http_request_url_options_callback

The logic that does the juggling was mostly copied from Node's source.

* Remove unnecessary overrider in intercept.

* Register ClientRequest callback on response event.

Instead of passing it all around the overrider.
This mimics how the native implementation does it.
@nockbot
Copy link
Collaborator

nockbot commented Jul 9, 2019

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

The release is available on:

Your semantic-release bot 📦🚀

medric added a commit to airtasker/spot that referenced this issue Jul 18, 2019
medric added a commit to airtasker/spot that referenced this issue Jul 19, 2019
medric added a commit to airtasker/spot that referenced this issue Jul 19, 2019
medric added a commit to airtasker/spot that referenced this issue Jul 19, 2019
* [TT-347]
- Add logic to proxy request when endpoint is not in draft state
- Address PR comments
- Write headers to proxied response
- Add test coverage for mockserver
- Updgrade nock to address nock/nock#1227
- Add test to ensure mock data is returned when expected
@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
* Allow three arg form of `request` and `ClientRequest`.

Fixes #1227

Beginning with v10.9.0, the `url` parameter can be passed along with a
separate options object to `http[s].request`, `http[s].get`, or to the
constructor of `ClientRequest`.

https://nodejs.org/api/http.html#http_http_request_url_options_callback

The logic that does the juggling was mostly copied from Node's source.

* Remove unnecessary overrider in intercept.

* Register ClientRequest callback on response event.

Instead of passing it all around the overrider.
This mimics how the native implementation does it.
gr2m pushed a commit that referenced this issue Sep 4, 2019
* Allow three arg form of `request` and `ClientRequest`.

Fixes #1227

Beginning with v10.9.0, the `url` parameter can be passed along with a
separate options object to `http[s].request`, `http[s].get`, or to the
constructor of `ClientRequest`.

https://nodejs.org/api/http.html#http_http_request_url_options_callback

The logic that does the juggling was mostly copied from Node's source.

* Remove unnecessary overrider in intercept.

* Register ClientRequest callback on response event.

Instead of passing it all around the overrider.
This mimics how the native implementation does it.
gr2m pushed a commit that referenced this issue Sep 5, 2019
* Allow three arg form of `request` and `ClientRequest`.

Fixes #1227

Beginning with v10.9.0, the `url` parameter can be passed along with a
separate options object to `http[s].request`, `http[s].get`, or to the
constructor of `ClientRequest`.

https://nodejs.org/api/http.html#http_http_request_url_options_callback

The logic that does the juggling was mostly copied from Node's source.

* Remove unnecessary overrider in intercept.

* Register ClientRequest callback on response event.

Instead of passing it all around the overrider.
This mimics how the native implementation does it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants