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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219import { prettyJSON } from '.'import { Hono } from '../../hono'
describe('JSON pretty by Middleware', () => { it('Should return pretty JSON output', async () => { const app = new Hono() app.use('*', prettyJSON()) app.get('/', (c) => { return c.json({ message: 'Hono!' }) })
const res = await app.request('http://localhost/?pretty') expect(res).not.toBeNull() expect(res.status).toBe(200) expect(await res.text()).toBe(`{ "message": "Hono!"}`) })
it('Should return pretty JSON output with 4 spaces', async () => { const app = new Hono() app.use('*', prettyJSON({ space: 4 })) app.get('/', (c) => { return c.json({ message: 'Hono!' }) })
const res = await app.request('http://localhost/?pretty') expect(res).not.toBeNull() expect(res.status).toBe(200) expect(await res.text()).toBe(`{ "message": "Hono!"}`) })
it('Should return pretty JSON output when middleware received custom query', async () => { const targetQuery = 'format'
const app = new Hono() app.use( '*', prettyJSON({ query: targetQuery, }) ) app.get('/', (c) => c.json({ message: 'Hono!', }) )
const prettyText = await (await app.request(`?${targetQuery}`)).text() expect(prettyText).toBe(`{ "message": "Hono!"}`) const nonPrettyText = await (await app.request('?pretty')).text() expect(nonPrettyText).toBe('{"message":"Hono!"}') })
it('Should force pretty JSON output when force option is true', async () => { const app = new Hono() app.use('*', prettyJSON({ force: true })) app.get('/', (c) => { return c.json({ message: 'Hono!' }) })
const resWithoutQuery = await (await app.request('http://localhost/')).text() expect(resWithoutQuery).toBe(`{ "message": "Hono!"}`)
const resWithQuery = await (await app.request('http://localhost/?pretty')).text() expect(resWithQuery).toBe(`{ "message": "Hono!"}`) })
it('Should return pretty JSON output for structured JSON content-types (+json)', async () => { const app = new Hono() app.use('*', prettyJSON({ force: true })) app.get('/problem', (c) => { return c.newResponse(JSON.stringify({ type: 'about:blank', title: 'Bad Request' }), 400, { 'Content-Type': 'application/problem+json', }) }) app.get('/jsonapi', (c) => { return c.newResponse(JSON.stringify({ data: { id: '1', type: 'articles' } }), 200, { 'Content-Type': 'application/vnd.api+json', }) })
const problemRes = await app.request('http://localhost/problem') expect(await problemRes.text()).toBe(`{ "type": "about:blank", "title": "Bad Request"}`)
const jsonApiRes = await app.request('http://localhost/jsonapi') expect(await jsonApiRes.text()).toBe(`{ "data": { "id": "1", "type": "articles" }}`) })
it('Should not touch responses with non-JSON content-types', async () => { const app = new Hono() app.use('*', prettyJSON({ force: true })) app.get('/text', (c) => c.text('{"message":"Hono!"}')) app.get('/json-seq', (c) => { return c.body('{"message":"Hono!"}', 200, { 'Content-Type': 'application/json-seq', }) })
const textRes = await app.request('http://localhost/text') expect(await textRes.text()).toBe('{"message":"Hono!"}')
const jsonSeqRes = await app.request('http://localhost/json-seq') expect(await jsonSeqRes.text()).toBe('{"message":"Hono!"}') })
it('Should not break 204 responses with a JSON content-type', async () => { const app = new Hono() app.use('*', prettyJSON({ force: true })) app.delete('/x', (c) => { c.header('Content-Type', 'application/json') return c.body(null, 204) })
const res = await app.request('http://localhost/x', { method: 'DELETE' }) expect(res.status).toBe(204) expect(res.body).toBeNull() })
it('Should not break 304 responses with a JSON content-type', async () => { const app = new Hono() app.use('*', prettyJSON({ force: true })) app.get('/x', (c) => { c.header('Content-Type', 'application/json') return c.body(null, 304) })
const res = await app.request('http://localhost/x') expect(res.status).toBe(304) expect(res.body).toBeNull() })
it('Should not break empty responses with a JSON content-type', async () => { const app = new Hono() app.use('*', prettyJSON()) app.get('/x', () => new Response('', { headers: { 'Content-Type': 'application/json' } }))
const res = await app.request('http://localhost/x?pretty') expect(res.status).toBe(200) expect(await res.text()).toBe('') })
it('Should pass through a response with an invalid JSON body unchanged', async () => { const app = new Hono() app.use('*', prettyJSON({ force: true })) app.get( '/x', () => new Response('not json', { status: 200, headers: { 'Content-Type': 'application/json', 'X-Custom': 'custom', ETag: '"abc"', 'Content-Length': '8', }, }) )
const res = await app.request('http://localhost/x') expect(res.status).toBe(200) expect(res.headers.get('X-Custom')).toBe('custom') expect(res.headers.get('ETag')).toBe('"abc"') expect(res.headers.get('Content-Length')).toBe('8') expect(await res.text()).toBe('not json') })
it('Should pass through a response whose body was already consumed by another middleware', async () => { const app = new Hono() app.use('*', prettyJSON({ force: true })) app.use('*', async (c, next) => { await next() await c.res.text() }) app.get('/x', (c) => c.json({ message: 'Hono!' }))
// The other middleware has read the body, so the bytes are no longer available; // pretty JSON must not turn this into a 500 and must leave the response as is. const res = await app.request('http://localhost/x') expect(res.status).toBe(200) expect(res.bodyUsed).toBe(true) await expect(res.text()).rejects.toThrow() })
it('Should remove a stale Content-Length when the body is prettified', async () => { const app = new Hono() app.use('*', prettyJSON({ force: true })) app.get( '/x', () => new Response('{"message":"Hono!"}', { headers: { 'Content-Type': 'application/json', 'Content-Length': '19' }, }) )
const res = await app.request('http://localhost/x') expect(await res.text()).toBe(`{ "message": "Hono!"}`) expect(res.headers.get('Content-Length')).toBeNull() })})