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 is not working when I am using proxy #2089
Comments
Can you provide more info on your use case? I'm pretty sure Node doesn't follow |
No, it follows as long as I have them declared in my dotenv file. This link proves it, check the HTTPS_PROXY and PROXY.
My use case is that in my actual code I'm calling an endpoint using Axios. To do unit test, I am trying to use Nock so that I can intercept the request to get the intended response, instead of getting the actual response from the endpoint. That endpoint is an intranet endpoint, which requires you to have proxy configured then only you will able to get actual response. Or else, it will be throwing errors related to certificate. |
I'm pretty sure those options are specific to the (now deprecated) request library: https://www.npmjs.com/package/request#controlling-proxy-behaviour-using-environment-variables |
But I'm actually using Axios library to do the actual http calls, and from their docs, it specified that proxy can be set using |
I think there is some confusion here. The link provided for @wanjuntham in your case, Axios supports the env var.
Note there is a bug in Axios around proxy protocols. |
Sorry, I wrote that ^^^ a few hours, but forgot to hit the big green button. @wanjuntham can you provide an example of how you're using Nock and Axios? |
Sure
I have a
In
I execute it by running command By doing so, the result of my code is getting the actual response from the endpoint, instead of the mocked response. |
Hmm, if I'm understanding correctly, it sounds like the requests are honoring the proxy, so they're being made to the proxy host. To intercept those, you need to set mock requests for the proxied request on the proxy host. Alternatively you could unset the proxy env vars when you're running tests and leave your mocks on the real hosts. |
Why didn't I think of this earlier?! It works flawlessly now! To everyone who are checking this issue, what I did to make it work is by writing these at the beginning of my code: process.env.HTTP_PROXY = ''
process.env.HTTPS_PROXY = '' By doing so, nock will capture accordingly without being messed up by mocks. |
I also just ran into this issue. It would be nice if there was a way to configure Nock in the tests to ignore the proxy (if that's even possible). For now, my workaround is to prepend my CI test script to unset the proxy variables: -yarn test
+HTTPS_PROXY= https_proxy= yarn test |
Hey @wanjuntham, @friederbluemle , Instead of resetting the environment variables, you can follow this approach to make nock intercept the requests with proxy configuration. (Because while testing, you might want to test with proxy config as it is )
Solution would be
|
Hello,
Nock seems unable to intercept the requests when I have declared environment variables
HTTP_PROXY
andHTTPS_PROXY
. Could you please check on this?Proxies are mandatory in my scenario, so I cant get rid of them.
The text was updated successfully, but these errors were encountered: