Skip to content

Commit

Permalink
fix(interceptor): don't require leading slash if Scope has a base pat…
Browse files Browse the repository at this point in the history
…hname (#2168)
  • Loading branch information
mastermatt committed Mar 7, 2021
1 parent cbee6fe commit bfbbbb7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
3 changes: 2 additions & 1 deletion lib/interceptor.js
Expand Up @@ -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('*')
) {
Expand Down
61 changes: 33 additions & 28 deletions tests/test_intercept.js
Expand Up @@ -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()
})

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit bfbbbb7

Please sign in to comment.