Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove charset parameter from MIME type of application/json #3743

Merged
merged 2 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading