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

feat: allow passing a function to enableNetConnect() #1889

Merged
merged 5 commits into from Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Expand Up @@ -1154,7 +1154,7 @@ For enabling any real HTTP requests (the default behavior):
nock.enableNetConnect()
```

You could allow real HTTP requests for certain host names by providing a string or a regular expression for the hostname:
You could allow real HTTP requests for certain host names by providing a string or a regular expression for the hostname, or a function that accepts the hostname and returns true or false:

```js
// Using a string
Expand All @@ -1163,6 +1163,11 @@ nock.enableNetConnect('amazon.com')
// Or a RegExp
nock.enableNetConnect(/(amazon|github)\.com/)

// Or a Function
nock.enableNetConnect(
host => host.includes('amazon.com') || host.includes('github.com')
)

http.get('http://www.amazon.com/')
http.get('http://github.com/')

Expand Down
5 changes: 5 additions & 0 deletions lib/intercept.js
Expand Up @@ -49,12 +49,17 @@ let allowNetConnect
* @example
* // Enables real requests for url that matches google and amazon
* nock.enableNetConnect(/(google|amazon)/);
* @example
* // Enables real requests for url that includes google
* nock.enableNetConnect(host => host.includes('google'));
*/
function enableNetConnect(matcher) {
if (typeof matcher === 'string') {
allowNetConnect = new RegExp(matcher)
} else if (matcher instanceof RegExp) {
allowNetConnect = matcher
} else if (typeof matcher === 'function') {
allowNetConnect = { test: matcher }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could also make the internal variable a function, but this seemed quite elegant - let me know if you want me to change this.

} else {
allowNetConnect = /.*/
}
Expand Down
35 changes: 35 additions & 0 deletions tests/test_net_connect.js
Expand Up @@ -89,4 +89,39 @@ describe('`enableNetConnect()`', () => {
/Nock: Disallowed net connect for "example.test:443\/"/
)
})

it('enables real HTTP request only for specified domain, via function', async () => {
const onResponse = sinon.spy()
const server = http.createServer((request, response) => {
onResponse()
response.writeHead(200)
response.end()
})
await new Promise(resolve => server.listen(resolve))

nock.enableNetConnect(host => host.includes('ocalhos'))

await got(`http://localhost:${server.address().port}/`)
expect(onResponse).to.have.been.calledOnce()

server.close()
})

it('disallows request for other domains, via function', async () => {
nock.enableNetConnect(host => host.includes('ocalhos'))

await assertRejects(
got('https://example.test/'),
/Nock: Disallowed net connect for "example.test:443\/"/
nikaspran marked this conversation as resolved.
Show resolved Hide resolved
)
})

it('passes the domain to be tested, via function', async () => {
const matcher = sinon.stub().returns(false)
nock.enableNetConnect(matcher)

await got('https://example.test/').catch(() => undefined) // ignore rejection, expected

expect(matcher).to.have.been.calledOnceWithExactly('example.test:443')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

})
})
4 changes: 3 additions & 1 deletion types/index.d.ts
Expand Up @@ -21,7 +21,9 @@ declare namespace nock {
function activeMocks(): string[]
function removeInterceptor(interceptor: Interceptor | ReqOptions): boolean
function disableNetConnect(): void
function enableNetConnect(matcher?: string | RegExp): void
function enableNetConnect(
matcher?: string | RegExp | ((host: string) => boolean)
): void
function load(path: string): Scope[]
function loadDefs(path: string): Definition[]
function define(defs: Definition[]): Scope[]
Expand Down
3 changes: 3 additions & 0 deletions types/tests.ts
Expand Up @@ -703,6 +703,9 @@ nock.enableNetConnect('example.test')
// or a RegExp
nock.enableNetConnect(/example\.(com|test)/)

// or a Function
nock.enableNetConnect(host => host.includes('example.com'))

nock.disableNetConnect()
nock.enableNetConnect('127.0.0.1') // Allow localhost connections so we can test local routes and mock servers.

Expand Down