-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttps_middleware_test.go
More file actions
53 lines (48 loc) · 1.3 KB
/
https_middleware_test.go
File metadata and controls
53 lines (48 loc) · 1.3 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
package routeit
import (
"crypto/tls"
"testing"
"time"
)
func TestUpgradeToHttpsMiddleware(t *testing.T) {
tests := []struct {
name string
opts TestRequestOptions
validateRes func(t *testing.T, res *TestResponse)
wantProceed bool
}{
{
name: "https uses HSTS",
opts: TestRequestOptions{TlsConnectionState: &tls.ConnectionState{}},
validateRes: func(t *testing.T, res *TestResponse) {
val := "max-age=31536000; includeSubdomains"
res.AssertHeaderMatchesString(t, "Strict-Transport-Security", val)
},
wantProceed: true,
},
{
name: "http redirected",
opts: TestRequestOptions{
Headers: []string{"Host", "example.com"},
},
validateRes: func(t *testing.T, res *TestResponse) {
res.AssertStatusCode(t, StatusMovedPermanently)
res.AssertHeaderMatchesString(t, "Location", "https://example.com:443/foo")
},
},
}
mware := upgradeToHttpsMiddleware(443, 365*24*time.Hour)
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
req := NewTestRequest(t, "/foo", GET, tc.opts)
res, proceeded, err := TestMiddleware(mware, req)
if err != nil {
t.Errorf(`err = %+v, wanted nil`, err)
}
if proceeded != tc.wantProceed {
t.Errorf(`proceeded = %t, wanted %t`, proceeded, tc.wantProceed)
}
tc.validateRes(t, res)
})
}
}