-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandler_test.go
More file actions
61 lines (53 loc) · 1.26 KB
/
handler_test.go
File metadata and controls
61 lines (53 loc) · 1.26 KB
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
package fatal
import (
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"os"
"testing"
)
func TestSimple(t *testing.T) {
log.SetOutput(ioutil.Discard)
log.SetFlags(0)
defer log.SetOutput(os.Stderr)
h := HandleFunc(func(w http.ResponseWriter, r *http.Request) {
panic("error")
}, nil)
req := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
h.ServeHTTP(w, req)
if w.Code != http.StatusInternalServerError {
t.Fail()
}
}
func TestRecoverHandler(t *testing.T) {
log.SetOutput(ioutil.Discard)
log.SetFlags(0)
defer log.SetOutput(os.Stderr)
status := http.StatusServiceUnavailable
err := "unknown error"
h := HandleFunc(func(w http.ResponseWriter, r *http.Request) {
panic(err)
}, &Options{
RecoverHandler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(status)
err := Error(r)
switch err.(type) {
case string:
w.Write([]byte(err.(string)))
default:
t.Errorf("error type: want string, got %T", err)
}
}),
})
req := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
h.ServeHTTP(w, req)
if w.Code != status {
t.Errorf("status code: want %v, got %v", status, w.Code)
}
if w.Body.String() != err {
t.Errorf("body: want %q, got %q", err, w.Body.String())
}
}