-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_handler_test.go
45 lines (36 loc) · 1.09 KB
/
error_handler_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
package middlewares
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
var tNotFound = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("the page could not be found"))
})
var tFound = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("the page could be found"))
})
func TestErrorBody(t *testing.T) {
req, err := http.NewRequest("GET", "/notfound", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := ErrorHandler(tNotFound)
handler.ServeHTTP(rr, req)
assert.Contains(t, rr.Body.String(), "the page could not be found")
assert.Equal(t, http.StatusNotFound, rr.Code)
}
func TestPassthrough(t *testing.T) {
req, err := http.NewRequest("GET", "/found", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := ErrorHandler(tFound)
handler.ServeHTTP(rr, req)
assert.Contains(t, rr.Body.String(), "the page could be found")
assert.Equal(t, http.StatusOK, rr.Code)
}