Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon committed Sep 29, 2023
1 parent c8d4307 commit a77f3e9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/h3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getHeaderLocales,
setCookieLocale,
} from './h3.ts'
import { parseAcceptLanguage } from './shared.ts'
import { DEFAULT_COOKIE_NAME, DEFAULT_LANG_TAG } from './constants.ts'

import type { App, H3Event } from 'h3'
Expand Down Expand Up @@ -55,6 +56,21 @@ describe('getHeaderLanguages', () => {
expect(getHeaderLanguages(mockEvent)).toEqual([])
})

test('parse option', () => {
const mockEvent = {
node: {
req: {
method: 'GET',
headers: {
'accept-language': 'en-US,en;q=0.9,ja;q=0.8',
},
},
},
} as H3Event
expect(getHeaderLanguages(mockEvent, { parser: parseAcceptLanguage }))
.toEqual(['en-US', 'en', 'ja'])
})

test('custom header', () => {
// @ts-ignore: for mocking
const mockEvent = {
Expand Down
4 changes: 3 additions & 1 deletion src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ export function getHeaderLanguagesWithGetter(
const langString = getter()
return langString
? name === ACCEPT_LANGUAGE_HEADER
? parseAcceptLanguage(langString)
? parser === parseDefaultHeader
? parseAcceptLanguage(langString)
: parser(langString)
: parser(langString)
: []
}
Expand Down

0 comments on commit a77f3e9

Please sign in to comment.