Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(setupWorker): set "response.url" in "response:*" events #2031

Merged
merged 2 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/browser/setupWorker/start/createResponseListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ export function createResponseListener(context: SetupWorkerInternalContext) {
responseJson,
)

/**
* Set response URL if it's not set already.
* @see https://github.com/mswjs/msw/issues/2030
* @see https://developer.mozilla.org/en-US/docs/Web/API/Response/url
*/
if (!response.url) {
Object.defineProperty(response, 'url', {
value: request.url,
enumerable: true,
writable: false,
})
}

context.emitter.emit(
responseJson.isMockedResponse ? 'response:mocked' : 'response:bypass',
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ worker.events.on(
async ({ response, request, requestId }) => {
const body = await response.clone().text()
console.warn(
`[response:mocked] ${body} ${request.method} ${request.url} ${requestId}`,
`[response:mocked] ${response.url} ${body} ${request.method} ${request.url} ${requestId}`,
)
},
)
Expand All @@ -50,7 +50,7 @@ worker.events.on(
async ({ response, request, requestId }) => {
const body = await response.clone().text()
console.warn(
`[response:bypass] ${body} ${request.method} ${request.url} ${requestId}`,
`[response:bypass] ${response.url} ${body} ${request.method} ${request.url} ${requestId}`,
)
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ test('emits events for a handled request and mocked response', async ({
`[request:start] GET ${url} ${requestId}`,
`[request:match] GET ${url} ${requestId}`,
`[request:end] GET ${url} ${requestId}`,
`[response:mocked] response-body GET ${url} ${requestId}`,
`[response:mocked] ${url} response-body GET ${url} ${requestId}`,
])
})

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

Expand All @@ -107,7 +107,7 @@ test('emits events for an unhandled request', async ({
`[request:start] GET ${url} ${requestId}`,
`[request:unhandled] GET ${url} ${requestId}`,
`[request:end] GET ${url} ${requestId}`,
`[response:bypass] majestic-unknown GET ${url} ${requestId}`,
`[response:bypass] ${url} majestic-unknown GET ${url} ${requestId}`,
])
})

Expand Down
Loading