forked from gin-gonic/gin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recovery_test.go
56 lines (46 loc) · 1.22 KB
/
recovery_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// 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"
"log"
"os"
"testing"
)
// TestPanicInHandler assert that panic has been recovered.
func TestPanicInHandler(t *testing.T) {
// SETUP
log.SetOutput(bytes.NewBuffer(nil)) // Disable panic logs for testing
r := New()
r.Use(Recovery())
r.GET("/recovery", func(_ *Context) {
panic("Oupps, Houston, we have a problem")
})
// RUN
w := PerformRequest(r, "GET", "/recovery")
// restore logging
log.SetOutput(os.Stderr)
if w.Code != 500 {
t.Errorf("Response code should be Internal Server Error, was: %s", w.Code)
}
}
// TestPanicWithAbort assert that panic has been recovered even if context.Abort was used.
func TestPanicWithAbort(t *testing.T) {
// SETUP
log.SetOutput(bytes.NewBuffer(nil))
r := New()
r.Use(Recovery())
r.GET("/recovery", func(c *Context) {
c.AbortWithStatus(400)
panic("Oupps, Houston, we have a problem")
})
// RUN
w := PerformRequest(r, "GET", "/recovery")
// restore logging
log.SetOutput(os.Stderr)
// TEST
if w.Code != 500 {
t.Errorf("Response code should be Bad request, was: %s", w.Code)
}
}