33 lines
744 B
Go
33 lines
744 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"))
|
|
})
|
|
http.ListenAndServe(":8000", New().Guard(mux))
|
|
}
|