Navigation Menu

Skip to content

Commit

Permalink
feat: allow passing a function to enableNetConnect() (#1889)
Browse files Browse the repository at this point in the history
* feat(1861): allow passing a function to enableNetConnect
* Add test to cover passed parameters
  • Loading branch information
nikaspran authored and gr2m committed Feb 16, 2020
1 parent 123832e commit 7f9e26c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
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 }
} 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\/"/
)
})

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')
})
})
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

0 comments on commit 7f9e26c

Please sign in to comment.