Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/mock/mock-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,19 @@ function deleteMockDispatch (mockDispatches, key) {
}

function buildKey (opts) {
const { path, method, body, headers, query } = opts
let { path, method, body, headers, query } = opts

// normalize path if it is a string
if (typeof path === 'string') {
while (path.endsWith('/')) {
path = path.slice(0, -1)
}

if (path.length === 0) {
path = '/'
}
}

return {
path,
method,
Expand Down
103 changes: 102 additions & 1 deletion test/mock-interceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { MockInterceptor, MockScope } = require('../lib/mock/mock-interceptor')
const MockAgent = require('../lib/mock/mock-agent')
const { kDispatchKey } = require('../lib/mock/mock-symbols')
const { InvalidArgumentError } = require('../lib/core/errors')
const { fetch } = require('../lib/web/fetch/index')

describe('MockInterceptor - path', () => {
test('should remove hash fragment from paths', t => {
Expand All @@ -14,7 +15,7 @@ describe('MockInterceptor - path', () => {
path: '#foobar',
method: ''
}, [])
t.strictEqual(mockInterceptor[kDispatchKey].path, '')
t.strictEqual(mockInterceptor[kDispatchKey].path, '/')
})
})

Expand Down Expand Up @@ -257,3 +258,103 @@ describe('MockInterceptor - replyContentLength', () => {
t.ok(result instanceof MockInterceptor)
})
})

describe('https://github.com/nodejs/undici/issues/3649', () => {
test('MockAgent should match with or without trailing slash /1', async (t) => {
t = tspl(t, { plan: 1 })

const mockAgent = new MockAgent()
mockAgent.disableNetConnect()
mockAgent
.get('https://localhost')
.intercept({ path: '/api/some-path' }).reply(200, { ok: true })

const res = await fetch(new URL('/api/some-path', 'https://localhost'), { dispatcher: mockAgent })

t.deepStrictEqual(await res.json(), { ok: true })
})

test('MockAgent should match with or without trailing slash /2', async (t) => {
t = tspl(t, { plan: 1 })

const mockAgent = new MockAgent()
mockAgent.disableNetConnect()
mockAgent
.get('https://localhost')
.intercept({ path: '/api/some-path' }).reply(200, { ok: true })

const res = await fetch(new URL('/api/some-path/', 'https://localhost'), { dispatcher: mockAgent })

t.deepStrictEqual(await res.json(), { ok: true })
})

test('MockAgent should match with or without trailing slash /3', async (t) => {
t = tspl(t, { plan: 1 })

const mockAgent = new MockAgent()
mockAgent.disableNetConnect()
mockAgent
.get('https://localhost')
.intercept({ path: '/api/some-path/' }).reply(200, { ok: true })

const res = await fetch(new URL('/api/some-path', 'https://localhost'), { dispatcher: mockAgent })

t.deepStrictEqual(await res.json(), { ok: true })
})

test('MockAgent should match with or without trailing slash /4', async (t) => {
t = tspl(t, { plan: 1 })

const mockAgent = new MockAgent()
mockAgent.disableNetConnect()
mockAgent
.get('https://localhost')
.intercept({ path: '/api/some-path/' }).reply(200, { ok: true })

const res = await fetch(new URL('/api/some-path/', 'https://localhost'), { dispatcher: mockAgent })

t.deepStrictEqual(await res.json(), { ok: true })
})

test('MockAgent should match with or without trailing slash /5', async (t) => {
t = tspl(t, { plan: 1 })

const mockAgent = new MockAgent()
mockAgent.disableNetConnect()
mockAgent
.get('https://localhost')
.intercept({ path: '/api/some-path////' }).reply(200, { ok: true })

const res = await fetch(new URL('/api/some-path//', 'https://localhost'), { dispatcher: mockAgent })

t.deepStrictEqual(await res.json(), { ok: true })
})

test('MockAgent should match with or without trailing slash /6', async (t) => {
t = tspl(t, { plan: 1 })

const mockAgent = new MockAgent()
mockAgent.disableNetConnect()
mockAgent
.get('https://localhost')
.intercept({ path: '/' }).reply(200, { ok: true })

const res = await fetch(new URL('', 'https://localhost'), { dispatcher: mockAgent })

t.deepStrictEqual(await res.json(), { ok: true })
})

test('MockAgent should match with or without trailing slash /7', async (t) => {
t = tspl(t, { plan: 1 })

const mockAgent = new MockAgent()
mockAgent.disableNetConnect()
mockAgent
.get('https://localhost')
.intercept({ path: '' }).reply(200, { ok: true })

const res = await fetch(new URL('/', 'https://localhost'), { dispatcher: mockAgent })

t.deepStrictEqual(await res.json(), { ok: true })
})
})
Loading