Locale cookie honours consent saved under a bumped policy version
The proxy parsed the consent cookie as version 1, so after an editor bumped the policy version NEXT_LOCALE was never saved. It now accepts the stored record's own version; policyVersion pins one. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
0fd57c1d6a
commit
7d835bdc49
@@ -0,0 +1,34 @@
|
||||
import { describe, expect, test } from 'vitest'
|
||||
|
||||
import { CONSENT_COOKIE, serializeConsent } from '../src/modules/consent/storage.js'
|
||||
import { createLocaleMiddleware } from '../src/modules/i18n/localeMiddleware.js'
|
||||
import { LOCALE_COOKIE_NAME } from '../src/modules/i18n/negotiateLocale.js'
|
||||
|
||||
const config = { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'PL' }, { code: 'en', label: 'EN' }] } as const
|
||||
const state = { analytics: false, functional: true, marketing: false, necessary: true }
|
||||
|
||||
function request(path: string, cookies: Record<string, string>) {
|
||||
const url = new URL(`https://acme.pl${path}`)
|
||||
return {
|
||||
cookies: { get: (name: string) => (name in cookies ? { value: cookies[name] } : undefined) },
|
||||
headers: new Headers(),
|
||||
nextUrl: { clone: () => new URL(url), pathname: url.pathname, search: url.search },
|
||||
url: url.toString(),
|
||||
}
|
||||
}
|
||||
|
||||
describe('locale cookie and the consent policy version', () => {
|
||||
const switchToEn = (version: number, policyVersion?: number) =>
|
||||
createLocaleMiddleware({ config, ...(policyVersion ? { policyVersion } : {}) })(
|
||||
request('/en/o-nas', { [CONSENT_COOKIE]: decodeURIComponent(serializeConsent(state, version)), [LOCALE_COOKIE_NAME]: 'pl' }) as never,
|
||||
)
|
||||
|
||||
test('a consent saved under a bumped policy version still lets the locale be saved', () => {
|
||||
expect(switchToEn(3).cookie).toMatchObject({ name: LOCALE_COOKIE_NAME, value: 'en' })
|
||||
})
|
||||
|
||||
test('policyVersion pins the version the proxy requires', () => {
|
||||
expect(switchToEn(3, 4).cookie).toBeUndefined()
|
||||
expect(switchToEn(4, 4).cookie).toMatchObject({ value: 'en' })
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user