diff --git a/README.md b/README.md index a9cbef8e6..97728522a 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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/') diff --git a/lib/intercept.js b/lib/intercept.js index ce1c0164e..deec18118 100644 --- a/lib/intercept.js +++ b/lib/intercept.js @@ -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 = /.*/ } diff --git a/tests/test_net_connect.js b/tests/test_net_connect.js index d911afbed..40325ec71 100644 --- a/tests/test_net_connect.js +++ b/tests/test_net_connect.js @@ -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') + }) }) diff --git a/types/index.d.ts b/types/index.d.ts index 38abc30e8..b58127639 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -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[] diff --git a/types/tests.ts b/types/tests.ts index acefc5e30..7bd12074e 100644 --- a/types/tests.ts +++ b/types/tests.ts @@ -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.