-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fixed to return 400 response when the request body is not FormData
- Loading branch information
Showing
2 changed files
with
40 additions
and
1 deletion.
There are no files selected for viewing
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
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,35 @@ | ||
import { Hono } from 'hono' | ||
import { z } from 'zod' | ||
import { parseWithZod } from '@conform-to/zod' | ||
import { conformValidator } from '../src' | ||
|
||
describe('Validate common processing', () => { | ||
const app = new Hono() | ||
const schema = z.object({ name: z.string() }) | ||
const route = app.post( | ||
'/author', | ||
conformValidator((formData) => parseWithZod(formData, { schema })), | ||
(c) => { | ||
const submission = c.req.valid('form') | ||
const value = submission.value | ||
return c.json({ success: true, message: `my name is ${value.name}` }) | ||
} | ||
) | ||
|
||
describe('When the request body is empty', () => { | ||
it('Should return 400 response', async () => { | ||
const res = await route.request('/author', { method: 'POST' }) | ||
expect(res.status).toBe(400) | ||
}) | ||
}) | ||
|
||
describe('When the request body is not FormData', () => { | ||
it('Should return 400 response', async () => { | ||
const res = await route.request('/author', { | ||
method: 'POST', | ||
body: JSON.stringify({ name: 'Space Cat!' }), | ||
}) | ||
expect(res.status).toBe(400) | ||
}) | ||
}) | ||
}) |