fix(validator): use the cached content (#2234)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix(validator): use the cached content * denoify
authored by Yusuke Wada · committed by GitHub · parent d67babf · Browse files · 3 files·+74 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+74 added−4 removed
- tests:+56 added−0 removed
- other:+18 added−4 removed
Provenance
- Commit
e2f1d14- Tree
746ff51- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
d67babf
3 files changed+74 added−4 removed
modified:deno_dist/validator/validator.ts+9 added-2 removed
65 unmodified lines666768697071727374717275767778794 unmodified lines84858687888990919293949545 unmodified lines65 unmodified lines if (!contentType || !contentType.startsWith('application/json')) { const message = `Invalid HTTP header: Content-Type=${contentType}` throw new HTTPException(400, { message }) } if (c.req.bodyCache.json) { value = await c.req.bodyCache.json break } /** * Get the arrayBuffer first, create JSON object via Response, * and cache the arrayBuffer in the c.req.bodyCache. * Get the arrayBuffer, create JSON object via Response, and cache the arrayBuffer in the c.req.bodyCache if there is no cached content in c.req.bodyCache.json. */ try { const arrayBuffer = c.req.bodyCache.arrayBuffer ?? (await c.req.raw.arrayBuffer()) value = await new Response(arrayBuffer).json()4 unmodified lines throw new HTTPException(400, { message }) } break case 'form': { if (c.req.bodyCache.formData) { value = c.req.bodyCache.formData break } try { const contentType = c.req.header('Content-Type') if (contentType) { const arrayBuffer = c.req.bodyCache.arrayBuffer ?? (await c.req.raw.arrayBuffer())45 unmodified linesmodified:src/validator/validator.test.ts+56 added
170 unmodified lines171 })172 })173 })174 175+describe('Cached contents', () => {176+ const app = new Hono()177+178+ app.post(179+ '/json',180+ async (c, next) => {181+ await c.req.json()182+ await next()183+ },184+ validator('json', (value) => {185+ return value186+ }),187+ async (c) => {188+ const data = await c.req.json()189+ return c.json(data, 200)190+ }191+ )192+193+ app.post(194+ '/form',195+ async (c, next) => {196+ await c.req.formData()197+ await next()198+ },199+ validator('form', (value) => {200+ return value201+ }),202+ async (c) => {203+ return c.json({ message: 'OK' }, 200)204+ }205+ )206+207+ it('Should return the cached JSON content', async () => {208+ const res = await app.request('/json', {209+ method: 'POST',210+ body: JSON.stringify({ message: 'Hello' }),211+ headers: {212+ 'Content-Type': 'application/json',213+ },214+ })215+ expect(res.status).toBe(200)216+ expect(await res.json()).toEqual({ message: 'Hello' })217+ })218+219+ it('Should return the cached form content', async () => {220+ const form = new FormData()221+ form.append('message', 'Hello')222+ const res = await app.request('/form', {223+ method: 'POST',224+ body: form,225+ })226+ expect(res.status).toBe(200)227+ expect(await res.json()).toEqual({ message: 'OK' })228+ })229+})230+231 describe('Validator middleware with a custom validation function', () => {232 const app = new Hono()233 234 const validationFunction: ValidationFunction<{ id: string }, { id: number }> = (v) => {694 unmodified linesmodified:src/validator/validator.ts+9 added-2 removed
65 unmodified lines66 if (!contentType || !contentType.startsWith('application/json')) {67 const message = `Invalid HTTP header: Content-Type=${contentType}`68 throw new HTTPException(400, { message })69 }70+ if (c.req.bodyCache.json) {71+ value = await c.req.bodyCache.json72+ break73+ }74 /**71- * Get the arrayBuffer first, create JSON object via Response,72- * and cache the arrayBuffer in the c.req.bodyCache.75+ * Get the arrayBuffer, create JSON object via Response, and cache the arrayBuffer in the c.req.bodyCache if there is no cached content in c.req.bodyCache.json.76 */77 try {78 const arrayBuffer = c.req.bodyCache.arrayBuffer ?? (await c.req.raw.arrayBuffer())79 value = await new Response(arrayBuffer).json()4 unmodified lines84 throw new HTTPException(400, { message })85 }86 break87 case 'form': {88+ if (c.req.bodyCache.formData) {89+ value = c.req.bodyCache.formData90+ break91+ }92 try {93 const contentType = c.req.header('Content-Type')94 if (contentType) {95 const arrayBuffer = c.req.bodyCache.arrayBuffer ?? (await c.req.raw.arrayBuffer())45 unmodified lines