|
1 | 1 | import { Hono } from 'hono'; |
2 | 2 | import type { MiddlewareHandler } from 'hono'; |
3 | 3 | import { describe, expect, it } from 'vite-plus/test'; |
| 4 | +import { objectToFormData } from '../../src/pages/navigation-engine.ts'; |
4 | 5 | import { |
5 | 6 | defineHandler, |
6 | 7 | defineMiddleware, |
@@ -350,4 +351,176 @@ describe('defineHandler.withValidator', () => { |
350 | 351 | body: { name: 'Bob' }, |
351 | 352 | }); |
352 | 353 | }); |
| 354 | + |
| 355 | + it('passes a multipart file to the schema and the handler', async () => { |
| 356 | + const photo = new File(['hello'], 'photo.png', { type: 'image/png' }); |
| 357 | + let seenBySchema: unknown; |
| 358 | + const handler = defineHandler.withValidator({ |
| 359 | + body: { |
| 360 | + '~standard': { |
| 361 | + version: 1, |
| 362 | + vendor: 'test', |
| 363 | + validate(value: unknown) { |
| 364 | + seenBySchema = value; |
| 365 | + const uploaded = (value as { photo?: unknown }).photo; |
| 366 | + if (!(uploaded instanceof File)) { |
| 367 | + return { issues: [{ message: 'expected a file', path: ['photo'] }] }; |
| 368 | + } |
| 369 | + return { value }; |
| 370 | + }, |
| 371 | + }, |
| 372 | + }, |
| 373 | + })((_c, { body }) => { |
| 374 | + const received = body as { title: string; photo: File }; |
| 375 | + return { |
| 376 | + title: received.title, |
| 377 | + name: received.photo.name, |
| 378 | + type: received.photo.type, |
| 379 | + size: received.photo.size, |
| 380 | + }; |
| 381 | + }); |
| 382 | + |
| 383 | + const app = createValidatedApp('post', '/test', handler); |
| 384 | + const res = await app.request( |
| 385 | + new Request('http://localhost/test', { |
| 386 | + method: 'POST', |
| 387 | + body: objectToFormData({ title: 'Sunset', photo }), |
| 388 | + }), |
| 389 | + ); |
| 390 | + |
| 391 | + expect(res.status).toBe(200); |
| 392 | + expect(await res.json()).toEqual({ |
| 393 | + title: 'Sunset', |
| 394 | + name: 'photo.png', |
| 395 | + type: 'image/png', |
| 396 | + size: photo.size, |
| 397 | + }); |
| 398 | + const parsed = seenBySchema as { title: string; photo: File }; |
| 399 | + expect(parsed.title).toBe('Sunset'); |
| 400 | + expect(parsed.photo).toBeInstanceOf(File); |
| 401 | + expect(parsed.photo.name).toBe('photo.png'); |
| 402 | + expect(await parsed.photo.text()).toBe('hello'); |
| 403 | + }); |
| 404 | + |
| 405 | + it('restores nested bracket keys from form bodies', async () => { |
| 406 | + const handler = defineHandler.withValidator({ |
| 407 | + body: passSchema(), |
| 408 | + })((_c, { body }) => ({ received: body })); |
| 409 | + const app = createValidatedApp('post', '/test', handler); |
| 410 | + const takenAt = new Date('2020-01-02T03:04:05.000Z'); |
| 411 | + const multipart = objectToFormData({ |
| 412 | + profile: { name: 'Ada' }, |
| 413 | + tags: ['work', 'urgent'], |
| 414 | + album: { photos: [{ name: 'a.png' }] }, |
| 415 | + published: true, |
| 416 | + count: 2, |
| 417 | + takenAt, |
| 418 | + note: null, |
| 419 | + }); |
| 420 | + multipart.append('photos[]', 'one'); |
| 421 | + multipart.append('photos[]', 'two'); |
| 422 | + multipart.append('__proto__[polluted]', 'yes'); |
| 423 | + |
| 424 | + const multipartRes = await app.request( |
| 425 | + new Request('http://localhost/test', { method: 'POST', body: multipart }), |
| 426 | + ); |
| 427 | + expect(multipartRes.status).toBe(200); |
| 428 | + expect(await multipartRes.json()).toEqual({ |
| 429 | + received: { |
| 430 | + profile: { name: 'Ada' }, |
| 431 | + tags: ['work', 'urgent'], |
| 432 | + album: { photos: [{ name: 'a.png' }] }, |
| 433 | + published: '1', |
| 434 | + count: '2', |
| 435 | + takenAt: '2020-01-02T03:04:05.000Z', |
| 436 | + note: '', |
| 437 | + photos: ['one', 'two'], |
| 438 | + ['__proto__']: { polluted: 'yes' }, |
| 439 | + }, |
| 440 | + }); |
| 441 | + expect('polluted' in Object.prototype).toBe(false); |
| 442 | + |
| 443 | + const params = new URLSearchParams(); |
| 444 | + params.append('profile[name]', 'Ada'); |
| 445 | + params.append('tags[0]', 'work'); |
| 446 | + params.append('tags[1]', 'urgent'); |
| 447 | + params.append('photos[]', 'one'); |
| 448 | + params.append('photos[]', 'two'); |
| 449 | + const urlencodedRes = await app.request( |
| 450 | + new Request('http://localhost/test', { |
| 451 | + method: 'POST', |
| 452 | + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, |
| 453 | + body: params.toString(), |
| 454 | + }), |
| 455 | + ); |
| 456 | + expect(urlencodedRes.status).toBe(200); |
| 457 | + expect(await urlencodedRes.json()).toEqual({ |
| 458 | + received: { |
| 459 | + profile: { name: 'Ada' }, |
| 460 | + tags: ['work', 'urgent'], |
| 461 | + photos: ['one', 'two'], |
| 462 | + }, |
| 463 | + }); |
| 464 | + }); |
| 465 | + |
| 466 | + it('keeps JSON body validation unchanged', async () => { |
| 467 | + const handler = defineHandler.withValidator({ |
| 468 | + body: passSchema(), |
| 469 | + })((_c, { body }) => ({ received: body })); |
| 470 | + const app = createValidatedApp('post', '/test', handler); |
| 471 | + |
| 472 | + const res = await app.request( |
| 473 | + new Request('http://localhost/test', { |
| 474 | + method: 'POST', |
| 475 | + headers: { 'Content-Type': 'application/json' }, |
| 476 | + body: JSON.stringify({ |
| 477 | + profile: { name: 'Ada' }, |
| 478 | + tags: ['work'], |
| 479 | + published: true, |
| 480 | + count: 2, |
| 481 | + note: null, |
| 482 | + }), |
| 483 | + }), |
| 484 | + ); |
| 485 | + |
| 486 | + expect(res.status).toBe(200); |
| 487 | + expect(await res.json()).toEqual({ |
| 488 | + received: { |
| 489 | + profile: { name: 'Ada' }, |
| 490 | + tags: ['work'], |
| 491 | + published: true, |
| 492 | + count: 2, |
| 493 | + note: null, |
| 494 | + }, |
| 495 | + }); |
| 496 | + }); |
| 497 | + |
| 498 | + it('does not report a bad form body as invalid JSON', async () => { |
| 499 | + const handler = defineHandler.withValidator({ |
| 500 | + body: failSchema('title is required'), |
| 501 | + })(() => ({ ok: true })); |
| 502 | + const app = createValidatedApp('post', '/test', handler); |
| 503 | + |
| 504 | + const malformed = await app.request( |
| 505 | + new Request('http://localhost/test', { |
| 506 | + method: 'POST', |
| 507 | + headers: { 'Content-Type': 'multipart/form-data; boundary=----bad' }, |
| 508 | + body: 'not a multipart body', |
| 509 | + }), |
| 510 | + ); |
| 511 | + expect(malformed.status).toBe(400); |
| 512 | + expect(((await malformed.json()) as { error: string }).error).toBe('Invalid form body'); |
| 513 | + |
| 514 | + const invalid = await app.request( |
| 515 | + new Request('http://localhost/test', { |
| 516 | + method: 'POST', |
| 517 | + body: objectToFormData({ title: '' }), |
| 518 | + }), |
| 519 | + ); |
| 520 | + expect(invalid.status).toBe(400); |
| 521 | + const json = (await invalid.json()) as ValidationIssueBody; |
| 522 | + expect(json.error).toBe('Validation failed'); |
| 523 | + expect(json.issues[0]?.slot).toBe('body'); |
| 524 | + expect(json.issues[0]?.issues[0]?.message).toBe('title is required'); |
| 525 | + }); |
353 | 526 | }); |
0 commit comments