-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
279 lines (270 loc) · 5.39 KB
/
main_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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package main
import (
"crypto/tls"
"fmt"
"net/http"
"net/url"
"reflect"
"testing"
)
func Test_buildRequestHeaders(t *testing.T) {
type args struct {
r *http.Request
}
tests := []struct {
name string
args args
want map[string]any
}{
{
name: "all headers copied",
args: args{r: &http.Request{Header: map[string][]string{
"Host": {"127.0.0.1"},
"X-Forwarded-For": {"192.168.1.1"},
"Authorization": {"Basic amltOm15c2VjcmV0cGFzc3dvcmQ="},
}}},
want: map[string]any{
"Host": "127.0.0.1",
"X-Forwarded-For": "192.168.1.1",
"Authorization": "Basic amltOm15c2VjcmV0cGFzc3dvcmQ=",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := buildRequestHeaders(tt.args.r); !reflect.DeepEqual(got, tt.want) {
t.Errorf("buildRequestHeaders() = %v, want %v", got, tt.want)
}
})
}
}
func Test_buildSSL(t *testing.T) {
type args struct {
r *http.Request
}
tests := []struct {
name string
args args
want map[string]any
}{
{
name: "all values present",
args: args{r: &http.Request{TLS: &tls.ConnectionState{
NegotiatedProtocol: "https",
CipherSuite: 1234,
ServerName: "gecho",
Version: 99,
}}},
want: map[string]any{
"negotiatedProtocol": "https",
"cipherSuite": uint16(1234),
"serverName": "gecho",
"version": uint16(99),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := buildSSL(tt.args.r); !reflect.DeepEqual(got, tt.want) {
fmt.Println(got)
fmt.Println(tt.want)
t.Errorf("buildSSL() = %v, want %v", got, tt.want)
}
})
}
}
func Test_buildSession(t *testing.T) {
type args struct {
r *http.Request
}
tests := []struct {
name string
args args
want map[string]any
}{
{
name: "all session values copied",
args: args{r: &http.Request{Header: map[string][]string{
"Cookie": {"some-cookie-value"},
}}},
want: map[string]any{
"cookie": []*http.Cookie{{Name: "some-cookie-value"}},
},
},
{
name: "empty session values",
args: args{r: &http.Request{}},
want: map[string]any{
"cookie": []*http.Cookie{},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := buildSession(tt.args.r); !reflect.DeepEqual(got, tt.want) {
t.Errorf("buildSession() = %v, want %v", got, tt.want)
}
})
}
}
func Test_buildURI(t *testing.T) {
type args struct {
r *http.Request
}
tests := []struct {
name string
args args
want map[string]any
}{
{
name: "no TLS or query params",
args: args{r: &http.Request{
Proto: "HTTP/1.1",
Method: http.MethodGet,
URL: &url.URL{
Path: "/path/to/thing",
},
}},
want: map[string]any{
"fullPath": "/path/to/thing",
"httpVersion": "HTTP/1.1",
"method": http.MethodGet,
"queryString": "",
"scheme": "http",
},
},
{
name: "with TLS and query params",
args: args{r: &http.Request{
TLS: &tls.ConnectionState{},
Proto: "HTTP/1.1",
Method: http.MethodGet,
URL: &url.URL{
Path: "/path/to/thing?params=true",
RawQuery: "params=true",
},
}},
want: map[string]any{
"fullPath": "/path/to/thing?params=true",
"httpVersion": "HTTP/1.1",
"method": http.MethodGet,
"queryString": "params=true",
"scheme": "https",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := buildURI(tt.args.r); !reflect.DeepEqual(got, tt.want) {
t.Errorf("buildURI() = %v, want %v", got, tt.want)
}
})
}
}
func Test_fileExists(t *testing.T) {
type args struct {
f string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "File does not exist",
args: args{f: "./madeup-file.txt"},
want: false,
},
{
name: "File does exist",
args: args{f: "./main.go"},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := fileExists(tt.args.f); got != tt.want {
t.Errorf("fileExists() = %v, want %v", got, tt.want)
}
})
}
}
func Test_getPort(t *testing.T) {
type args struct {
a string
s *tls.ConnectionState
}
tests := []struct {
name string
args args
want string
}{
{
name: "port 443",
args: args{
a: "127.0.0.1:443",
s: &tls.ConnectionState{},
},
want: "443",
},
{
name: "port 443",
args: args{
a: "127.0.0.1",
s: &tls.ConnectionState{},
},
want: "443",
},
{
name: "port 80",
args: args{
a: "127.0.0.1",
s: nil,
},
want: "80",
},
{
name: "port 8443",
args: args{
a: "127.0.0.1:8443",
s: &tls.ConnectionState{},
},
want: "8443",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getPort(tt.args.a, tt.args.s); got != tt.want {
t.Errorf("getPort() = %v, want %v", got, tt.want)
}
})
}
}
func Test_getScheme(t *testing.T) {
type args struct {
s *tls.ConnectionState
}
tests := []struct {
name string
args args
want string
}{
{
name: "https scheme",
args: args{s: &tls.ConnectionState{}},
want: "https",
},
{
name: "http scheme",
args: args{s: nil},
want: "http",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getScheme(tt.args.s); got != tt.want {
t.Errorf("getScheme() = %v, want %v", got, tt.want)
}
})
}
}