From 44455d0eb5c3b52635cfed12141e3c66456f4b13 Mon Sep 17 00:00:00 2001 From: Artem Zakharchenko Date: Fri, 25 Aug 2023 12:43:03 +0200 Subject: [PATCH] test: model bodyUsed tests after fetch --- .../fetch-request-body-used.test.ts | 94 +++++-------------- 1 file changed, 21 insertions(+), 73 deletions(-) diff --git a/test/modules/fetch/compliance/fetch-request-body-used.test.ts b/test/modules/fetch/compliance/fetch-request-body-used.test.ts index ea217f85..14ff1582 100644 --- a/test/modules/fetch/compliance/fetch-request-body-used.test.ts +++ b/test/modules/fetch/compliance/fetch-request-body-used.test.ts @@ -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' @@ -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() 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() 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') })