Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(interceptor): don't require leading slash if Scope has a base pathname #2168

Merged
merged 1 commit into from Mar 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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