-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhttprequest_test.go
179 lines (156 loc) · 5.46 KB
/
httprequest_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
package sentry
import (
"net/http"
"net/url"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func ExampleHTTPRequest() {
cl := NewClient(
Release("v1.0.0"),
)
// Add your 404 handler to the default mux
http.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
cl := cl.With(
// Set the HTTP request context for your request's client
HTTPRequest(req).WithHeaders(),
)
res.Header().Set("Content-Type", "application/json")
res.WriteHeader(404)
res.Write([]byte(`{"error":"Not Found","message":"We could not find the route you requested, please check your URL and try again."}`))
// Capture the problem using your request's client
cl.Capture(
Message("Route Not Found: [%s] %s", req.Method, req.URL.Path),
Level(Warning),
)
})
}
func TestHTTPRequest(t *testing.T) {
r, err := http.NewRequest("GET", "https://example.com/test?testing=1&password=test", nil)
assert.Nil(t, err, "should be able to create an HTTP request object")
r.RemoteAddr = "127.0.0.1:12835"
r.Header.Set("Host", "example.com")
r.Header.Set("X-Forwarded-Proto", "https")
r.Header.Set("Cookie", "testing=1")
r.Header.Set("X-Testing", "1")
r.Header.Set("X-API-Key", "secret")
assert.NotNil(t, HTTPRequest(nil), "it should not return nil if no request is provided")
o := HTTPRequest(r)
assert.NotNil(t, o, "should not return a nil option")
assert.Implements(t, (*Option)(nil), o, "it should implement the Option interface")
assert.Equal(t, "request", o.Class(), "it should use the right option class")
if assert.Implements(t, (*OmitableOption)(nil), o, "it should implement the OmitableOption interface") {
assert.False(t, o.(OmitableOption).Omit(), "it should return false if there is a request")
assert.True(t, HTTPRequest(nil).(OmitableOption).Omit(), "it should return true if there is no request")
}
tm := "GET"
tu := "https://example.com/test"
tq := url.Values{
"testing": {"1"},
"password": {sanitizationString},
}
var td interface{} = nil
th := map[string]string{}
te := map[string]string{}
tc := ""
cases := []struct {
Name string
Opt Option
Setup func()
}{
{"Default", HTTPRequest(r), func() {}},
{"Default.Sanitize()", HTTPRequest(r).Sanitize("testing"), func() {
tq = url.Values{
"testing": {sanitizationString},
"password": {sanitizationString},
}
}},
{"WithCookies()", HTTPRequest(r).WithCookies(), func() {
tc = "testing=1"
}},
{"WithHeaders()", HTTPRequest(r).WithHeaders(), func() {
th = map[string]string{
"Host": "example.com",
"Cookie": "testing=1",
"X-Testing": "1",
"X-Forwarded-Proto": "https",
"X-Api-Key": "secret",
}
}},
{"WithHeaders().Sanitize()", HTTPRequest(r).WithHeaders().Sanitize("key"), func() {
th = map[string]string{
"Host": "example.com",
"Cookie": "testing=1",
"X-Testing": "1",
"X-Forwarded-Proto": "https",
"X-Api-Key": sanitizationString,
}
}},
{"WithEnv()", HTTPRequest(r).WithEnv(), func() {
te = map[string]string{
"REMOTE_ADDR": "127.0.0.1",
"REMOTE_PORT": "12835",
}
}},
{"WithEnv().Sanitize()", HTTPRequest(r).WithEnv().Sanitize("secret"), func() {
os.Setenv("SECRET", "secret")
te = map[string]string{
"REMOTE_ADDR": "127.0.0.1",
"REMOTE_PORT": "12835",
"SECRET": "********",
}
}},
{"WithData()", HTTPRequest(r).WithData("testing"), func() {
td = "testing"
}},
}
for _, testCase := range cases {
testCase := testCase
t.Run(testCase.Name, func(t *testing.T) {
tq = url.Values{
"testing": {"1"},
"password": {sanitizationString},
}
td = nil
th = map[string]string{}
te = map[string]string{}
tc = ""
testCase.Setup()
hr, ok := testCase.Opt.(*httpRequestOption)
assert.True(t, ok, "the option should actually be a *httpRequestOption")
d := hr.buildData()
assert.NotNil(t, d, "the built data should not be nil")
assert.Equal(t, tm, d.Method, "the method should be correct")
assert.Equal(t, tu, d.URL, "the url should be correct")
assert.Equal(t, tq.Encode(), d.Query, "the query should be correct")
assert.Equal(t, td, d.Data, "the data should be correct")
assert.Equal(t, th, d.Headers, "the headers should be correct")
for k, v := range te {
if assert.Contains(t, d.Env, k, "the environment should include the %s entry", k) {
assert.Equal(t, v, d.Env[k], "the value of the %s environment variable should be correct", k)
}
}
assert.Equal(t, tc, d.Cookies, "the cookies should be correct")
})
}
t.Run("MarshalJSON()", func(t *testing.T) {
o := HTTPRequest(r).WithHeaders().WithCookies().WithData("test").Sanitize("key", "secret")
require.NotNil(t, o, "the option should not be nil")
assert.Equal(t, map[string]interface{}{
"cookies": "testing=1",
"data": "test",
"headers": map[string]interface{}{
"Cookie": "testing=1",
"Host": "example.com",
"X-Api-Key": "********",
"X-Forwarded-Proto": "https",
"X-Testing": "1",
},
"method": "GET",
"query_string": "password=%2A%2A%2A%2A%2A%2A%2A%2A&testing=1",
"url": "https://example.com/test",
}, testOptionsSerialize(t, o), "the request option should be serialized correctly")
})
}