-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoption_test.go
More file actions
147 lines (125 loc) · 3.68 KB
/
Copy pathoption_test.go
File metadata and controls
147 lines (125 loc) · 3.68 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package ip_test
import (
"errors"
"net/http/httptest"
"strings"
"testing"
"github.com/skarm/ip"
)
func TestNewRejectsInvalidTrustedProxy(t *testing.T) {
_, err := ip.New(ip.WithTrustedProxies("not-a-cidr"))
if !errors.Is(err, ip.ErrInvalidTrustedProxy) {
t.Fatalf("expected ErrInvalidTrustedProxy, got %v", err)
}
}
func TestNewRejectsAllowListWithoutTrustedProxies(t *testing.T) {
_, err := ip.New(ip.WithProxyMode(ip.ProxiesAllowedList))
if !errors.Is(err, ip.ErrMissingTrustedProxies) {
t.Fatalf("expected ErrMissingTrustedProxies, got %v", err)
}
}
func TestProxyModeString(t *testing.T) {
tests := []struct {
mode ip.ProxyMode
want string
}{
{mode: ip.ProxiesDenied, want: "ProxiesDenied"},
{mode: ip.ProxiesAllowedList, want: "ProxiesAllowedList"},
{mode: ip.ProxiesAllowedAll, want: "ProxiesAllowedAll"},
{mode: ip.ProxyMode(99), want: "ProxyMode(99)"},
}
for _, tt := range tests {
if got := tt.mode.String(); got != tt.want {
t.Fatalf("mode %d: expected %q, got %q", tt.mode, tt.want, got)
}
}
}
func TestWithHeadersRejectsEmptyHeader(t *testing.T) {
_, err := ip.New(ip.WithHeaders("X-Real-IP", ""))
if !errors.Is(err, ip.ErrInvalidConfig) {
t.Fatalf("expected ErrInvalidConfig, got %v", err)
}
}
func TestWithProxyModeRejectsInvalidMode(t *testing.T) {
_, err := ip.New(ip.WithProxyMode(ip.ProxyMode(99)))
if !errors.Is(err, ip.ErrInvalidConfig) {
t.Fatalf("expected ErrInvalidConfig, got %v", err)
}
if err == nil || !strings.Contains(err.Error(), "ProxyMode(99)") {
t.Fatalf("expected invalid mode value in error, got %v", err)
}
}
func TestWithProxyModeAllowAll(t *testing.T) {
ex, err := ip.New(ip.WithProxyMode(ip.ProxiesAllowedAll))
if err != nil {
t.Fatalf("New() error = %v", err)
}
req := httptest.NewRequest("GET", "/", nil)
req.RemoteAddr = "203.0.113.1:443"
req.Header.Set(ip.XForwardedFor, "198.51.100.10")
got, err := ex.Extract(req)
if err != nil {
t.Fatalf("Extract() error = %v", err)
}
if got.String() != "198.51.100.10" {
t.Fatalf("expected IP %q, got %q", "198.51.100.10", got.String())
}
}
func TestWithHeadersClonesInputAndTreatsCustomHeaderAsSingleIP(t *testing.T) {
headers := []string{"X-Custom-IP"}
ex, err := ip.New(
ip.WithHeaders(headers...),
ip.WithTrustedProxies("10.0.0.2"),
)
if err != nil {
t.Fatalf("New() error = %v", err)
}
headers[0] = "X-Other-IP"
req := httptest.NewRequest("GET", "/", nil)
req.RemoteAddr = "10.0.0.2:443"
req.Header.Set("X-Custom-IP", "198.51.100.10")
got, err := ex.Extract(req)
if err != nil {
t.Fatalf("Extract() error = %v", err)
}
if got.String() != "198.51.100.10" {
t.Fatalf("expected IP %q, got %q", "198.51.100.10", got.String())
}
}
func TestWithHeadersDedupesLogicalHeaderNames(t *testing.T) {
ex, err := ip.New(
ip.WithStrict(),
ip.WithHeaders(ip.XRealIP, "x-real-ip"),
ip.WithTrustedProxies("10.0.0.2"),
)
if err != nil {
t.Fatalf("New() error = %v", err)
}
req := httptest.NewRequest("GET", "/", nil)
req.RemoteAddr = "10.0.0.2:443"
req.Header["X-Real-IP"] = []string{"198.51.100.10", "198.51.100.20"}
_, err = ex.Extract(req)
if !errors.Is(err, ip.ErrAmbiguousHeader) {
t.Fatalf("expected ErrAmbiguousHeader, got %v", err)
}
}
func TestMust(t *testing.T) {
ex := ip.Must(ip.New(ip.WithProxyMode(ip.ProxiesDenied)))
if ex == nil {
t.Fatal("expected non-nil extractor")
}
}
func TestMustPanicsOnInvalidConfig(t *testing.T) {
defer func() {
if recover() == nil {
t.Fatal("expected panic")
}
}()
_ = ip.Must(ip.New(ip.WithTrustedProxies("not-a-cidr")))
}
func TestMustWithStrictOption(t *testing.T) {
ex := ip.Must(ip.New(ip.WithStrict(), ip.WithTrustedProxies("10.0.0.2")))
if ex == nil {
t.Fatal("expected non-nil extractor")
}
}