|
| 1 | +import { rewriteProxiedCookie, rewriteProxiedCookies } from 'functions/cookie-utils' |
| 2 | + |
| 3 | +describe('rewriteProxiedCookie', () => { |
| 4 | + describe('SameSite pass-through', () => { |
| 5 | + it('preserves SameSite=None (session cookies minted by platform-service, INC-344)', () => { |
| 6 | + const result = rewriteProxiedCookie('x-session-id=abc123; Path=/; Secure; HttpOnly; SameSite=None') |
| 7 | + expect(result).toBe('x-session-id=abc123; Path=/; Secure; HttpOnly; SameSite=None') |
| 8 | + }) |
| 9 | + |
| 10 | + it('preserves SameSite=Lax', () => { |
| 11 | + const result = rewriteProxiedCookie('x-device-id=dev456; Path=/; Secure; HttpOnly; SameSite=Lax') |
| 12 | + expect(result).toBe('x-device-id=dev456; Path=/; Secure; HttpOnly; SameSite=Lax') |
| 13 | + }) |
| 14 | + |
| 15 | + it('preserves SameSite=Strict on arbitrary cookies', () => { |
| 16 | + const result = rewriteProxiedCookie('other=value; Path=/; Secure; SameSite=Strict') |
| 17 | + expect(result).toBe('other=value; Path=/; Secure; SameSite=Strict') |
| 18 | + }) |
| 19 | + |
| 20 | + it('does not add SameSite when the backend omits it', () => { |
| 21 | + const result = rewriteProxiedCookie('other=value; Path=/; Secure') |
| 22 | + expect(result).not.toContain('SameSite') |
| 23 | + }) |
| 24 | + }) |
| 25 | + |
| 26 | + describe('Domain and prefix stripping', () => { |
| 27 | + it('strips Domain and keeps SameSite untouched', () => { |
| 28 | + const result = rewriteProxiedCookie('x-session-id=abc123; Domain=.uniswap.org; Path=/; Secure; SameSite=None') |
| 29 | + expect(result).not.toContain('Domain') |
| 30 | + expect(result).toContain('SameSite=None') |
| 31 | + }) |
| 32 | + |
| 33 | + it('strips __Secure- prefix without touching other attributes', () => { |
| 34 | + const result = rewriteProxiedCookie('__Secure-x-session-id=abc123; Path=/; Secure; SameSite=None') |
| 35 | + expect(result).toBe('x-session-id=abc123; Path=/; Secure; SameSite=None') |
| 36 | + }) |
| 37 | + |
| 38 | + it('strips __Host- prefix and Domain', () => { |
| 39 | + const result = rewriteProxiedCookie('__Host-other=value; Domain=.uniswap.org; Path=/; Secure; SameSite=Strict') |
| 40 | + expect(result).toBe('other=value; Path=/; Secure; SameSite=Strict') |
| 41 | + }) |
| 42 | + |
| 43 | + it('leaves a plain cookie without Domain or prefixes unchanged', () => { |
| 44 | + const result = rewriteProxiedCookie('other=value') |
| 45 | + expect(result).toBe('other=value') |
| 46 | + }) |
| 47 | + }) |
| 48 | +}) |
| 49 | + |
| 50 | +describe('rewriteProxiedCookies', () => { |
| 51 | + it('returns the response unchanged when there are no Set-Cookie headers', () => { |
| 52 | + const response = new Response('body', { status: 200 }) |
| 53 | + expect(rewriteProxiedCookies(response)).toBe(response) |
| 54 | + }) |
| 55 | + |
| 56 | + it('rewrites multiple cookies, preserving each SameSite as sent', () => { |
| 57 | + const headers = new Headers() |
| 58 | + headers.append('Set-Cookie', 'x-session-id=abc; Domain=.uniswap.org; Path=/; Secure; HttpOnly; SameSite=None') |
| 59 | + headers.append('Set-Cookie', 'other=value; Path=/; Secure; SameSite=Strict') |
| 60 | + const response = new Response('body', { status: 200, headers }) |
| 61 | + |
| 62 | + const rewritten = rewriteProxiedCookies(response) |
| 63 | + const cookies = rewritten.headers.getSetCookie() |
| 64 | + |
| 65 | + expect(cookies).toEqual([ |
| 66 | + 'x-session-id=abc; Path=/; Secure; HttpOnly; SameSite=None', |
| 67 | + 'other=value; Path=/; Secure; SameSite=Strict', |
| 68 | + ]) |
| 69 | + }) |
| 70 | +}) |
0 commit comments