request.test.ts
This file
Path
src/request.test.ts
141 lines · 3.6 KiB · TypeScript
Last changed by
fix(req): fix inferring types in `req.valid()` (#1351)Yusuke Wadabefore adoption2faaa15
History of this fileTake it
The walk stopped after 200 commits: lines older than 309c38a point at it.
denoifyYusuke Wadabefore adoption309c38a
lines 1–4why, one step back1import { HonoRequest } from './request'23describe('Query', () => {4 test('req.query() and req.queries()', () => {fix(req): decode special chars in URL params (#1010)Yusuke Wadabefore adoption9ddedc4
lines 5–7why, one step back5 const rawRequest = new Request('http://localhost?page=2&tag=A&tag=B')6 const req = new HonoRequest(rawRequest)7denoifyYusuke Wadabefore adoption309c38a
lines 8–20why, one step back8 const page = req.query('page')9 expect(page).not.toBeUndefined()10 expect(page).toBe('2')1112 const q = req.query('q')13 expect(q).toBeUndefined()1415 const tags = req.queries('tag')16 expect(tags).not.toBeUndefined()17 expect(tags).toEqual(['A', 'B'])1819 const q2 = req.queries('q2')20 expect(q2).toBeUndefined()fix(req): decode special chars in URL params (#1010)Yusuke Wadabefore adoption9ddedc4
lines 21–31why, one step back21 })2223 test('decode special chars', () => {24 const rawRequest = new Request('http://localhost?mail=framework%40hono.dev&tag=%401&tag=%402')25 const req = new HonoRequest(rawRequest)2627 const mail = req.query('mail')28 expect(mail).toBe('framework@hono.dev')2930 const tags = req.queries('tag')31 expect(tags).toEqual(['@1', '@2'])denoifyYusuke Wadabefore adoption309c38a
lines 32–46why, one step back32 })33})3435describe('req.addValidatedData() and req.data()', () => {36 const rawRequest = new Request('http://localhost')3738 const payload = {39 title: 'hello',40 author: {41 name: 'young man',42 age: 20,43 },44 }4546 test('add data - json', () => {fix(req): fix inferring types in `req.valid()` (#1351)Yusuke Wadabefore adoption2faaa15
line 47why, one step back47 const req = new HonoRequest<'/', { json: typeof payload }>(rawRequest)denoifyYusuke Wadabefore adoption309c38a
lines 48–53why, one step back48 req.addValidatedData('json', payload)49 const data = req.valid('json')50 expect(data).toEqual(payload)51 })5253 test('replace data - json', () => {fix(req): fix inferring types in `req.valid()` (#1351)Yusuke Wadabefore adoption2faaa15
line 54why, one step back54 const req = new HonoRequest<'/', { json: typeof payload }>(rawRequest)denoifyYusuke Wadabefore adoption309c38a
lines 55–70why, one step back55 req.addValidatedData('json', payload)56 req.addValidatedData('json', {57 tag: ['sport', 'music'],58 author: {59 tall: 170,60 },61 })62 const data = req.valid('json')63 expect(data).toEqual({64 author: {65 tall: 170,66 },67 tag: ['sport', 'music'],68 })69 })70})fix: empty string is a valid header value (#1056)Alex Errantbefore adoption7c6ebe8
lines 71–78why, one step back7172describe('headers', () => {73 test('empty string is a valid header value', () => {74 const req = new HonoRequest(new Request('http://localhost', { headers: { foo: '' } }))75 const foo = req.header('foo')76 expect(foo).toEqual('')77 })78})feat(req): cache body content (#1333)Yusuke Wadabefore adoptionf254fdc
lines 79–141why, one step back7980describe('Body methods', () => {81 test('req.text()', async () => {82 const req = new HonoRequest(83 new Request('http://localhost', {84 method: 'POST',85 body: 'foo',86 })87 )88 expect(await req.text()).toBe('foo')89 expect(await req.text()).toBe('foo') // Should be cached90 })9192 test('req.json()', async () => {93 const req = new HonoRequest(94 new Request('http://localhost', {95 method: 'POST',96 body: '{"foo":"bar"}',97 })98 )99 expect(await req.json()).toEqual({ foo: 'bar' })100 expect(await req.json()).toEqual({ foo: 'bar' }) // Should be cached101 })102103 test('req.arrayBuffer()', async () => {104 const buffer = new ArrayBuffer(8)105 const req = new HonoRequest(106 new Request('http://localhost', {107 method: 'POST',108 body: buffer,109 })110 )111 expect(await req.arrayBuffer()).toEqual(buffer)112 expect(await req.arrayBuffer()).toEqual(buffer) // Should be cached113 })114115 test('req.blob()', async () => {116 const blob = new Blob(['foo'], {117 type: 'text/plain',118 })119 const req = new HonoRequest(120 new Request('http://localhost', {121 method: 'POST',122 body: blob,123 })124 )125 expect(await req.blob()).toEqual(blob)126 expect(await req.blob()).toEqual(blob) // Should be cached127 })128129 test('req.formData()', async () => {130 const data = new FormData()131 data.append('foo', 'bar')132 const req = new HonoRequest(133 new Request('http://localhost', {134 method: 'POST',135 body: data,136 })137 )138 expect((await req.formData()).get('foo')).toBe('bar')139 expect((await req.formData()).get('foo')).toBe('bar') // Should be cached140 })141})