|
| 1 | +import { getRedirectUri } from './redirect-uri.js'; |
| 2 | +import type { Request } from 'express'; |
| 3 | +import { EnvVariables } from '../env/env.service.js'; |
| 4 | + |
| 5 | +describe('getRedirectUri', () => { |
| 6 | + const baseEnv: EnvVariables = { |
| 7 | + frontendPort: '3000', |
| 8 | + } as any; |
| 9 | + |
| 10 | + const makeReq = (headers: Record<string, any> = {}, hostname = 'example.com', protocol = 'http'): Request => |
| 11 | + ({ |
| 12 | + headers, |
| 13 | + hostname, |
| 14 | + protocol, |
| 15 | + } as Request); |
| 16 | + |
| 17 | + it('uses x-forwarded-proto and x-forwarded-host when present', () => { |
| 18 | + const req = makeReq({ |
| 19 | + 'x-forwarded-proto': 'https', |
| 20 | + 'x-forwarded-host': 'forwarded.com', |
| 21 | + }); |
| 22 | + const result = getRedirectUri(req, baseEnv); |
| 23 | + expect(result).toBe('https://forwarded.com:3000/callback?storageType=none'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('uses first value when x-forwarded-proto and host are arrays', () => { |
| 27 | + const req = makeReq({ |
| 28 | + 'x-forwarded-proto': ['https', 'http'], |
| 29 | + 'x-forwarded-host': ['multi.com:8080', 'other.com'], |
| 30 | + }); |
| 31 | + const result = getRedirectUri(req, baseEnv); |
| 32 | + expect(result).toBe('https://multi.com:3000/callback?storageType=none'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('falls back to request protocol and hostname if headers missing', () => { |
| 36 | + const req = makeReq({}, 'local.dev', 'http'); |
| 37 | + const result = getRedirectUri(req, baseEnv); |
| 38 | + expect(result).toBe('http://local.dev:3000/callback?storageType=none'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('omits port if standard (80 or 443)', () => { |
| 42 | + const env80 = { frontendPort: '80' } as EnvVariables; |
| 43 | + const env443 = { frontendPort: '443' } as EnvVariables; |
| 44 | + const req = makeReq({ 'x-forwarded-proto': 'https', 'x-forwarded-host': 'secure.com' }); |
| 45 | + expect(getRedirectUri(req, env80)).toBe('https://secure.com/callback?storageType=none'); |
| 46 | + expect(getRedirectUri(req, env443)).toBe('https://secure.com/callback?storageType=none'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('omits port if env.frontendPort is empty', () => { |
| 50 | + const envEmpty = { frontendPort: '' } as EnvVariables; |
| 51 | + const req = makeReq({ 'x-forwarded-proto': 'https', 'x-forwarded-host': 'noport.com' }); |
| 52 | + const result = getRedirectUri(req, envEmpty); |
| 53 | + expect(result).toBe('https://noport.com/callback?storageType=none'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('extracts hostname correctly when x-forwarded-host includes port', () => { |
| 57 | + const req = makeReq({ 'x-forwarded-proto': 'https', 'x-forwarded-host': 'withport.com:8080' }); |
| 58 | + const result = getRedirectUri(req, baseEnv); |
| 59 | + expect(result).toBe('https://withport.com:3000/callback?storageType=none'); |
| 60 | + }); |
| 61 | +}); |
0 commit comments