forked from SierraSoftworks/sentry-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttprequest_test.go
More file actions
183 lines (150 loc) · 5.07 KB
/
httprequest_test.go
File metadata and controls
183 lines (150 loc) · 5.07 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
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
package sentry
import (
"net/http"
"net/url"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
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) {
Convey("HTTPRequest", t, func() {
r, err := http.NewRequest("GET", "https://example.com/test?testing=1&password=test", nil)
So(err, ShouldBeNil)
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")
Convey("HTTPRequest()", func() {
Convey("Should return an Option", func() {
So(HTTPRequest(r), ShouldImplement, (*Option)(nil))
})
Convey("Should not return nil if request is nil", func() {
So(HTTPRequest(nil), ShouldNotBeNil)
})
})
Convey("Should use the correct Class()", func() {
So(HTTPRequest(r).Class(), ShouldEqual, "request")
})
Convey("Omit()", func() {
Convey("Should return false with a valid request", func() {
So(HTTPRequest(r).(*httpRequestOption).Omit(), ShouldBeFalse)
})
Convey("Should return true if no request was provided", func() {
So(HTTPRequest(nil).(*httpRequestOption).Omit(), ShouldBeTrue)
})
})
Convey("buildData()", func() {
Convey("With the default config", func() {
opt := HTTPRequest(r)
hr, ok := opt.(*httpRequestOption)
So(ok, ShouldBeTrue)
d := hr.buildData()
So(d, ShouldNotBeNil)
So(d.Method, ShouldEqual, "GET")
So(d.URL, ShouldEqual, "https://example.com/test")
So(d.Query, ShouldEqual, url.Values{
"testing": {"1"},
"password": {"********"},
}.Encode())
So(d.Data, ShouldBeNil)
So(d.Headers, ShouldResemble, map[string]string{})
So(d.Env, ShouldResemble, map[string]string{})
So(d.Cookies, ShouldEqual, "")
})
Convey("With cookies enabled", func() {
opt := HTTPRequest(r).WithCookies()
hr, ok := opt.(*httpRequestOption)
So(ok, ShouldBeTrue)
d := hr.buildData()
So(d, ShouldNotBeNil)
So(d.Method, ShouldEqual, "GET")
So(d.URL, ShouldEqual, "https://example.com/test")
So(d.Query, ShouldEqual, url.Values{
"testing": {"1"},
"password": {"********"},
}.Encode())
So(d.Data, ShouldBeNil)
So(d.Headers, ShouldResemble, map[string]string{})
So(d.Env, ShouldResemble, map[string]string{})
So(d.Cookies, ShouldEqual, "testing=1")
})
Convey("With headers enabled", func() {
opt := HTTPRequest(r).WithHeaders()
hr, ok := opt.(*httpRequestOption)
So(ok, ShouldBeTrue)
d := hr.buildData()
So(d, ShouldNotBeNil)
So(d.Method, ShouldEqual, "GET")
So(d.URL, ShouldEqual, "https://example.com/test")
So(d.Query, ShouldEqual, url.Values{
"testing": {"1"},
"password": {"********"},
}.Encode())
So(d.Data, ShouldBeNil)
So(d.Headers, ShouldResemble, map[string]string{
"Host": "example.com",
"Cookie": "testing=1",
"X-Testing": "1",
"X-Forwarded-Proto": "https",
})
So(d.Env, ShouldResemble, map[string]string{})
So(d.Cookies, ShouldEqual, "")
})
Convey("With env enabled", func() {
opt := HTTPRequest(r).WithEnv()
hr, ok := opt.(*httpRequestOption)
So(ok, ShouldBeTrue)
d := hr.buildData()
So(d, ShouldNotBeNil)
So(d.Method, ShouldEqual, "GET")
So(d.URL, ShouldEqual, "https://example.com/test")
So(d.Query, ShouldEqual, url.Values{
"testing": {"1"},
"password": {"********"},
}.Encode())
So(d.Data, ShouldBeNil)
So(d.Headers, ShouldResemble, map[string]string{})
So(d.Env["REMOTE_ADDR"], ShouldEqual, "127.0.0.1")
So(d.Env["REMOTE_PORT"], ShouldEqual, "12835")
So(d.Cookies, ShouldEqual, "")
})
Convey("With data provided", func() {
opt := HTTPRequest(r).WithData("testing")
hr, ok := opt.(*httpRequestOption)
So(ok, ShouldBeTrue)
d := hr.buildData()
So(d, ShouldNotBeNil)
So(d.Method, ShouldEqual, "GET")
So(d.URL, ShouldEqual, "https://example.com/test")
So(d.Query, ShouldEqual, url.Values{
"testing": {"1"},
"password": {"********"},
}.Encode())
So(d.Data, ShouldEqual, "testing")
So(d.Headers, ShouldResemble, map[string]string{})
So(d.Env, ShouldResemble, map[string]string{})
So(d.Cookies, ShouldEqual, "")
})
})
})
}