-
Notifications
You must be signed in to change notification settings - Fork 3
/
testutils_test.go
46 lines (38 loc) · 996 Bytes
/
testutils_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
package helmet
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
var mockNext = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
})
func addXPoweredByHelmetMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(HeaderXPoweredBy, "Helmet")
next.ServeHTTP(w, r)
})
}
func newRecorderRequest(t *testing.T) (*httptest.ResponseRecorder, *http.Request) {
rr := httptest.NewRecorder()
r, err := http.NewRequest(http.MethodGet, "/", nil)
if err != nil {
t.Fatal(err)
}
return rr, r
}
func testMockNext(t *testing.T, resp *http.Response) {
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected: %d\tActual: %d\n", http.StatusOK, resp.StatusCode)
}
defer resp.Body.Close()
buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
body := string(buf)
if body != "OK" {
t.Errorf("Expected: %s\tActual: %s\n", "OK", body)
}
}