2015-05-22 18:37:40 +00:00
|
|
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-05-22 18:37:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestPanicInHandler assert that panic has been recovered.
|
|
|
|
func TestPanicInHandler(t *testing.T) {
|
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
router := New()
|
|
|
|
router.Use(RecoveryWithWriter(buffer))
|
|
|
|
router.GET("/recovery", func(_ *Context) {
|
|
|
|
panic("Oupps, Houston, we have a problem")
|
|
|
|
})
|
|
|
|
// RUN
|
|
|
|
w := performRequest(router, "GET", "/recovery")
|
|
|
|
// TEST
|
|
|
|
assert.Equal(t, w.Code, 500)
|
|
|
|
assert.Contains(t, buffer.String(), "Panic recovery -> Oupps, Houston, we have a problem")
|
|
|
|
assert.Contains(t, buffer.String(), "TestPanicInHandler")
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestPanicWithAbort assert that panic has been recovered even if context.Abort was used.
|
|
|
|
func TestPanicWithAbort(t *testing.T) {
|
|
|
|
router := New()
|
|
|
|
router.Use(RecoveryWithWriter(nil))
|
|
|
|
router.GET("/recovery", func(c *Context) {
|
|
|
|
c.AbortWithStatus(400)
|
|
|
|
panic("Oupps, Houston, we have a problem")
|
|
|
|
})
|
|
|
|
// RUN
|
|
|
|
w := performRequest(router, "GET", "/recovery")
|
|
|
|
// TEST
|
|
|
|
assert.Equal(t, w.Code, 500) // NOT SURE
|
|
|
|
}
|