Repository navigation
Expand file tree
/
Copy pathlan_client_test.go
More file actions
66 lines (60 loc) · 1.56 KB
/
Copy pathlan_client_test.go
File metadata and controls
66 lines (60 loc) · 1.56 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
65
66
package api
import (
"net"
"net/http"
"testing"
)
func TestIsLANScopeIP(t *testing.T) {
tests := []struct {
ip string
want bool
}{
{"127.0.0.1", true},
{"::1", true},
{"192.168.1.1", true},
{"10.0.0.1", true},
{"172.16.0.1", true},
{"172.31.255.255", true},
{"172.32.0.1", false},
{"100.64.0.1", true},
{"169.254.1.1", true},
{"fe80::1", true},
{"8.8.8.8", false},
{"", false},
}
for _, tc := range tests {
var ip net.IP
if tc.ip != "" {
ip = net.ParseIP(tc.ip)
}
if got := IsLANScopeIP(ip); got != tc.want {
t.Errorf("IsLANScopeIP(%q) = %v, want %v", tc.ip, got, tc.want)
}
}
}
func TestClientIPFromRequest_RemoteAddrIPv4(t *testing.T) {
r := &http.Request{RemoteAddr: "192.168.1.10:54321"}
if got := ClientIPFromRequest(r); !got.Equal(net.ParseIP("192.168.1.10")) {
t.Fatalf("ClientIPFromRequest RemoteAddr = %v", got)
}
}
func TestClientIPFromRequest_IgnoresXFFByDefault(t *testing.T) {
t.Setenv(envTrustProxy, "")
r := &http.Request{
RemoteAddr: "192.168.1.10:1",
Header: http.Header{"X-Forwarded-For": []string{"8.8.8.8"}},
}
if got := ClientIPFromRequest(r); !got.Equal(net.ParseIP("192.168.1.10")) {
t.Fatalf("expected RemoteAddr IP, got %v", got)
}
}
func TestClientIPFromRequest_TrustProxyUsesXFF(t *testing.T) {
t.Setenv(envTrustProxy, "1")
r := &http.Request{
RemoteAddr: "10.0.0.1:1",
Header: http.Header{"X-Forwarded-For": []string{"192.168.2.2, 8.8.8.8"}},
}
if got := ClientIPFromRequest(r); !got.Equal(net.ParseIP("192.168.2.2")) {
t.Fatalf("expected first XFF IP, got %v", got)
}
}