| 1 |
package raven |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/http" |
| 5 |
"net/url" |
| 6 |
"reflect" |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
type testcase struct { |
| 11 |
request *http.Request |
| 12 |
*Http |
| 13 |
} |
| 14 |
|
| 15 |
func newBaseRequest() *http.Request { |
| 16 |
u, _ := url.Parse("http://example.com/") |
| 17 |
header := make(http.Header) |
| 18 |
header.Add("Foo", "bar") |
| 19 |
|
| 20 |
req := &http.Request{ |
| 21 |
Method: "GET", |
| 22 |
URL: u, |
| 23 |
Proto: "HTTP/1.1", |
| 24 |
ProtoMajor: 1, |
| 25 |
ProtoMinor: 1, |
| 26 |
Header: header, |
| 27 |
Host: u.Host, |
| 28 |
RemoteAddr: "127.0.0.1:8000", |
| 29 |
} |
| 30 |
return req |
| 31 |
} |
| 32 |
|
| 33 |
func newBaseHttp() *Http { |
| 34 |
h := &Http{ |
| 35 |
Method: "GET", |
| 36 |
Cookies: "", |
| 37 |
Query: "", |
| 38 |
URL: "http://example.com/", |
| 39 |
Headers: map[string]string{"Foo": "bar"}, |
| 40 |
Env: map[string]string{"REMOTE_ADDR": "127.0.0.1", "REMOTE_PORT": "8000"}, |
| 41 |
} |
| 42 |
h.Headers["Host"] = "example.com" |
| 43 |
return h |
| 44 |
} |
| 45 |
|
| 46 |
func NewRequest() testcase { |
| 47 |
return testcase{newBaseRequest(), newBaseHttp()} |
| 48 |
} |
| 49 |
|
| 50 |
func NewRequestIPV6() testcase { |
| 51 |
req := newBaseRequest() |
| 52 |
req.RemoteAddr = "[:1]:8000" |
| 53 |
|
| 54 |
h := newBaseHttp() |
| 55 |
h.Env = map[string]string{"REMOTE_ADDR": ":1", "REMOTE_PORT": "8000"} |
| 56 |
return testcase{req, h} |
| 57 |
} |
| 58 |
|
| 59 |
func NewRequestMultipleHeaders() testcase { |
| 60 |
req := newBaseRequest() |
| 61 |
req.Header.Add("Foo", "baz") |
| 62 |
|
| 63 |
h := newBaseHttp() |
| 64 |
h.Headers["Foo"] = "bar,baz" |
| 65 |
return testcase{req, h} |
| 66 |
} |
| 67 |
|
| 68 |
func NewSecureRequest() testcase { |
| 69 |
req := newBaseRequest() |
| 70 |
req.Header.Add("X-Forwarded-Proto", "https") |
| 71 |
|
| 72 |
h := newBaseHttp() |
| 73 |
h.URL = "https://example.com/" |
| 74 |
h.Headers["X-Forwarded-Proto"] = "https" |
| 75 |
return testcase{req, h} |
| 76 |
} |
| 77 |
|
| 78 |
func NewCookiesRequest() testcase { |
| 79 |
val := "foo=bar; bar=baz" |
| 80 |
req := newBaseRequest() |
| 81 |
req.Header.Add("Cookie", val) |
| 82 |
|
| 83 |
h := newBaseHttp() |
| 84 |
h.Cookies = val |
| 85 |
h.Headers["Cookie"] = val |
| 86 |
return testcase{req, h} |
| 87 |
} |
| 88 |
|
| 89 |
var newHttpTests = []testcase{ |
| 90 |
NewRequest(), |
| 91 |
NewRequestIPV6(), |
| 92 |
NewRequestMultipleHeaders(), |
| 93 |
NewSecureRequest(), |
| 94 |
NewCookiesRequest(), |
| 95 |
} |
| 96 |
|
| 97 |
func TestNewHttp(t *testing.T) { |
| 98 |
for _, test := range newHttpTests { |
| 99 |
actual := NewHttp(test.request) |
| 100 |
if actual.Method != test.Method { |
| 101 |
t.Errorf("incorrect Method: got %s, want %s", actual.Method, test.Method) |
| 102 |
} |
| 103 |
if actual.Cookies != test.Cookies { |
| 104 |
t.Errorf("incorrect Cookies: got %s, want %s", actual.Cookies, test.Cookies) |
| 105 |
} |
| 106 |
if actual.Query != test.Query { |
| 107 |
t.Errorf("incorrect Query: got %s, want %s", actual.Query, test.Query) |
| 108 |
} |
| 109 |
if actual.URL != test.URL { |
| 110 |
t.Errorf("incorrect URL: got %s, want %s", actual.URL, test.URL) |
| 111 |
} |
| 112 |
if !reflect.DeepEqual(actual.Headers, test.Headers) { |
| 113 |
t.Errorf("incorrect Headers: got %+v, want %+v", actual.Headers, test.Headers) |
| 114 |
} |
| 115 |
if !reflect.DeepEqual(actual.Env, test.Env) { |
| 116 |
t.Errorf("incorrect Env: got %+v, want %+v", actual.Env, test.Env) |
| 117 |
} |
| 118 |
if !reflect.DeepEqual(actual.Data, test.Data) { |
| 119 |
t.Errorf("incorrect Data: got %+v, want %+v", actual.Data, test.Data) |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
var sanitizeQueryTests = []struct { |
| 125 |
input, output string |
| 126 |
}{ |
| 127 |
{"foo=bar", "foo=bar"}, |
| 128 |
{"password=foo", "password=********"}, |
| 129 |
{"passphrase=foo", "passphrase=********"}, |
| 130 |
{"passwd=foo", "passwd=********"}, |
| 131 |
{"secret=foo", "secret=********"}, |
| 132 |
{"secretstuff=foo", "secretstuff=********"}, |
| 133 |
{"foo=bar&secret=foo", "foo=bar&secret=********"}, |
| 134 |
{"secret=foo&secret=bar", "secret=********"}, |
| 135 |
} |
| 136 |
|
| 137 |
func parseQuery(q string) url.Values { |
| 138 |
r, _ := url.ParseQuery(q) |
| 139 |
return r |
| 140 |
} |
| 141 |
|
| 142 |
func TestSanitizeQuery(t *testing.T) { |
| 143 |
for _, test := range sanitizeQueryTests { |
| 144 |
actual := sanitizeQuery(parseQuery(test.input)) |
| 145 |
expected := parseQuery(test.output) |
| 146 |
if !reflect.DeepEqual(actual, expected) { |
| 147 |
t.Errorf("incorrect sanitization: got %+v, want %+v", actual, expected) |
| 148 |
} |
| 149 |
} |
| 150 |
} |