Skip to content

Commit

Permalink
fix: Remove charset parameter from MIME type of application/json (#…
Browse files Browse the repository at this point in the history
…3743)

* Remove charset parameter from MIME type of application/json

* Remove charset parameter from MIME type of application/json (Other than `serveStatic()`)
  • Loading branch information
SaekiTominaga authored Dec 14, 2024
1 parent 47bb23c commit baa231e
Show file tree
Hide file tree
Showing 12 changed files with 20 additions and 20 deletions.
4 changes: 2 additions & 2 deletions runtime-tests/lambda/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ describe('AWS Lambda Adapter for Hono', () => {
expect(albResponse.statusCode).toBe(200)
expect(albResponse.headers).toEqual(
expect.objectContaining({
'content-type': 'application/json; charset=UTF-8',
'content-type': 'application/json',
})
)
})
Expand Down Expand Up @@ -598,7 +598,7 @@ describe('AWS Lambda Adapter for Hono', () => {
expect(albResponse.multiValueHeaders).toBeDefined()
expect(albResponse.multiValueHeaders).toEqual(
expect.objectContaining({
'content-type': ['application/json; charset=UTF-8'],
'content-type': ['application/json'],
})
)
})
Expand Down
2 changes: 1 addition & 1 deletion src/adapter/aws-lambda/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('isContentTypeBinary', () => {
expect(isContentTypeBinary('text/javascript')).toBe(false)
expect(isContentTypeBinary('application/json')).toBe(false)
expect(isContentTypeBinary('application/ld+json')).toBe(false)
expect(isContentTypeBinary('application/json; charset=UTF-8')).toBe(false)
expect(isContentTypeBinary('application/json')).toBe(false)
})
})

Expand Down
2 changes: 1 addition & 1 deletion src/adapter/lambda-edge/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('isContentTypeBinary', () => {
expect(isContentTypeBinary('text/javascript')).toBe(false)
expect(isContentTypeBinary('application/json')).toBe(false)
expect(isContentTypeBinary('application/ld+json')).toBe(false)
expect(isContentTypeBinary('application/json; charset=UTF-8')).toBe(false)
expect(isContentTypeBinary('application/json')).toBe(false)
})
})

