forked from unrolled/secure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
secure_integration_test.go
88 lines (69 loc) · 2.13 KB
/
secure_integration_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
// +build integration
package secure
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/urfave/negroni"
)
// go test -tags=integration
func TestIntegration(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "bar")
})
secureMiddleware := New(Options{
ContentTypeNosniff: true,
FrameDeny: true,
})
n := negroni.New()
n.Use(negroni.HandlerFunc(secureMiddleware.HandlerFuncWithNext))
n.UseHandler(mux)
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "http://example.com/foo", nil)
n.ServeHTTP(res, req)
expect(t, res.Code, http.StatusOK)
expect(t, res.Body.String(), "bar")
expect(t, res.Header().Get("X-Frame-Options"), "DENY")
expect(t, res.Header().Get("X-Content-Type-Options"), "nosniff")
}
func TestIntegrationWithError(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "bar")
})
secureMiddleware := New(Options{
ContentTypeNosniff: true,
FrameDeny: true,
AllowedHosts: []string{"www.example.com", "sub.example.com"},
})
n := negroni.New()
n.Use(negroni.HandlerFunc(secureMiddleware.HandlerFuncWithNext))
n.UseHandler(mux)
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/foo", nil)
req.Host = "www3.example.com"
n.ServeHTTP(res, req)
expect(t, res.Code, http.StatusInternalServerError)
}
func TestIntegrationForRequestOnly(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "bar")
})
secureMiddleware := New(Options{
ContentTypeNosniff: true,
FrameDeny: true,
})
n := negroni.New()
n.Use(negroni.HandlerFunc(secureMiddleware.HandlerFuncWithNextForRequestOnly))
n.UseHandler(mux)
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "http://example.com/foo", nil)
n.ServeHTTP(res, req)
expect(t, res.Code, http.StatusOK)
expect(t, res.Body.String(), "bar")
expect(t, res.Header().Get("X-Frame-Options"), "")
expect(t, res.Header().Get("X-Content-Type-Options"), "")
}