Repository navigation
Expand file tree
/
Copy pathgetClientIp.accessibility.test.ts
More file actions
67 lines (55 loc) · 2.01 KB
/
Copy pathgetClientIp.accessibility.test.ts
File metadata and controls
67 lines (55 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { describe, test, expect } from 'vitest';
import { NextRequest } from 'next/server';
import { getClientIp } from './getClientIp';
describe('getClientIp - Accessibility Standards & Screen Reader Aria Compliance', () => {
test('Aria Label Compliance: resolves request.ip as the primary accessible identity source', () => {
const req = new NextRequest('http://localhost:3000/api/test');
Object.defineProperty(req, 'ip', {
value: '203.0.113.10',
writable: true,
});
expect(getClientIp(req)).toBe('203.0.113.10');
});
test('Focus Outline Behavior: ignores untrusted forwarded headers when no trusted peer exists', () => {
const req = new Request('http://localhost:3000/api/test', {
headers: {
'x-forwarded-for': '198.51.100.5',
},
});
expect(getClientIp(req)).toBe('127.0.0.1');
});
test('Tooltip Announcement: resolves the correct client IP through a trusted proxy chain', () => {
const req = new Request('http://localhost:3000/api/test', {
headers: {
'x-forwarded-for': '198.51.100.5, 203.0.113.10, 127.0.0.1',
},
});
const ip = getClientIp(req, {
proxyConfig: {
trustedProxies: ['127.0.0.1', '203.0.113.10'],
trustPrivateRanges: true,
},
directIp: '127.0.0.1',
});
expect(ip).toBe('198.51.100.5');
});
test('Tab Ordering: wildcard trust returns the leftmost client IP in the chain', () => {
const req = new Request('http://localhost:3000/api/test', {
headers: {
'x-forwarded-for': '198.51.100.5, 203.0.113.10',
},
});
const ip = getClientIp(req, {
proxyConfig: {
trustedProxies: ['*'],
trustPrivateRanges: false,
},
directIp: '203.0.113.10',
});
expect(ip).toBe('198.51.100.5');
});
test('Heading Hierarchy: falls back to localhost in development/test environments when no IP data exists', () => {
const req = new Request('http://localhost:3000/api/test');
expect(getClientIp(req)).toBe('127.0.0.1');
});
});