Skip to content

Commit

Permalink
test: add request formdata file size test (#1995)
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito authored Jan 25, 2024
1 parent 396b12f commit 17bf9fc
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion test/node/rest-api/request/body/body-form-data.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ const server = setupServer(
const formData = await request.formData()
return HttpResponse.json(Array.from(formData.entries()))
}),
http.post('http://localhost/file', async ({ request }) => {
const formData = await request.formData()
const file = formData.get('file') as File | null

if (!file) {
throw HttpResponse.text('Missing file', { status: 400 })
}

return HttpResponse.json({
name: file.name,
size: file.size,
content: await file.text(),
})
}),
)

beforeAll(() => {
Expand All @@ -19,7 +33,7 @@ afterAll(() => {
server.close()
})

test('reads FormData request body', async () => {
it('supports FormData request body', async () => {
// Note that creating a `FormData` instance in Node/JSDOM differs
// from the same instance in a real browser. Follow the instructions
// of your `fetch` polyfill to learn more.
Expand All @@ -39,3 +53,23 @@ test('reads FormData request body', async () => {
['password', 'secret123'],
])
})

it('respects Blob size in request body', async () => {
const blob = new Blob([JSON.stringify({ data: 1 })], {
type: 'application/json',
})
const formData = new FormData()
formData.set('file', blob, 'data.json')

const response = await fetch('http://localhost/file', {
method: 'POST',
body: formData,
})

expect(response.status).toBe(200)
expect(await response.json()).toEqual({
name: 'data.json',
size: blob.size,
content: await blob.text(),
})
})

0 comments on commit 17bf9fc

Please sign in to comment.