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.
|
|
|
|
|
2018-11-06 05:40:20 +00:00
|
|
|
// +build go1.7
|
|
|
|
|
2014-08-08 12:50:52 +00:00
|
|
|
package gin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-11-06 05:40:20 +00:00
|
|
|
"net"
|
2018-08-14 01:51:56 +00:00
|
|
|
"net/http"
|
2018-11-06 05:40:20 +00:00
|
|
|
"os"
|
|
|
|
"syscall"
|
2014-08-08 12:50:52 +00:00
|
|
|
"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
|
2018-08-14 01:51:56 +00:00
|
|
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
2018-09-23 07:15:23 +00:00
|
|
|
assert.Contains(t, buffer.String(), "panic recovered")
|
2016-01-26 18:28:41 +00:00
|
|
|
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")
|
2018-09-23 07:15:23 +00:00
|
|
|
assert.NotContains(t, buffer.String(), "GET /recovery")
|
|
|
|
|
|
|
|
// Debug mode prints the request
|
|
|
|
SetMode(DebugMode)
|
|
|
|
// RUN
|
|
|
|
w = performRequest(router, "GET", "/recovery")
|
|
|
|
// TEST
|
|
|
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
|
|
|
assert.Contains(t, buffer.String(), "GET /recovery")
|
|
|
|
|
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) {
|
2018-08-14 01:51:56 +00:00
|
|
|
c.AbortWithStatus(http.StatusBadRequest)
|
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
|
2018-08-14 01:51:56 +00:00
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
2014-08-08 12:50:52 +00:00
|
|
|
}
|
2018-08-09 09:20:06 +00:00
|
|
|
|
|
|
|
func TestSource(t *testing.T) {
|
|
|
|
bs := source(nil, 0)
|
|
|
|
assert.Equal(t, []byte("???"), bs)
|
|
|
|
|
|
|
|
in := [][]byte{
|
|
|
|
[]byte("Hello world."),
|
|
|
|
[]byte("Hi, gin.."),
|
|
|
|
}
|
|
|
|
bs = source(in, 10)
|
|
|
|
assert.Equal(t, []byte("???"), bs)
|
|
|
|
|
|
|
|
bs = source(in, 1)
|
|
|
|
assert.Equal(t, []byte("Hello world."), bs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFunction(t *testing.T) {
|
|
|
|
bs := function(1)
|
|
|
|
assert.Equal(t, []byte("???"), bs)
|
|
|
|
}
|
2018-11-06 05:40:20 +00:00
|
|
|
|
|
|
|
// TestPanicWithBrokenPipe asserts that recovery specifically handles
|
|
|
|
// writing responses to broken pipes
|
|
|
|
func TestPanicWithBrokenPipe(t *testing.T) {
|
|
|
|
const expectCode = 204
|
|
|
|
|
|
|
|
expectMsgs := map[syscall.Errno]string{
|
2018-11-22 01:17:44 +00:00
|
|
|
syscall.EPIPE: "Broken pipe",
|
|
|
|
syscall.ECONNRESET: "connection reset by peer",
|
2018-11-06 05:40:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for errno, expectMsg := range expectMsgs {
|
|
|
|
t.Run(expectMsg, func(t *testing.T) {
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
router := New()
|
|
|
|
router.Use(RecoveryWithWriter(&buf))
|
|
|
|
router.GET("/recovery", func(c *Context) {
|
|
|
|
// Start writing response
|
|
|
|
c.Header("X-Test", "Value")
|
|
|
|
c.Status(expectCode)
|
|
|
|
|
|
|
|
// Oops. Client connection closed
|
|
|
|
e := &net.OpError{Err: &os.SyscallError{Err: errno}}
|
|
|
|
panic(e)
|
|
|
|
})
|
|
|
|
// RUN
|
|
|
|
w := performRequest(router, "GET", "/recovery")
|
|
|
|
// TEST
|
|
|
|
assert.Equal(t, expectCode, w.Code)
|
|
|
|
assert.Contains(t, buf.String(), expectMsg)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|