-
Notifications
You must be signed in to change notification settings - Fork 124
/
error_test.go
172 lines (137 loc) · 5.14 KB
/
error_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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package web
import (
"bytes"
"fmt"
"github.com/stretchr/testify/assert"
"log"
"net/http"
"strings"
"testing"
)
func ErrorHandlerWithNoContext(w ResponseWriter, r *Request, err interface{}) {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Contextless Error")
}
func TestNoErrorHandler(t *testing.T) {
router := New(Context{})
router.Get("/action", (*Context).ErrorAction)
admin := router.Subrouter(AdminContext{}, "/admin")
admin.Get("/action", (*AdminContext).ErrorAction)
rw, req := newTestRequest("GET", "/action")
router.ServeHTTP(rw, req)
assertResponse(t, rw, "Application Error", http.StatusInternalServerError)
rw, req = newTestRequest("GET", "/admin/action")
router.ServeHTTP(rw, req)
assertResponse(t, rw, "Application Error", http.StatusInternalServerError)
}
func TestHandlerOnRoot(t *testing.T) {
router := New(Context{})
router.Error((*Context).ErrorHandler)
router.Get("/action", (*Context).ErrorAction)
admin := router.Subrouter(AdminContext{}, "/admin")
admin.Get("/action", (*AdminContext).ErrorAction)
rw, req := newTestRequest("GET", "/action")
router.ServeHTTP(rw, req)
assertResponse(t, rw, "My Error", http.StatusInternalServerError)
rw, req = newTestRequest("GET", "/admin/action")
router.ServeHTTP(rw, req)
assertResponse(t, rw, "My Error", http.StatusInternalServerError)
}
func TestContextlessError(t *testing.T) {
router := New(Context{})
router.Error(ErrorHandlerWithNoContext)
router.Get("/action", (*Context).ErrorAction)
admin := router.Subrouter(AdminContext{}, "/admin")
admin.Get("/action", (*AdminContext).ErrorAction)
rw, req := newTestRequest("GET", "/action")
router.ServeHTTP(rw, req)
assert.Equal(t, http.StatusInternalServerError, rw.Code)
assertResponse(t, rw, "Contextless Error", http.StatusInternalServerError)
rw, req = newTestRequest("GET", "/admin/action")
router.ServeHTTP(rw, req)
assertResponse(t, rw, "Contextless Error", http.StatusInternalServerError)
}
func TestMultipleErrorHandlers(t *testing.T) {
router := New(Context{})
router.Error((*Context).ErrorHandler)
router.Get("/action", (*Context).ErrorAction)
admin := router.Subrouter(AdminContext{}, "/admin")
admin.Error((*AdminContext).ErrorHandler)
admin.Get("/action", (*AdminContext).ErrorAction)
rw, req := newTestRequest("GET", "/action")
router.ServeHTTP(rw, req)
assertResponse(t, rw, "My Error", http.StatusInternalServerError)
rw, req = newTestRequest("GET", "/admin/action")
router.ServeHTTP(rw, req)
assertResponse(t, rw, "Admin Error", http.StatusInternalServerError)
}
func TestMultipleErrorHandlers2(t *testing.T) {
router := New(Context{})
router.Get("/action", (*Context).ErrorAction)
admin := router.Subrouter(AdminContext{}, "/admin")
admin.Error((*AdminContext).ErrorHandler)
admin.Get("/action", (*AdminContext).ErrorAction)
api := router.Subrouter(APIContext{}, "/api")
api.Error((*APIContext).ErrorHandler)
api.Get("/action", (*APIContext).ErrorAction)
rw, req := newTestRequest("GET", "/action")
router.ServeHTTP(rw, req)
assertResponse(t, rw, "Application Error", http.StatusInternalServerError)
rw, req = newTestRequest("GET", "/admin/action")
router.ServeHTTP(rw, req)
assertResponse(t, rw, "Admin Error", http.StatusInternalServerError)
rw, req = newTestRequest("GET", "/api/action")
router.ServeHTTP(rw, req)
assertResponse(t, rw, "Api Error", http.StatusInternalServerError)
}
func TestRootMiddlewarePanic(t *testing.T) {
router := New(Context{})
router.Middleware((*Context).ErrorMiddleware)
router.Error((*Context).ErrorHandler)
admin := router.Subrouter(AdminContext{}, "/admin")
admin.Error((*AdminContext).ErrorHandler)
admin.Get("/action", (*AdminContext).ErrorAction)
rw, req := newTestRequest("GET", "/admin/action")
router.ServeHTTP(rw, req)
assertResponse(t, rw, "My Error", 500)
}
func TestNonRootMiddlewarePanic(t *testing.T) {
router := New(Context{})
router.Error((*Context).ErrorHandler)
admin := router.Subrouter(AdminContext{}, "/admin")
admin.Middleware((*AdminContext).ErrorMiddleware)
admin.Error((*AdminContext).ErrorHandler)
admin.Get("/action", (*AdminContext).ErrorAction)
rw, req := newTestRequest("GET", "/admin/action")
router.ServeHTTP(rw, req)
assertResponse(t, rw, "Admin Error", 500)
}
func TestPanicLogging(t *testing.T) {
var buf bytes.Buffer
// Set the panichandler to our own time, then set it back after the test is done:
oldHandler := PanicHandler
PanicHandler = logPanicReporter{
log: log.New(&buf, "", 0),
}
defer func() {
PanicHandler = oldHandler
}()
router := New(Context{})
router.Get("/action", (*Context).ErrorAction)
rw, req := newTestRequest("GET", "/action")
router.ServeHTTP(rw, req)
assertResponse(t, rw, "Application Error", 500)
if !strings.HasPrefix(buf.String(), "PANIC") {
t.Error("Expected to have our PanicHandler be logged to.")
}
}
func TestConsistentContext(t *testing.T) {
router := New(Context{})
router.Error((*Context).ErrorHandler)
admin := router.Subrouter(Context{}, "/admin")
admin.Error((*Context).ErrorHandlerSecondary)
admin.Get("/foo", (*Context).ErrorAction)
rw, req := newTestRequest("GET", "/admin/foo")
router.ServeHTTP(rw, req)
assertResponse(t, rw, "My Secondary Error", 500)
}