Expand Down
4 changes: 2 additions & 2 deletions src/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('Context', () => {
it('c.json()', async () => {
const res = c.json({ message: 'Hello' }, 201, { 'X-Custom': 'Message' })
expect(res.status).toBe(201)
expect(res.headers.get('Content-Type')).toMatch('application/json; charset=UTF-8')
expect(res.headers.get('Content-Type')).toMatch('application/json')
const text = await res.text()
expect(text).toBe('{"message":"Hello"}')
expect(res.headers.get('X-Custom')).toBe('Message')
Expand Down Expand Up @@ -182,7 +182,7 @@ describe('Context', () => {
c.status(404)
const res = c.json({ hono: 'great app' })
expect(res.status).toBe(404)
expect(res.headers.get('Content-Type')).toMatch('application/json; charset=UTF-8')
expect(res.headers.get('Content-Type')).toMatch('application/json')
const obj: { [key: string]: string } = await res.json()
expect(obj['hono']).toBe('great app')
})
Expand Down
2 changes: 1 addition & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ export class Context<
): JSONRespondReturn<T, U> => {
const body = JSON.stringify(object)
this.#preparedHeaders ??= {}
this.#preparedHeaders['content-type'] = 'application/json; charset=UTF-8'
this.#preparedHeaders['content-type'] = 'application/json'
/* eslint-disable @typescript-eslint/no-explicit-any */
return (
typeof arg === 'number' ? this.#newResponse(body, arg, headers) : this.#newResponse(body, arg)
Expand Down
4 changes: 2 additions & 2 deletions src/middleware/basic-auth/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ describe('Basic Auth by Middleware', () => {
const res = await app.request(req)
expect(res).not.toBeNull()
expect(res.status).toBe(401)
expect(res.headers.get('Content-Type')).toMatch('application/json; charset=UTF-8')
expect(res.headers.get('Content-Type')).toMatch('application/json')
expect(handlerExecuted).toBeFalsy()
expect(await res.text()).toBe('{"message":"Custom unauthorized message as object"}')
})
Expand All @@ -314,7 +314,7 @@ describe('Basic Auth by Middleware', () => {
const res = await app.request(req)
expect(res).not.toBeNull()
expect(res.status).toBe(401)
expect(res.headers.get('Content-Type')).toMatch('application/json; charset=UTF-8')
expect(res.headers.get('Content-Type')).toMatch('application/json')
expect(handlerExecuted).toBeFalsy()
expect(await res.text()).toBe('{"message":"Custom unauthorized message as function object"}')
})
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/basic-auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export const basicAuth = (
status,
headers: {
...headers,
'content-type': 'application/json; charset=UTF-8',
'content-type': 'application/json',
},
})
throw new HTTPException(status, { res })
Expand Down
12 changes: 6 additions & 6 deletions src/middleware/bearer-auth/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ describe('Bearer Auth by Middleware', () => {
const res = await app.request(req)
expect(res).not.toBeNull()
expect(res.status).toBe(401)
expect(res.headers.get('Content-Type')).toMatch('application/json; charset=UTF-8')
expect(res.headers.get('Content-Type')).toMatch('application/json')
expect(handlerExecuted).toBeFalsy()
expect(await res.text()).toBe('{"message":"Custom no authentication header message as object"}')
})
Expand All @@ -423,7 +423,7 @@ describe('Bearer Auth by Middleware', () => {
const res = await app.request(req)
expect(res).not.toBeNull()
expect(res.status).toBe(401)
expect(res.headers.get('Content-Type')).toMatch('application/json; charset=UTF-8')
expect(res.headers.get('Content-Type')).toMatch('application/json')
expect(handlerExecuted).toBeFalsy()
expect(await res.text()).toBe(
'{"message":"Custom no authentication header message as function object"}'
Expand All @@ -450,7 +450,7 @@ describe('Bearer Auth by Middleware', () => {
const res = await app.request(req)
expect(res).not.toBeNull()
expect(res.status).toBe(400)
expect(res.headers.get('Content-Type')).toMatch('application/json; charset=UTF-8')
expect(res.headers.get('Content-Type')).toMatch('application/json')
expect(handlerExecuted).toBeFalsy()
expect(await res.text()).toBe(
'{"message":"Custom invalid authentication header message as object"}'
Expand All @@ -477,7 +477,7 @@ describe('Bearer Auth by Middleware', () => {
const res = await app.request(req)
expect(res).not.toBeNull()
expect(res.status).toBe(400)
expect(res.headers.get('Content-Type')).toMatch('application/json; charset=UTF-8')
expect(res.headers.get('Content-Type')).toMatch('application/json')
expect(handlerExecuted).toBeFalsy()
expect(await res.text()).toBe(
'{"message":"Custom invalid authentication header message as function object"}'
Expand All @@ -500,7 +500,7 @@ describe('Bearer Auth by Middleware', () => {
const res = await app.request(req)
expect(res).not.toBeNull()
expect(res.status).toBe(401)
expect(res.headers.get('Content-Type')).toMatch('application/json; charset=UTF-8')
expect(res.headers.get('Content-Type')).toMatch('application/json')
expect(handlerExecuted).toBeFalsy()
expect(await res.text()).toBe('{"message":"Custom invalid token message as object"}')
})
Expand All @@ -521,7 +521,7 @@ describe('Bearer Auth by Middleware', () => {
const res = await app.request(req)
expect(res).not.toBeNull()
expect(res.status).toBe(401)
expect(res.headers.get('Content-Type')).toMatch('application/json; charset=UTF-8')
expect(res.headers.get('Content-Type')).toMatch('application/json')
expect(handlerExecuted).toBeFalsy()
expect(await res.text()).toBe('{"message":"Custom invalid token message as function object"}')
})
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/bearer-auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export const bearerAuth = (options: BearerAuthOptions): MiddlewareHandler => {
status,
headers: {
...headers,
'content-type': 'application/json; charset=UTF-8',
'content-type': 'application/json',
},
})
throw new HTTPException(status, { res })
Expand Down
2 changes: 1 addition & 1 deletion src/utils/mime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('mime', () => {
it('getMimeType', () => {
expect(getMimeType('hello.txt')).toBe('text/plain; charset=utf-8')
expect(getMimeType('hello.html')).toBe('text/html; charset=utf-8')
expect(getMimeType('hello.json')).toBe('application/json; charset=utf-8')
expect(getMimeType('hello.json')).toBe('application/json')
expect(getMimeType('favicon.ico')).toBe('image/x-icon')
expect(getMimeType('good.morning.hello.gif')).toBe('image/gif')
expect(getMimeType('goodmorninghellogif')).toBeUndefined()
Expand Down
2 changes: 1 addition & 1 deletion src/utils/mime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const getMimeType = (
return
}
let mimeType = mimes[match[1]]
if ((mimeType && mimeType.startsWith('text')) || mimeType === 'application/json') {
if (mimeType && mimeType.startsWith('text')) {
mimeType += '; charset=utf-8'
}
return mimeType
Expand Down
2 changes: 1 addition & 1 deletion src/validator/validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ describe('JSON', () => {
const res = await app.request('http://localhost/post', {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf8',
'Content-Type': 'application/json',
},
body: JSON.stringify({ foo: 'bar' }),
})
Expand Down

0 comments on commit baa231e

Please sign in to comment.