-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
434 lines (392 loc) · 10.7 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
package main
import (
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func makeBodyMatch(rules ...ConfigRule) HTTPMatch {
return HTTPMatch{RuleOptions: RuleOptions{Body: rules}}
}
func makeQuerystringMatch(rules ...ConfigRule) HTTPMatch {
return HTTPMatch{RuleOptions: RuleOptions{Querystring: rules}}
}
func makeRequest(body string, contentType string) *http.Request {
request, _ := http.NewRequest("GET", "", strings.NewReader(body))
if contentType != "" {
request.Header.Add("Content-Type", contentType)
}
return request
}
func TestIsSamePath(t *testing.T) {
type testCase struct {
name string
a string
b string
out bool
}
cases := []testCase{
{name: "with exact matches", a: "/v1", b: "/v1", out: true},
{name: "with trailing slashes", a: "/v1", b: "/v1/", out: true},
{name: "with trailing slashes", a: "/v1//", b: "/v1/", out: true},
{name: "with trailing slashes and different paths", a: "/v2", b: "/v1", out: false},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
match := isSamePath(c.a, c.b)
assert.Equal(t, match, c.out)
})
}
}
func TestIsSameCaseInsensitive(t *testing.T) {
t.Log("Running with equavalent strings")
isSame := isSameCaseInsensitive("aba", "aBa")
assert.True(t, isSame)
t.Log("Running with non-equavalent strings")
isNotSame := isSameCaseInsensitive("aca", "aBa")
assert.False(t, isNotSame)
}
func TestMergeUrl(t *testing.T) {
type testCase struct {
name string
destination *url.URL
source *url.URL
out string
}
cases := []testCase{
{
name: "with different paths",
destination: &url.URL{Path: "/v1"},
source: &url.URL{Path: "/users"},
out: "/v1/users",
},
{
name: "with a new querystring",
destination: &url.URL{RawQuery: "a=2"},
source: &url.URL{Path: "/users", RawQuery: "b=3"},
out: "/users?b=3",
},
{
name: "with a new fragment",
destination: &url.URL{Path: "/v1", Fragment: "anchor1"},
source: &url.URL{Fragment: "anchor2"},
out: "/v1#anchor2",
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
result := mergeURL(c.destination, c.source)
assert.Equal(t, c.out, result.String())
})
}
}
func TestRedact(t *testing.T) {
type testCase struct {
name string
match HTTPMatch
value interface{}
out interface{}
}
cases := []testCase{
{
name: "with a number",
match: makeBodyMatch(),
value: 10.0,
out: 0.0,
},
{
name: "with nil",
match: makeBodyMatch(),
value: nil,
out: nil,
},
{
name: "with an unsupported value",
match: makeBodyMatch(),
value: 12,
out: nil,
},
{
name: "with a string",
match: makeBodyMatch(),
value: "data",
out: "REDACTED",
},
{
name: "with a boolean",
match: makeBodyMatch(),
value: true,
out: false,
},
{
name: "with a slice of values",
match: makeBodyMatch(),
value: []interface{}{"a", 20.0, true},
out: []interface{}{"REDACTED", 0.0, false},
},
{
name: "with a map of values",
match: makeBodyMatch(),
value: map[string]interface{}{"string": "data", "bool": true},
out: map[string]interface{}{"string": "REDACTED", "bool": false},
},
{
name: "with a map of values",
match: makeBodyMatch(),
value: map[string]interface{}{"string": "data", "bool": true},
out: map[string]interface{}{"string": "REDACTED", "bool": false},
},
{
name: "with a map of values that are themselves containers",
match: makeBodyMatch(),
value: map[string]interface{}{"array": []interface{}{"str1", "str2"}},
out: map[string]interface{}{"array": []interface{}{"REDACTED", "REDACTED"}},
},
{
name: "with a whitelist",
match: makeBodyMatch(ConfigRule{Whitelist: "$.bleep"}, ConfigRule{Whitelist: "$.array[0]"}),
value: map[string]interface{}{"array": []interface{}{"str1", "str2"}},
out: map[string]interface{}{"array": []interface{}{"str1", "REDACTED"}},
},
{
name: "with a whitelist that matches two keys",
match: makeBodyMatch(ConfigRule{Whitelist: "$.bleep"}, ConfigRule{Whitelist: "$.array[0]"}),
value: map[string]interface{}{"array": []interface{}{"str1", "str2"}, "bleep": "bloop"},
out: map[string]interface{}{"array": []interface{}{"str1", "REDACTED"}, "bleep": "bloop"},
},
{
name: "with a wildcard whitelist",
match: makeBodyMatch(ConfigRule{Whitelist: "$.array[*]"}),
value: map[string]interface{}{"array": []interface{}{"str1", "str2"}},
out: map[string]interface{}{"array": []interface{}{"str1", "str2"}},
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
value := redact(c.match, c.value, "$")
assert.Equal(t, c.out, value)
})
}
}
func TestMapBody(t *testing.T) {
type testCase struct {
name string
match HTTPMatch
body string
out string
}
cases := []testCase{
{
name: "with a basic body",
match: makeBodyMatch(),
body: `{"a": "bloop"}`,
out: `{"a":"REDACTED"}`,
},
{
name: "with a basic body and a whitelist",
match: makeBodyMatch(ConfigRule{Whitelist: "$.a"}),
body: `{"a": "bloop", "b": [true, "hey"]}`,
out: `{"a":"bloop","b":[false,"REDACTED"]}`,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
result, err := mapBody(c.match, "application/json", []byte(c.body))
if err != nil {
t.Fail()
}
assert.Equal(t, string(result[:]), c.out)
})
}
}
func TestMapBodyWithWrongContentType(t *testing.T) {
body := []byte("{ query: { id } }")
result, err := mapBody(HTTPMatch{}, "application/graphql", body)
if err != nil {
t.Fail()
}
assert.Equal(t, result, []byte{})
}
func TestGetContentType(t *testing.T) {
request, err := http.NewRequest("GET", "", strings.NewReader(""))
if err != nil {
t.Fail()
}
request.Header.Add("Content-Type", "application/json; charset=utf8")
result := getContentType(request)
assert.Equal(t, result, "application/json")
}
func TestRedactBody(t *testing.T) {
type testCase struct {
name string
match HTTPMatch
request *http.Request
expectedBody string
}
cases := []testCase{
{
name: "with an empty JSON body",
match: HTTPMatch{},
request: makeRequest(`{}`, "application/json"),
expectedBody: "{}",
},
{
name: "with a non-empty JSON body",
match: HTTPMatch{},
request: makeRequest(`{"a": "data"}`, "application/json"),
expectedBody: `{"a":"REDACTED"}`,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
err := redactBody(c.match, c.request)
if err != nil {
t.Fail()
}
body, err := ioutil.ReadAll(c.request.Body)
if err != nil {
t.Fail()
}
assert.Equal(t, string(body[:]), c.expectedBody)
assert.Equal(t, strconv.FormatInt(c.request.ContentLength, 10), c.request.Header.Get("Content-Length"))
assert.Equal(t, c.request.ContentLength, int64(len(body)))
})
}
}
func TestRedactQuerystring(t *testing.T) {
type testCase struct {
name string
match HTTPMatch
in url.URL
out string
}
cases := []testCase{
{
name: "with empty query",
match: HTTPMatch{},
in: url.URL{RawQuery: ""},
out: "",
},
{
name: "with a query with no whitelist",
match: HTTPMatch{},
in: url.URL{RawQuery: "a=2&b=3"},
out: "a=REDACTED&b=REDACTED",
},
{
name: "with a query with a single whitelist value",
match: makeQuerystringMatch(ConfigRule{Whitelist: "a"}),
in: url.URL{RawQuery: "a=2&b=3"},
out: "a=2&b=REDACTED",
},
{
name: "with a query with many whitelist values",
match: makeQuerystringMatch(ConfigRule{Whitelist: "a"}, ConfigRule{Whitelist: "b"}),
in: url.URL{RawQuery: "a=2&b=3&c=2"},
out: "a=2&b=3&c=REDACTED",
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
result := redactQuerystring(c.match, &c.in)
assert.Equal(t, result, c.out)
})
}
}
func TestMakeDirectory(t *testing.T) {
makeRequestWithExtras := func(method string, path string, query string, body string) *http.Request {
request := makeRequest(body, "application/json")
request.Method = method
request.URL.Path = path
request.URL.RawQuery = query
return request
}
config := Config{
ProxyPass: "https://api.usebutton.com/ingest",
Match: MatchOptions{
HTTP: []HTTPMatch{
HTTPMatch{
Path: "/v1/whitelist",
Method: "GET",
RuleOptions: RuleOptions{
Body: []ConfigRule{
ConfigRule{
Whitelist: "$.a.b",
},
ConfigRule{
Whitelist: "$.a.c",
},
},
Querystring: []ConfigRule{
ConfigRule{
Whitelist: "a",
},
},
},
},
},
},
}
director, _ := makeDirector(config)
type testCase struct {
name string
request *http.Request
expectedBody string
expectedQuery string
expectedURL string
}
cases := []testCase{
{
name: "with an empty payload",
request: makeRequest("{}", "application/json"),
expectedBody: "{}",
expectedURL: "https://api.usebutton.com/ingest",
},
{
name: "with an unsupported content-type",
request: makeRequest("{ id }", "application/graphql"),
expectedBody: "",
expectedURL: "https://api.usebutton.com/ingest",
},
{
name: "with a json payload",
request: makeRequestWithExtras("", "", "", `{"a":{"b":10}}`),
expectedBody: `{"a":{"b":0}}`,
expectedURL: "https://api.usebutton.com/ingest",
},
{
name: "with a json payload an exemptions",
request: makeRequestWithExtras("GET", "/v1/whitelist", "a=1&c=2", `{"a":{"b":10,"c":"data","d":10}}`),
expectedBody: `{"a":{"b":10,"c":"data","d":0}}`,
expectedURL: "https://api.usebutton.com/ingest/v1/whitelist?a=1&c=REDACTED",
},
{
name: "with a json payload an exemptions but wrong method",
request: makeRequestWithExtras("POST", "/v1/whitelist", "a=1", `{"a":{"b":10}}`),
expectedBody: `{"a":{"b":0}}`,
expectedURL: "https://api.usebutton.com/ingest/v1/whitelist?a=REDACTED",
},
{
name: "with a json payload an exemptions but wrong pathname",
request: makeRequestWithExtras("GET", "/v2/whitelist", "a=1", `{"a":{"b":10}}`),
expectedBody: `{"a":{"b":0}}`,
expectedURL: "https://api.usebutton.com/ingest/v2/whitelist?a=REDACTED",
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
director(c.request)
body, err := ioutil.ReadAll(c.request.Body)
if err != nil {
t.Fail()
}
assert.Equal(t, string(body[:]), c.expectedBody)
assert.Equal(t, c.request.URL.String(), c.expectedURL)
assert.Equal(t, c.request.Host, "api.usebutton.com")
assert.Equal(t, c.request.Header.Get("x-privacy-proxy-redacted"), "1")
})
}
}