-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy_test.go
57 lines (52 loc) · 1.02 KB
/
proxy_test.go
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
package main
import (
"net/http/httptest"
"testing"
)
func TestReadUserIP(t *testing.T) {
//arrange
testCases := []struct {
name string
ipHeaders map[string]string
expected string
}{
{
name: "X-Real-Ip header",
ipHeaders: map[string]string{
"X-Real-Ip": "192.168.0.1",
},
expected: "192.168.0.1",
},
{
name: "X-Forwarded-For header",
ipHeaders: map[string]string{
"X-Forwarded-For": "192.168.0.2",
},
expected: "192.168.0.2",
},
{
name: "RemoteAddr field",
expected: "192.168.0.4",
},
{
name: "No headers",
ipHeaders: map[string]string{
"Other-Header": "value",
},
expected: "192.168.0.4",
},
}
for _, tc := range testCases {
req := httptest.NewRequest("GET", "/", nil)
req.RemoteAddr = "192.168.0.4" // Default RemoteAddr
for k, v := range tc.ipHeaders {
req.Header.Set(k, v)
}
//act
got := readUserIP(req)
//assert
if got != tc.expected {
t.Errorf("%s: readUserIP() = %s; want %s", tc.name, got, tc.expected)
}
}
}