From 69ccb67ca82cc0a3bd1e04cb213ac69736e8d381 Mon Sep 17 00:00:00 2001 From: Wilson Page Date: Thu, 6 Jan 2022 15:46:53 +0000 Subject: [PATCH] Fix allowUnocked not working w/ regex host + request body match --- lib/interceptor.js | 6 ++++++ tests/test_allow_unmocked.js | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/lib/interceptor.js b/lib/interceptor.js index 7af5f6a12..43113072b 100644 --- a/lib/interceptor.js +++ b/lib/interceptor.js @@ -413,6 +413,12 @@ module.exports = class Interceptor { } matchHostName(options) { + const { basePath } = this.scope + + if (basePath instanceof RegExp) { + return basePath.test(options.hostname) + } + return options.hostname === this.scope.urlParts.hostname } diff --git a/tests/test_allow_unmocked.js b/tests/test_allow_unmocked.js index d36684c31..cdf309c53 100644 --- a/tests/test_allow_unmocked.js +++ b/tests/test_allow_unmocked.js @@ -141,6 +141,25 @@ describe('allowUnmocked option', () => { scope.done() }) + it('allow unmocked passthrough with regex host & mismatched bodies', async () => { + const { origin } = await startHttpServer((request, response) => { + response.writeHead(200) + response.write('{"message":"server response"}') + response.end() + }) + + nock(/localhost/, { allowUnmocked: true }) + .post('/post', { some: 'other data' }) + .reply(404, '{"message":"server response"}') + + const { body, statusCode } = await got.post(`${origin}/post`, { + json: { some: 'data' }, + responseType: 'json', + }) + expect(statusCode).to.equal(200) + expect(body).to.deep.equal({ message: 'server response' }) + }) + // https://github.com/nock/nock/issues/1867 it('match path using callback with allowUnmocked', async () => { const scope = nock('http://example.test', { allowUnmocked: true })