Skip to content
Myr

fix(accepts): match media types and language tags case-insensitively (#5376)

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

* fix(accepts): match media types and language tags case-insensitively `matchType()` compared the accept header entry and the supported value as raw strings, so `Accept: TEXT/HTML` did not match a supported `text/html`, and `Accept-Language: en-us` did not match `en-US`. Both are case-insensitive: media types by RFC 9110 section 8.3.1 and language tags by RFC 4647 section 2.1. #5064 already made `parseBody()` and `validator()` match media types case-insensitively, for #5060. `accepts()` was not covered by that. Compares case-folded. `defaultMatch()` returns the caller's own `supports` entry, so the casing an application registered is preserved. * test: call `accepts()` directly instead of going through a Hono app ---------

authored by Harshit Prakash · committed by GitHub · parent cb5bea3 · Browse files · 2 files+46 added−5 removed
This commit

State

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

Trailers

  • Co-authored-by: Yusuke Wada <yusuke@kamawada.com>

Diff stats

2 files+46 added−5 removed

  • tests:+35 added−0 removed
  • other:+11 added−5 removed

2 files+46 added−5 removed

modified:src/helper/accepts/accepts.test.ts+35 added
1import { Hono } from '../..'2import { parseAccept } from '../../utils/accept'3import type { AcceptHeader } from '../../utils/headers'4import type { Accept, acceptsConfig, acceptsOptions } from './accepts'5import { accepts, defaultMatch } from './accepts'67describe('parseAccept', () => {
230 unmodified lines
238 }239 const result = accepts(c, options)240 expect(result).toBe('text/html')241 })242243 describe('case insensitivity', () => {244 const pick = (header: AcceptHeader, value: string, supports: string[]) => {245 const c = {246 req: { header: () => value },247 // eslint-disable-next-line @typescript-eslint/no-explicit-any248 } as any249 return accepts(c, { header, supports, default: 'DEFAULT' })250 }251252 // Media types are case-insensitive (RFC 9110 §8.3.1)253 test.each(['text/html', 'TEXT/HTML', 'Text/Html', 'text/HTML'])(254 'Accept: %s matches text/html',255 (value) => {256 expect(pick('Accept', value, ['text/html'])).toBe('text/html')257 }258 )259260 test('a case-different subtype wildcard still matches', () => {261 expect(pick('Accept', 'TEXT/*', ['text/html'])).toBe('text/html')262 })263264 // Language tags are case-insensitive (RFC 4647 §2.1)265 test.each(['en-US', 'EN-US', 'en-us', 'En-Us'])(266 'Accept-Language: %s matches en-US',267 (value) => {268 expect(pick('Accept-Language', value, ['en-US'])).toBe('en-US')269 }270 )271272 test('a case-different entry with q=0 is still not acceptable', () => {273 expect(pick('Accept-Language', 'EN;q=0', ['en'])).toBe('DEFAULT')274 })275 })276})277278describe('Usage', () => {279 test('decide compression by Accept-Encoding header', async () => {
65 unmodified lines
modified:src/helper/accepts/accepts.ts+11 added-5 removed