request.test.ts
This file
Path
src/request.test.ts
223 lines · 6.1 KiB · TypeScript
Last changed by
Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c
History of this fileTake it
The walk stopped after 200 commits: lines older than ba04dac point at it.
Merge pull request #1436 from honojs/nextYusuke Wadabefore adoptionba04dac
line 1why, one step back1import { HonoRequest } from './request'Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c
line 2why, one step back2import type { RouterRoute } from './types'Merge pull request #1436 from honojs/nextYusuke Wadabefore adoptionba04dac
lines 3–35why, one step back34describe('Query', () => {5 test('req.query() and req.queries()', () => {6 const rawRequest = new Request('http://localhost?page=2&tag=A&tag=B')7 const req = new HonoRequest(rawRequest)89 const page = req.query('page')10 expect(page).not.toBeUndefined()11 expect(page).toBe('2')1213 const q = req.query('q')14 expect(q).toBeUndefined()1516 const tags = req.queries('tag')17 expect(tags).not.toBeUndefined()18 expect(tags).toEqual(['A', 'B'])1920 const q2 = req.queries('q2')21 expect(q2).toBeUndefined()22 })2324 test('decode special chars', () => {25 const rawRequest = new Request('http://localhost?mail=framework%40hono.dev&tag=%401&tag=%402')26 const req = new HonoRequest(rawRequest)2728 const mail = req.query('mail')29 expect(mail).toBe('framework@hono.dev')3031 const tags = req.queries('tag')32 expect(tags).toEqual(['@1', '@2'])33 })34})35fix: References this._s only when paramStash is passed from the router (#1715)Taku Amanobefore adoptionae3546e
lines 36–38why, one step back36describe('Param', () => {37 test('req.param() withth ParamStash', () => {38 const rawRequest = new Request('http://localhost?page=2&tag=A&tag=B')Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c
lines 39–45why, one step back39 const req = new HonoRequest<'/:id/:name'>(rawRequest, '/123/key', [40 [41 [[undefined, {} as RouterRoute], { id: 0 }],42 [[undefined, {} as RouterRoute], { id: 0, name: 1 }],43 ],44 ['123', 'key'],45 ])fix: References this._s only when paramStash is passed from the router (#1715)Taku Amanobefore adoptionae3546e
lines 46–49why, one step back4647 expect(req.param('id')).toBe('123')48 expect(req.param('name')).toBe(undefined)49Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c
line 50why, one step back50 req.routeIndex = 1fix: References this._s only when paramStash is passed from the router (#1715)Taku Amanobefore adoptionae3546e
lines 51–56why, one step back51 expect(req.param('id')).toBe('123')52 expect(req.param('name')).toBe('key')53 })5455 test('req.param() without ParamStash', () => {56 const rawRequest = new Request('http://localhost?page=2&tag=A&tag=B')Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c
lines 57–62why, one step back57 const req = new HonoRequest<'/:id/:name'>(rawRequest, '/123/key', [58 [59 [[undefined, {} as RouterRoute], { id: '123' }],60 [[undefined, {} as RouterRoute], { id: '456', name: 'key' }],61 ],62 ])fix: References this._s only when paramStash is passed from the router (#1715)Taku Amanobefore adoptionae3546e
line 63why, one step back63Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c
lines 64–65why, one step back64 expect(req.param('id')).toBe('123')65 expect(req.param('name')).toBe(undefined)fix: References this._s only when paramStash is passed from the router (#1715)Taku Amanobefore adoptionae3546e
line 66why, one step back66Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c
line 67why, one step back67 req.routeIndex = 1fix: References this._s only when paramStash is passed from the router (#1715)Taku Amanobefore adoptionae3546e
lines 68–69why, one step back68 expect(req.param('id')).toBe('456')69 expect(req.param('name')).toBe('key')Merge pull request #1774 from honojs/nextYusuke Wadabefore adoptionb21f33c
lines 70–113why, one step back70 })71})7273describe('matchedRoutes', () => {74 test('req.routePath', () => {75 const handlerA = () => {}76 const handlerB = () => {}77 const rawRequest = new Request('http://localhost?page=2&tag=A&tag=B')78 const req = new HonoRequest<'/:id/:name'>(rawRequest, '/123/key', [79 [80 [[handlerA, { handler: handlerA, method: 'GET', path: '/:id' }], { id: '123' }],81 [82 [handlerA, { handler: handlerB, method: 'GET', path: '/:id/:name' }],83 { id: '456', name: 'key' },84 ],85 ],86 ])8788 expect(req.matchedRoutes).toEqual([89 { handler: handlerA, method: 'GET', path: '/:id' },90 { handler: handlerB, method: 'GET', path: '/:id/:name' },91 ])92 })93})9495describe('routePath', () => {96 test('req.routePath', () => {97 const handlerA = () => {}98 const handlerB = () => {}99 const rawRequest = new Request('http://localhost?page=2&tag=A&tag=B')100 const req = new HonoRequest<'/:id/:name'>(rawRequest, '/123/key', [101 [102 [[handlerA, { handler: handlerA, method: 'GET', path: '/:id' }], { id: '123' }],103 [104 [handlerA, { handler: handlerB, method: 'GET', path: '/:id/:name' }],105 { id: '456', name: 'key' },106 ],107 ],108 ])109110 expect(req.routePath).toBe('/:id')111112 req.routeIndex = 1113 expect(req.routePath).toBe('/:id/:name')fix: References this._s only when paramStash is passed from the router (#1715)Taku Amanobefore adoptionae3546e
lines 114–116why, one step back114 })115})116Merge pull request #1436 from honojs/nextYusuke Wadabefore adoptionba04dac
lines 117–223why, one step back117describe('req.addValidatedData() and req.data()', () => {118 const rawRequest = new Request('http://localhost')119120 const payload = {121 title: 'hello',122 author: {123 name: 'young man',124 age: 20,125 },126 }127128 test('add data - json', () => {129 const req = new HonoRequest<'/', { json: typeof payload }>(rawRequest)130 req.addValidatedData('json', payload)131 const data = req.valid('json')132 expect(data).toEqual(payload)133 })134135 test('replace data - json', () => {136 const req = new HonoRequest<'/', { json: typeof payload }>(rawRequest)137 req.addValidatedData('json', payload)138 req.addValidatedData('json', {139 tag: ['sport', 'music'],140 author: {141 tall: 170,142 },143 })144 const data = req.valid('json')145 expect(data).toEqual({146 author: {147 tall: 170,148 },149 tag: ['sport', 'music'],150 })151 })152})153154describe('headers', () => {155 test('empty string is a valid header value', () => {156 const req = new HonoRequest(new Request('http://localhost', { headers: { foo: '' } }))157 const foo = req.header('foo')158 expect(foo).toEqual('')159 })160})161162describe('Body methods', () => {163 test('req.text()', async () => {164 const req = new HonoRequest(165 new Request('http://localhost', {166 method: 'POST',167 body: 'foo',168 })169 )170 expect(await req.text()).toBe('foo')171 expect(await req.text()).toBe('foo') // Should be cached172 })173174 test('req.json()', async () => {175 const req = new HonoRequest(176 new Request('http://localhost', {177 method: 'POST',178 body: '{"foo":"bar"}',179 })180 )181 expect(await req.json()).toEqual({ foo: 'bar' })182 expect(await req.json()).toEqual({ foo: 'bar' }) // Should be cached183 })184185 test('req.arrayBuffer()', async () => {186 const buffer = new ArrayBuffer(8)187 const req = new HonoRequest(188 new Request('http://localhost', {189 method: 'POST',190 body: buffer,191 })192 )193 expect(await req.arrayBuffer()).toEqual(buffer)194 expect(await req.arrayBuffer()).toEqual(buffer) // Should be cached195 })196197 test('req.blob()', async () => {198 const blob = new Blob(['foo'], {199 type: 'text/plain',200 })201 const req = new HonoRequest(202 new Request('http://localhost', {203 method: 'POST',204 body: blob,205 })206 )207 expect(await req.blob()).toEqual(blob)208 expect(await req.blob()).toEqual(blob) // Should be cached209 })210211 test('req.formData()', async () => {212 const data = new FormData()213 data.append('foo', 'bar')214 const req = new HonoRequest(215 new Request('http://localhost', {216 method: 'POST',217 body: data,218 })219 )220 expect((await req.formData()).get('foo')).toBe('bar')221 expect((await req.formData()).get('foo')).toBe('bar') // Should be cached222 })223})