-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_test.ts
55 lines (51 loc) · 1.63 KB
/
mod_test.ts
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
import {
describe,
expect,
it,
run,
} from 'https://deno.land/x/[email protected]/mod.ts'
import { all, RequestWithConnection } from './mod.ts'
import { ConnInfo } from 'https://deno.land/[email protected]/http/server.ts'
const createReq = (
hostname: string,
headers?: Record<string, string>
): RequestWithConnection =>
({
conn: {
remoteAddr: {
hostname,
port: 8081,
transport: 'tcp',
},
} as ConnInfo,
headers: new Headers(headers || {}),
} as unknown as RequestWithConnection)
describe('all(req, trust)', () => {
it('with no headers should return socket address', () => {
const req = createReq('127.0.0.1')
expect(all(req)).toEqual(['127.0.0.1'])
})
it('with x-forwarded-for header should include x-forwarded-for', () => {
const req = createReq('127.0.0.1', { 'x-forwarded-for': '10.0.0.1' })
expect(all(req)).toEqual(['127.0.0.1', '10.0.0.1'])
})
it('with x-forwarded-for header should include x-forwarded-for in correct order', () => {
const req = createReq('127.0.0.1', {
'x-forwarded-for': '10.0.0.1, 10.0.0.2',
})
expect(all(req)).toEqual(['127.0.0.1', '10.0.0.2', '10.0.0.1'])
})
it('with trust argument should stop at first untrusted', () => {
const req = createReq('127.0.0.1', {
'x-forwarded-for': '10.0.0.1, 10.0.0.2',
})
expect(all(req, '127.0.0.1')).toEqual(['127.0.0.1', '10.0.0.2'])
})
it('with trust argument should be only socket address for no trust', () => {
const req = createReq('127.0.0.1', {
'x-forwarded-for': '10.0.0.1, 10.0.0.2',
})
expect(all(req, [])).toEqual(['127.0.0.1'])
})
})
run()