From 9dddafd3a2af774fe6dd1c100e3795ea9c352fc0 Mon Sep 17 00:00:00 2001 From: Nagadev Date: Sat, 6 Apr 2024 13:43:43 -0400 Subject: [PATCH 1/2] fix: preserving qparams in url if origin is same, which prints relative url in warning message --- src/core/utils/request/toPublicUrl.test.ts | 4 +++- src/core/utils/request/toPublicUrl.ts | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/core/utils/request/toPublicUrl.test.ts b/src/core/utils/request/toPublicUrl.test.ts index 388e0aaa0..eef68e0a9 100644 --- a/src/core/utils/request/toPublicUrl.test.ts +++ b/src/core/utils/request/toPublicUrl.test.ts @@ -10,7 +10,9 @@ test('returns an absolute request URL withouth search params', () => { expect(toPublicUrl(new URL('http://localhost/path'))).toBe('/path') - expect(toPublicUrl(new URL('http://localhost/path?foo=bar'))).toBe('/path') + expect(toPublicUrl(new URL('http://localhost/path?foo=bar'))).toBe( + '/path?foo=bar', + ) }) it('returns a relative URL given the request to the same origin', () => { diff --git a/src/core/utils/request/toPublicUrl.ts b/src/core/utils/request/toPublicUrl.ts index f03aa410b..91ab2f14b 100644 --- a/src/core/utils/request/toPublicUrl.ts +++ b/src/core/utils/request/toPublicUrl.ts @@ -9,7 +9,9 @@ export function toPublicUrl(url: string | URL): string { const urlInstance = url instanceof URL ? url : new URL(url) + const [, relativeUrl] = urlInstance.href.split(urlInstance.origin) + return urlInstance.origin === location.origin - ? urlInstance.pathname + ? relativeUrl : urlInstance.origin + urlInstance.pathname } From d50e764f11ffef8e833f1ce5524de4eb197ce2e5 Mon Sep 17 00:00:00 2001 From: nagadevkrishna Date: Thu, 11 Apr 2024 22:46:43 -0400 Subject: [PATCH 2/2] fix: preserving search params by appending them to final publicUrl --- .../utils/request/onUnhandledRequest.test.ts | 34 ++++++++++++++++--- src/core/utils/request/onUnhandledRequest.ts | 2 +- src/core/utils/request/toPublicUrl.test.ts | 4 +-- src/core/utils/request/toPublicUrl.ts | 4 +-- 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/core/utils/request/onUnhandledRequest.test.ts b/src/core/utils/request/onUnhandledRequest.test.ts index fc717c952..7cb9826fc 100644 --- a/src/core/utils/request/onUnhandledRequest.test.ts +++ b/src/core/utils/request/onUnhandledRequest.test.ts @@ -7,10 +7,10 @@ import { } from './onUnhandledRequest' const fixtures = { - warningWithoutSuggestions: `\ + warningWithoutSuggestions: (url = `/api`) => `\ [MSW] Warning: intercepted a request without a matching request handler: - • GET /api + • GET ${url} If you still wish to intercept this unhandled request, please create a request handler for it. Read more: https://mswjs.io/docs/getting-started/mocks`, @@ -46,7 +46,9 @@ test('supports the "bypass" request strategy', async () => { test('supports the "warn" request strategy', async () => { await onUnhandledRequest(new Request(new URL('http://localhost/api')), 'warn') - expect(console.warn).toHaveBeenCalledWith(fixtures.warningWithoutSuggestions) + expect(console.warn).toHaveBeenCalledWith( + fixtures.warningWithoutSuggestions(), + ) }) test('supports the "error" request strategy', async () => { @@ -103,7 +105,9 @@ test('supports calling default strategies from the custom callback function', as test('does not print any suggestions given no handlers to suggest', async () => { await onUnhandledRequest(new Request(new URL('http://localhost/api')), 'warn') - expect(console.warn).toHaveBeenCalledWith(fixtures.warningWithoutSuggestions) + expect(console.warn).toHaveBeenCalledWith( + fixtures.warningWithoutSuggestions(), + ) }) test('throws an exception given unknown request strategy', async () => { @@ -117,3 +121,25 @@ test('throws an exception given unknown request strategy', async () => { '[MSW] Failed to react to an unhandled request: unknown strategy "invalid-strategy". Please provide one of the supported strategies ("bypass", "warn", "error") or a custom callback function as the value of the "onUnhandledRequest" option.', ) }) + +test('prints with a relative URL and search params', async () => { + await onUnhandledRequest( + new Request(new URL('http://localhost/api?foo=boo')), + 'warn', + ) + + expect(console.warn).toHaveBeenCalledWith( + fixtures.warningWithoutSuggestions(`/api?foo=boo`), + ) +}) + +test('prints with an absolute URL and search params', async () => { + await onUnhandledRequest( + new Request(new URL('https://mswjs.io/api?foo=boo')), + 'warn', + ) + + expect(console.warn).toHaveBeenCalledWith( + fixtures.warningWithoutSuggestions(`https://mswjs.io/api?foo=boo`), + ) +}) diff --git a/src/core/utils/request/onUnhandledRequest.ts b/src/core/utils/request/onUnhandledRequest.ts index 55e7dcef7..fcf4d5055 100644 --- a/src/core/utils/request/onUnhandledRequest.ts +++ b/src/core/utils/request/onUnhandledRequest.ts @@ -22,7 +22,7 @@ export async function onUnhandledRequest( strategy: UnhandledRequestStrategy = 'warn', ): Promise { const url = new URL(request.url) - const publicUrl = toPublicUrl(url) + const publicUrl = toPublicUrl(url) + url.search const unhandledRequestMessage = `intercepted a request without a matching request handler:\n\n \u2022 ${request.method} ${publicUrl}\n\nIf you still wish to intercept this unhandled request, please create a request handler for it.\nRead more: https://mswjs.io/docs/getting-started/mocks` diff --git a/src/core/utils/request/toPublicUrl.test.ts b/src/core/utils/request/toPublicUrl.test.ts index eef68e0a9..388e0aaa0 100644 --- a/src/core/utils/request/toPublicUrl.test.ts +++ b/src/core/utils/request/toPublicUrl.test.ts @@ -10,9 +10,7 @@ test('returns an absolute request URL withouth search params', () => { expect(toPublicUrl(new URL('http://localhost/path'))).toBe('/path') - expect(toPublicUrl(new URL('http://localhost/path?foo=bar'))).toBe( - '/path?foo=bar', - ) + expect(toPublicUrl(new URL('http://localhost/path?foo=bar'))).toBe('/path') }) it('returns a relative URL given the request to the same origin', () => { diff --git a/src/core/utils/request/toPublicUrl.ts b/src/core/utils/request/toPublicUrl.ts index 91ab2f14b..f03aa410b 100644 --- a/src/core/utils/request/toPublicUrl.ts +++ b/src/core/utils/request/toPublicUrl.ts @@ -9,9 +9,7 @@ export function toPublicUrl(url: string | URL): string { const urlInstance = url instanceof URL ? url : new URL(url) - const [, relativeUrl] = urlInstance.href.split(urlInstance.origin) - return urlInstance.origin === location.origin - ? relativeUrl + ? urlInstance.pathname : urlInstance.origin + urlInstance.pathname }