|
| 1 | +import { afterEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { createLivePricesFetchClient } from '~/state/livePrices/createLivePricesFetchClient' |
| 3 | + |
| 4 | +vi.mock('@universe/gating', () => ({ |
| 5 | + getIsSessionServiceEnabled: (): boolean => false, |
| 6 | +})) |
| 7 | + |
| 8 | +/** |
| 9 | + * On web the session is an HttpOnly cookie; the browser only attaches it to |
| 10 | + * cross-origin EventSubscriptionService requests when the fetch runs with |
| 11 | + * `credentials: 'include'`. Without it the entry gateway sees no session and |
| 12 | + * Subscribe / RefreshSession fail with 401 — these tests are that contract. |
| 13 | + */ |
| 14 | +describe('createLivePricesFetchClient', () => { |
| 15 | + afterEach(() => { |
| 16 | + vi.unstubAllGlobals() |
| 17 | + }) |
| 18 | + |
| 19 | + function setup(): { fetchMock: ReturnType<typeof vi.fn> } { |
| 20 | + const fetchMock = vi.fn(async () => new Response(JSON.stringify({}), { status: 200 })) |
| 21 | + vi.stubGlobal('fetch', fetchMock) |
| 22 | + return { fetchMock } |
| 23 | + } |
| 24 | + |
| 25 | + it('sends credentials: include on Subscribe so the web session cookie is attached', async () => { |
| 26 | + const { fetchMock } = setup() |
| 27 | + const client = createLivePricesFetchClient({ subscriptionApiUrl: 'https://entry-gateway.example.com' }) |
| 28 | + |
| 29 | + await client.post('/uniswap.notificationservice.v1.EventSubscriptionService/Subscribe', { |
| 30 | + body: JSON.stringify({ connectionId: 'connection-1' }), |
| 31 | + }) |
| 32 | + |
| 33 | + expect(fetchMock).toHaveBeenCalledWith( |
| 34 | + 'https://entry-gateway.example.com/uniswap.notificationservice.v1.EventSubscriptionService/Subscribe', |
| 35 | + expect.objectContaining({ credentials: 'include', method: 'POST' }), |
| 36 | + ) |
| 37 | + }) |
| 38 | + |
| 39 | + it('sends credentials: include on RefreshSession so the web session cookie is attached', async () => { |
| 40 | + const { fetchMock } = setup() |
| 41 | + const client = createLivePricesFetchClient({ subscriptionApiUrl: 'https://entry-gateway.example.com' }) |
| 42 | + |
| 43 | + await client.post('/uniswap.notificationservice.v1.EventSubscriptionService/RefreshSession', { |
| 44 | + body: JSON.stringify({ connectionId: 'connection-1' }), |
| 45 | + }) |
| 46 | + |
| 47 | + expect(fetchMock).toHaveBeenCalledWith( |
| 48 | + 'https://entry-gateway.example.com/uniswap.notificationservice.v1.EventSubscriptionService/RefreshSession', |
| 49 | + expect.objectContaining({ credentials: 'include', method: 'POST' }), |
| 50 | + ) |
| 51 | + }) |
| 52 | + |
| 53 | + it('still applies the request-source header alongside the credentials mode', async () => { |
| 54 | + const { fetchMock } = setup() |
| 55 | + const client = createLivePricesFetchClient({ subscriptionApiUrl: 'https://entry-gateway.example.com' }) |
| 56 | + |
| 57 | + await client.post('/uniswap.notificationservice.v1.EventSubscriptionService/Subscribe', { |
| 58 | + body: JSON.stringify({ connectionId: 'connection-1' }), |
| 59 | + }) |
| 60 | + |
| 61 | + const init = fetchMock.mock.calls[0]?.[1] as RequestInit |
| 62 | + expect(new Headers(init.headers).get('x-request-source')).toBeTruthy() |
| 63 | + }) |
| 64 | +}) |
0 commit comments