Skip to content

Commit

Permalink
feat: add in request body message to onUnhandledRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
bitttttten committed Jul 30, 2024
1 parent 6647ec8 commit 8dc67bc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
30 changes: 30 additions & 0 deletions src/core/utils/request/onUnhandledRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ const fixtures = {
• 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`,
warningWithResonseBody: (url = `/api`) => `\
[MSW] Warning: intercepted a request without a matching request handler:
• POST ${url}
• Request body: {\"variables\":{\"id\":\"abc-123\"},\"query\":\"query UserName($id: String!) { user(id: $id) { name } } }\"}
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`,

Expand Down Expand Up @@ -51,6 +60,27 @@ test('supports the "warn" request strategy', async () => {
)
})

test('supports the "warn" request strategy with request body', async () => {
await onUnhandledRequest(
new Request(new URL('http://localhost/api'), {
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
},
cache: 'no-store',
body: JSON.stringify({
variables: {
id: 'abc-123',
},
query: 'query UserName($id: String!) { user(id: $id) { name } } }',
}),
}),
)

expect(console.warn).toHaveBeenCalledWith(fixtures.warningWithResonseBody())
})

test('supports the "error" request strategy', async () => {
await expect(
onUnhandledRequest(new Request(new URL('http://localhost/api')), 'error'),
Expand Down
5 changes: 3 additions & 2 deletions src/core/utils/request/onUnhandledRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ export async function onUnhandledRequest(
const url = new URL(request.url)
const publicUrl = toPublicUrl(url) + url.search

const messageDetails = `\n\n \u2022 ${request.method} ${publicUrl}\n\n \u2022 Request body: ${await request.text()}`
const unhandledRequestMessage = `intercepted a request without a matching request handler:${messageDetails}\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`
const requestBody = await request.clone().text()
const messageDetails = `\n\n \u2022 ${request.method} ${publicUrl}\n\n${requestBody ? ` \u2022 Request body: ${await request.text()}\n\n` : ''}`
const unhandledRequestMessage = `intercepted a request without a matching request handler:${messageDetails}If you still wish to intercept this unhandled request, please create a request handler for it.\nRead more: https://mswjs.io/docs/getting-started/mocks`

function applyStrategy(strategy: UnhandledRequestStrategy) {
switch (strategy) {
Expand Down

0 comments on commit 8dc67bc

Please sign in to comment.