Skip to content

Commit

Permalink
test: add request/response body reading test (#620)
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito authored Aug 13, 2024
1 parent 26ad35a commit 7f86ee6
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions test/features/events/response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,27 @@ it('fetch: emits the "response" event upon the original response', async () => {

expect(isMockedResponse).toBe(false)
})

it('supports reading the request and response bodies in the "response" listener', async () => {
const requestCallback = vi.fn()
const responseCallback = vi.fn()
const responseListener = vi.fn<HttpRequestEventMap['response']>(
async ({ request, response }) => {
requestCallback(await request.clone().text())
responseCallback(await response.clone().text())
}
)
interceptor.on('response', responseListener)

await nodeFetch(httpServer.https.url('/user'), {
method: 'POST',
body: 'request-body',
})

await waitForExpect(() => {
expect(responseListener).toHaveBeenCalledTimes(1)
})

expect(requestCallback).toHaveBeenCalledWith('request-body')
expect(responseCallback).toHaveBeenCalledWith('mocked-response-text')
})

0 comments on commit 7f86ee6

Please sign in to comment.