2022-05-28 05:42:28 +03:00
|
|
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
2014-08-29 21:49:50 +04:00
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2014-08-08 16:50:52 +04:00
|
|
|
package gin
|
|
|
|
|
|
|
|
import (
|
2019-04-18 05:45:37 +03:00
|
|
|
"fmt"
|
2018-11-06 08:40:20 +03:00
|
|
|
"net"
|
2018-08-14 04:51:56 +03:00
|
|
|
"net/http"
|
2018-11-06 08:40:20 +03:00
|
|
|
"os"
|
2018-12-05 00:58:35 +03:00
|
|
|
"strings"
|
2018-11-06 08:40:20 +03:00
|
|
|
"syscall"
|
2014-08-08 16:50:52 +04:00
|
|
|
"testing"
|
2015-04-08 14:30:17 +03:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2014-08-08 16:50:52 +04:00
|
|
|
)
|
|
|
|
|
2019-04-18 05:45:37 +03:00
|
|
|
func TestPanicClean(t *testing.T) {
|
2023-01-20 04:51:42 +03:00
|
|
|
buffer := new(strings.Builder)
|
2019-04-18 05:45:37 +03:00
|
|
|
router := New()
|
|
|
|
password := "my-super-secret-password"
|
|
|
|
router.Use(RecoveryWithWriter(buffer))
|
|
|
|
router.GET("/recovery", func(c *Context) {
|
|
|
|
c.AbortWithStatus(http.StatusBadRequest)
|
|
|
|
panic("Oupps, Houston, we have a problem")
|
|
|
|
})
|
|
|
|
// RUN
|
2022-01-02 14:07:44 +03:00
|
|
|
w := PerformRequest(router, "GET", "/recovery",
|
2019-04-18 05:45:37 +03:00
|
|
|
header{
|
|
|
|
Key: "Host",
|
|
|
|
Value: "www.google.com",
|
|
|
|
},
|
|
|
|
header{
|
|
|
|
Key: "Authorization",
|
|
|
|
Value: fmt.Sprintf("Bearer %s", password),
|
|
|
|
},
|
|
|
|
header{
|
|
|
|
Key: "Content-Type",
|
|
|
|
Value: "application/json",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
// TEST
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
|
|
|
|
|
|
// Check the buffer does not have the secret key
|
|
|
|
assert.NotContains(t, buffer.String(), password)
|
|
|
|
}
|
|
|
|
|
2014-08-08 16:50:52 +04:00
|
|
|
// TestPanicInHandler assert that panic has been recovered.
|
|
|
|
func TestPanicInHandler(t *testing.T) {
|
2023-01-20 04:51:42 +03:00
|
|
|
buffer := new(strings.Builder)
|
2015-04-08 14:30:17 +03:00
|
|
|
router := New()
|
2015-05-12 16:22:13 +03:00
|
|
|
router.Use(RecoveryWithWriter(buffer))
|
2015-04-08 14:30:17 +03:00
|
|
|
router.GET("/recovery", func(_ *Context) {
|
2015-04-08 03:58:35 +03:00
|
|
|
panic("Oupps, Houston, we have a problem")
|
2014-08-08 16:50:52 +04:00
|
|
|
})
|
2014-08-18 21:48:48 +04:00
|
|
|
// RUN
|
2022-01-02 14:07:44 +03:00
|
|
|
w := PerformRequest(router, "GET", "/recovery")
|
2015-04-08 14:30:17 +03:00
|
|
|
// TEST
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
2018-09-23 10:15:23 +03:00
|
|
|
assert.Contains(t, buffer.String(), "panic recovered")
|
2016-01-26 21:28:41 +03:00
|
|
|
assert.Contains(t, buffer.String(), "Oupps, Houston, we have a problem")
|
2020-07-09 04:40:00 +03:00
|
|
|
assert.Contains(t, buffer.String(), t.Name())
|
2018-09-23 10:15:23 +03:00
|
|
|
assert.NotContains(t, buffer.String(), "GET /recovery")
|
|
|
|
|
|
|
|
// Debug mode prints the request
|
|
|
|
SetMode(DebugMode)
|
|
|
|
// RUN
|
2022-01-02 14:07:44 +03:00
|
|
|
w = PerformRequest(router, "GET", "/recovery")
|
2018-09-23 10:15:23 +03:00
|
|
|
// TEST
|
|
|
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
|
|
|
assert.Contains(t, buffer.String(), "GET /recovery")
|
|
|
|
|
2019-02-18 05:10:45 +03:00
|
|
|
SetMode(TestMode)
|
2014-08-11 14:37:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestPanicWithAbort assert that panic has been recovered even if context.Abort was used.
|
|
|
|
func TestPanicWithAbort(t *testing.T) {
|
2015-04-08 14:30:17 +03:00
|
|
|
router := New()
|
2015-05-12 16:22:13 +03:00
|
|
|
router.Use(RecoveryWithWriter(nil))
|
2015-04-08 14:30:17 +03:00
|
|
|
router.GET("/recovery", func(c *Context) {
|
2018-08-14 04:51:56 +03:00
|
|
|
c.AbortWithStatus(http.StatusBadRequest)
|
2015-04-08 03:58:35 +03:00
|
|
|
panic("Oupps, Houston, we have a problem")
|
2014-08-11 14:37:35 +04:00
|
|
|
})
|
2014-08-18 21:48:48 +04:00
|
|
|
// RUN
|
2022-01-02 14:07:44 +03:00
|
|
|
w := PerformRequest(router, "GET", "/recovery")
|
2014-08-18 21:48:48 +04:00
|
|
|
// TEST
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
2014-08-08 16:50:52 +04:00
|
|
|
}
|
2018-08-09 12:20:06 +03:00
|
|
|
|
|
|
|
func TestSource(t *testing.T) {
|
|
|
|
bs := source(nil, 0)
|
2021-07-27 02:59:53 +03:00
|
|
|
assert.Equal(t, dunno, bs)
|
2018-08-09 12:20:06 +03:00
|
|
|
|
|
|
|
in := [][]byte{
|
|
|
|
[]byte("Hello world."),
|
|
|
|
[]byte("Hi, gin.."),
|
|
|
|
}
|
|
|
|
bs = source(in, 10)
|
2021-07-27 02:59:53 +03:00
|
|
|
assert.Equal(t, dunno, bs)
|
2018-08-09 12:20:06 +03:00
|
|
|
|
|
|
|
bs = source(in, 1)
|
|
|
|
assert.Equal(t, []byte("Hello world."), bs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFunction(t *testing.T) {
|
|
|
|
bs := function(1)
|
2021-07-27 02:59:53 +03:00
|
|
|
assert.Equal(t, dunno, bs)
|
2018-08-09 12:20:06 +03:00
|
|
|
}
|
2018-11-06 08:40:20 +03: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-12-05 00:58:35 +03:00
|
|
|
syscall.EPIPE: "broken pipe",
|
2018-11-22 04:17:44 +03:00
|
|
|
syscall.ECONNRESET: "connection reset by peer",
|
2018-11-06 08:40:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for errno, expectMsg := range expectMsgs {
|
|
|
|
t.Run(expectMsg, func(t *testing.T) {
|
2023-01-20 04:51:42 +03:00
|
|
|
var buf strings.Builder
|
2018-11-06 08:40:20 +03:00
|
|
|
|
|
|
|
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
|
2022-01-02 14:07:44 +03:00
|
|
|
w := PerformRequest(router, "GET", "/recovery")
|
2018-11-06 08:40:20 +03:00
|
|
|
// TEST
|
|
|
|
assert.Equal(t, expectCode, w.Code)
|
2018-12-05 00:58:35 +03:00
|
|
|
assert.Contains(t, strings.ToLower(buf.String()), expectMsg)
|
2018-11-06 08:40:20 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-07-09 04:40:00 +03:00
|
|
|
|
|
|
|
func TestCustomRecoveryWithWriter(t *testing.T) {
|
2023-01-20 04:51:42 +03:00
|
|
|
errBuffer := new(strings.Builder)
|
|
|
|
buffer := new(strings.Builder)
|
2020-07-09 04:40:00 +03:00
|
|
|
router := New()
|
2022-03-21 04:43:17 +03:00
|
|
|
handleRecovery := func(c *Context, err any) {
|
2020-07-09 04:40:00 +03:00
|
|
|
errBuffer.WriteString(err.(string))
|
|
|
|
c.AbortWithStatus(http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
router.Use(CustomRecoveryWithWriter(buffer, handleRecovery))
|
|
|
|
router.GET("/recovery", func(_ *Context) {
|
|
|
|
panic("Oupps, Houston, we have a problem")
|
|
|
|
})
|
|
|
|
// RUN
|
2022-01-02 14:07:44 +03:00
|
|
|
w := PerformRequest(router, "GET", "/recovery")
|
2020-07-09 04:40:00 +03:00
|
|
|
// TEST
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
|
|
assert.Contains(t, buffer.String(), "panic recovered")
|
|
|
|
assert.Contains(t, buffer.String(), "Oupps, Houston, we have a problem")
|
|
|
|
assert.Contains(t, buffer.String(), t.Name())
|
|
|
|
assert.NotContains(t, buffer.String(), "GET /recovery")
|
|
|
|
|
|
|
|
// Debug mode prints the request
|
|
|
|
SetMode(DebugMode)
|
|
|
|
// RUN
|
2022-01-02 14:07:44 +03:00
|
|
|
w = PerformRequest(router, "GET", "/recovery")
|
2020-07-09 04:40:00 +03:00
|
|
|
// TEST
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
|
|
assert.Contains(t, buffer.String(), "GET /recovery")
|
|
|
|
|
|
|
|
assert.Equal(t, strings.Repeat("Oupps, Houston, we have a problem", 2), errBuffer.String())
|
|
|
|
|
|
|
|
SetMode(TestMode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCustomRecovery(t *testing.T) {
|
2023-01-20 04:51:42 +03:00
|
|
|
errBuffer := new(strings.Builder)
|
|
|
|
buffer := new(strings.Builder)
|
2020-07-09 04:40:00 +03:00
|
|
|
router := New()
|
|
|
|
DefaultErrorWriter = buffer
|
2022-03-21 04:43:17 +03:00
|
|
|
handleRecovery := func(c *Context, err any) {
|
2020-07-09 04:40:00 +03:00
|
|
|
errBuffer.WriteString(err.(string))
|
|
|
|
c.AbortWithStatus(http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
router.Use(CustomRecovery(handleRecovery))
|
|
|
|
router.GET("/recovery", func(_ *Context) {
|
|
|
|
panic("Oupps, Houston, we have a problem")
|
|
|
|
})
|
|
|
|
// RUN
|
2022-01-02 14:07:44 +03:00
|
|
|
w := PerformRequest(router, "GET", "/recovery")
|
2020-07-09 04:40:00 +03:00
|
|
|
// TEST
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
|
|
assert.Contains(t, buffer.String(), "panic recovered")
|
|
|
|
assert.Contains(t, buffer.String(), "Oupps, Houston, we have a problem")
|
|
|
|
assert.Contains(t, buffer.String(), t.Name())
|
|
|
|
assert.NotContains(t, buffer.String(), "GET /recovery")
|
|
|
|
|
|
|
|
// Debug mode prints the request
|
|
|
|
SetMode(DebugMode)
|
|
|
|
// RUN
|
2022-01-02 14:07:44 +03:00
|
|
|
w = PerformRequest(router, "GET", "/recovery")
|
2020-07-09 04:40:00 +03:00
|
|
|
// TEST
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
|
|
assert.Contains(t, buffer.String(), "GET /recovery")
|
|
|
|
|
|
|
|
assert.Equal(t, strings.Repeat("Oupps, Houston, we have a problem", 2), errBuffer.String())
|
|
|
|
|
|
|
|
SetMode(TestMode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecoveryWithWriterWithCustomRecovery(t *testing.T) {
|
2023-01-20 04:51:42 +03:00
|
|
|
errBuffer := new(strings.Builder)
|
|
|
|
buffer := new(strings.Builder)
|
2020-07-09 04:40:00 +03:00
|
|
|
router := New()
|
|
|
|
DefaultErrorWriter = buffer
|
2022-03-21 04:43:17 +03:00
|
|
|
handleRecovery := func(c *Context, err any) {
|
2020-07-09 04:40:00 +03:00
|
|
|
errBuffer.WriteString(err.(string))
|
|
|
|
c.AbortWithStatus(http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
router.Use(RecoveryWithWriter(DefaultErrorWriter, handleRecovery))
|
|
|
|
router.GET("/recovery", func(_ *Context) {
|
|
|
|
panic("Oupps, Houston, we have a problem")
|
|
|
|
})
|
|
|
|
// RUN
|
2022-01-02 14:07:44 +03:00
|
|
|
w := PerformRequest(router, "GET", "/recovery")
|
2020-07-09 04:40:00 +03:00
|
|
|
// TEST
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
|
|
assert.Contains(t, buffer.String(), "panic recovered")
|
|
|
|
assert.Contains(t, buffer.String(), "Oupps, Houston, we have a problem")
|
|
|
|
assert.Contains(t, buffer.String(), t.Name())
|
|
|
|
assert.NotContains(t, buffer.String(), "GET /recovery")
|
|
|
|
|
|
|
|
// Debug mode prints the request
|
|
|
|
SetMode(DebugMode)
|
|
|
|
// RUN
|
2022-01-02 14:07:44 +03:00
|
|
|
w = PerformRequest(router, "GET", "/recovery")
|
2020-07-09 04:40:00 +03:00
|
|
|
// TEST
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
|
|
assert.Contains(t, buffer.String(), "GET /recovery")
|
|
|
|
|
|
|
|
assert.Equal(t, strings.Repeat("Oupps, Houston, we have a problem", 2), errBuffer.String())
|
|
|
|
|
|
|
|
SetMode(TestMode)
|
|
|
|
}
|