Download next.js/test/e2e/edge-can-read-request-body/index.test.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 3.27 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/test/e2e/edge-can-read-request-body/index.test.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/next.js/test/e2e/edge-can-read-request-body/index.test.ts
-
curl -L -o index.test.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/test/e2e/edge-can-read-request-body/index.test.ts
3.27 kB
| import { createNext, FileRef } from 'e2e-utils' | |
| import { NextInstance } from 'e2e-utils' | |
| import { fetchViaHTTP, renderViaHTTP } from 'next-test-utils' | |
| import path from 'path' | |
| import type { Response } from 'node-fetch' | |
| async function serialize(response: Response) { | |
| return { | |
| text: await response.text(), | |
| headers: Object.fromEntries(response.headers), | |
| status: response.status, | |
| } | |
| } | |
| describe('Edge can read request body', () => { | |
| let next: NextInstance | |
| beforeAll(async () => { | |
| next = await createNext({ | |
| files: new FileRef(path.resolve(__dirname, './app')), | |
| dependencies: {}, | |
| }) | |
| }) | |
| afterAll(() => next.destroy()) | |
| it('renders the static page', async () => { | |
| const html = await renderViaHTTP(next.url, '/api/nothing') | |
| expect(html).toContain('ok') | |
| }) | |
| describe('middleware', () => { | |
| it('reads a JSON body', async () => { | |
| const response = await fetchViaHTTP( | |
| next.url, | |
| '/api/nothing?middleware-handler=json', | |
| null, | |
| { | |
| method: 'POST', | |
| body: JSON.stringify({ hello: 'world' }), | |
| } | |
| ) | |
| expect(await serialize(response)).toMatchObject({ | |
| text: expect.stringContaining('ok'), | |
| status: 200, | |
| headers: { | |
| 'x-req-type': 'json', | |
| 'x-serialized': '{"hello":"world"}', | |
| }, | |
| }) | |
| }) | |
| it('reads a text body', async () => { | |
| try { | |
| const response = await fetchViaHTTP( | |
| next.url, | |
| '/api/nothing?middleware-handler=text', | |
| null, | |
| { | |
| method: 'POST', | |
| body: JSON.stringify({ hello: 'world' }), | |
| } | |
| ) | |
| expect(await serialize(response)).toMatchObject({ | |
| text: expect.stringContaining('ok'), | |
| status: 200, | |
| headers: { | |
| 'x-req-type': 'text', | |
| 'x-serialized': '{"hello":"world"}', | |
| }, | |
| }) | |
| } catch (err) { | |
| console.log('FAILED', err) | |
| } | |
| }) | |
| it('reads an URL encoded form data', async () => { | |
| const response = await fetchViaHTTP( | |
| next.url, | |
| '/api/nothing?middleware-handler=formData', | |
| null, | |
| { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/x-www-form-urlencoded', | |
| }, | |
| body: new URLSearchParams({ hello: 'world' }).toString(), | |
| } | |
| ) | |
| expect(await serialize(response)).toMatchObject({ | |
| text: expect.stringContaining('ok'), | |
| status: 200, | |
| headers: { | |
| 'x-req-type': 'formData', | |
| 'x-serialized': '{"hello":"world"}', | |
| }, | |
| }) | |
| }) | |
| it('reads a multipart form data', async () => { | |
| const formData = new FormData() | |
| formData.append('hello', 'world') | |
| // @ts-expect-error use `fetchViaHTTP` when we drop `node-fetch` | |
| const response: Response = await fetch( | |
| new URL(next.url + '/api/nothing?middleware-handler=formData'), | |
| { method: 'POST', body: formData } | |
| ) | |
| expect(await serialize(response)).toMatchObject({ | |
| text: expect.stringContaining('ok'), | |
| status: 200, | |
| headers: { | |
| 'x-req-type': 'formData', | |
| 'x-serialized': '{"hello":"world"}', | |
| }, | |
| }) | |
| }) | |
| }) | |
| }) | |