Skip to content

Commit 0e9d65f

Browse files
authoredFeb 12, 2024··
fix(setupWorker): set "response.url" in "response:*" events (#2031)
1 parent a076142 commit 0e9d65f

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed
 

‎src/browser/setupWorker/start/createResponseListener.ts

+13
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ export function createResponseListener(context: SetupWorkerInternalContext) {
4848
responseJson,
4949
)
5050

51+
/**
52+
* Set response URL if it's not set already.
53+
* @see https://github.com/mswjs/msw/issues/2030
54+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Response/url
55+
*/
56+
if (!response.url) {
57+
Object.defineProperty(response, 'url', {
58+
value: request.url,
59+
enumerable: true,
60+
writable: false,
61+
})
62+
}
63+
5164
context.emitter.emit(
5265
responseJson.isMockedResponse ? 'response:mocked' : 'response:bypass',
5366
{

‎test/browser/msw-api/setup-worker/life-cycle-events/on.mocks.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ worker.events.on(
4040
async ({ response, request, requestId }) => {
4141
const body = await response.clone().text()
4242
console.warn(
43-
`[response:mocked] ${body} ${request.method} ${request.url} ${requestId}`,
43+
`[response:mocked] ${response.url} ${body} ${request.method} ${request.url} ${requestId}`,
4444
)
4545
},
4646
)
@@ -50,7 +50,7 @@ worker.events.on(
5050
async ({ response, request, requestId }) => {
5151
const body = await response.clone().text()
5252
console.warn(
53-
`[response:bypass] ${body} ${request.method} ${request.url} ${requestId}`,
53+
`[response:bypass] ${response.url} ${body} ${request.method} ${request.url} ${requestId}`,
5454
)
5555
},
5656
)

‎test/browser/msw-api/setup-worker/life-cycle-events/on.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ test('emits events for a handled request and mocked response', async ({
5454
`[request:start] GET ${url} ${requestId}`,
5555
`[request:match] GET ${url} ${requestId}`,
5656
`[request:end] GET ${url} ${requestId}`,
57-
`[response:mocked] response-body GET ${url} ${requestId}`,
57+
`[response:mocked] ${url} response-body GET ${url} ${requestId}`,
5858
])
5959
})
6060

@@ -80,7 +80,7 @@ test('emits events for a handled request with no response', async ({
8080
expect(consoleSpy.get('warning')).toEqual([
8181
`[request:start] POST ${url} ${requestId}`,
8282
`[request:end] POST ${url} ${requestId}`,
83-
`[response:bypass] original-response POST ${url} ${requestId}`,
83+
`[response:bypass] ${url} original-response POST ${url} ${requestId}`,
8484
])
8585
})
8686

@@ -107,7 +107,7 @@ test('emits events for an unhandled request', async ({
107107
`[request:start] GET ${url} ${requestId}`,
108108
`[request:unhandled] GET ${url} ${requestId}`,
109109
`[request:end] GET ${url} ${requestId}`,
110-
`[response:bypass] majestic-unknown GET ${url} ${requestId}`,
110+
`[response:bypass] ${url} majestic-unknown GET ${url} ${requestId}`,
111111
])
112112
})
113113

0 commit comments

Comments
 (0)
Please sign in to comment.