-
-
Notifications
You must be signed in to change notification settings - Fork 536
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: do not treat static asset requests as unhandled by default (#2440)
- Loading branch information
1 parent
d0729ae
commit eb45e7a
Showing
8 changed files
with
156 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* Determines if the given request is a static asset request. | ||
* Useful when deciding which unhandled requests to ignore. | ||
* @note Despite being ignored, you can still intercept and mock | ||
* static assets by creating request handlers for them. | ||
* | ||
* @example | ||
* import { isCommonAssetRequest } from 'msw' | ||
* | ||
* await worker.start({ | ||
* onUnhandledRequest(request, print) { | ||
* if (!isCommonAssetRequest(request)) { | ||
* print.warning() | ||
* } | ||
* } | ||
* }) | ||
*/ | ||
export function isCommonAssetRequest(request: Request): boolean { | ||
const url = new URL(request.url) | ||
|
||
// Ignore certain protocols. | ||
if (url.protocol === 'file:') { | ||
return true | ||
} | ||
|
||
// Ignore static assets hosts. | ||
if (/(fonts\.googleapis\.com)/.test(url.hostname)) { | ||
return true | ||
} | ||
|
||
// Ignore node modules served over HTTP. | ||
if (/node_modules/.test(url.pathname)) { | ||
return true | ||
} | ||
|
||
// Ignore internal Vite requests, like "/@vite/client". | ||
if (url.pathname.includes('@vite')) { | ||
return true | ||
} | ||
|
||
// Ignore common static assets. | ||
return /\.(s?css|less|m?jsx?|m?tsx?|html|ttf|otf|woff|woff2|eot|gif|jpe?g|png|avif|webp|svg|mp4|webm|ogg|mov|mp3|wav|ogg|flac|aac|pdf|txt|csv|json|xml|md|zip|tar|gz|rar|7z)$/i.test( | ||
url.pathname, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 0 additions & 14 deletions
14
test/browser/msw-api/setup-worker/start/on-unhandled-request/error.mocks.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 15 additions & 13 deletions
28
test/node/msw-api/setup-server/scenarios/on-unhandled-request/warn.node.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,36 @@ | ||
/** | ||
* @vitest-environment node | ||
*/ | ||
// @vitest-environment node | ||
import { setupServer } from 'msw/node' | ||
import { HttpResponse, http } from 'msw' | ||
|
||
const server = setupServer( | ||
http.get('https://test.mswjs.io/user', () => { | ||
return HttpResponse.json({ firstName: 'John' }) | ||
}), | ||
) | ||
const server = setupServer() | ||
|
||
beforeAll(() => { | ||
server.listen({ onUnhandledRequest: 'warn' }) | ||
vi.spyOn(global.console, 'warn').mockImplementation(() => void 0) | ||
}) | ||
|
||
afterEach(() => { | ||
vi.clearAllMocks() | ||
}) | ||
|
||
afterAll(() => { | ||
server.close() | ||
vi.restoreAllMocks() | ||
}) | ||
|
||
test('warns on unhandled request when using the "warn" value', async () => { | ||
const res = await fetch('https://test.mswjs.io') | ||
test('warns on unhandled request when using the "warn" strategy', async () => { | ||
await fetch('https://test.mswjs.io/user') | ||
|
||
expect(res).toHaveProperty('status', 404) | ||
expect(console.warn).toBeCalledWith(`\ | ||
[MSW] Warning: intercepted a request without a matching request handler: | ||
• GET https://test.mswjs.io/ | ||
• GET https://test.mswjs.io/user | ||
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`) | ||
}) | ||
|
||
test('ignores common static assets when using the "warn" strategy', async () => { | ||
await fetch('https://example.com/styles/main.css') | ||
|
||
expect(console.warn).not.toHaveBeenCalled() | ||
}) |