|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import type { OnHeadersReceivedListenerDetails, Session } from 'electron' |
| 5 | +import { describe, expect, it, vi } from 'vitest' |
| 6 | +import { |
| 7 | + keepSessionCookiesAcrossRestarts, |
| 8 | + SESSION_COOKIE_LIFETIME_SECONDS, |
| 9 | + withSessionCookieLifetime, |
| 10 | + withSessionCookieMaxAge, |
| 11 | +} from '@/main/browser-agent/session-cookies' |
| 12 | + |
| 13 | +vi.mock('electron', () => import('@/test/electron-mock')) |
| 14 | + |
| 15 | +const NOW = 1_800_000_000 |
| 16 | +const MAX_AGE = `Max-Age=${SESSION_COOKIE_LIFETIME_SECONDS}` |
| 17 | + |
| 18 | +describe('withSessionCookieLifetime', () => { |
| 19 | + it('gives an expiry-less cookie the bounded lifetime', () => { |
| 20 | + expect(withSessionCookieLifetime({ url: 'https://example.com/', name: 'a' }, NOW)).toEqual({ |
| 21 | + url: 'https://example.com/', |
| 22 | + name: 'a', |
| 23 | + expirationDate: NOW + SESSION_COOKIE_LIFETIME_SECONDS, |
| 24 | + }) |
| 25 | + }) |
| 26 | + |
| 27 | + it('leaves a cookie that already expires untouched', () => { |
| 28 | + const cookie = { url: 'https://example.com/', name: 'a', expirationDate: NOW + 60 } |
| 29 | + expect(withSessionCookieLifetime(cookie, NOW)).toBe(cookie) |
| 30 | + }) |
| 31 | +}) |
| 32 | + |
| 33 | +describe('withSessionCookieMaxAge', () => { |
| 34 | + it('adds Max-Age to a session cookie', () => { |
| 35 | + expect(withSessionCookieMaxAge('sid=abc; Path=/; Secure; HttpOnly')).toBe( |
| 36 | + `sid=abc; Path=/; Secure; HttpOnly; ${MAX_AGE}` |
| 37 | + ) |
| 38 | + expect(withSessionCookieMaxAge('sid=abc')).toBe(`sid=abc; ${MAX_AGE}`) |
| 39 | + }) |
| 40 | + |
| 41 | + it('leaves cookies with an expiry or a deletion untouched', () => { |
| 42 | + for (const value of [ |
| 43 | + 'sid=abc; Expires=Wed, 21 Oct 2030 07:28:00 GMT', |
| 44 | + 'sid=abc; max-age=60', |
| 45 | + 'sid=; Path=/; Max-Age=0', |
| 46 | + 'sid=abc;EXPIRES=Thu, 01 Jan 1970 00:00:00 GMT', |
| 47 | + ]) { |
| 48 | + expect(withSessionCookieMaxAge(value)).toBe(value) |
| 49 | + } |
| 50 | + }) |
| 51 | + |
| 52 | + it('reads attributes only, never the cookie value', () => { |
| 53 | + expect(withSessionCookieMaxAge('max-age=1')).toBe(`max-age=1; ${MAX_AGE}`) |
| 54 | + }) |
| 55 | +}) |
| 56 | + |
| 57 | +describe('keepSessionCookiesAcrossRestarts', () => { |
| 58 | + type Listener = ( |
| 59 | + details: Pick<OnHeadersReceivedListenerDetails, 'responseHeaders'>, |
| 60 | + callback: (response: { responseHeaders?: Record<string, string[]> }) => void |
| 61 | + ) => void |
| 62 | + |
| 63 | + function install(): Listener { |
| 64 | + let listener: Listener | undefined |
| 65 | + const ses = { |
| 66 | + webRequest: { |
| 67 | + onHeadersReceived: vi.fn((handler: Listener) => { |
| 68 | + listener = handler |
| 69 | + }), |
| 70 | + }, |
| 71 | + } |
| 72 | + keepSessionCookiesAcrossRestarts(ses as unknown as Session) |
| 73 | + if (!listener) throw new Error('listener not installed') |
| 74 | + return listener |
| 75 | + } |
| 76 | + |
| 77 | + it('rewrites every Set-Cookie value and keeps the other headers', () => { |
| 78 | + const callback = vi.fn() |
| 79 | + install()( |
| 80 | + { |
| 81 | + responseHeaders: { |
| 82 | + 'content-type': ['text/html'], |
| 83 | + 'Set-Cookie': ['sid=abc; HttpOnly', 'keep=1; Max-Age=60'], |
| 84 | + }, |
| 85 | + }, |
| 86 | + callback |
| 87 | + ) |
| 88 | + |
| 89 | + expect(callback).toHaveBeenCalledWith({ |
| 90 | + responseHeaders: { |
| 91 | + 'content-type': ['text/html'], |
| 92 | + 'Set-Cookie': [`sid=abc; HttpOnly; ${MAX_AGE}`, 'keep=1; Max-Age=60'], |
| 93 | + }, |
| 94 | + }) |
| 95 | + }) |
| 96 | + |
| 97 | + it('leaves responses without cookies untouched', () => { |
| 98 | + const callback = vi.fn() |
| 99 | + install()({ responseHeaders: { 'content-type': ['text/html'] } }, callback) |
| 100 | + install()({}, callback) |
| 101 | + |
| 102 | + expect(callback).toHaveBeenNthCalledWith(1, {}) |
| 103 | + expect(callback).toHaveBeenNthCalledWith(2, {}) |
| 104 | + }) |
| 105 | +}) |
0 commit comments