-
-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathforwarded.test.ts
More file actions
33 lines (28 loc) · 1.12 KB
/
forwarded.test.ts
File metadata and controls
33 lines (28 loc) · 1.12 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
import type { IncomingMessage } from 'node:http'
import { describe, expect, it } from 'vitest'
import { forwarded } from '../../packages/forwarded/src'
import { createReq } from '../../test_helpers/createReq'
describe('forwarded(req)', () => {
it('should work with `X-Forwarded-For` header', () => {
const req = createReq('127.0.0.1') as IncomingMessage
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'
}) as IncomingMessage
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'
}) as IncomingMessage
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 '
}) as IncomingMessage
expect(forwarded(req)).toEqual(['127.0.0.1', '10.0.0.1', '10.0.0.2'])
})
})