-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_test.go
180 lines (177 loc) · 3.22 KB
/
http_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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package accesslog
import (
"net/http"
"net/url"
"testing"
)
func TestDefaultHTTPLogEntry_isIgnored(t *testing.T) {
type fields struct {
cfg *httpConfig
r *http.Request
}
tests := []struct {
name string
fields fields
want bool
}{
{
name: "ignored",
fields: fields{
cfg: &httpConfig{
ignoredPaths: map[string][]string{
"GET": {"/abc"},
},
},
r: &http.Request{
Method: "GET",
URL: &url.URL{Path: "/abc"},
},
},
want: true,
},
{
name: "method not matched",
fields: fields{
cfg: &httpConfig{
ignoredPaths: map[string][]string{
"GET": {"/abc"},
},
},
r: &http.Request{
Method: "POST",
URL: &url.URL{Path: "/abc"},
},
},
want: false,
},
{
name: "with slash",
fields: fields{
cfg: &httpConfig{
ignoredPaths: map[string][]string{
"GET": {"/abc/def"},
},
},
r: &http.Request{
Method: "GET",
URL: &url.URL{Path: "/abc/def"},
},
},
want: true,
},
{
name: "path not start with slash",
fields: fields{
cfg: &httpConfig{
ignoredPaths: map[string][]string{
"GET": {"/abc/def"},
},
},
r: &http.Request{
Method: "GET",
URL: &url.URL{Path: "abc/def"},
},
},
want: true,
},
{
name: "with asterisk",
fields: fields{
cfg: &httpConfig{
ignoredPaths: map[string][]string{
"GET": {"/a*/def"},
},
},
r: &http.Request{
Method: "GET",
URL: &url.URL{Path: "/afasdfsd/def"},
},
},
want: true,
},
{
name: "with asterisk 2",
fields: fields{
cfg: &httpConfig{
ignoredPaths: map[string][]string{
"GET": {"/abc/*/def"},
},
},
r: &http.Request{
Method: "GET",
URL: &url.URL{Path: "/abc/fasdfsd/def"},
},
},
want: true,
},
{
name: "with asterisk but path has slash",
fields: fields{
cfg: &httpConfig{
ignoredPaths: map[string][]string{
"GET": {"/ab*"},
},
},
r: &http.Request{
Method: "GET",
URL: &url.URL{Path: "/abce/def"},
},
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
le := &DefaultHTTPLogEntry{
cfg: tt.fields.cfg,
r: tt.fields.r,
}
if got := le.isIgnored(); got != tt.want {
t.Errorf("isIgnored() = %v, want %v", got, tt.want)
}
})
}
}
func Test_clientIP(t *testing.T) {
tests := []struct {
name string
h http.Header
want string
}{
{
name: "true-client-ip",
h: http.Header{
"True-Client-Ip": []string{"255.255.255.255"},
},
want: "255.255.255.255",
},
{
name: "x-forwarded-for",
h: http.Header{
"X-Forwarded-For": []string{"255.255.255.255"},
},
want: "255.255.255.255",
},
{
name: "x-real-ip",
h: http.Header{
"X-Real-Ip": []string{"255.255.255.255"},
},
want: "255.255.255.255",
},
{
name: "x-envoy-external-address",
h: http.Header{
"X-Envoy-External-Address": []string{"255.255.255.255"},
},
want: "255.255.255.255",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := clientIP(tt.h); got != tt.want {
t.Errorf("clientIP() = %v, want %v", got, tt.want)
}
})
}
}