-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_test.go
More file actions
64 lines (58 loc) · 1.16 KB
/
request_test.go
File metadata and controls
64 lines (58 loc) · 1.16 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
package httpx
import (
"context"
"net/http"
"testing"
)
func TestRequestIP(t *testing.T) {
const defaultAddr = "128.10.20.30:50"
testCases := []struct {
headers http.Header
want string
}{
{
want: "128.10.20.30",
},
{
headers: http.Header{
"Cf-Connecting-Ip": []string{"192.0.2.5"},
},
want: "192.0.2.5",
},
{
headers: http.Header{
"True-Client-IP": []string{"192.0.20.50"},
"X-Real-Ip": []string{"192.0.2.5"},
},
want: "192.0.2.5",
},
{
headers: http.Header{
"X-Real-Ip": []string{"20.20.20.42"},
"X-Forwarded-For": []string{"203.0.113.42"},
},
want: "20.20.20.42",
},
{
headers: http.Header{
"X-Forwarded-For": []string{"203.0.113.42"},
},
want: "203.0.113.42",
},
{
headers: http.Header{
"X-Forwarded-For": []string{"203.0.113.42, 198.51.100.17, 192.0.2.5"},
},
want: "203.0.113.42",
},
}
for _, tc := range testCases {
r := MustGetRequest(context.Background(), "/")
r.Header = tc.headers
r.RemoteAddr = defaultAddr
addr := RequestIP(r)
if have := addr.String(); have != tc.want {
t.Errorf("\nhave: %v\nwant: %v", have, tc.want)
}
}
}