2014-08-08 12:50:52 +00:00
|
|
|
package gin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestPanicInHandler assert that panic has been recovered.
|
|
|
|
func TestPanicInHandler(t *testing.T) {
|
2014-08-18 17:48:48 +00:00
|
|
|
// SETUP
|
|
|
|
log.SetOutput(bytes.NewBuffer(nil)) // Disable panic logs for testing
|
|
|
|
r := New()
|
|
|
|
r.Use(Recovery())
|
2014-08-11 10:37:35 +00:00
|
|
|
r.GET("/recovery", func(_ *Context) {
|
2014-08-08 12:50:52 +00:00
|
|
|
panic("Oupps, Houston, we have a problem")
|
|
|
|
})
|
|
|
|
|
2014-08-18 17:48:48 +00:00
|
|
|
// RUN
|
|
|
|
w := PerformRequest(r, "GET", "/recovery")
|
2014-08-08 12:50:52 +00:00
|
|
|
|
|
|
|
// restore logging
|
|
|
|
log.SetOutput(os.Stderr)
|
|
|
|
|
|
|
|
if w.Code != 500 {
|
|
|
|
t.Errorf("Response code should be Internal Server Error, was: %s", w.Code)
|
|
|
|
}
|
2014-08-11 10:37:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestPanicWithAbort assert that panic has been recovered even if context.Abort was used.
|
|
|
|
func TestPanicWithAbort(t *testing.T) {
|
2014-08-18 17:48:48 +00:00
|
|
|
// SETUP
|
2014-08-11 10:37:35 +00:00
|
|
|
log.SetOutput(bytes.NewBuffer(nil))
|
2014-08-18 17:48:48 +00:00
|
|
|
r := New()
|
|
|
|
r.Use(Recovery())
|
2014-08-11 10:37:35 +00:00
|
|
|
r.GET("/recovery", func(c *Context) {
|
|
|
|
c.Abort(400)
|
|
|
|
panic("Oupps, Houston, we have a problem")
|
|
|
|
})
|
|
|
|
|
2014-08-18 17:48:48 +00:00
|
|
|
// RUN
|
|
|
|
w := PerformRequest(r, "GET", "/recovery")
|
2014-08-11 10:37:35 +00:00
|
|
|
|
|
|
|
// restore logging
|
|
|
|
log.SetOutput(os.Stderr)
|
|
|
|
|
2014-08-18 17:48:48 +00:00
|
|
|
// TEST
|
|
|
|
if w.Code != 500 {
|
2014-08-11 10:37:35 +00:00
|
|
|
t.Errorf("Response code should be Bad request, was: %s", w.Code)
|
|
|
|
}
|
2014-08-08 12:50:52 +00:00
|
|
|
}
|