proxy.test.ts63 lines · main
| 1 | import { describe, expect, test } from 'bun:test'; |
| 2 | |
| 3 | import { |
| 4 | appAuthPathToFdiSuffix, |
| 5 | proxyBrivenEngineAuth, |
| 6 | resolveFdiTarget, |
| 7 | } from './proxy.js'; |
| 8 | |
| 9 | describe('briven-engine first-party proxy', () => { |
| 10 | test('maps app path to FDI suffix', () => { |
| 11 | expect(appAuthPathToFdiSuffix('/api/auth/signup')).toBe('/signup'); |
| 12 | expect(appAuthPathToFdiSuffix('/api/auth/signinup/code')).toBe( |
| 13 | '/signinup/code', |
| 14 | ); |
| 15 | expect(appAuthPathToFdiSuffix('/api/auth/')).toBe(''); |
| 16 | }); |
| 17 | |
| 18 | test('resolves FDI target on api origin', () => { |
| 19 | expect(resolveFdiTarget({ apiOrigin: 'http://localhost:3001' })).toBe( |
| 20 | 'http://localhost:3001/v1/auth-core/fdi', |
| 21 | ); |
| 22 | }); |
| 23 | |
| 24 | test('forwards Set-Cookie from upstream onto app response', async () => { |
| 25 | const upstream = new Response(JSON.stringify({ status: 'OK' }), { |
| 26 | status: 200, |
| 27 | headers: { |
| 28 | 'content-type': 'application/json', |
| 29 | 'set-cookie': |
| 30 | 'sAccessToken=abc; Path=/; HttpOnly, sRefreshToken=def; Path=/; HttpOnly', |
| 31 | }, |
| 32 | }); |
| 33 | // Node/Bun may not multi set-cookie; simulate getSetCookie |
| 34 | Object.defineProperty(upstream.headers, 'getSetCookie', { |
| 35 | value: () => [ |
| 36 | 'sAccessToken=abc; Path=/; HttpOnly', |
| 37 | 'sRefreshToken=def; Path=/; HttpOnly', |
| 38 | ], |
| 39 | }); |
| 40 | |
| 41 | const fetchMock: typeof fetch = async () => upstream; |
| 42 | const req = new Request('http://app.local/api/auth/signup', { |
| 43 | method: 'POST', |
| 44 | body: '{}', |
| 45 | headers: { 'content-type': 'application/json' }, |
| 46 | }); |
| 47 | const res = await proxyBrivenEngineAuth(req, { |
| 48 | apiOrigin: 'http://api.local', |
| 49 | projectId: 'p_test', |
| 50 | fetch: fetchMock, |
| 51 | }); |
| 52 | expect(res.status).toBe(200); |
| 53 | expect(res.headers.get('x-briven-proxy')).toBe('first-party'); |
| 54 | expect(res.headers.get('x-briven-engine')).toBe('briven-engine'); |
| 55 | const cookies = |
| 56 | typeof (res.headers as Headers & { getSetCookie?: () => string[] }) |
| 57 | .getSetCookie === 'function' |
| 58 | ? (res.headers as Headers & { getSetCookie: () => string[] }).getSetCookie() |
| 59 | : [res.headers.get('set-cookie') ?? '']; |
| 60 | const joined = cookies.join(' | '); |
| 61 | expect(joined).toContain('sAccessToken=abc'); |
| 62 | }); |
| 63 | }); |