Repository navigation
Expand file tree
/
Copy pathdebug_test.go
More file actions
108 lines (95 loc) · 2.12 KB
/
Copy pathdebug_test.go
File metadata and controls
108 lines (95 loc) · 2.12 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
package debug
import (
"bytes"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestVerboseEnabled(t *testing.T) {
b := &bytes.Buffer{}
output = b
Verbose = true
Println("World")
if b.String() != "World\n" {
t.Error("expected 'World' got", b.String())
}
}
func TestVerboseDisabled(t *testing.T) {
b := &bytes.Buffer{}
output = b
Verbose = false
Println("World")
if b.String() != "" {
t.Error("expected '' got", b.String())
}
}
func TestDumpRequest(t *testing.T) {
testCases := []struct {
desc string
auth string
verbose bool
unmask bool
}{
{
desc: "Do not attempt to dump request if 'Verbose' is set to false",
auth: "",
verbose: false,
unmask: false,
},
{
desc: "Dump request without authorization header",
auth: "", //not set
verbose: true,
unmask: false,
},
{
desc: "Dump request with malformed 'Authorization' header",
auth: "malformed",
verbose: true,
unmask: true,
},
{
desc: "Dump request with properly formed 'Authorization' header",
auth: "Bearer abc12-345abcde1234-5abc12",
verbose: true,
unmask: false,
},
}
b := &bytes.Buffer{}
output = b
for _, tc := range testCases {
Verbose = tc.verbose
UnmaskAPIKey = tc.unmask
r, _ := http.NewRequest("GET", "https://api.example.com/bogus", nil)
if tc.auth != "" {
r.Header.Set("Authorization", tc.auth)
}
DumpRequest(r)
if tc.verbose {
assert.Regexp(t, "GET /bogus", b.String(), tc.desc)
assert.Equal(t, tc.auth, r.Header.Get("Authorization"), tc.desc)
if tc.unmask {
assert.Regexp(t, "Authorization: "+tc.auth, b.String(), tc.desc)
}
} else {
assert.NotRegexp(t, "GET /bogus", b.String(), tc.desc)
}
}
}
func TestDumpResponse(t *testing.T) {
b := &bytes.Buffer{}
output = b
Verbose = true
r := &http.Response{
StatusCode: 200,
ProtoMajor: 1,
ProtoMinor: 1,
}
DumpResponse(r)
assert.Regexp(t, "HTTP/1.1 200 OK", b.String())
}
func TestRedact(t *testing.T) {
fakeToken := "1a11111aaaa111aa1a11111a11111aa1"
expected := "1a11*************************aa1"
assert.Equal(t, expected, Redact(fakeToken))
}