Skip to content

Commit 34f1676

Browse files
authored
fix: remove hash from intercept dispatch key (#1273)
1 parent cedc7d2 commit 34f1676

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

lib/mock/mock-interceptor.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ class MockInterceptor {
6666
if (typeof opts.method === 'undefined') {
6767
throw new InvalidArgumentError('opts.method must be defined')
6868
}
69+
// See https://github.com/nodejs/undici/issues/1245
70+
// As per RFC 3986, clients are not supposed to send URI
71+
// fragments to servers when they retrieve a document,
72+
if (typeof opts.path === 'string') {
73+
// Matches https://github.com/nodejs/undici/blob/main/lib/fetch/index.js#L1811
74+
const parsedURL = new URL(opts.path, 'data://')
75+
opts.path = parsedURL.pathname + parsedURL.search
76+
}
6977

7078
this[kDispatchKey] = buildKey(opts)
7179
this[kDispatches] = mockDispatches

test/mock-interceptor.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,22 @@
33
const { test } = require('tap')
44
const { MockInterceptor, MockScope } = require('../lib/mock/mock-interceptor')
55
const MockAgent = require('../lib/mock/mock-agent');
6+
const { kDispatchKey } = require('../lib/mock/mock-symbols');
67
const { InvalidArgumentError } = require('../lib/core/errors')
78

9+
10+
test('MockInterceptor - path', t => {
11+
t.plan(1)
12+
t.test('should remove hash fragment from paths', t => {
13+
t.plan(1)
14+
const mockInterceptor = new MockInterceptor({
15+
path: '#foobar',
16+
method: ''
17+
}, [])
18+
t.equal(mockInterceptor[kDispatchKey].path, '')
19+
})
20+
})
21+
822
test('MockInterceptor - reply', t => {
923
t.plan(2)
1024

0 commit comments

Comments
 (0)