Skip to content

Commit a2153c9

Browse files
bittttttenkettanaito
andauthoredAug 28, 2024··
feat: print request body in onUnhandledRequest message (#2227)
Co-authored-by: Artem Zakharchenko <kettanaito@gmail.com>
1 parent 78360c8 commit a2153c9

File tree

5 files changed

+51
-18
lines changed

5 files changed

+51
-18
lines changed
 

‎src/core/utils/request/onUnhandledRequest.test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ const fixtures = {
1212
1313
• GET ${url}
1414
15+
If you still wish to intercept this unhandled request, please create a request handler for it.
16+
Read more: https://mswjs.io/docs/getting-started/mocks`,
17+
warningWithResponseBody: (url = `/api`) => `\
18+
[MSW] Warning: intercepted a request without a matching request handler:
19+
20+
• POST ${url}
21+
22+
• Request body: {\"variables\":{\"id\":\"abc-123\"},\"query\":\"query UserName($id: String!) { user(id: $id) { name } }\"}
23+
1524
If you still wish to intercept this unhandled request, please create a request handler for it.
1625
Read more: https://mswjs.io/docs/getting-started/mocks`,
1726

@@ -51,6 +60,25 @@ test('supports the "warn" request strategy', async () => {
5160
)
5261
})
5362

63+
test('supports the "warn" request strategy with request body', async () => {
64+
await onUnhandledRequest(
65+
new Request(new URL('http://localhost/api'), {
66+
method: 'POST',
67+
headers: {
68+
'Content-Type': 'application/json',
69+
},
70+
body: JSON.stringify({
71+
variables: {
72+
id: 'abc-123',
73+
},
74+
query: 'query UserName($id: String!) { user(id: $id) { name } }',
75+
}),
76+
}),
77+
)
78+
79+
expect(console.warn).toHaveBeenCalledWith(fixtures.warningWithResponseBody())
80+
})
81+
5482
test('supports the "error" request strategy', async () => {
5583
await expect(
5684
onUnhandledRequest(new Request(new URL('http://localhost/api')), 'error'),

‎src/core/utils/request/onUnhandledRequest.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ export async function onUnhandledRequest(
2424
const url = new URL(request.url)
2525
const publicUrl = toPublicUrl(url) + url.search
2626

27-
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`
27+
const requestBody =
28+
request.method === 'HEAD' || request.method === 'GET'
29+
? null
30+
: await request.clone().text()
31+
const messageDetails = `\n\n \u2022 ${request.method} ${publicUrl}\n\n${requestBody ? ` \u2022 Request body: ${requestBody}\n\n` : ''}`
32+
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`
2833

2934
function applyStrategy(strategy: UnhandledRequestStrategy) {
3035
switch (strategy) {

‎test/browser/graphql-api/anonymous-operation.test.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ test('does not warn on anonymous GraphQL operation when no GraphQL handlers are
4141

4242
const endpointUrl = httpServer.http.url('/graphql')
4343
const response = await query(endpointUrl, {
44+
// Intentionally anonymous query.
4445
query: gql`
45-
# Intentionally anonymous query.
4646
query {
4747
user {
4848
id
@@ -71,6 +71,8 @@ test('does not warn on anonymous GraphQL operation when no GraphQL handlers are
7171
7272
• POST ${endpointUrl}
7373
74+
• Request body: {\"query\":\"\\n query {\\n user {\\n id\\n }\\n }\\n \"}
75+
7476
If you still wish to intercept this unhandled request, please create a request handler for it.
7577
Read more: https://mswjs.io/docs/getting-started/mocks`,
7678
])
@@ -105,9 +107,9 @@ test('warns on handled anonymous GraphQL operation', async ({
105107

106108
const endpointUrl = httpServer.http.url('/graphql')
107109
const response = await query(endpointUrl, {
110+
// Intentionally anonymous query.
111+
// It will be handled in the "graphql.operation()" handler above.
108112
query: gql`
109-
# Intentionally anonymous query.
110-
# It will be handled in the "graphql.operation()" handler above.
111113
query {
112114
user {
113115
id
@@ -170,9 +172,9 @@ test('does not print a warning on anonymous GraphQL operation handled by "graphq
170172

171173
const endpointUrl = httpServer.http.url('/graphql')
172174
const response = await query(endpointUrl, {
175+
// Intentionally anonymous query.
176+
// It will be handled in the "graphql.operation()" handler above.
173177
query: gql`
174-
# Intentionally anonymous query.
175-
# It will be handled in the "graphql.operation()" handler above.
176178
query {
177179
user {
178180
id

‎test/node/regressions/many-request-handlers-jsdom.test.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
/**
2-
* @vitest-environment jsdom
3-
*/
1+
// @vitest-environment jsdom
42
import { HttpServer } from '@open-draft/test-server/http'
53
import { graphql, http, HttpResponse } from 'msw'
64
import { setupServer } from 'msw/node'
@@ -74,8 +72,9 @@ describe('http handlers', () => {
7472
body: 'request-body-',
7573
},
7674
)
77-
// Each clone is a new AbortSignal listener which needs to be registered
78-
expect(requestCloneSpy).toHaveBeenCalledTimes(1)
75+
// Each clone is a new AbortSignal listener which needs to be registered.
76+
// One clone is `onUnhandledRequest` reading the request body to print.
77+
expect(requestCloneSpy).toHaveBeenCalledTimes(2)
7978
expect(httpResponse.status).toBe(500)
8079
expect(processErrorSpy).not.toHaveBeenCalled()
8180
})
@@ -122,7 +121,7 @@ describe('graphql handlers', () => {
122121
})
123122

124123
expect(unhandledResponse.status).toEqual(500)
125-
expect(requestCloneSpy).toHaveBeenCalledTimes(2)
124+
expect(requestCloneSpy).toHaveBeenCalledTimes(3)
126125
// Must not print any memory leak warnings.
127126
expect(processErrorSpy).not.toHaveBeenCalled()
128127
})

‎test/node/regressions/many-request-handlers.test.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
/**
2-
* @vitest-environment node
3-
*/
1+
// @vitest-environment node
42
import { HttpServer } from '@open-draft/test-server/http'
53
import { graphql, http, HttpResponse } from 'msw'
64
import { setupServer } from 'msw/node'
@@ -75,8 +73,9 @@ describe('http handlers', () => {
7573
body: 'request-body-',
7674
},
7775
)
78-
// Each clone is a new AbortSignal listener which needs to be registered
79-
expect(requestCloneSpy).toHaveBeenCalledTimes(1)
76+
// Each clone is a new AbortSignal listener which needs to be registered.
77+
// One clone is `onUnhandledRequest` reading the request body to print.
78+
expect(requestCloneSpy).toHaveBeenCalledTimes(2)
8079
expect(httpResponse.status).toBe(500)
8180
expect(stdErrSpy).not.toHaveBeenCalled()
8281
})
@@ -124,7 +123,7 @@ describe('graphql handlers', () => {
124123
})
125124

126125
expect(unhandledResponse.status).toEqual(500)
127-
expect(requestCloneSpy).toHaveBeenCalledTimes(2)
126+
expect(requestCloneSpy).toHaveBeenCalledTimes(3)
128127
// Must not print any memory leak warnings.
129128
expect(stdErrSpy).not.toHaveBeenCalled()
130129
})

0 commit comments

Comments
 (0)
Please sign in to comment.