@@ -73,4 +73,50 @@ describe('JSON pretty by Middleware', () => {
7373 "message": "Hono!"
7474}` )
7575 } )
76+
77+ 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+ } )
90+
91+ const problemRes = await app . request ( 'http://localhost/problem' )
92+ expect ( await problemRes . text ( ) ) . toBe ( `{
93+ "type": "about:blank",
94+ "title": "Bad Request"
95+ }` )
96+
97+ 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+ } )
105+
106+ 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+ } )
115+
116+ const textRes = await app . request ( 'http://localhost/text' )
117+ expect ( await textRes . text ( ) ) . toBe ( '{"message":"Hono!"}' )
118+
119+ const jsonSeqRes = await app . request ( 'http://localhost/json-seq' )
120+ expect ( await jsonSeqRes . text ( ) ) . toBe ( '{"message":"Hono!"}' )
121+ } )
76122} )
0 commit comments