Skip to content

Commit

Permalink
use form-data for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Oct 31, 2022
1 parent 8c41a80 commit 4956fc7
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 31 deletions.
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -141,6 +141,7 @@
"faunadb": "2.6.1",
"firebase": "7.14.5",
"flat": "5.0.2",
"form-data": "4.0.0",
"fs-extra": "9.0.0",
"get-port": "5.1.1",
"glob": "7.1.6",
Expand Down Expand Up @@ -204,6 +205,7 @@
"tsec": "0.2.1",
"turbo": "1.5.3",
"typescript": "4.8.2",
"undici": "5.12.0",
"wait-port": "0.2.2",
"webpack": "5.74.0",
"webpack-bundle-analyzer": "4.3.0"
Expand Down
4 changes: 2 additions & 2 deletions packages/next/compiled/undici/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/next/package.json
Expand Up @@ -276,7 +276,7 @@
"timers-browserify": "2.0.12",
"tty-browserify": "0.0.1",
"ua-parser-js": "0.7.28",
"undici": "5.11.0",
"undici": "5.12.0",
"unistore": "3.4.1",
"util": "0.12.4",
"uuid": "8.3.2",
Expand Down
21 changes: 17 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 31 additions & 24 deletions test/e2e/edge-can-read-request-body/index.test.ts
@@ -1,13 +1,9 @@
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { EdgeRuntime } from 'next/dist/compiled/edge-runtime'
import { renderViaHTTP } from 'next-test-utils'
import { fetchViaHTTP, renderViaHTTP } from 'next-test-utils'
import FormData from 'form-data'
import path from 'path'

const runtime = new EdgeRuntime()
const fetch = runtime.evaluate('fetch')
const FormData = runtime.evaluate('FormData')

async function serialize(response: Response) {
return {
text: await response.text(),
Expand Down Expand Up @@ -52,21 +48,28 @@ describe('Edge can read request body', () => {
})

it('reads a text body', async () => {
const response = await fetch(
`${next.url}/api/nothing?middleware-handler=text`,
{
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"}',
},
})
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 () => {
Expand All @@ -92,14 +95,18 @@ describe('Edge can read request body', () => {

it('reads a multipart form data', async () => {
const formData = new FormData()
formData.set('hello', 'world')
const response = await fetch(
`${next.url}/api/nothing?middleware-handler=formData`,
formData.append('hello', 'world')

const response = await fetchViaHTTP(
next.url,
'/api/nothing?middleware-handler=formData',
null,
{
method: 'POST',
body: formData,
}
)

expect(await serialize(response)).toMatchObject({
text: expect.stringContaining('ok'),
status: 200,
Expand Down

0 comments on commit 4956fc7

Please sign in to comment.