Debian Codegraph @

secure_integration_test.go

raw symbols SCIP
1 // +build integration
2
3 package secure
4
5 import (
6 "fmt"
7 "net/http"
8 "net/http/httptest"
9 "testing"
10
11 "github.com/urfave/negroni"
12 )
13
14 // go test -tags=integration
15
16 func TestIntegration(t *testing.T) {
17 mux := http.NewServeMux()
18 mux.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
19 fmt.Fprintf(w, "bar")
20 })
21
22 secureMiddleware := New(Options{
23 ContentTypeNosniff: true,
24 FrameDeny: true,
25 })
26
27 n := negroni.New()
28 n.Use(negroni.HandlerFunc(secureMiddleware.HandlerFuncWithNext))
29 n.UseHandler(mux)
30
31 res := httptest.NewRecorder()
32 req, _ := http.NewRequest("GET", "http://example.com/foo", nil)
33 n.ServeHTTP(res, req)
34
35 expect(t, res.Code, http.StatusOK)
36 expect(t, res.Body.String(), "bar")
37 expect(t, res.Header().Get("X-Frame-Options"), "DENY")
38 expect(t, res.Header().Get("X-Content-Type-Options"), "nosniff")
39 }
40
41 func TestIntegrationWithError(t *testing.T) {
42 mux := http.NewServeMux()
43 mux.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
44 fmt.Fprintf(w, "bar")
45 })
46
47 secureMiddleware := New(Options{
48 ContentTypeNosniff: true,
49 FrameDeny: true,
50 AllowedHosts: []string{"www.example.com", "sub.example.com"},
51 })
52
53 n := negroni.New()
54 n.Use(negroni.HandlerFunc(secureMiddleware.HandlerFuncWithNext))
55 n.UseHandler(mux)
56
57 res := httptest.NewRecorder()
58 req, _ := http.NewRequest("GET", "/foo", nil)
59 req.Host = "www3.example.com"
60 n.ServeHTTP(res, req)
61
62 expect(t, res.Code, http.StatusInternalServerError)
63 }
64
65 func TestIntegrationForRequestOnly(t *testing.T) {
66 mux := http.NewServeMux()
67 mux.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
68 fmt.Fprintf(w, "bar")
69 })
70
71 secureMiddleware := New(Options{
72 ContentTypeNosniff: true,
73 FrameDeny: true,
74 })
75
76 n := negroni.New()
77 n.Use(negroni.HandlerFunc(secureMiddleware.HandlerFuncWithNextForRequestOnly))
78 n.UseHandler(mux)
79
80 res := httptest.NewRecorder()
81 req, _ := http.NewRequest("GET", "http://example.com/foo", nil)
82 n.ServeHTTP(res, req)
83
84 expect(t, res.Code, http.StatusOK)
85 expect(t, res.Body.String(), "bar")
86 expect(t, res.Header().Get("X-Frame-Options"), "")
87 expect(t, res.Header().Get("X-Content-Type-Options"), "")
88 }