-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecoverer_test.go
101 lines (83 loc) · 2.71 KB
/
recoverer_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (c) Liam Stanley <[email protected]>. All rights reserved. Use
// of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package recoverer
import (
"bytes"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
const panicError = "this should never actually panic"
func getTestHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
panic(panicError)
}
}
func TestNew(t *testing.T) {
at := assert.New(t)
tests := []struct {
description string
url string
acceptHeaders string
options Options
wantBodyGlob string
wantTypeGlob string
}{
{
description: "html show", url: "/",
acceptHeaders: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
options: Options{Show: true, Simple: false},
wantBodyGlob: "(?s)<html .*</html>",
},
{
description: "html show (url 2)", url: "/another/url",
acceptHeaders: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
options: Options{Show: true, Simple: false},
wantBodyGlob: "(?s)<html .*</html>",
},
{
description: "html show, but request plaintext", url: "/another/url",
acceptHeaders: "text/plain",
options: Options{Show: true, Simple: false},
wantBodyGlob: "(?s)panic: .*in: .*stack at time of panic:",
},
{
description: "simple show", url: "/",
acceptHeaders: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
options: Options{Show: true, Simple: true},
wantBodyGlob: "(?s)panic: .*in: .*stack at time of panic:",
},
{
description: "hide", url: "/",
acceptHeaders: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
options: Options{Show: false},
wantBodyGlob: http.StatusText(http.StatusInternalServerError),
},
}
var logger bytes.Buffer
for _, tt := range tests {
// Set our custom logger.
logger.Reset()
tt.options.Logger = &logger
server := httptest.NewServer(New(tt.options)(getTestHandler()))
client := &http.Client{}
req, err := http.NewRequest("GET", server.URL+tt.url, nil)
at.NoError(err, tt.description)
req.Header.Set("Accept", tt.acceptHeaders)
resp, err := client.Do(req)
at.NoError(err, tt.description)
// Status code should always be an internal service error.
at.Equal(resp.StatusCode, 500, tt.description)
b, err := ioutil.ReadAll(resp.Body)
at.NoError(err, tt.description)
at.Regexp(tt.wantBodyGlob, string(b), tt.description)
// Verify that the logged output is actually logged.
line, err := logger.ReadString(0xa)
at.NoError(err, tt.description)
at.Equal("panic: "+panicError+"\n", line, tt.description)
server.Close()
}
}