-
Notifications
You must be signed in to change notification settings - Fork 124
/
web_test.go
124 lines (101 loc) · 2.9 KB
/
web_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
package web
import (
"fmt"
"net/http"
"net/http/httptest"
"runtime"
"strings"
"testing"
)
//
// This file will contain helpers and general things the rest of the suite needs
//
type nullPanicReporter struct{}
func (l nullPanicReporter) Panic(url string, err interface{}, stack string) {
// no op
}
func init() {
// This disables printing panics to stderr during testing, because that is very noisy,
// and we purposefully test some panics.
PanicHandler = nullPanicReporter{}
}
// Return's the caller's caller info.
func callerInfo() string {
_, file, line, ok := runtime.Caller(2)
if !ok {
return ""
}
parts := strings.Split(file, "/")
file = parts[len(parts)-1]
return fmt.Sprintf("%s:%d", file, line)
}
// Make a testing request
func newTestRequest(method, path string) (*httptest.ResponseRecorder, *http.Request) {
request, _ := http.NewRequest(method, path, nil)
recorder := httptest.NewRecorder()
return recorder, request
}
func assertResponse(t *testing.T, rr *httptest.ResponseRecorder, body string, code int) {
if gotBody := strings.TrimSpace(string(rr.Body.Bytes())); body != gotBody {
t.Errorf("assertResponse: expected body to be %s but got %s. (caller: %s)", body, gotBody, callerInfo())
}
if code != rr.Code {
t.Errorf("assertResponse: expected code to be %d but got %d. (caller: %s)", code, rr.Code, callerInfo())
}
}
//
// Some default contexts and possible error handlers / actions
//
type Context struct{}
type AdminContext struct {
*Context
}
type APIContext struct {
*Context
}
type SiteContext struct {
*Context
}
type TicketsContext struct {
*AdminContext
}
func (c *Context) ErrorMiddleware(w ResponseWriter, r *Request, next NextMiddlewareFunc) {
var x, y int
fmt.Fprintln(w, x/y)
}
func (c *Context) ErrorHandler(w ResponseWriter, r *Request, err interface{}) {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "My Error")
}
func (c *Context) ErrorHandlerSecondary(w ResponseWriter, r *Request, err interface{}) {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "My Secondary Error")
}
func (c *Context) ErrorAction(w ResponseWriter, r *Request) {
var x, y int
fmt.Fprintln(w, x/y)
}
func (c *AdminContext) ErrorMiddleware(w ResponseWriter, r *Request, next NextMiddlewareFunc) {
var x, y int
fmt.Fprintln(w, x/y)
}
func (c *AdminContext) ErrorHandler(w ResponseWriter, r *Request, err interface{}) {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Admin Error")
}
func (c *AdminContext) ErrorAction(w ResponseWriter, r *Request) {
var x, y int
fmt.Fprintln(w, x/y)
}
func (c *APIContext) ErrorHandler(w ResponseWriter, r *Request, err interface{}) {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Api Error")
}
func (c *APIContext) ErrorAction(w ResponseWriter, r *Request) {
var x, y int
fmt.Fprintln(w, x/y)
}
func (c *TicketsContext) ErrorAction(w ResponseWriter, r *Request) {
var x, y int
fmt.Fprintln(w, x/y)
}