Skip to content

Commit baa231e

Browse files
fix: Remove charset parameter from MIME type of application/json (#3743)
* Remove charset parameter from MIME type of application/json * Remove charset parameter from MIME type of application/json (Other than `serveStatic()`)
1 parent 47bb23c commit baa231e

12 files changed

Lines changed: 20 additions & 20 deletions

File tree

runtime-tests/lambda/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ describe('AWS Lambda Adapter for Hono', () => {
537537
expect(albResponse.statusCode).toBe(200)
538538
expect(albResponse.headers).toEqual(
539539
expect.objectContaining({
540-
'content-type': 'application/json; charset=UTF-8',
540+
'content-type': 'application/json',
541541
})
542542
)
543543
})
@@ -598,7 +598,7 @@ describe('AWS Lambda Adapter for Hono', () => {
598598
expect(albResponse.multiValueHeaders).toBeDefined()
599599
expect(albResponse.multiValueHeaders).toEqual(
600600
expect.objectContaining({
601-
'content-type': ['application/json; charset=UTF-8'],
601+
'content-type': ['application/json'],
602602
})
603603
)
604604
})

src/adapter/aws-lambda/handler.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('isContentTypeBinary', () => {
1313
expect(isContentTypeBinary('text/javascript')).toBe(false)
1414
expect(isContentTypeBinary('application/json')).toBe(false)
1515
expect(isContentTypeBinary('application/ld+json')).toBe(false)
16-
expect(isContentTypeBinary('application/json; charset=UTF-8')).toBe(false)
16+
expect(isContentTypeBinary('application/json')).toBe(false)
1717
})
1818
})
1919

src/adapter/lambda-edge/handler.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('isContentTypeBinary', () => {
1313
expect(isContentTypeBinary('text/javascript')).toBe(false)
1414
expect(isContentTypeBinary('application/json')).toBe(false)
1515
expect(isContentTypeBinary('application/ld+json')).toBe(false)
16-
expect(isContentTypeBinary('application/json; charset=UTF-8')).toBe(false)
16+
expect(isContentTypeBinary('application/json')).toBe(false)
1717
})
1818
})
1919

