Adjusted tests for recovery.

This commit is contained in:
Sasha Myasoedov 2014-08-11 13:37:35 +03:00
parent 685d2c99cf
commit f2176c3100

View File

@ -11,14 +11,14 @@ import (
// TestPanicInHandler assert that panic has been recovered. // TestPanicInHandler assert that panic has been recovered.
func TestPanicInHandler(t *testing.T) { func TestPanicInHandler(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil) req, _ := http.NewRequest("GET", "/recovery", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
// Disable panic logs for testing // Disable panic logs for testing
log.SetOutput(bytes.NewBuffer(nil)) log.SetOutput(bytes.NewBuffer(nil))
r := Default() r := Default()
r.GET("/", func(_ *Context) { r.GET("/recovery", func(_ *Context) {
panic("Oupps, Houston, we have a problem") panic("Oupps, Houston, we have a problem")
}) })
@ -32,7 +32,43 @@ func TestPanicInHandler(t *testing.T) {
} }
bodyAsString := w.Body.String() bodyAsString := w.Body.String()
// fixme: //fixme: no message provided?
if bodyAsString != "" {
t.Errorf("Response body should be empty, was %s", bodyAsString)
}
//fixme:
if len(w.HeaderMap) != 0 {
t.Errorf("No headers should be provided, was %s", w.HeaderMap)
}
}
// TestPanicWithAbort assert that panic has been recovered even if context.Abort was used.
func TestPanicWithAbort(t *testing.T) {
req, _ := http.NewRequest("GET", "/recovery", nil)
w := httptest.NewRecorder()
// Disable panic logs for testing
log.SetOutput(bytes.NewBuffer(nil))
r := Default()
r.GET("/recovery", func(c *Context) {
c.Abort(400)
panic("Oupps, Houston, we have a problem")
})
r.ServeHTTP(w, req)
// restore logging
log.SetOutput(os.Stderr)
// fixme: why not 500?
if w.Code != 400 {
t.Errorf("Response code should be Bad request, was: %s", w.Code)
}
bodyAsString := w.Body.String()
//fixme: no message provided?
if bodyAsString != "" { if bodyAsString != "" {
t.Errorf("Response body should be empty, was %s", bodyAsString) t.Errorf("Response body should be empty, was %s", bodyAsString)
} }