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(FetchResponse): set urlList #690 #695

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions src/interceptors/ClientRequest/MockHttpSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export class MockHttpSocket extends MockSocket {
private responseStream?: Readable
private originalSocket?: net.Socket


constructor(options: MockHttpSocketOptions) {
super({
write: (chunk, encoding, callback) => {
Expand Down Expand Up @@ -592,6 +593,7 @@ export class MockHttpSocket extends MockSocket {
this.request,
'Failed to handle a response: request does not exist'
)
FetchResponse.setUrl(this.request.url, response)

/**
* @fixme Stop relying on the "X-Request-Id" request header
Expand Down
8 changes: 8 additions & 0 deletions src/utils/fetchUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ export class FetchResponse extends Response {
configurable: true,
writable: false,
})

const stateSymbol = Object.getOwnPropertySymbols(response).find(
(symbol) => symbol.description === 'state'
)
if (stateSymbol) {
const state = Reflect.get(response, stateSymbol) as object
Reflect.set(state, 'urlList', [new URL(url)])
}
}

constructor(body?: BodyInit | null, init: FetchResponseInit = {}) {
Expand Down
7 changes: 6 additions & 1 deletion test/features/events/response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ it('ClientRequest: emits the "response" event for a mocked response', async () =
expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.headers.get('x-response-type')).toBe('mocked')
expect(response.clone().url).toBe(request.url)
expect(await response.text()).toBe('mocked-response-text')

expect(isMockedResponse).toBe(true)
Expand Down Expand Up @@ -144,6 +145,7 @@ it('ClientRequest: emits the "response" event upon the original response', async
expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.headers.get('x-response-type')).toBe('original')
expect(response.clone().url).toBe(request.url)
expect(await response.text()).toBe('original-response-text')

expect(isMockedResponse).toBe(false)
Expand Down Expand Up @@ -176,6 +178,7 @@ it('XMLHttpRequest: emits the "response" event upon a mocked response', async ()
expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.headers.get('x-response-type')).toBe('mocked')
expect(response.clone().url).toBe(request.url)
expect(await response.text()).toBe('mocked-response-text')
expect(isMockedResponse).toBe(true)

Expand Down Expand Up @@ -219,6 +222,7 @@ it('XMLHttpRequest: emits the "response" event upon the original response', asyn
expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.headers.get('x-response-type')).toBe('original')
expect(response.clone().url).toBe(request.url)
expect(await response.text()).toBe('original-response-text')

expect(isMockedResponse).toBe(false)
Expand Down Expand Up @@ -251,6 +255,7 @@ it('fetch: emits the "response" event upon a mocked response', async () => {
expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.headers.get('x-response-type')).toBe('mocked')
expect(response.clone().url).toBe(request.url)
expect(await response.text()).toBe('mocked-response-text')

expect(isMockedResponse).toBe(true)
Expand All @@ -274,7 +279,6 @@ it('fetch: emits the "response" event upon the original response', async () => {

const [{ response, request, isMockedResponse }] =
responseListener.mock.calls[0]

expect(request.method).toBe('POST')
expect(request.url).toBe(httpServer.https.url('/account'))
expect(request.headers.get('x-request-custom')).toBe('yes')
Expand All @@ -284,6 +288,7 @@ it('fetch: emits the "response" event upon the original response', async () => {
expect(response.status).toBe(200)
expect(response.statusText).toBe('OK')
expect(response.headers.get('x-response-type')).toBe('original')
expect(response.clone().url).toBe(request.url)
expect(await response.text()).toBe('original-response-text')

expect(isMockedResponse).toBe(false)
Expand Down