-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_test.go
More file actions
258 lines (214 loc) · 7.11 KB
/
handler_test.go
File metadata and controls
258 lines (214 loc) · 7.11 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package hsts
import (
"crypto/tls"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func TestThatHTTPRequestsAreRedirectedToHTTPS(t *testing.T) {
// Create the handler to wrap.
wrappedHandler := &TestHandler{
body: []byte("Shouldn't see this content over HTTP, only over HTTPS."),
}
// Create the middleware and pass it the wrapped handler.
hstsHandler := NewHandler(wrappedHandler)
redirectionTests := []struct {
inputURL string
expectedRedirect string
method string
}{
{
inputURL: "http://www.example.com",
expectedRedirect: "https://www.example.com",
method: http.MethodGet,
},
{
inputURL: "http://example.com",
expectedRedirect: "https://example.com",
method: http.MethodPost,
},
{
inputURL: "http://example.com:80",
expectedRedirect: "https://example.com:80",
method: http.MethodPost,
},
{
inputURL: "http://example.com/test/",
expectedRedirect: "https://example.com/test/",
method: http.MethodPost,
},
{
inputURL: "/test",
expectedRedirect: "https://example.com/test",
method: http.MethodPost,
},
}
for _, test := range redirectionTests {
// Create a mock request to capture the result.
w := httptest.NewRecorder()
req, _ := http.NewRequest(test.method, test.inputURL, nil)
req.Host = "example.com"
// Act.
hstsHandler.ServeHTTP(w, req)
// Assert.
actual := struct {
body string
redirectedTo string
statusCode int
stsHeader string
}{
body: strings.TrimSpace(string(w.Body.Bytes())),
redirectedTo: w.Header().Get("Location"),
statusCode: w.Code,
stsHeader: w.Header().Get("Strict-Transport-Security"),
}
if test.expectedRedirect != actual.redirectedTo {
t.Errorf("For a HTTP %s to %s, expected redirect to %s, but was %s", test.method, test.inputURL, test.expectedRedirect, actual.redirectedTo)
}
if http.StatusMovedPermanently != actual.statusCode {
t.Errorf("For a HTTP %s to %s, expected a status code of 301 Moved Permanently but was %d", test.method, test.inputURL, actual.statusCode)
}
if test.method == http.MethodGet {
expectedBody := "<a href=\"" + test.expectedRedirect + "\">Moved Permanently</a>."
if actual.body != expectedBody {
t.Errorf("For a HTTP %s to %s, expected a redirect link in the body \"%s\" but the body contained \"%s\"", test.method, test.inputURL, expectedBody, actual.body)
}
}
if actual.stsHeader != "" {
t.Errorf("The STS header should only be set on HTTPS requests. But over HTTP it was %s", actual.stsHeader)
}
}
}
func TestThatCommonHTTPHeadersAreUsedToDetermineSSLStatus(t *testing.T) {
r, _ := http.NewRequest("GET", "/", nil)
if isHTTPS(r, true) {
t.Error("The request was HTTP, but this was not detected")
}
r.Header.Add("X-Forwarded-Proto", "https")
if !isHTTPS(r, true) {
t.Error("The request was HTTPS, but this was not detected")
}
if isHTTPS(r, false) {
t.Error("The request was declared HTTPS by the header, but this was not enabled.")
}
}
func TestThatTLSIsUsedToDetermineSSLStatus(t *testing.T) {
r, _ := http.NewRequest("GET", "/", nil)
r.TLS = &tls.ConnectionState{
HandshakeComplete: true,
}
if !isHTTPS(r, false) {
t.Error("The request was HTTPS, but this was not detected")
}
}
func TestThatTLSDoesNotOverrideHTTPHeadersToDetermineSSLStatus(t *testing.T) {
r, _ := http.NewRequest("GET", "/", nil)
r.TLS = &tls.ConnectionState{
HandshakeComplete: true,
}
r.Header.Add("X-Forwarded-Proto", "http")
if isHTTPS(r, true) {
t.Error("The request from the load balancer to the service was HTTPS, but the request from the user to the load balancer was HTTP.")
}
}
func TestThatTheHostCanBeOverridden(t *testing.T) {
// Create the handler to wrap.
wrappedHandler := &TestHandler{
body: []byte("Shouldn't see this content over HTTP, only over HTTPS."),
}
// Create the middleware and pass it the wrapped handler.
hstsHandler := NewHandler(wrappedHandler)
hstsHandler.HostOverride = "subdomain.example.com"
// Create a mock request to capture the result.
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "http://example.com/test", nil)
// Act.
hstsHandler.ServeHTTP(w, req)
// Assert.
expectedRedirect := "https://subdomain.example.com/test"
actualRedirect := w.Header().Get("Location")
if actualRedirect != expectedRedirect {
t.Errorf("Expected a redirect to %s, but got a redirect to %s", expectedRedirect, actualRedirect)
}
}
func TestThatHTTPSRequestsHaveTheHeaderApplied(t *testing.T) {
// Create the handler to wrap.
wrappedHandler := &TestHandler{
body: []byte("Secure content!"),
}
// Create the middleware and pass it the wrapped handler.
hstsHandler := NewHandler(wrappedHandler)
urls := []string{
"https://www.example.com",
"https://example.com",
"https://example.com:443",
"https://example.com:443/test/another?query=123",
"https://example.com:443/test/another?query=123&more=456#789",
}
for _, url := range urls {
// Create a mock request to capture the result.
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", url, nil)
// Act.
hstsHandler.ServeHTTP(w, req)
// Assert.
actual := struct {
body string
redirectedTo string
statusCode int
stsHeader string
}{
body: string(w.Body.Bytes()),
redirectedTo: w.Header().Get("Location"),
statusCode: w.Code,
stsHeader: w.Header().Get("Strict-Transport-Security"),
}
if actual.redirectedTo != "" {
t.Errorf("For a HTTPS GET to %s, expected no redirect but received one to %s", url, actual.redirectedTo)
}
if actual.statusCode != http.StatusOK {
t.Errorf("For a HTTPS GET to %s, expected a status code of 200 OK but was %d", url, actual.statusCode)
}
if actual.body != "Secure content!" {
t.Errorf("For a HTTPS GET to %s, expected a body of %s but the body was actually %s", url, "Secure content!", actual.body)
}
if !strings.Contains(actual.stsHeader, "max-age=10886400;") {
t.Errorf("Expected the STS header to contain a max-age of 10886400 (18 weeks in seconds), but the header was \"%s\"", actual.stsHeader)
}
}
}
func TestHeaderValueIncludesPreloadDirective(t *testing.T) {
expected := "max-age=3600; includeSubDomains"
actual := createHeaderValue(time.Hour, false)
if expected != actual {
t.Errorf("Expected header without preload of \"%s\", but got \"%s\"", expected, actual)
}
expected = "max-age=3600; includeSubDomains; preload"
actual = createHeaderValue(time.Hour, true)
if expected != actual {
t.Errorf("Expected header with a preload of \"%s\", but got \"%s\"", expected, actual)
}
}
type TestHandler struct {
body []byte
wasExecuted bool
}
func (th *TestHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
w.Write(th.body)
th.wasExecuted = true
w.Header().Add("x-test", "x-test-value")
}
func BenchmarkCreateheaderValueOld(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
createHeaderValue(time.Hour, true)
}
}
func BenchmarkCreateheaderValueNew(b *testing.B) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
createHeaderValueNew(time.Hour, true)
}
}