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 is not working when I am using proxy #2089

Closed
wanjuntham opened this issue Sep 22, 2020 · 11 comments
Closed

Nock is not working when I am using proxy #2089

wanjuntham opened this issue Sep 22, 2020 · 11 comments
Labels
support General questions or support.

Comments

@wanjuntham
Copy link

Hello,

Nock seems unable to intercept the requests when I have declared environment variables HTTP_PROXY and HTTPS_PROXY. Could you please check on this?

Proxies are mandatory in my scenario, so I cant get rid of them.

@mastermatt
Copy link
Member

Can you provide more info on your use case? I'm pretty sure Node doesn't follow HTTP_PROXY out of the box. And Nock doesn't intercept based on what the OS is actually attempting to send over the wire, just what Node thinks it's connecting to.

@wanjuntham
Copy link
Author

I'm pretty sure Node doesn't follow HTTP_PROXY out of the box.

No, it follows as long as I have them declared in my dotenv file. This link proves it, check the HTTPS_PROXY and PROXY.

Can you provide more info on your use case?

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.

@paulmelnikow
Copy link
Member

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

@wanjuntham
Copy link
Author

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 http_proxy and https_proxy

@mastermatt
Copy link
Member

I think there is some confusion here. The link provided for npm-config (https://docs.npmjs.com/misc/config#https-proxy) is describing the use of that env var when NPM is making requests, not the behavior of Node itself.
The conversation about adding this support to Node lives here, tl;dr it was never added.

@wanjuntham in your case, Axios supports the env var.

proxy defines the hostname and port of the proxy server. You can also define your proxy using the conventional http_proxy and https_proxy environment variables. ...

Note there is a bug in Axios around proxy protocols.

@mastermatt
Copy link
Member

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?

@wanjuntham
Copy link
Author

wanjuntham commented Sep 29, 2020

can you provide an example of how you're using Nock and Axios?

Sure
Here is a portion of my code, I gotta mask the endpoints as I am working in a company

const scope = nock('https://baseurl.com').post('/endpoint', body, { reqheaders: headersObject}).reply(201, 'nock accepted!')
Axios.defaults.adapter = require('axios/lib/adapters/http')
Axios.defaults.baseURL = 'https://baseurl.com'
Axios.post('/endpoint', body, {headers : headersObject}).then(res =>{
    console.log(res.data)
    console.log("status code: "+ res.status)
}).catch(err =>{
    console.error(err)
    console.error(err.response)
    console.error("status code: " + err.response)
});

I have a .env file which is loaded beforehand using ts-node -r dotenv/config myfile.ts
Inside .env file, I have all the proxy variables declared.

HTTPS_PROXY="https://username:password@proxy.com/"
HTTP_PROXY="http://username:password@proxy.com/"

In package.json, I have wrote the command required to try it.

  "scripts": {
    "debug": "ts-node -r dotenv/config myfile.ts"
}

I execute it by running command npm run debug in my console.

By doing so, the result of my code is getting the actual response from the endpoint, instead of the mocked response.

@paulmelnikow
Copy link
Member

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.

@wanjuntham
Copy link
Author

wanjuntham commented Oct 1, 2020

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.

@friederbluemle
Copy link

friederbluemle commented Jul 15, 2023

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

@arunprakashadithyan
Copy link

arunprakashadithyan commented Mar 31, 2024

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 )
https://medium.com/@arunprakashadithyan/mocking-http-requests-along-with-proxy-configuration-using-nock-d3985c558a76

axios.get(`https://jsonplaceholder.typicode.com/comments?postId=1`, {
        proxy: {
            protocol: "http",
            host: "149.129.239.170",
            port: "8002"

        }})

Solution would be

nock('http://149.129.239.170:8002', {allowUnmocked: true}).persist()
  .get(resource => resource.includes('/comments'))
  .query({postId: 1})
  .reply(200, [{
  "postId": 1,
  "id": 1,
  "name": "dummy email",
  "email": "Eliseo@gardner.biz",
  "body": "dummy body"
}])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support General questions or support.
Projects
None yet
Development

No branches or pull requests

5 participants