index.test.ts
This file
Path
src/middleware/pretty-json/index.test.ts
218 lines · 6.5 KiB · TypeScript
Last changed by
chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2
History of this fileTake it
The walk stopped after 200 commits: lines older than f0b094d point at it.
Merge commit from forkYusuke Wadabefore adoptionf0b094d
line 1why, one step back1import { prettyJSON } from '.'chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2
line 2why, one step back2import { Hono } from '../../hono'Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 3–75why, one step back34describe('JSON pretty by Middleware', () => {5 it('Should return pretty JSON output', async () => {6 const app = new Hono()7 app.use('*', prettyJSON())8 app.get('/', (c) => {9 return c.json({ message: 'Hono!' })10 })1112 const res = await app.request('http://localhost/?pretty')13 expect(res).not.toBeNull()14 expect(res.status).toBe(200)15 expect(await res.text()).toBe(`{16 "message": "Hono!"17}`)18 })1920 it('Should return pretty JSON output with 4 spaces', async () => {21 const app = new Hono()22 app.use('*', prettyJSON({ space: 4 }))23 app.get('/', (c) => {24 return c.json({ message: 'Hono!' })25 })2627 const res = await app.request('http://localhost/?pretty')28 expect(res).not.toBeNull()29 expect(res.status).toBe(200)30 expect(await res.text()).toBe(`{31 "message": "Hono!"32}`)33 })3435 it('Should return pretty JSON output when middleware received custom query', async () => {36 const targetQuery = 'format'3738 const app = new Hono()39 app.use(40 '*',41 prettyJSON({42 query: targetQuery,43 })44 )45 app.get('/', (c) =>46 c.json({47 message: 'Hono!',48 })49 )5051 const prettyText = await (await app.request(`?${targetQuery}`)).text()52 expect(prettyText).toBe(`{53 "message": "Hono!"54}`)55 const nonPrettyText = await (await app.request('?pretty')).text()56 expect(nonPrettyText).toBe('{"message":"Hono!"}')57 })5859 it('Should force pretty JSON output when force option is true', async () => {60 const app = new Hono()61 app.use('*', prettyJSON({ force: true }))62 app.get('/', (c) => {63 return c.json({ message: 'Hono!' })64 })6566 const resWithoutQuery = await (await app.request('http://localhost/')).text()67 expect(resWithoutQuery).toBe(`{68 "message": "Hono!"69}`)7071 const resWithQuery = await (await app.request('http://localhost/?pretty')).text()72 expect(resWithQuery).toBe(`{73 "message": "Hono!"74}`)75 })feat(pretty-json): support structured JSON content-types (+json) (#5226)ADITYA KUMAR JHAbefore adoption5ad469a
lines 76–121why, one step back7677 it('Should return pretty JSON output for structured JSON content-types (+json)', async () => {78 const app = new Hono()79 app.use('*', prettyJSON({ force: true }))80 app.get('/problem', (c) => {81 return c.newResponse(JSON.stringify({ type: 'about:blank', title: 'Bad Request' }), 400, {82 'Content-Type': 'application/problem+json',83 })84 })85 app.get('/jsonapi', (c) => {86 return c.newResponse(JSON.stringify({ data: { id: '1', type: 'articles' } }), 200, {87 'Content-Type': 'application/vnd.api+json',88 })89 })9091 const problemRes = await app.request('http://localhost/problem')92 expect(await problemRes.text()).toBe(`{93 "type": "about:blank",94 "title": "Bad Request"95}`)9697 const jsonApiRes = await app.request('http://localhost/jsonapi')98 expect(await jsonApiRes.text()).toBe(`{99 "data": {100 "id": "1",101 "type": "articles"102 }103}`)104 })105106 it('Should not touch responses with non-JSON content-types', async () => {107 const app = new Hono()108 app.use('*', prettyJSON({ force: true }))109 app.get('/text', (c) => c.text('{"message":"Hono!"}'))110 app.get('/json-seq', (c) => {111 return c.body('{"message":"Hono!"}', 200, {112 'Content-Type': 'application/json-seq',113 })114 })115116 const textRes = await app.request('http://localhost/text')117 expect(await textRes.text()).toBe('{"message":"Hono!"}')118119 const jsonSeqRes = await app.request('http://localhost/json-seq')120 expect(await jsonSeqRes.text()).toBe('{"message":"Hono!"}')121 })fix(pretty-json): don't break responses with unparseable JSON bodies (#5377)Neil Daquioagbefore adoptionf950277
lines 122–217why, one step back122123 it('Should not break 204 responses with a JSON content-type', async () => {124 const app = new Hono()125 app.use('*', prettyJSON({ force: true }))126 app.delete('/x', (c) => {127 c.header('Content-Type', 'application/json')128 return c.body(null, 204)129 })130131 const res = await app.request('http://localhost/x', { method: 'DELETE' })132 expect(res.status).toBe(204)133 expect(res.body).toBeNull()134 })135136 it('Should not break 304 responses with a JSON content-type', async () => {137 const app = new Hono()138 app.use('*', prettyJSON({ force: true }))139 app.get('/x', (c) => {140 c.header('Content-Type', 'application/json')141 return c.body(null, 304)142 })143144 const res = await app.request('http://localhost/x')145 expect(res.status).toBe(304)146 expect(res.body).toBeNull()147 })148149 it('Should not break empty responses with a JSON content-type', async () => {150 const app = new Hono()151 app.use('*', prettyJSON())152 app.get('/x', () => new Response('', { headers: { 'Content-Type': 'application/json' } }))153154 const res = await app.request('http://localhost/x?pretty')155 expect(res.status).toBe(200)156 expect(await res.text()).toBe('')157 })158159 it('Should pass through a response with an invalid JSON body unchanged', async () => {160 const app = new Hono()161 app.use('*', prettyJSON({ force: true }))162 app.get(163 '/x',164 () =>165 new Response('not json', {166 status: 200,167 headers: {168 'Content-Type': 'application/json',169 'X-Custom': 'custom',170 ETag: '"abc"',171 'Content-Length': '8',172 },173 })174 )175176 const res = await app.request('http://localhost/x')177 expect(res.status).toBe(200)178 expect(res.headers.get('X-Custom')).toBe('custom')179 expect(res.headers.get('ETag')).toBe('"abc"')180 expect(res.headers.get('Content-Length')).toBe('8')181 expect(await res.text()).toBe('not json')182 })183184 it('Should pass through a response whose body was already consumed by another middleware', async () => {185 const app = new Hono()186 app.use('*', prettyJSON({ force: true }))187 app.use('*', async (c, next) => {188 await next()189 await c.res.text()190 })191 app.get('/x', (c) => c.json({ message: 'Hono!' }))192193 // The other middleware has read the body, so the bytes are no longer available;194 // pretty JSON must not turn this into a 500 and must leave the response as is.195 const res = await app.request('http://localhost/x')196 expect(res.status).toBe(200)197 expect(res.bodyUsed).toBe(true)198 await expect(res.text()).rejects.toThrow()199 })200201 it('Should remove a stale Content-Length when the body is prettified', async () => {202 const app = new Hono()203 app.use('*', prettyJSON({ force: true }))204 app.get(205 '/x',206 () =>207 new Response('{"message":"Hono!"}', {208 headers: { 'Content-Type': 'application/json', 'Content-Length': '19' },209 })210 )211212 const res = await app.request('http://localhost/x')213 expect(await res.text()).toBe(`{214 "message": "Hono!"215}`)216 expect(res.headers.get('Content-Length')).toBeNull()217 })Merge commit from forkYusuke Wadabefore adoptionf0b094d
line 218why, one step back218})