Skip to content

Commit bfbbbb7

Browse files
authored
fix(interceptor): don't require leading slash if Scope has a base pathname (#2168)
1 parent cbee6fe commit bfbbbb7

2 files changed

Lines changed: 35 additions & 29 deletions

File tree

lib/interceptor.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ module.exports = class Interceptor {
3232
// When enabled filteringScope ignores the passed URL entirely so we skip validation.
3333

3434
if (
35-
!scope.scopeOptions.filteringScope &&
3635
uriIsStr &&
36+
!scope.scopeOptions.filteringScope &&
37+
!scope.basePathname &&
3738
!uri.startsWith('/') &&
3839
!uri.startsWith('*')
3940
) {

tests/test_intercept.js

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,52 @@ describe('Intercept', () => {
2626
)
2727
})
2828

29-
it("when the path doesn't include a leading slash it raises an error", () => {
29+
it("should throw when the path doesn't include a leading slash and there is no base path", () => {
3030
expect(() => nock('http://example.test').get('no-leading-slash')).to.throw(
3131
"Non-wildcard URL path strings must begin with a slash (otherwise they won't match anything)"
3232
)
3333
})
3434

35+
// https://github.com/nock/nock/issues/1730
36+
it('should throw when the path is empty and there is no base path', () => {
37+
expect(() => nock('http://example.test').get('')).to.throw(
38+
"Non-wildcard URL path strings must begin with a slash (otherwise they won't match anything) (got: )"
39+
)
40+
})
41+
3542
it('should intercept a basic GET request', async () => {
36-
const scope = nock('http://example.test')
37-
.get('/')
38-
.reply(200, 'Hello World!')
43+
const scope = nock('http://example.test').get('/').reply(201)
3944

40-
const { statusCode, body } = await got('http://example.test/', {
41-
responseType: 'buffer',
42-
})
45+
const { statusCode } = await got('http://example.test/')
4346

44-
expect(statusCode).to.equal(200)
45-
expect(body).to.be.an.instanceOf(Buffer)
46-
expect(body.toString('utf8')).to.equal('Hello World!')
47+
expect(statusCode).to.equal(201)
4748
scope.done()
4849
})
4950

50-
it('get gets mocked with relative base path', async () => {
51-
const scope = nock('http://example.test/abc')
52-
.get('/def')
53-
.reply(200, 'Hello World!')
51+
it('should intercept a request with a base path', async () => {
52+
const scope = nock('http://example.test/abc').get('/def').reply(201)
5453

55-
const { statusCode, body } = await got('http://example.test/abc/def', {
56-
responseType: 'buffer',
57-
})
54+
const { statusCode } = await got('http://example.test/abc/def')
5855

59-
expect(statusCode).to.equal(200)
60-
expect(body).to.be.an.instanceOf(Buffer)
61-
expect(body.toString('utf8')).to.equal('Hello World!')
56+
expect(statusCode).to.equal(201)
57+
scope.done()
58+
})
59+
60+
it('should intercept a request with a base path and no interceptor path', async () => {
61+
const scope = nock('http://example.test/abc').get('').reply(201)
62+
63+
const { statusCode } = await got('http://example.test/abc')
64+
65+
expect(statusCode).to.equal(201)
66+
scope.done()
67+
})
68+
69+
it('should intercept a request with a base path and an interceptor path without a leading slash', async () => {
70+
const scope = nock('http://example.test/abc').get('def').reply(201)
71+
72+
const { statusCode } = await got('http://example.test/abcdef')
73+
74+
expect(statusCode).to.equal(201)
6275
scope.done()
6376
})
6477

@@ -970,14 +983,6 @@ describe('Intercept', () => {
970983
.flushHeaders()
971984
})
972985

973-
// https://github.com/nock/nock/issues/1730
974-
it('URL path without leading slash throws expected error', done => {
975-
expect(() => nock('http://example.test').get('')).to.throw(
976-
"Non-wildcard URL path strings must begin with a slash (otherwise they won't match anything) (got: )"
977-
)
978-
done()
979-
})
980-
981986
it('wildcard param URL should not throw error', done => {
982987
expect(() => nock('http://example.test').get('*')).not.to.throw()
983988
done()

0 commit comments

Comments
 (0)