From 8886320499fd1860f65107b6075914d9221b729e Mon Sep 17 00:00:00 2001 From: SaekiTominaga Date: Wed, 11 Dec 2024 13:46:29 +0900 Subject: [PATCH 1/2] Remove charset parameter from MIME type of application/json --- src/utils/mime.test.ts | 2 +- src/utils/mime.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/mime.test.ts b/src/utils/mime.test.ts index 70961c813..f89fbc06b 100644 --- a/src/utils/mime.test.ts +++ b/src/utils/mime.test.ts @@ -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() diff --git a/src/utils/mime.ts b/src/utils/mime.ts index 40f250af3..01aa08dc9 100644 --- a/src/utils/mime.ts +++ b/src/utils/mime.ts @@ -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 From c30bf0d9f82b80bd8f2882c5b99b01d77623928f Mon Sep 17 00:00:00 2001 From: SaekiTominaga Date: Thu, 12 Dec 2024 19:56:13 +0900 Subject: [PATCH 2/2] Remove charset parameter from MIME type of application/json (Other than `serveStatic()`) --- runtime-tests/lambda/index.test.ts | 4 ++-- src/adapter/aws-lambda/handler.test.ts | 2 +- src/adapter/lambda-edge/handler.test.ts | 2 +- src/context.test.ts | 4 ++-- src/context.ts | 2 +- src/middleware/basic-auth/index.test.ts | 4 ++-- src/middleware/basic-auth/index.ts | 2 +- src/middleware/bearer-auth/index.test.ts | 12 ++++++------ src/middleware/bearer-auth/index.ts | 2 +- src/validator/validator.test.ts | 2 +- 10 files changed, 18 insertions(+), 18 deletions(-) diff --git a/runtime-tests/lambda/index.test.ts b/runtime-tests/lambda/index.test.ts index e5b06de19..adf9fc835 100644 --- a/runtime-tests/lambda/index.test.ts +++ b/runtime-tests/lambda/index.test.ts @@ -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', }) ) }) @@ -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'], }) ) }) diff --git a/src/adapter/aws-lambda/handler.test.ts b/src/adapter/aws-lambda/handler.test.ts index 1d4358aec..97c7da4c1 100644 --- a/src/adapter/aws-lambda/handler.test.ts +++ b/src/adapter/aws-lambda/handler.test.ts @@ -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) }) }) diff --git a/src/adapter/lambda-edge/handler.test.ts b/src/adapter/lambda-edge/handler.test.ts index aa17c904a..d9bbba863 100644 --- a/src/adapter/lambda-edge/handler.test.ts +++ b/src/adapter/lambda-edge/handler.test.ts @@ -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) }) }) diff --git a/src/context.test.ts b/src/context.test.ts index 622852604..8230a9548 100644 --- a/src/context.test.ts +++ b/src/context.test.ts @@ -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') @@ -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') }) diff --git a/src/context.ts b/src/context.ts index 86124e008..8447f2128 100644 --- a/src/context.ts +++ b/src/context.ts @@ -777,7 +777,7 @@ export class Context< ): JSONRespondReturn => { 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) diff --git a/src/middleware/basic-auth/index.test.ts b/src/middleware/basic-auth/index.test.ts index e64d2db59..c4065e60e 100644 --- a/src/middleware/basic-auth/index.test.ts +++ b/src/middleware/basic-auth/index.test.ts @@ -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"}') }) @@ -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"}') }) diff --git a/src/middleware/basic-auth/index.ts b/src/middleware/basic-auth/index.ts index aa4ca72d0..45d5f6c65 100644 --- a/src/middleware/basic-auth/index.ts +++ b/src/middleware/basic-auth/index.ts @@ -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 }) diff --git a/src/middleware/bearer-auth/index.test.ts b/src/middleware/bearer-auth/index.test.ts index 4dca87f22..2b559c021 100644 --- a/src/middleware/bearer-auth/index.test.ts +++ b/src/middleware/bearer-auth/index.test.ts @@ -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"}') }) @@ -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"}' @@ -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"}' @@ -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"}' @@ -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"}') }) @@ -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"}') }) diff --git a/src/middleware/bearer-auth/index.ts b/src/middleware/bearer-auth/index.ts index 0e86d4d47..7a83345f4 100644 --- a/src/middleware/bearer-auth/index.ts +++ b/src/middleware/bearer-auth/index.ts @@ -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 }) diff --git a/src/validator/validator.test.ts b/src/validator/validator.test.ts index 83af4481a..05ff812fd 100644 --- a/src/validator/validator.test.ts +++ b/src/validator/validator.test.ts @@ -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' }), })