Skip to content
Myr

request.test.ts

This file

Path

src/request.test.ts

254 lines · 7.1 KiB · TypeScript

Last changed by

Merge pull request #2454 from honojs/nextYusuke Wadabefore adoptioneb97ee2

History of this file

Take it

The walk stopped after 200 commits: lines older than 8addfd4 point at it.

chore: upgrading vitest to v1.0 (#1830)watanybefore adoption8addfd4

lines 1–161why, one step back
1import { HonoRequest } from './request'
2import type { RouterRoute } from './types'34describe('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})3536describe('Param', () => {37 test('req.param() withth ParamStash', () => {38 const rawRequest = new Request('http://localhost?page=2&tag=A&tag=B')39 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 ])4647 expect(req.param('id')).toBe('123')48 expect(req.param('name')).toBe(undefined)4950 req.routeIndex = 151 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')57 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 ])6364 expect(req.param('id')).toBe('123')65 expect(req.param('name')).toBe(undefined)6667 req.routeIndex = 168 expect(req.param('id')).toBe('456')69 expect(req.param('name')).toBe('key')70 })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')114 })115})116117describe('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})161

Merge pull request #2454 from honojs/nextYusuke Wadabefore adoptioneb97ee2

lines 162–166why, one step back
162const text = '{"foo":"bar"}'
163const json = { foo: 'bar' }164const buffer = new TextEncoder().encode('{"foo":"bar"}').buffer165166describe('Body methods with caching', () => {

chore: upgrading vitest to v1.0 (#1830)watanybefore adoption8addfd4

lines 167–170why, one step back
167 test('req.text()', async () => {
168 const req = new HonoRequest(169 new Request('http://localhost', {170 method: 'POST',

Merge pull request #2454 from honojs/nextYusuke Wadabefore adoptioneb97ee2

line 171why, one step back
171 body: text,

chore: upgrading vitest to v1.0 (#1830)watanybefore adoption8addfd4

lines 172–173why, one step back
172 })
173 )

Merge pull request #2454 from honojs/nextYusuke Wadabefore adoptioneb97ee2

lines 174–182why, one step back
174 expect(await req.text()).toEqual(text)
175 expect(await req.text()).toEqual(text)176 expect(await req.json()).toEqual(json)177 expect(await req.arrayBuffer()).toEqual(buffer)178 expect(await req.blob()).toEqual(179 new Blob([text], {180 type: 'text/plain;charset=utf-8',181 })182 )

chore: upgrading vitest to v1.0 (#1830)watanybefore adoption8addfd4

lines 183–191why, one step back
183 })
184185 test('req.json()', async () => {186 const req = new HonoRequest(187 new Request('http://localhost', {188 method: 'POST',189 body: '{"foo":"bar"}',190 })191 )

Merge pull request #2454 from honojs/nextYusuke Wadabefore adoptioneb97ee2

lines 192–200why, one step back
192 expect(await req.json()).toEqual(json)
193 expect(await req.json()).toEqual(json)194 expect(await req.text()).toEqual(text)195 expect(await req.arrayBuffer()).toEqual(buffer)196 expect(await req.blob()).toEqual(197 new Blob([text], {198 type: 'text/plain;charset=utf-8',199 })200 )

chore: upgrading vitest to v1.0 (#1830)watanybefore adoption8addfd4

lines 201–203why, one step back
201 })
202203 test('req.arrayBuffer()', async () => {

Merge pull request #2454 from honojs/nextYusuke Wadabefore adoptioneb97ee2

line 204why, one step back
204 const buffer = new TextEncoder().encode('{"foo":"bar"}').buffer

chore: upgrading vitest to v1.0 (#1830)watanybefore adoption8addfd4

lines 205–211why, one step back
205 const req = new HonoRequest(
206 new Request('http://localhost', {207 method: 'POST',208 body: buffer,209 })210 )211 expect(await req.arrayBuffer()).toEqual(buffer)

Merge pull request #2454 from honojs/nextYusuke Wadabefore adoptioneb97ee2

lines 212–219why, one step back
212 expect(await req.arrayBuffer()).toEqual(buffer)
213 expect(await req.text()).toEqual(text)214 expect(await req.json()).toEqual(json)215 expect(await req.blob()).toEqual(216 new Blob([text], {217 type: '',218 })219 )

chore: upgrading vitest to v1.0 (#1830)watanybefore adoption8addfd4

lines 220–222why, one step back
220 })
221222 test('req.blob()', async () => {

Merge pull request #2454 from honojs/nextYusuke Wadabefore adoptioneb97ee2

lines 223–224why, one step back
223 const blob = new Blob(['{"foo":"bar"}'], {
224 type: 'application/json',

chore: upgrading vitest to v1.0 (#1830)watanybefore adoption8addfd4

lines 225–232why, one step back
225 })
226 const req = new HonoRequest(227 new Request('http://localhost', {228 method: 'POST',229 body: blob,230 })231 )232 expect(await req.blob()).toEqual(blob)

Merge pull request #2454 from honojs/nextYusuke Wadabefore adoptioneb97ee2

lines 233–236why, one step back
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)

chore: upgrading vitest to v1.0 (#1830)watanybefore adoption8addfd4

lines 237–248why, one step back
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')

Merge pull request #2454 from honojs/nextYusuke Wadabefore adoptioneb97ee2

lines 249–252why, one step back
249 expect((await req.formData()).get('foo')).toBe('bar')
250 expect(async () => await req.text()).not.toThrow()251 expect(async () => await req.arrayBuffer()).not.toThrow()252 expect(async () => await req.blob()).not.toThrow()

chore: upgrading vitest to v1.0 (#1830)watanybefore adoption8addfd4

lines 253–254why, one step back
253 })
254})