-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_test.ts
47 lines (40 loc) · 1.4 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
import type { RequestWithConnection } from './mod.ts'
import { forwarded } from './mod.ts'
import type { ConnInfo } from 'https://deno.land/std@0.197.0/http/server.ts'
import { describe, it, expect, run } from 'https://deno.land/x/tincan@1.0.1/mod.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('forwarded(req)', () => {
it('should work with `X-Forwarded-For` header', () => {
const req = createReq('127.0.0.1')
expect(forwarded(req)).toEqual(['127.0.0.1'])
})
it('should include entries from `X-Forwarded-For`', () => {
const req = createReq('127.0.0.1', {
'x-forwarded-for': '10.0.0.2, 10.0.0.1'
})
expect(forwarded(req)).toEqual(['127.0.0.1', '10.0.0.1', '10.0.0.2'])
})
it('should skip blank entries', () => {
const req = createReq('127.0.0.1', {
'x-forwarded-for': '10.0.0.2,, 10.0.0.1'
})
expect(forwarded(req)).toEqual(['127.0.0.1', '10.0.0.1', '10.0.0.2'])
})
it('should trim leading OWS', () => {
const req = createReq('127.0.0.1', {
'x-forwarded-for': ' 10.0.0.2 , , 10.0.0.1 '
})
expect(forwarded(req)).toEqual(['127.0.0.1', '10.0.0.1', '10.0.0.2'])
})
})
run()