transfer.sh/vendor/github.com/PuerkitoBio/ghost/handlers/panic_test.go
Remco cb6e5cb0c7 Major rewrite
* use dep for vendoring
* lets encrypt
* moved web to transfer.sh-web repo
* single command install
* added first tests
2017-03-22 18:09:21 +01:00

62 lines
1.2 KiB
Go

package handlers
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestPanic(t *testing.T) {
h := PanicHandler(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
panic("test")
}), nil)
s := httptest.NewServer(h)
defer s.Close()
res, err := http.Get(s.URL)
if err != nil {
panic(err)
}
assertStatus(http.StatusInternalServerError, res.StatusCode, t)
}
func TestNoPanic(t *testing.T) {
h := PanicHandler(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
}), nil)
s := httptest.NewServer(h)
defer s.Close()
res, err := http.Get(s.URL)
if err != nil {
panic(err)
}
assertStatus(http.StatusOK, res.StatusCode, t)
}
func TestPanicCustom(t *testing.T) {
h := PanicHandler(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
panic("ok")
}),
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
err, ok := GetPanicError(w)
if !ok {
panic("no panic error found")
}
w.WriteHeader(501)
w.Write([]byte(err.(string)))
}))
s := httptest.NewServer(h)
defer s.Close()
res, err := http.Get(s.URL)
if err != nil {
panic(err)
}
assertStatus(501, res.StatusCode, t)
assertBody([]byte("ok"), res, t)
}