Skip to content

Commit

Permalink
fix: mock interceptor should ignore trailing slashes
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Sep 30, 2024
1 parent ae18f0e commit eddfbf8
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/mock/mock-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,12 @@ function deleteMockDispatch (mockDispatches, key) {
}

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

while (path.length > 1 && path.endsWith('/')) {
path = path.slice(0, -1)
}

return {
path,
method,
Expand Down
78 changes: 78 additions & 0 deletions test/issue-3649.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
'use strict'

const { tspl } = require('@matteo.collina/tspl')
const { describe, test, beforeEach } = require('node:test')
const { MockAgent, fetch, setGlobalDispatcher, getGlobalDispatcher } = require('..')

describe('https://github.com/nodejs/undici/issues/3649', () => {
const undiciGlobalDispatcher = getGlobalDispatcher()
if (!undiciGlobalDispatcher) throw new Error('Could not find the global Undici dispatcher')

let mockAgent

beforeEach(() => {
mockAgent = new MockAgent()
mockAgent.disableNetConnect()
setGlobalDispatcher(mockAgent)
})

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

0 comments on commit eddfbf8

Please sign in to comment.