Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add request formdata file size test #1995

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This verifies that the Blob sent from the request is properly available in the response resolver.

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(),
})
})
Loading