-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware_test.go
94 lines (74 loc) · 2.37 KB
/
middleware_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
package registry
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/justinas/alice"
)
// PanicStore only ever panics, used to make sure that responses to users
// still go out as application/json with appropriate errors, regardless of
// whether the backend store is causing fits. This can't perfectly handle the
// near heat death of the universe, but you'll at least have your towel.
type PanicStore struct {
}
func (s PanicStore) GetTemplate(name string) (Template, error) {
panic("NO TEMPLATE FOR YOU")
}
func (s PanicStore) RegisterTemplate(tmpl Template) (Template, error) {
panic("NO REGISTERING TEMPLATES FOR YOU")
}
func (s PanicStore) ListTemplates() ([]Template, error) {
panic("NO LISTING TEMPLATES FOR YOU")
}
func (s PanicStore) UpdateTemplate(tmpl Template) (Template, error) {
panic("NO UPDATING TEMPLATES FOR YOU")
}
func (s PanicStore) DeleteTemplate(name string) (Template, error) {
panic("NO DELETING TEMPLATES FOR YOU")
}
func (s PanicStore) Authorize(inner http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
panic("NO AUTHORIZATION FOR YOU")
})
}
type hokeyRequest struct {
Request *http.Request
}
func newHokeyRequest(method string, urlStr string, body io.Reader) *http.Request {
req, _ := http.NewRequest(method, urlStr, body)
return req
}
func TestPanicRecovery(t *testing.T) {
registry := Registry{
Store: PanicStore{},
AuthStore: PanicStore{},
}
handlerFuncs := []http.HandlerFunc{
registry.TemplateCreate,
registry.TemplateUpdate,
registry.TemplateShow,
registry.TemplateIndex,
}
middleware := alice.New(recoverHandler)
for _, handlerFunc := range handlerFuncs {
req, _ := http.NewRequest("GET", "", nil)
w := httptest.NewRecorder()
middleware.Then(handlerFunc).ServeHTTP(w, req)
matchedAPIError(t, DontPanicError, w)
}
// Panics should be recovered from with the DefaultRouter
router := NewDefaultRouter(registry)
requests := []*http.Request{
newHokeyRequest("GET", "/templates", strings.NewReader("{}")),
newHokeyRequest("POST", "/templates", strings.NewReader("{}")),
newHokeyRequest("GET", "/templates/env", strings.NewReader("{}")),
newHokeyRequest("PUT", "/templates/env", strings.NewReader("{}")),
}
for _, req := range requests {
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
matchedAPIError(t, DontPanicError, w)
}
}