More unit tests
This commit is contained in:
parent
ac0ad2fed8
commit
54b3decc21
@ -18,21 +18,3 @@ func TestIsDebugging(t *testing.T) {
|
|||||||
SetMode(TestMode)
|
SetMode(TestMode)
|
||||||
assert.False(t, IsDebugging())
|
assert.False(t, IsDebugging())
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
|
||||||
// func TestDebugPrint(t *testing.T) {
|
|
||||||
// buffer := bytes.NewBufferString("")
|
|
||||||
// debugLogger.
|
|
||||||
// log.SetOutput(buffer)
|
|
||||||
|
|
||||||
// SetMode(ReleaseMode)
|
|
||||||
// debugPrint("This is a example")
|
|
||||||
// assert.Equal(t, buffer.Len(), 0)
|
|
||||||
|
|
||||||
// SetMode(DebugMode)
|
|
||||||
// debugPrint("This is %s", "a example")
|
|
||||||
// assert.Equal(t, buffer.String(), "[GIN-debug] This is a example")
|
|
||||||
|
|
||||||
// SetMode(TestMode)
|
|
||||||
// log.SetOutput(os.Stdout)
|
|
||||||
// }
|
|
||||||
|
43
recovery.go
43
recovery.go
@ -7,9 +7,9 @@ package gin
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -20,6 +20,31 @@ var (
|
|||||||
slash = []byte("/")
|
slash = []byte("/")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Recovery returns a middleware that recovers from any panics and writes a 500 if there was one.
|
||||||
|
// While Gin is in development mode, Recovery will also output the panic as HTML.
|
||||||
|
func Recovery() HandlerFunc {
|
||||||
|
return RecoveryWithFile(DefaultWriter)
|
||||||
|
}
|
||||||
|
|
||||||
|
func RecoveryWithFile(out io.Writer) HandlerFunc {
|
||||||
|
var logger *log.Logger
|
||||||
|
if out != nil {
|
||||||
|
logger = log.New(out, "", log.LstdFlags)
|
||||||
|
}
|
||||||
|
return func(c *Context) {
|
||||||
|
defer func() {
|
||||||
|
if err := recover(); err != nil {
|
||||||
|
if logger != nil {
|
||||||
|
stack := stack(3)
|
||||||
|
logger.Printf("Gin Panic Recover!! -> %s\n%s\n", err, stack)
|
||||||
|
}
|
||||||
|
c.AbortWithStatus(500)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// stack returns a nicely formated stack frame, skipping skip frames
|
// stack returns a nicely formated stack frame, skipping skip frames
|
||||||
func stack(skip int) []byte {
|
func stack(skip int) []byte {
|
||||||
buf := new(bytes.Buffer) // the returned data
|
buf := new(bytes.Buffer) // the returned data
|
||||||
@ -80,19 +105,3 @@ func function(pc uintptr) []byte {
|
|||||||
name = bytes.Replace(name, centerDot, dot, -1)
|
name = bytes.Replace(name, centerDot, dot, -1)
|
||||||
return name
|
return name
|
||||||
}
|
}
|
||||||
|
|
||||||
// Recovery returns a middleware that recovers from any panics and writes a 500 if there was one.
|
|
||||||
// While Gin is in development mode, Recovery will also output the panic as HTML.
|
|
||||||
func Recovery() HandlerFunc {
|
|
||||||
return func(c *Context) {
|
|
||||||
defer func() {
|
|
||||||
if err := recover(); err != nil {
|
|
||||||
stack := stack(3)
|
|
||||||
log.Printf("PANIC: %s\n%s", err, stack)
|
|
||||||
c.Writer.WriteHeader(http.StatusInternalServerError)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
c.Next()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -6,51 +6,37 @@ package gin
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestPanicInHandler assert that panic has been recovered.
|
// TestPanicInHandler assert that panic has been recovered.
|
||||||
func TestPanicInHandler(t *testing.T) {
|
func TestPanicInHandler(t *testing.T) {
|
||||||
// SETUP
|
buffer := new(bytes.Buffer)
|
||||||
log.SetOutput(bytes.NewBuffer(nil)) // Disable panic logs for testing
|
router := New()
|
||||||
r := New()
|
router.Use(RecoveryWithFile(buffer))
|
||||||
r.Use(Recovery())
|
router.GET("/recovery", func(_ *Context) {
|
||||||
r.GET("/recovery", func(_ *Context) {
|
|
||||||
panic("Oupps, Houston, we have a problem")
|
panic("Oupps, Houston, we have a problem")
|
||||||
})
|
})
|
||||||
|
|
||||||
// RUN
|
// RUN
|
||||||
w := performRequest(r, "GET", "/recovery")
|
w := performRequest(router, "GET", "/recovery")
|
||||||
|
// TEST
|
||||||
// restore logging
|
assert.Equal(t, w.Code, 500)
|
||||||
log.SetOutput(os.Stderr)
|
assert.Contains(t, buffer.String(), "Gin Panic Recover!! -> Oupps, Houston, we have a problem")
|
||||||
|
assert.Contains(t, buffer.String(), "TestPanicInHandler")
|
||||||
if w.Code != 500 {
|
|
||||||
t.Errorf("Response code should be Internal Server Error, was: %d", w.Code)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestPanicWithAbort assert that panic has been recovered even if context.Abort was used.
|
// TestPanicWithAbort assert that panic has been recovered even if context.Abort was used.
|
||||||
func TestPanicWithAbort(t *testing.T) {
|
func TestPanicWithAbort(t *testing.T) {
|
||||||
// SETUP
|
router := New()
|
||||||
log.SetOutput(bytes.NewBuffer(nil))
|
router.Use(RecoveryWithFile(nil))
|
||||||
r := New()
|
router.GET("/recovery", func(c *Context) {
|
||||||
r.Use(Recovery())
|
|
||||||
r.GET("/recovery", func(c *Context) {
|
|
||||||
c.AbortWithStatus(400)
|
c.AbortWithStatus(400)
|
||||||
panic("Oupps, Houston, we have a problem")
|
panic("Oupps, Houston, we have a problem")
|
||||||
})
|
})
|
||||||
|
|
||||||
// RUN
|
// RUN
|
||||||
w := performRequest(r, "GET", "/recovery")
|
w := performRequest(router, "GET", "/recovery")
|
||||||
|
|
||||||
// restore logging
|
|
||||||
log.SetOutput(os.Stderr)
|
|
||||||
|
|
||||||
// TEST
|
// TEST
|
||||||
if w.Code != 500 {
|
assert.Equal(t, w.Code, 500) // NOT SURE
|
||||||
t.Errorf("Response code should be Bad request, was: %d", w.Code)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user