-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathforcessl_test.go
125 lines (95 loc) · 3.46 KB
/
forcessl_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
package forceSSL
import (
"github.com/ant0ine/go-json-rest/rest"
"github.com/ant0ine/go-json-rest/rest/test"
"net/http"
"testing"
)
type JSON map[string]interface{}
var (
simplePostData = JSON{
"email": "[email protected]",
"password": "password",
}
)
func simpleGetEndpoint(w rest.ResponseWriter, r *rest.Request) {
w.WriteJson(simplePostData)
}
func simplePostEndpoint(w rest.ResponseWriter, r *rest.Request) {
body := struct {
Email string `json:"email"`
Password string `json:"password"`
}{}
r.DecodeJsonPayload(&body)
w.WriteJson(body)
}
func NewAPI(forceSSLMiddleware *Middleware) http.Handler {
api := rest.NewApi()
api.Use(forceSSLMiddleware)
router, _ := rest.MakeRouter(
rest.Post("/", simplePostEndpoint),
rest.Get("/", simpleGetEndpoint),
)
api.SetApp(router)
return api.MakeHandler()
}
func TestUnconfiguredForceSSLMiddleware(t *testing.T) {
handler := NewAPI(&Middleware{})
req := test.MakeSimpleRequest("GET", "http://localhost/", nil)
recorded := test.RunRequest(t, handler, req)
recorded.CodeIs(http.StatusForbidden)
recorded.BodyIs("SSL Required.")
}
func TestTrustXFPHeaderForceSSLMiddleware(t *testing.T) {
handler := NewAPI(&Middleware{
TrustXFPHeader: true,
})
getRequest := test.MakeSimpleRequest("GET", "http://localhost/", nil)
getRequest.Header.Set("X-Forwarded-Proto", "http")
recordedGet := test.RunRequest(t, handler, getRequest)
recordedGet.CodeIs(http.StatusForbidden)
recordedGet.BodyIs("SSL Required.")
postRequest := test.MakeSimpleRequest("POST", "http://localhost/", simplePostData)
postRequest.Header.Set("X-Forwarded-Proto", "http")
recordedPost := test.RunRequest(t, handler, postRequest)
recordedPost.CodeIs(http.StatusForbidden)
recordedPost.BodyIs("SSL Required.")
}
func TestGetEnable301RedirectsForceSSLMiddleware(t *testing.T) {
handler := NewAPI(&Middleware{
Enable301Redirects: true,
})
getRequest := test.MakeSimpleRequest("GET", "http://localhost/", nil)
getRequest.Header.Set("X-Forwarded-Proto", "http")
recordedGet := test.RunRequest(t, handler, getRequest)
recordedGet.CodeIs(http.StatusMovedPermanently)
postRequest := test.MakeSimpleRequest("POST", "http://localhost/", simplePostData)
postRequest.Header.Set("X-Forwarded-Proto", "http")
recordedPost := test.RunRequest(t, handler, postRequest)
recordedPost.CodeIs(http.StatusMovedPermanently)
}
func TestMessageForceSSLMiddleware(t *testing.T) {
message := "Custom message!"
handler := NewAPI(&Middleware{
Message: message,
})
getRequest := test.MakeSimpleRequest("GET", "http://localhost/", nil)
recordedGet := test.RunRequest(t, handler, getRequest)
recordedGet.CodeIs(http.StatusForbidden)
recordedGet.BodyIs(message)
postRequest := test.MakeSimpleRequest("POST", "http://localhost/", simplePostData)
recordedPost := test.RunRequest(t, handler, postRequest)
recordedPost.CodeIs(http.StatusForbidden)
recordedPost.BodyIs(message)
}
func TestValidGetHTTPSRequestForceSSLMiddleware(t *testing.T) {
handler := NewAPI(&Middleware{})
getRequest := test.MakeSimpleRequest("GET", "https://localhost/", nil)
recordedGet := test.RunRequest(t, handler, getRequest)
recordedGet.CodeIs(http.StatusOK)
recordedGet.BodyIs(`{"email":"[email protected]","password":"password"}`)
postRequest := test.MakeSimpleRequest("POST", "https://localhost/", simplePostData)
recordedPost := test.RunRequest(t, handler, postRequest)
recordedPost.CodeIs(http.StatusOK)
recordedPost.BodyIs(`{"email":"[email protected]","password":"password"}`)
}