guard/guard_test.go

37 lines
825 B
Go

package guard
import (
"errors"
"net/http"
"testing"
)
func TestGuard(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/panic", func(rw http.ResponseWriter, r *http.Request) {
panic(nil)
})
mux.HandleFunc("/custerr", func(rw http.ResponseWriter, r *http.Request) {
panic(Error{
Code: 404,
Message: "custerr",
ID: "test",
})
})
mux.HandleFunc("/err", func(rw http.ResponseWriter, r *http.Request) {
panic(errors.New("err"))
})
mux.HandleFunc("/500", func(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(500)
rw.Write([]byte("err"))
})
mux.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {
rw.Write([]byte("ok"))
})
g := New()
g.Skip = func(r *http.Request) bool {
return r.URL.Path == "/skip"
}
http.ListenAndServe(":8000", g.Guard(mux))
}