Skip to content

Commit e3bfece

Browse files
committed
fix: remove search params in output of toPublicUrl in node environments
Make `toPublicUrl` more consistent among environments and prevent `onUnhandledRequest` from presenting a duplicated query string Also add node env test suites for `toPublicUrl` and `onUnhandledRequest` Refs: #2628
1 parent 063564e commit e3bfece

File tree

3 files changed

+85
-7
lines changed

3 files changed

+85
-7
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// @vitest-environment node
2+
import { onUnhandledRequest } from './onUnhandledRequest'
3+
4+
const fixtures = {
5+
warningWithoutSuggestions: (url = `/api`) => `\
6+
[MSW] Warning: intercepted a request without a matching request handler:
7+
8+
• GET ${url}
9+
10+
If you still wish to intercept this unhandled request, please create a request handler for it.
11+
Read more: https://mswjs.io/docs/http/intercepting-requests`,
12+
warningWithResponseBody: (url = `/api`) => `\
13+
[MSW] Warning: intercepted a request without a matching request handler:
14+
15+
• POST ${url}
16+
17+
• Request body: {"variables":{"id":"abc-123"},"query":"query UserName($id: String!) { user(id: $id) { name } }"}
18+
19+
If you still wish to intercept this unhandled request, please create a request handler for it.
20+
Read more: https://mswjs.io/docs/http/intercepting-requests`,
21+
22+
errorWithoutSuggestions: `\
23+
[MSW] Error: intercepted a request without a matching request handler:
24+
25+
• GET /api
26+
27+
If you still wish to intercept this unhandled request, please create a request handler for it.
28+
Read more: https://mswjs.io/docs/http/intercepting-requests`,
29+
}
30+
31+
beforeEach(() => {
32+
vi.spyOn(console, 'warn').mockImplementation(() => void 0)
33+
vi.spyOn(console, 'error').mockImplementation(() => void 0)
34+
})
35+
36+
afterEach(() => {
37+
vi.restoreAllMocks()
38+
})
39+
40+
test('prints with an absolute URL and search params', async () => {
41+
await onUnhandledRequest(
42+
new Request(new URL('https://mswjs.io/api?foo=boo')),
43+
'warn',
44+
)
45+
46+
expect(console.warn).toHaveBeenCalledWith(
47+
fixtures.warningWithoutSuggestions(`https://mswjs.io/api?foo=boo`),
48+
)
49+
50+
await onUnhandledRequest(
51+
new Request(new URL('http://localhost/api?foo=boo')),
52+
'warn',
53+
)
54+
55+
expect(console.warn).toHaveBeenCalledWith(
56+
fixtures.warningWithoutSuggestions(`http://localhost/api?foo=boo`),
57+
)
58+
})
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @vitest-environment node
3+
*/
4+
import { toPublicUrl } from './toPublicUrl'
5+
6+
test('returns an absolute request URL without search params', () => {
7+
expect(toPublicUrl(new URL('https://test.mswjs.io/path'))).toBe(
8+
'https://test.mswjs.io/path',
9+
)
10+
11+
expect(toPublicUrl(new URL('http://192.168.0.10/path'))).toBe(
12+
'http://192.168.0.10/path',
13+
)
14+
15+
// URLs returned in a Node.js environment are never relative.
16+
expect(toPublicUrl(new URL('http://localhost/path?foo=bar'))).toBe(
17+
'http://localhost/path',
18+
)
19+
})

src/core/utils/request/toPublicUrl.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
* to the current origin. Otherwise returns an absolute URL.
44
*/
55
export function toPublicUrl(url: string | URL): string {
6-
if (typeof location === 'undefined') {
7-
return url.toString()
8-
}
9-
106
const urlInstance = url instanceof URL ? url : new URL(url)
117

12-
return urlInstance.origin === location.origin
13-
? urlInstance.pathname
14-
: urlInstance.origin + urlInstance.pathname
8+
if (
9+
typeof location !== 'undefined' &&
10+
urlInstance.origin === location.origin
11+
) {
12+
return urlInstance.pathname
13+
}
14+
15+
return urlInstance.origin + urlInstance.pathname
1516
}

0 commit comments

Comments
 (0)