Skip to content
Myr

request.test.ts

This file

Path

src/request.test.ts

315 lines · 8.8 KiB · TypeScript

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than 7ee829b point at it.

v4.4.7Yusuke Wadabefore adoption7ee829b

lines 1–315why, one step back
1import { HonoRequest } from './request'
2import type { RouterRoute } from './types'34type RecursiveRecord<K extends string, T> = {5 [key in K]: T | RecursiveRecord<K, T>6}78describe('Query', () => {9 test('req.query() and req.queries()', () => {10 const rawRequest = new Request('http://localhost?page=2&tag=A&tag=B')11 const req = new HonoRequest(rawRequest)1213 const page = req.query('page')14 expect(page).not.toBeUndefined()15 expect(page).toBe('2')1617 const q = req.query('q')18 expect(q).toBeUndefined()1920 const tags = req.queries('tag')21 expect(tags).not.toBeUndefined()22 expect(tags).toEqual(['A', 'B'])2324 const q2 = req.queries('q2')25 expect(q2).toBeUndefined()26 })2728 test('decode special chars', () => {29 const rawRequest = new Request('http://localhost?mail=framework%40hono.dev&tag=%401&tag=%402')30 const req = new HonoRequest(rawRequest)3132 const mail = req.query('mail')33 expect(mail).toBe('framework@hono.dev')3435 const tags = req.queries('tag')36 expect(tags).toEqual(['@1', '@2'])37 })38})3940describe('Param', () => {41 test('req.param() with ParamStash', () => {42 const rawRequest = new Request('http://localhost?page=2&tag=A&tag=B')43 const req = new HonoRequest<'/:id/:name'>(rawRequest, '/123/key', [44 [45 [[undefined, {} as RouterRoute], { id: 0 }],46 [[undefined, {} as RouterRoute], { id: 0, name: 1 }],47 ],48 ['123', 'key'],49 ])5051 expect(req.param('id')).toBe('123')52 expect(req.param('name')).toBe(undefined)5354 req.routeIndex = 155 expect(req.param('id')).toBe('123')56 expect(req.param('name')).toBe('key')57 })5859 test('req.param() without ParamStash', () => {60 const rawRequest = new Request('http://localhost?page=2&tag=A&tag=B')61 const req = new HonoRequest<'/:id/:name'>(rawRequest, '/123/key', [62 [63 [[undefined, {} as RouterRoute], { id: '123' }],64 [[undefined, {} as RouterRoute], { id: '456', name: 'key' }],65 ],66 ])6768 expect(req.param('id')).toBe('123')69 expect(req.param('name')).toBe(undefined)7071 req.routeIndex = 172 expect(req.param('id')).toBe('456')73 expect(req.param('name')).toBe('key')74 })75})7677describe('matchedRoutes', () => {78 test('req.routePath', () => {79 const handlerA = () => {}80 const handlerB = () => {}81 const rawRequest = new Request('http://localhost?page=2&tag=A&tag=B')82 const req = new HonoRequest<'/:id/:name'>(rawRequest, '/123/key', [83 [84 [[handlerA, { handler: handlerA, method: 'GET', path: '/:id' }], { id: '123' }],85 [86 [handlerA, { handler: handlerB, method: 'GET', path: '/:id/:name' }],87 { id: '456', name: 'key' },88 ],89 ],90 ])9192 expect(req.matchedRoutes).toEqual([93 { handler: handlerA, method: 'GET', path: '/:id' },94 { handler: handlerB, method: 'GET', path: '/:id/:name' },95 ])96 })97})9899describe('routePath', () => {100 test('req.routePath', () => {101 const handlerA = () => {}102 const handlerB = () => {}103 const rawRequest = new Request('http://localhost?page=2&tag=A&tag=B')104 const req = new HonoRequest<'/:id/:name'>(rawRequest, '/123/key', [105 [106 [[handlerA, { handler: handlerA, method: 'GET', path: '/:id' }], { id: '123' }],107 [108 [handlerA, { handler: handlerB, method: 'GET', path: '/:id/:name' }],109 { id: '456', name: 'key' },110 ],111 ],112 ])113114 expect(req.routePath).toBe('/:id')115116 req.routeIndex = 1117 expect(req.routePath).toBe('/:id/:name')118 })119})120121describe('req.addValidatedData() and req.data()', () => {122 const rawRequest = new Request('http://localhost')123124 const payload = {125 title: 'hello',126 author: {127 name: 'young man',128 age: 20,129 },130 }131132 test('add data - json', () => {133 const req = new HonoRequest<'/', { json: typeof payload }>(rawRequest)134 req.addValidatedData('json', payload)135 const data = req.valid('json')136 expect(data).toEqual(payload)137 })138139 test('replace data - json', () => {140 const req = new HonoRequest<'/', { json: typeof payload }>(rawRequest)141 req.addValidatedData('json', payload)142 req.addValidatedData('json', {143 tag: ['sport', 'music'],144 author: {145 tall: 170,146 },147 })148 const data = req.valid('json')149 expect(data).toEqual({150 author: {151 tall: 170,152 },153 tag: ['sport', 'music'],154 })155 })156})157158describe('headers', () => {159 test('empty string is a valid header value', () => {160 const req = new HonoRequest(new Request('http://localhost', { headers: { foo: '' } }))161 const foo = req.header('foo')162 expect(foo).toEqual('')163 })164})165166const text = '{"foo":"bar"}'167const json = { foo: 'bar' }168const buffer = new TextEncoder().encode('{"foo":"bar"}').buffer169170describe('Body methods with caching', () => {171 test('req.text()', async () => {172 const req = new HonoRequest(173 new Request('http://localhost', {174 method: 'POST',175 body: text,176 })177 )178 expect(await req.text()).toEqual(text)179 expect(await req.json()).toEqual(json)180 expect(await req.arrayBuffer()).toEqual(buffer)181 expect(await req.blob()).toEqual(182 new Blob([text], {183 type: 'text/plain;charset=utf-8',184 })185 )186 })187188 test('req.json()', async () => {189 const req = new HonoRequest(190 new Request('http://localhost', {191 method: 'POST',192 body: '{"foo":"bar"}',193 })194 )195 expect(await req.json()).toEqual(json)196 expect(await req.text()).toEqual(text)197 expect(await req.arrayBuffer()).toEqual(buffer)198 expect(await req.blob()).toEqual(199 new Blob([text], {200 type: 'text/plain;charset=utf-8',201 })202 )203 })204205 test('req.arrayBuffer()', async () => {206 const buffer = new TextEncoder().encode('{"foo":"bar"}').buffer207 const req = new HonoRequest(208 new Request('http://localhost', {209 method: 'POST',210 body: buffer,211 })212 )213 expect(await req.arrayBuffer()).toEqual(buffer)214 expect(await req.text()).toEqual(text)215 expect(await req.json()).toEqual(json)216 expect(await req.blob()).toEqual(217 new Blob([text], {218 type: '',219 })220 )221 })222223 test('req.blob()', async () => {224 const blob = new Blob(['{"foo":"bar"}'], {225 type: 'application/json',226 })227 const req = new HonoRequest(228 new Request('http://localhost', {229 method: 'POST',230 body: blob,231 })232 )233 expect(await req.blob()).toEqual(blob)234 expect(await req.text()).toEqual(text)235 expect(await req.json()).toEqual(json)236 expect(await req.arrayBuffer()).toEqual(buffer)237 })238239 test('req.formData()', async () => {240 const data = new FormData()241 data.append('foo', 'bar')242 const req = new HonoRequest(243 new Request('http://localhost', {244 method: 'POST',245 body: data,246 })247 )248 expect((await req.formData()).get('foo')).toBe('bar')249 expect(async () => await req.text()).not.toThrow()250 expect(async () => await req.arrayBuffer()).not.toThrow()251 expect(async () => await req.blob()).not.toThrow()252 })253254 describe('req.parseBody()', async () => {255 it('should parse form data', async () => {256 const data = new FormData()257 data.append('foo', 'bar')258 const req = new HonoRequest(259 new Request('http://localhost', {260 method: 'POST',261 body: data,262 })263 )264 expect((await req.parseBody())['foo']).toBe('bar')265 expect(async () => await req.text()).not.toThrow()266 expect(async () => await req.arrayBuffer()).not.toThrow()267 expect(async () => await req.blob()).not.toThrow()268 })269270 describe('Return type', () => {271 let req: HonoRequest272 beforeEach(() => {273 const data = new FormData()274 data.append('foo', 'bar')275 req = new HonoRequest(276 new Request('http://localhost', {277 method: 'POST',278 body: data,279 })280 )281 })282283 it('without options', async () => {284 expectTypeOf((await req.parseBody())['key']).toEqualTypeOf<string | File>()285 })286287 it('{all: true}', async () => {288 expectTypeOf((await req.parseBody({ all: true }))['key']).toEqualTypeOf<289 string | File | (string | File)[]290 >()291 })292293 it('{dot: true}', async () => {294 expectTypeOf((await req.parseBody({ dot: true }))['key']).toEqualTypeOf<295 string | File | RecursiveRecord<string, string | File>296 >()297 })298299 it('{all: true, dot: true}', async () => {300 expectTypeOf((await req.parseBody({ all: true, dot: true }))['key']).toEqualTypeOf<301 | string302 | File303 | (string | File)[]304 | RecursiveRecord<string, string | File | (string | File)[]>305 >()306 })307308 it('specify return type explicitly', async () => {309 expectTypeOf(310 await req.parseBody<{ key1: string; key2: string }>({ all: true, dot: true })311 ).toEqualTypeOf<{ key1: string; key2: string }>()312 })313 })314 })315})