-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
recovery_test.go
75 lines (58 loc) · 1.99 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2021 Flamego. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package flamego
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRecovery(t *testing.T) {
t.Run("recovery from panic", func(t *testing.T) {
f := NewWithLogger(&bytes.Buffer{})
f.Use(Recovery())
f.Use(func() { panic("here is a panic!") })
f.Get("/", func() {})
resp := httptest.NewRecorder()
req, err := http.NewRequest(http.MethodGet, "/", nil)
assert.Nil(t, err)
f.ServeHTTP(resp, req)
assert.Equal(t, http.StatusInternalServerError, resp.Code)
assert.Equal(t, "text/html", resp.Header().Get("Content-Type"))
assert.Contains(t, resp.Body.String(), "PANIC")
})
t.Run("recovery from panic in non-development mode", func(t *testing.T) {
SetEnv(EnvTypeProd)
defer SetEnv(EnvTypeDev)
f := NewWithLogger(&bytes.Buffer{})
f.Use(Recovery())
f.Use(func() { panic("here is a panic!") })
f.Get("/", func() {})
resp := httptest.NewRecorder()
req, err := http.NewRequest(http.MethodGet, "/", nil)
assert.Nil(t, err)
f.ServeHTTP(resp, req)
assert.Equal(t, http.StatusInternalServerError, resp.Code)
assert.Equal(t, "text/plain", resp.Header().Get("Content-Type"))
assert.Equal(t, http.StatusText(http.StatusInternalServerError), resp.Body.String())
})
t.Run("Revocery panic to another ResponseWriter", func(t *testing.T) {
resp := httptest.NewRecorder()
resp2 := httptest.NewRecorder()
f := NewWithLogger(&bytes.Buffer{})
f.Use(Recovery())
f.Use(func(c Context) {
c.MapTo(resp2, (*http.ResponseWriter)(nil))
panic("here is a panic!")
})
f.Get("/", func() {})
req, err := http.NewRequest(http.MethodGet, "/", nil)
assert.Nil(t, err)
f.ServeHTTP(resp, req)
assert.Equal(t, http.StatusInternalServerError, resp2.Code)
assert.Equal(t, "text/html", resp2.Header().Get("Content-Type"))
assert.Contains(t, resp2.Body.String(), "PANIC")
})
}