From 1eb19ce470d31daf6533dde6b886a6ecca0572cd Mon Sep 17 00:00:00 2001 From: Nikas Praninskas Date: Mon, 10 Feb 2020 12:05:18 +0200 Subject: [PATCH 1/4] feat(1861): allow passing a function to enableNetConnect --- README.md | 5 ++++- lib/intercept.js | 5 +++++ tests/test_net_connect.js | 35 +++++++++++++++++++++++++++++------ types/index.d.ts | 2 +- types/tests.ts | 3 +++ 5 files changed, 42 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index a9cbef8e6..4d9eabf4c 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,9 @@ 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 b3c6afdb8..7404942e3 100644 --- a/lib/intercept.js +++ b/lib/intercept.js @@ -50,6 +50,9 @@ 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) { // TODO-12.x: Replace with `typeof matcher === 'string'`. @@ -57,6 +60,8 @@ function enableNetConnect(matcher) { allowNetConnect = new RegExp(matcher) } else if (matcher instanceof RegExp) { allowNetConnect = matcher + } else if (matcher instanceof Function) { + allowNetConnect = { test: matcher } } else { allowNetConnect = /.*/ } diff --git a/tests/test_net_connect.js b/tests/test_net_connect.js index 30822bc2b..71557d289 100644 --- a/tests/test_net_connect.js +++ b/tests/test_net_connect.js @@ -19,8 +19,7 @@ describe('`disableNetConnect()`', () => { await assertRejects( got('https://other.example.test/'), - Error, - 'Nock: Disallowed net connect for "other.example.test:443/"' + /Nock: Disallowed net connect for "other.example.test:443\/"/ ) }) @@ -61,8 +60,7 @@ describe('`enableNetConnect()`', () => { await assertRejects( got('https://example.test/'), - Error, - 'Nock: Disallowed net connect for "example.test:80/"' + /Nock: Disallowed net connect for "example.test:443\/"/ ) }) @@ -88,8 +86,33 @@ describe('`enableNetConnect()`', () => { await assertRejects( got('https://example.test/'), - Error, - 'Nock: Disallowed net connect for "example.test:80/"' + /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\/"/ ) }) }) diff --git a/types/index.d.ts b/types/index.d.ts index 38abc30e8..20212358b 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -21,7 +21,7 @@ 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. From 2c8b6f8c68d8d95e316ffd00936076dc182434da Mon Sep 17 00:00:00 2001 From: Nikas Praninskas Date: Mon, 10 Feb 2020 12:12:54 +0200 Subject: [PATCH 2/4] lint: run prettier --- README.md | 4 +++- types/index.d.ts | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4d9eabf4c..97728522a 100644 --- a/README.md +++ b/README.md @@ -1164,7 +1164,9 @@ nock.enableNetConnect('amazon.com') nock.enableNetConnect(/(amazon|github)\.com/) // Or a Function -nock.enableNetConnect(host => host.includes('amazon.com') || host.includes('github.com')) +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/types/index.d.ts b/types/index.d.ts index 20212358b..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 | ((host: string) => boolean)): 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[] From 51810a5a28319d33a86910c43e1624eeaac05ea1 Mon Sep 17 00:00:00 2001 From: Nikas Praninskas Date: Mon, 10 Feb 2020 16:15:43 +0200 Subject: [PATCH 3/4] Use typeof instead of instanceof (PR #1850) --- lib/intercept.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/intercept.js b/lib/intercept.js index 7404942e3..383d4fafa 100644 --- a/lib/intercept.js +++ b/lib/intercept.js @@ -60,7 +60,7 @@ function enableNetConnect(matcher) { allowNetConnect = new RegExp(matcher) } else if (matcher instanceof RegExp) { allowNetConnect = matcher - } else if (matcher instanceof Function) { + } else if (typeof matcher === 'function') { allowNetConnect = { test: matcher } } else { allowNetConnect = /.*/ From e9bdb0fc8484385927b7d12e64bcd29a58b83357 Mon Sep 17 00:00:00 2001 From: Nikas Praninskas Date: Mon, 10 Feb 2020 17:19:18 +0200 Subject: [PATCH 4/4] Add test to cover passed parameters --- tests/test_net_connect.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/test_net_connect.js b/tests/test_net_connect.js index 71557d289..5a5f1710c 100644 --- a/tests/test_net_connect.js +++ b/tests/test_net_connect.js @@ -115,4 +115,13 @@ describe('`enableNetConnect()`', () => { /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') + }) })