|
| 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