2014-08-29 17:49:50 +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.
|
|
|
|
|
2014-08-08 12:50:52 +00:00
|
|
|
package gin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
2015-04-08 11:30:17 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2014-08-08 12:50:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestPanicInHandler assert that panic has been recovered.
|
|
|
|
func TestPanicInHandler(t *testing.T) {
|
2015-04-08 11:30:17 +00:00
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
router := New()
|
2015-05-12 13:22:13 +00:00
|
|
|
router.Use(RecoveryWithWriter(buffer))
|
2015-04-08 11:30:17 +00:00
|
|
|
router.GET("/recovery", func(_ *Context) {
|
2015-04-08 00:58:35 +00:00
|
|
|
panic("Oupps, Houston, we have a problem")
|
2014-08-08 12:50:52 +00:00
|
|
|
})
|
2014-08-18 17:48:48 +00:00
|
|
|
// RUN
|
2015-04-08 11:30:17 +00:00
|
|
|
w := performRequest(router, "GET", "/recovery")
|
|
|
|
// TEST
|
|
|
|
assert.Equal(t, w.Code, 500)
|
2016-01-26 18:28:41 +00:00
|
|
|
assert.Contains(t, buffer.String(), "GET /recovery")
|
|
|
|
assert.Contains(t, buffer.String(), "Oupps, Houston, we have a problem")
|
2015-04-08 11:30:17 +00:00
|
|
|
assert.Contains(t, buffer.String(), "TestPanicInHandler")
|
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) {
|
2015-04-08 11:30:17 +00:00
|
|
|
router := New()
|
2015-05-12 13:22:13 +00:00
|
|
|
router.Use(RecoveryWithWriter(nil))
|
2015-04-08 11:30:17 +00:00
|
|
|
router.GET("/recovery", func(c *Context) {
|
2014-10-08 19:37:26 +00:00
|
|
|
c.AbortWithStatus(400)
|
2015-04-08 00:58:35 +00:00
|
|
|
panic("Oupps, Houston, we have a problem")
|
2014-08-11 10:37:35 +00:00
|
|
|
})
|
2014-08-18 17:48:48 +00:00
|
|
|
// RUN
|
2015-04-08 11:30:17 +00:00
|
|
|
w := performRequest(router, "GET", "/recovery")
|
2014-08-18 17:48:48 +00:00
|
|
|
// TEST
|
2015-04-08 11:30:17 +00:00
|
|
|
assert.Equal(t, w.Code, 500) // NOT SURE
|
2014-08-08 12:50:52 +00:00
|
|
|
}
|