|
1 | 1 | import { getRedirectUri } from './redirect-uri.js'; |
2 | 2 | import type { Request } from 'express'; |
3 | | -import { EnvVariables } from '../env/env.service.js'; |
4 | 3 |
|
5 | 4 | describe('getRedirectUri', () => { |
6 | | - const baseEnv: EnvVariables = { |
7 | | - frontendPort: '3000', |
8 | | - } as any; |
| 5 | + const makeReq = (options: Partial<Request>): Request => |
| 6 | + ({ |
| 7 | + headers: options.headers || {}, |
| 8 | + hostname: options.hostname || 'example.com', |
| 9 | + protocol: options.protocol || 'http', |
| 10 | + }) as Request; |
9 | 11 |
|
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'); |
| 12 | + it('uses x-forwarded headers for proto, host, and port', () => { |
| 13 | + const req = makeReq({ |
| 14 | + headers: { |
| 15 | + 'x-forwarded-proto': 'https', |
| 16 | + 'x-forwarded-host': 'forwarded.com', |
| 17 | + 'x-forwarded-port': '8443', |
| 18 | + }, |
24 | 19 | }); |
| 20 | + const result = getRedirectUri(req); |
| 21 | + expect(result).toBe('https://forwarded.com:8443/callback?storageType=none'); |
| 22 | + }); |
25 | 23 |
|
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'); |
| 24 | + it('handles x-forwarded headers as arrays', () => { |
| 25 | + const req = makeReq({ |
| 26 | + headers: { |
| 27 | + 'x-forwarded-proto': ['https', 'http'], |
| 28 | + 'x-forwarded-host': ['multi.com:8080', 'other.com'], |
| 29 | + 'x-forwarded-port': ['8081', '8082'], |
| 30 | + }, |
33 | 31 | }); |
| 32 | + const result = getRedirectUri(req); |
| 33 | + expect(result).toBe('https://multi.com:8081/callback?storageType=none'); |
| 34 | + }); |
34 | 35 |
|
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'); |
| 36 | + it('omits standard ports 80 and 443', () => { |
| 37 | + const req80 = makeReq({ |
| 38 | + headers: { |
| 39 | + 'x-forwarded-proto': 'http', |
| 40 | + 'x-forwarded-host': 'plain.com', |
| 41 | + 'x-forwarded-port': '80', |
| 42 | + }, |
39 | 43 | }); |
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'); |
| 44 | + const req443 = makeReq({ |
| 45 | + headers: { |
| 46 | + 'x-forwarded-proto': 'https', |
| 47 | + 'x-forwarded-host': 'secure.com', |
| 48 | + 'x-forwarded-port': '443', |
| 49 | + }, |
47 | 50 | }); |
| 51 | + expect(getRedirectUri(req80)).toBe( |
| 52 | + 'http://plain.com/callback?storageType=none', |
| 53 | + ); |
| 54 | + expect(getRedirectUri(req443)).toBe( |
| 55 | + 'https://secure.com/callback?storageType=none', |
| 56 | + ); |
| 57 | + }); |
48 | 58 |
|
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'); |
| 59 | + it('falls back to host header port when x-forwarded-port missing', () => { |
| 60 | + const req = makeReq({ |
| 61 | + headers: { |
| 62 | + host: 'local.dev:3000', |
| 63 | + 'x-forwarded-proto': 'http', |
| 64 | + }, |
54 | 65 | }); |
| 66 | + const result = getRedirectUri(req); |
| 67 | + expect(result).toBe('http://example.com:3000/callback?storageType=none'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('falls back to request protocol and hostname if no headers', () => { |
| 71 | + const req = makeReq({ hostname: 'app.local', protocol: 'http' }); |
| 72 | + const result = getRedirectUri(req); |
| 73 | + expect(result).toBe('http://app.local/callback?storageType=none'); |
| 74 | + }); |
55 | 75 |
|
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'); |
| 76 | + it('extracts hostname correctly from x-forwarded-host with port', () => { |
| 77 | + const req = makeReq({ |
| 78 | + headers: { |
| 79 | + 'x-forwarded-proto': 'https', |
| 80 | + 'x-forwarded-host': 'withport.com:9090', |
| 81 | + }, |
60 | 82 | }); |
| 83 | + const result = getRedirectUri(req); |
| 84 | + expect(result).toBe('https://withport.com/callback?storageType=none'); |
| 85 | + }); |
61 | 86 | }); |
0 commit comments