Skip to content

Commit 10cf8ad

Browse files
committed
fix: mock interceptor should ignore trailing slashes
1 parent ae18f0e commit 10cf8ad

File tree

2 files changed

+112
-1
lines changed

2 files changed

+112
-1
lines changed

lib/mock/mock-utils.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,16 @@ function deleteMockDispatch (mockDispatches, key) {
178178
}
179179

180180
function buildKey (opts) {
181-
const { path, method, body, headers, query } = opts
181+
let { path, method, body, headers, query } = opts
182+
183+
while (path.endsWith('/')) {
184+
path = path.slice(0, -1)
185+
}
186+
187+
if (path.length === 0) {
188+
path = '/'
189+
}
190+
182191
return {
183192
path,
184193
method,

test/issue-3649.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
'use strict'
2+
3+
const { tspl } = require('@matteo.collina/tspl')
4+
const { describe, test, beforeEach } = require('node:test')
5+
const { MockAgent, fetch, setGlobalDispatcher, getGlobalDispatcher } = require('..')
6+
7+
describe('https://github.com/nodejs/undici/issues/3649', () => {
8+
const undiciGlobalDispatcher = getGlobalDispatcher()
9+
if (!undiciGlobalDispatcher) throw new Error('Could not find the global Undici dispatcher')
10+
11+
let mockAgent
12+
13+
beforeEach(() => {
14+
mockAgent = new MockAgent()
15+
mockAgent.disableNetConnect()
16+
setGlobalDispatcher(mockAgent)
17+
})
18+
19+
test('MockAgent should match with or without trailing slash /1', async (t) => {
20+
t = tspl(t, { plan: 1 })
21+
22+
mockAgent
23+
.get('https://localhost')
24+
.intercept({ path: '/api/some-path' }).reply(200, { ok: true })
25+
26+
const res = await fetch(new URL('/api/some-path', 'https://localhost'))
27+
28+
t.deepStrictEqual(await res.json(), { ok: true })
29+
})
30+
31+
test('MockAgent should match with or without trailing slash /2', async (t) => {
32+
t = tspl(t, { plan: 1 })
33+
34+
mockAgent
35+
.get('https://localhost')
36+
.intercept({ path: '/api/some-path' }).reply(200, { ok: true })
37+
38+
const res = await fetch(new URL('/api/some-path/', 'https://localhost'))
39+
40+
t.deepStrictEqual(await res.json(), { ok: true })
41+
})
42+
43+
test('MockAgent should match with or without trailing slash /3', async (t) => {
44+
t = tspl(t, { plan: 1 })
45+
46+
mockAgent
47+
.get('https://localhost')
48+
.intercept({ path: '/api/some-path/' }).reply(200, { ok: true })
49+
50+
const res = await fetch(new URL('/api/some-path', 'https://localhost'))
51+
52+
t.deepStrictEqual(await res.json(), { ok: true })
53+
})
54+
55+
test('MockAgent should match with or without trailing slash /4', async (t) => {
56+
t = tspl(t, { plan: 1 })
57+
58+
mockAgent
59+
.get('https://localhost')
60+
.intercept({ path: '/api/some-path/' }).reply(200, { ok: true })
61+
62+
const res = await fetch(new URL('/api/some-path/', 'https://localhost'))
63+
64+
t.deepStrictEqual(await res.json(), { ok: true })
65+
})
66+
67+
test('MockAgent should match with or without trailing slash /5', async (t) => {
68+
t = tspl(t, { plan: 1 })
69+
70+
mockAgent
71+
.get('https://localhost')
72+
.intercept({ path: '/api/some-path////' }).reply(200, { ok: true })
73+
74+
const res = await fetch(new URL('/api/some-path//', 'https://localhost'))
75+
76+
t.deepStrictEqual(await res.json(), { ok: true })
77+
})
78+
79+
test('MockAgent should match with or without trailing slash /6', async (t) => {
80+
t = tspl(t, { plan: 1 })
81+
82+
mockAgent
83+
.get('https://localhost')
84+
.intercept({ path: '/' }).reply(200, { ok: true })
85+
86+
const res = await fetch(new URL('', 'https://localhost'))
87+
88+
t.deepStrictEqual(await res.json(), { ok: true })
89+
})
90+
91+
test('MockAgent should match with or without trailing slash /7', async (t) => {
92+
t = tspl(t, { plan: 1 })
93+
94+
mockAgent
95+
.get('https://localhost')
96+
.intercept({ path: '' }).reply(200, { ok: true })
97+
98+
const res = await fetch(new URL('/', 'https://localhost'))
99+
100+
t.deepStrictEqual(await res.json(), { ok: true })
101+
})
102+
})

0 commit comments

Comments
 (0)