-
-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(fetch): add "bodyUsed" compliance (#403)
- Loading branch information
1 parent
0adedc5
commit d5f1d0d
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
test/modules/fetch/compliance/fetch-request-body-used.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
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' | ||
|
||
const interceptor = new FetchInterceptor() | ||
|
||
const httpServer = new HttpServer((app) => { | ||
app.post('/resource', express.text(), (req, res) => | ||
res.send(`received: ${req.body}`) | ||
) | ||
}) | ||
|
||
beforeAll(async () => { | ||
interceptor.apply() | ||
await httpServer.listen() | ||
}) | ||
|
||
afterEach(() => { | ||
interceptor['emitter'].removeAllListeners() | ||
}) | ||
|
||
afterAll(async () => { | ||
interceptor.dispose() | ||
await httpServer.close() | ||
}) | ||
|
||
it('request body is unused in the listener when using Request argument', async () => { | ||
const requestInListenerPromise = new DeferredPromise<Request>() | ||
interceptor.on('request', ({ request }) => { | ||
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 | ||
|
||
const requestInListener = await requestInListenerPromise | ||
const bodyUsedInListener = requestInListener.bodyUsed | ||
|
||
const response = await responsePromise | ||
const bodyUsedAfterResponse = request.bodyUsed | ||
|
||
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('request body is unused in the listener when using input and init arguments', async () => { | ||
const requestInListenerPromise = new DeferredPromise<Request>() | ||
interceptor.on('request', ({ request }) => { | ||
requestInListenerPromise.resolve(request) | ||
}) | ||
|
||
const responsePromise = fetch(httpServer.http.url('/resource'), { | ||
method: 'POST', | ||
body: 'Hello server', | ||
}) | ||
|
||
const requestInListener = await requestInListenerPromise | ||
const bodyUsedInListener = requestInListener.bodyUsed | ||
|
||
const response = await responsePromise | ||
|
||
expect(bodyUsedInListener).toBe(false) | ||
|
||
expect(await response.text()).toBe('received: Hello server') | ||
}) |