import { getAuthSessionCookieCandidates, getAuthSessionCookieName, PLAIN_AUTH_SESSION_COOKIE_NAME, resolveSecureAuthCookieFromRequest, SECURE_AUTH_SESSION_COOKIE_NAME, } from '@/lib/auth-cookie' describe('auth cookie protocol resolution', () => { it('HTTPS 请求使用 secure cookie', () => { expect( resolveSecureAuthCookieFromRequest({ headers: { 'x-forwarded-proto': 'https', }, }) ).toBe(true) }) it('HTTP 请求使用普通 cookie', () => { expect( resolveSecureAuthCookieFromRequest({ headers: { 'x-forwarded-proto': 'http', }, nextUrl: { protocol: 'http:', }, }) ).toBe(false) }) it('优先和兜底 cookie 名称都可返回', () => { expect(getAuthSessionCookieName(true)).toBe(SECURE_AUTH_SESSION_COOKIE_NAME) expect(getAuthSessionCookieName(false)).toBe(PLAIN_AUTH_SESSION_COOKIE_NAME) expect(getAuthSessionCookieCandidates(true)).toEqual([ SECURE_AUTH_SESSION_COOKIE_NAME, PLAIN_AUTH_SESSION_COOKIE_NAME, ]) expect(getAuthSessionCookieCandidates(false)).toEqual([ PLAIN_AUTH_SESSION_COOKIE_NAME, SECURE_AUTH_SESSION_COOKIE_NAME, ]) }) })