From bfbbbb740d42e8e75aecc332d432fa0b415237f7 Mon Sep 17 00:00:00 2001 From: "Matt R. Wilson" Date: Sun, 7 Mar 2021 13:42:54 -0700 Subject: [PATCH] fix(interceptor): don't require leading slash if Scope has a base pathname (#2168) --- lib/interceptor.js | 3 +- tests/test_intercept.js | 61 ++++++++++++++++++++++------------------- 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/lib/interceptor.js b/lib/interceptor.js index 0a76bba42..8c79b2d80 100644 --- a/lib/interceptor.js +++ b/lib/interceptor.js @@ -32,8 +32,9 @@ module.exports = class Interceptor { // When enabled filteringScope ignores the passed URL entirely so we skip validation. if ( - !scope.scopeOptions.filteringScope && uriIsStr && + !scope.scopeOptions.filteringScope && + !scope.basePathname && !uri.startsWith('/') && !uri.startsWith('*') ) { diff --git a/tests/test_intercept.js b/tests/test_intercept.js index bf6293e73..419ba4a92 100644 --- a/tests/test_intercept.js +++ b/tests/test_intercept.js @@ -26,39 +26,52 @@ describe('Intercept', () => { ) }) - it("when the path doesn't include a leading slash it raises an error", () => { + it("should throw when the path doesn't include a leading slash and there is no base path", () => { expect(() => nock('http://example.test').get('no-leading-slash')).to.throw( "Non-wildcard URL path strings must begin with a slash (otherwise they won't match anything)" ) }) + // https://github.com/nock/nock/issues/1730 + it('should throw when the path is empty and there is no base path', () => { + expect(() => nock('http://example.test').get('')).to.throw( + "Non-wildcard URL path strings must begin with a slash (otherwise they won't match anything) (got: )" + ) + }) + it('should intercept a basic GET request', async () => { - const scope = nock('http://example.test') - .get('/') - .reply(200, 'Hello World!') + const scope = nock('http://example.test').get('/').reply(201) - const { statusCode, body } = await got('http://example.test/', { - responseType: 'buffer', - }) + const { statusCode } = await got('http://example.test/') - expect(statusCode).to.equal(200) - expect(body).to.be.an.instanceOf(Buffer) - expect(body.toString('utf8')).to.equal('Hello World!') + expect(statusCode).to.equal(201) scope.done() }) - it('get gets mocked with relative base path', async () => { - const scope = nock('http://example.test/abc') - .get('/def') - .reply(200, 'Hello World!') + it('should intercept a request with a base path', async () => { + const scope = nock('http://example.test/abc').get('/def').reply(201) - const { statusCode, body } = await got('http://example.test/abc/def', { - responseType: 'buffer', - }) + const { statusCode } = await got('http://example.test/abc/def') - expect(statusCode).to.equal(200) - expect(body).to.be.an.instanceOf(Buffer) - expect(body.toString('utf8')).to.equal('Hello World!') + expect(statusCode).to.equal(201) + scope.done() + }) + + it('should intercept a request with a base path and no interceptor path', async () => { + const scope = nock('http://example.test/abc').get('').reply(201) + + const { statusCode } = await got('http://example.test/abc') + + expect(statusCode).to.equal(201) + scope.done() + }) + + it('should intercept a request with a base path and an interceptor path without a leading slash', async () => { + const scope = nock('http://example.test/abc').get('def').reply(201) + + const { statusCode } = await got('http://example.test/abcdef') + + expect(statusCode).to.equal(201) scope.done() }) @@ -970,14 +983,6 @@ describe('Intercept', () => { .flushHeaders() }) - // https://github.com/nock/nock/issues/1730 - it('URL path without leading slash throws expected error', done => { - expect(() => nock('http://example.test').get('')).to.throw( - "Non-wildcard URL path strings must begin with a slash (otherwise they won't match anything) (got: )" - ) - done() - }) - it('wildcard param URL should not throw error', done => { expect(() => nock('http://example.test').get('*')).not.to.throw() done()