Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Remove charset parameter from MIME type of application/json * Remove charset parameter from MIME type of application/json (Other than `serveStatic()`)

authored by SaekiTominaga · committed by GitHub · parent 47bb23c · Browse files · 12 files+20 added−20 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

12 files+20 added−20 removed

  • tests:+16 added−16 removed
  • other:+4 added−4 removed

12 files+20 added−20 removed

modified:runtime-tests/lambda/index.test.ts+2 added-2 removed
535 unmodified lines
536 const albResponse = await handler(event)537 expect(albResponse.statusCode).toBe(200)538 expect(albResponse.headers).toEqual(539 expect.objectContaining({540 'content-type': 'application/json; charset=UTF-8',540 'content-type': 'application/json',541 })542 )543 })544
52 unmodified lines
597 expect(albResponse.statusCode).toBe(200)598 expect(albResponse.multiValueHeaders).toBeDefined()599 expect(albResponse.multiValueHeaders).toEqual(600 expect.objectContaining({601 'content-type': ['application/json; charset=UTF-8'],601 'content-type': ['application/json'],602 })603 )604 })605
425 unmodified lines
modified:src/adapter/aws-lambda/handler.test.ts+1 added-1 removed
modified:src/adapter/lambda-edge/handler.test.ts+1 added-1 removed
11 unmodified lines
12 expect(isContentTypeBinary('text/css')).toBe(false)13 expect(isContentTypeBinary('text/javascript')).toBe(false)14 expect(isContentTypeBinary('application/json')).toBe(false)15 expect(isContentTypeBinary('application/ld+json')).toBe(false)16 expect(isContentTypeBinary('application/json; charset=UTF-8')).toBe(false)16 expect(isContentTypeBinary('application/json')).toBe(false)17 })18})1920describe('createBody', () => {
18 unmodified lines
modified:src/middleware/basic-auth/index.test.ts+2 added-2 removed
293 unmodified lines
294 const req = new Request('http://localhost/auth-custom-invalid-user-message-object')295 const res = await app.request(req)296 expect(res).not.toBeNull()297 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')299 expect(handlerExecuted).toBeFalsy()300 expect(await res.text()).toBe('{"message":"Custom unauthorized message as object"}')301 })302
10 unmodified lines
313 const req = new Request('http://localhost/auth-custom-invalid-user-message-function-object')314 const res = await app.request(req)315 expect(res).not.toBeNull()316 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')318 expect(handlerExecuted).toBeFalsy()319 expect(await res.text()).toBe('{"message":"Custom unauthorized message as function object"}')320 })321})
modified:src/middleware/basic-auth/index.ts+1 added-1 removed
118 unmodified lines
119 : new Response(JSON.stringify(responseMessage), {120 status,121 headers: {122 ...headers,123 'content-type': 'application/json; charset=UTF-8',123 'content-type': 'application/json',124 },125 })126 throw new HTTPException(status, { res })127 }
1 unmodified line
modified:src/middleware/bearer-auth/index.test.ts+6 added-6 removed
398 unmodified lines
399 const req = new Request('http://localhost/auth-custom-no-authentication-header-message-object')400 const res = await app.request(req)401 expect(res).not.toBeNull()402 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')404 expect(handlerExecuted).toBeFalsy()405 expect(await res.text()).toBe('{"message":"Custom no authentication header message as object"}')406 })407
14 unmodified lines
422 )423 const res = await app.request(req)424 expect(res).not.toBeNull()425 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')427 expect(handlerExecuted).toBeFalsy()428 expect(await res.text()).toBe(429 '{"message":"Custom no authentication header message as function object"}'430 )
18 unmodified lines
449 req.headers.set('Authorization', 'Beare abcdefg12345-._~+/=')450 const res = await app.request(req)451 expect(res).not.toBeNull()452 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')454 expect(handlerExecuted).toBeFalsy()455 expect(await res.text()).toBe(456 '{"message":"Custom invalid authentication header message as object"}'457 )
18 unmodified lines
476 req.headers.set('Authorization', 'Beare abcdefg12345-._~+/=')477 const res = await app.request(req)478 expect(res).not.toBeNull()479 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')481 expect(handlerExecuted).toBeFalsy()482 expect(await res.text()).toBe(483 '{"message":"Custom invalid authentication header message as function object"}'484 )
14 unmodified lines
499 req.headers.set('Authorization', 'Bearer invalid-token')500 const res = await app.request(req)501 expect(res).not.toBeNull()502 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')504 expect(handlerExecuted).toBeFalsy()505 expect(await res.text()).toBe('{"message":"Custom invalid token message as object"}')506 })507
12 unmodified lines
520 req.headers.set('Authorization', 'Bearer invalid-token')521 const res = await app.request(req)522 expect(res).not.toBeNull()523 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')525 expect(handlerExecuted).toBeFalsy()526 expect(await res.text()).toBe('{"message":"Custom invalid token message as function object"}')527 })528})
modified:src/middleware/bearer-auth/index.ts+1 added-1 removed
101 unmodified lines
102 : new Response(JSON.stringify(responseMessage), {103 status,104 headers: {105 ...headers,106 'content-type': 'application/json; charset=UTF-8',106 'content-type': 'application/json',107 },108 })109 throw new HTTPException(status, { res })110 }
49 unmodified lines
modified:src/utils/mime.test.ts+1 added-1 removed
7 unmodified lines
8describe('mime', () => {9 it('getMimeType', () => {10 expect(getMimeType('hello.txt')).toBe('text/plain; charset=utf-8')11 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')13 expect(getMimeType('favicon.ico')).toBe('image/x-icon')14 expect(getMimeType('good.morning.hello.gif')).toBe('image/gif')15 expect(getMimeType('goodmorninghellogif')).toBeUndefined()16 expect(getMimeType('indexjs.abcd')).toBeUndefined()
25 unmodified lines
modified:src/utils/mime.ts+1 added-1 removed
11 unmodified lines
12 if (!match) {13 return14 }15 let mimeType = mimes[match[1]]16 if ((mimeType && mimeType.startsWith('text')) || mimeType === 'application/json') {16 if (mimeType && mimeType.startsWith('text')) {17 mimeType += '; charset=utf-8'18 }19 return mimeType20}
75 unmodified lines
modified:src/validator/validator.test.ts+1 added-1 removed
138 unmodified lines
139 it('Should validate if Content-Type is a application/json with a charset', async () => {140 const res = await app.request('http://localhost/post', {141 method: 'POST',142 headers: {143 'Content-Type': 'application/json; charset=utf8',143 'Content-Type': 'application/json',144 },145 body: JSON.stringify({ foo: 'bar' }),146 })147 expect(res.status).toBe(200)
1062 unmodified lines
modified:src/context.test.ts+2 added-2 removed
49 unmodified lines
5051 it('c.json()', async () => {52 const res = c.json({ message: 'Hello' }, 201, { 'X-Custom': 'Message' })53 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')55 const text = await res.text()56 expect(text).toBe('{"message":"Hello"}')57 expect(res.headers.get('X-Custom')).toBe('Message')58 })
122 unmodified lines
181 it('Complex pattern', async () => {182 c.status(404)183 const res = c.json({ hono: 'great app' })184 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')186 const obj: { [key: string]: string } = await res.json()187 expect(obj['hono']).toBe('great app')188 })189
327 unmodified lines
modified:src/context.ts+1 added-1 removed
775 unmodified lines
776 headers?: HeaderRecord777 ): JSONRespondReturn<T, U> => {778 const body = JSON.stringify(object)779 this.#preparedHeaders ??= {}780 this.#preparedHeaders['content-type'] = 'application/json; charset=UTF-8'780 this.#preparedHeaders['content-type'] = 'application/json'781 /* eslint-disable @typescript-eslint/no-explicit-any */782 return (783 typeof arg === 'number' ? this.#newResponse(body, arg, headers) : this.#newResponse(body, arg)784 ) as any
64 unmodified lines