Skip to content

Commit

Permalink
test: model bodyUsed tests after fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Aug 25, 2023
1 parent 46ea7f1 commit 44455d0
Showing 1 changed file with 21 additions and 73 deletions.
94 changes: 21 additions & 73 deletions test/modules/fetch/compliance/fetch-request-body-used.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { it, expect, beforeAll, afterEach, afterAll } from 'vitest'
import * as express from 'express'
import { DeferredPromise } from '@open-draft/deferred-promise'
import { HttpServer } from '@open-draft/test-server/http'
import { FetchInterceptor } from '../../../../src/interceptors/fetch'

Expand All @@ -25,106 +26,53 @@ afterAll(async () => {
await httpServer.close()
})

it('does not mark request body as used for request with no listeners', async () => {
const request = new Request(httpServer.http.url('/resource'), {
method: 'POST',
body: 'Hello server',
})
const responsePromise = fetch(request)
const bodyUsedAfterFetch = request.bodyUsed

await interceptor['emitter'].untilIdle('request')
const bodyUsedAfterListeners = request.bodyUsed

const response = await responsePromise
const bodyUsedAfterResponse = request.bodyUsed

expect(bodyUsedAfterFetch).toBe(false)
expect(bodyUsedAfterListeners).toBe(false)
expect(bodyUsedAfterResponse).toBe(false)

expect(await response.text()).toBe('received: Hello server')
})

it('does not mark request body as used for request with a bypass listener', async () => {
it('request body is unused in the listener when using Request argument', async () => {
const requestInListenerPromise = new DeferredPromise<Request>()
interceptor.on('request', ({ request }) => {
return
requestInListenerPromise.resolve(request)
})

const request = new Request(httpServer.http.url('/resource'), {
method: 'POST',
body: 'Hello server',
})
const bodyUsedBeforeFetch = request.bodyUsed

const responsePromise = fetch(request)
const bodyUsedAfterFetch = request.bodyUsed

await interceptor['emitter'].untilIdle('request')
const bodyUsedAfterListeners = request.bodyUsed
const requestInListener = await requestInListenerPromise
const bodyUsedInListener = requestInListener.bodyUsed

const response = await responsePromise
const bodyUsedAfterResponse = request.bodyUsed

expect(bodyUsedAfterFetch).toBe(false)
expect(bodyUsedAfterListeners).toBe(false)
expect(bodyUsedAfterResponse).toBe(false)
expect(bodyUsedBeforeFetch).toBe(false)
expect(bodyUsedInListener).toBe(false)
// Fetch reads the request body in order to send it.
expect(bodyUsedAfterFetch).toBe(true)
expect(bodyUsedAfterResponse).toBe(true)

expect(await response.text()).toBe('received: Hello server')
})

it('does not mark request body as used for request with a mocked response listener', async () => {
it('request body is unused in the listener when using input and init arguments', async () => {
const requestInListenerPromise = new DeferredPromise<Request>()
interceptor.on('request', ({ request }) => {
// The request body remains unused because the listener
// never read it. Nothing else should.
request.respondWith(new Response('Mocked response'))
})

const request = new Request(httpServer.http.url('/resource'), {
method: 'POST',
body: 'Hello server',
})
const responsePromise = fetch(request)
const bodyUsedAfterFetch = request.bodyUsed

await interceptor['emitter'].untilIdle('request')
const bodyUsedAfterListeners = request.bodyUsed

const response = await responsePromise
const bodyUsedAfterResponse = request.bodyUsed

expect(bodyUsedAfterFetch).toBe(false)
expect(bodyUsedAfterListeners).toBe(false)
expect(bodyUsedAfterResponse).toBe(false)

expect(await response.text()).toBe('Mocked response')
})

it('marks request body as used if the mocked response listener reads it', async () => {
interceptor.on('request', async ({ request }) => {
const text = await request.text()

// The request body remains unused because the listener
// never read it. Nothing else should.
request.respondWith(new Response(`mocked: ${text}`))
requestInListenerPromise.resolve(request)
})

const request = new Request(httpServer.http.url('/resource'), {
const responsePromise = fetch(httpServer.http.url('/resource'), {
method: 'POST',
body: 'Hello server',
})
const responsePromise = fetch(request)
const bodyUsedAfterFetch = request.bodyUsed

await interceptor['emitter'].untilIdle('request')
const bodyUsedAfterListeners = request.bodyUsed
const requestInListener = await requestInListenerPromise
const bodyUsedInListener = requestInListener.bodyUsed

const response = await responsePromise
const bodyUsedAfterResponse = request.bodyUsed

expect(bodyUsedAfterFetch).toBe(false)
// Since the request listener reads the request body,
// it should only be marked as read after the listeners are done.
expect(bodyUsedAfterListeners).toBe(true)
expect(bodyUsedAfterResponse).toBe(true)
expect(bodyUsedInListener).toBe(false)

expect(await response.text()).toBe('mocked: Hello server')
expect(await response.text()).toBe('received: Hello server')
})

0 comments on commit 44455d0

Please sign in to comment.