forked from getsentry/raven-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_test.go
149 lines (130 loc) · 3.43 KB
/
http_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
package raven
import (
"net/http"
"net/url"
"reflect"
"testing"
)
type testcase struct {
request *http.Request
*Http
}
func newBaseRequest() *http.Request {
u, _ := url.Parse("http://example.com/")
header := make(http.Header)
header.Add("Foo", "bar")
req := &http.Request{
Method: "GET",
URL: u,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: header,
Host: u.Host,
RemoteAddr: "127.0.0.1:8000",
}
return req
}
func newBaseHttp() *Http {
h := &Http{
Method: "GET",
Cookies: "",
Query: "",
URL: "http://example.com/",
Headers: map[string]string{"Foo": "bar"},
Env: map[string]string{"REMOTE_ADDR": "127.0.0.1", "REMOTE_PORT": "8000"},
}
return h
}
func NewRequest() testcase {
return testcase{newBaseRequest(), newBaseHttp()}
}
func NewRequestIPV6() testcase {
req := newBaseRequest()
req.RemoteAddr = "[:1]:8000"
h := newBaseHttp()
h.Env = map[string]string{"REMOTE_ADDR": ":1", "REMOTE_PORT": "8000"}
return testcase{req, h}
}
func NewRequestMultipleHeaders() testcase {
req := newBaseRequest()
req.Header.Add("Foo", "baz")
h := newBaseHttp()
h.Headers["Foo"] = "bar,baz"
return testcase{req, h}
}
func NewSecureRequest() testcase {
req := newBaseRequest()
req.Header.Add("X-Forwarded-Proto", "https")
h := newBaseHttp()
h.URL = "https://example.com/"
h.Headers["X-Forwarded-Proto"] = "https"
return testcase{req, h}
}
func NewCookiesRequest() testcase {
val := "foo=bar; bar=baz"
req := newBaseRequest()
req.Header.Add("Cookie", val)
h := newBaseHttp()
h.Cookies = val
h.Headers["Cookie"] = val
return testcase{req, h}
}
var newHttpTests = []testcase{
NewRequest(),
NewRequestIPV6(),
NewRequestMultipleHeaders(),
NewSecureRequest(),
NewCookiesRequest(),
}
func TestNewHttp(t *testing.T) {
for _, test := range newHttpTests {
actual := NewHttp(test.request)
if actual.Method != test.Method {
t.Errorf("incorrect Method: got %s, want %s", actual.Method, test.Method)
}
if actual.Cookies != test.Cookies {
t.Errorf("incorrect Cookies: got %s, want %s", actual.Cookies, test.Cookies)
}
if actual.Query != test.Query {
t.Errorf("incorrect Query: got %s, want %s", actual.Query, test.Query)
}
if actual.URL != test.URL {
t.Errorf("incorrect URL: got %s, want %s", actual.URL, test.URL)
}
if !reflect.DeepEqual(actual.Headers, test.Headers) {
t.Errorf("incorrect Headers: got %+v, want %+v", actual.Headers, test.Headers)
}
if !reflect.DeepEqual(actual.Env, test.Env) {
t.Errorf("incorrect Env: got %+v, want %+v", actual.Env, test.Env)
}
if !reflect.DeepEqual(actual.Data, test.Data) {
t.Errorf("incorrect Data: got %+v, want %+v", actual.Data, test.Data)
}
}
}
var sanitizeQueryTests = []struct {
input, output string
}{
{"foo=bar", "foo=bar"},
{"password=foo", "password=********"},
{"passphrase=foo", "passphrase=********"},
{"passwd=foo", "passwd=********"},
{"secret=foo", "secret=********"},
{"secretstuff=foo", "secretstuff=********"},
{"foo=bar&secret=foo", "foo=bar&secret=********"},
{"secret=foo&secret=bar", "secret=********"},
}
func parseQuery(q string) url.Values {
r, _ := url.ParseQuery(q)
return r
}
func TestSanitizeQuery(t *testing.T) {
for _, test := range sanitizeQueryTests {
actual := sanitizeQuery(parseQuery(test.input))
expected := parseQuery(test.output)
if !reflect.DeepEqual(actual, expected) {
t.Errorf("incorrect sanitization: got %+v, want %+v", actual, expected)
}
}
}