src/context.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('Context', () => {
5151
it('c.json()', async () => {
5252
const res = c.json({ message: 'Hello' }, 201, { 'X-Custom': 'Message' })
5353
expect(res.status).toBe(201)
54-
expect(res.headers.get('Content-Type')).toMatch('application/json; charset=UTF-8')
54+
expect(res.headers.get('Content-Type')).toMatch('application/json')
5555
const text = await res.text()
5656
expect(text).toBe('{"message":"Hello"}')
5757
expect(res.headers.get('X-Custom')).toBe('Message')
@@ -182,7 +182,7 @@ describe('Context', () => {
182182
c.status(404)
183183
const res = c.json({ hono: 'great app' })
184184
expect(res.status).toBe(404)
185-
expect(res.headers.get('Content-Type')).toMatch('application/json; charset=UTF-8')
185+
expect(res.headers.get('Content-Type')).toMatch('application/json')
186186
const obj: { [key: string]: string } = await res.json()
187187
expect(obj['hono']).toBe('great app')
188188
})

src/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ export class Context<
777777
): JSONRespondReturn<T, U> => {
778778
const body = JSON.stringify(object)
779779
this.#preparedHeaders ??= {}
780-
this.#preparedHeaders['content-type'] = 'application/json; charset=UTF-8'
780+
this.#preparedHeaders['content-type'] = 'application/json'
781781
/* eslint-disable @typescript-eslint/no-explicit-any */
782782
return (
783783
typeof arg === 'number' ? this.#newResponse(body, arg, headers) : this.#newResponse(body, arg)

src/middleware/basic-auth/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ describe('Basic Auth by Middleware', () => {
295295
const res = await app.request(req)
296296
expect(res).not.toBeNull()
297297
expect(res.status).toBe(401)
298-
expect(res.headers.get('Content-Type')).toMatch('application/json; charset=UTF-8')
298+
expect(res.headers.get('Content-Type')).toMatch('application/json')
299299
expect(handlerExecuted).toBeFalsy()
300300
expect(await res.text()).toBe('{"message":"Custom unauthorized message as object"}')
301301
})
@@ -314,7 +314,7 @@ describe('Basic Auth by Middleware', () => {
314314
const res = await app.request(req)
315315
expect(res).not.toBeNull()
316316
expect(res.status).toBe(401)
317-
expect(res.headers.get('Content-Type')).toMatch('application/json; charset=UTF-8')
317+
expect(res.headers.get('Content-Type')).toMatch('application/json')
318318
expect(handlerExecuted).toBeFalsy()
319319
expect(await res.text()).toBe('{"message":"Custom unauthorized message as function object"}')
320320
})

src/middleware/basic-auth/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export const basicAuth = (
120120
status,
121121
headers: {
122122
...headers,
123-
'content-type': 'application/json; charset=UTF-8',
123+
'content-type': 'application/json',
124124
},
125125
})
126126
throw new HTTPException(status, { res })

src/middleware/bearer-auth/index.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ describe('Bearer Auth by Middleware', () => {
400400
const res = await app.request(req)
401401
expect(res).not.toBeNull()
402402
expect(res.status).toBe(401)
403-
expect(res.headers.get('Content-Type')).toMatch('application/json; charset=UTF-8')
403+
expect(res.headers.get('Content-Type')).toMatch('application/json')
404404
expect(handlerExecuted).toBeFalsy()
405405
expect(await res.text()).toBe('{"message":"Custom no authentication header message as object"}')
406406
})
@@ -423,7 +423,7 @@ describe('Bearer Auth by Middleware', () => {
423423
const res = await app.request(req)
424424
expect(res).not.toBeNull()
425425
expect(res.status).toBe(401)
426-
expect(res.headers.get('Content-Type')).toMatch('application/json; charset=UTF-8')
426+
expect(res.headers.get('Content-Type')).toMatch('application/json')
427427
expect(handlerExecuted).toBeFalsy()
428428
expect(await res.text()).toBe(
429429
'{"message":"Custom no authentication header message as function object"}'
@@ -450,7 +450,7 @@ describe('Bearer Auth by Middleware', () => {
450450
const res = await app.request(req)
451451
expect(res).not.toBeNull()
452452
expect(res.status).toBe(400)
453-
expect(res.headers.get('Content-Type')).toMatch('application/json; charset=UTF-8')
453+
expect(res.headers.get('Content-Type')).toMatch('application/json')
454454
expect(handlerExecuted).toBeFalsy()
455455
expect(await res.text()).toBe(
456456
'{"message":"Custom invalid authentication header message as object"}'
@@ -477,7 +477,7 @@ describe('Bearer Auth by Middleware', () => {
477477
const res = await app.request(req)
478478
expect(res).not.toBeNull()
479479
expect(res.status).toBe(400)
480-
expect(res.headers.get('Content-Type')).toMatch('application/json; charset=UTF-8')
480+
expect(res.headers.get('Content-Type')).toMatch('application/json')
481481
expect(handlerExecuted).toBeFalsy()
482482
expect(await res.text()).toBe(
483483
'{"message":"Custom invalid authentication header message as function object"}'
@@ -500,7 +500,7 @@ describe('Bearer Auth by Middleware', () => {
500500
const res = await app.request(req)
501501
expect(res).not.toBeNull()
502502
expect(res.status).toBe(401)
503-
expect(res.headers.get('Content-Type')).toMatch('application/json; charset=UTF-8')
503+
expect(res.headers.get('Content-Type')).toMatch('application/json')
504504
expect(handlerExecuted).toBeFalsy()
505505
expect(await res.text()).toBe('{"message":"Custom invalid token message as object"}')
506506
})
@@ -521,7 +521,7 @@ describe('Bearer Auth by Middleware', () => {
521521
const res = await app.request(req)
522522
expect(res).not.toBeNull()
523523
expect(res.status).toBe(401)
524-
expect(res.headers.get('Content-Type')).toMatch('application/json; charset=UTF-8')
524+
expect(res.headers.get('Content-Type')).toMatch('application/json')
525525
expect(handlerExecuted).toBeFalsy()
526526
expect(await res.text()).toBe('{"message":"Custom invalid token message as function object"}')
527527
})

src/middleware/bearer-auth/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export const bearerAuth = (options: BearerAuthOptions): MiddlewareHandler => {
103103
status,
104104
headers: {
105105
...headers,
106-
'content-type': 'application/json; charset=UTF-8',
106+
'content-type': 'application/json',
107107
},
108108
})
109109
throw new HTTPException(status, { res })

src/utils/mime.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('mime', () => {
99
it('getMimeType', () => {
1010
expect(getMimeType('hello.txt')).toBe('text/plain; charset=utf-8')
1111
expect(getMimeType('hello.html')).toBe('text/html; charset=utf-8')
12-
expect(getMimeType('hello.json')).toBe('application/json; charset=utf-8')
12+
expect(getMimeType('hello.json')).toBe('application/json')
1313
expect(getMimeType('favicon.ico')).toBe('image/x-icon')
1414
expect(getMimeType('good.morning.hello.gif')).toBe('image/gif')
1515
expect(getMimeType('goodmorninghellogif')).toBeUndefined()

0 commit comments

Comments
 (0)