forked from gocraft/web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_test.go
105 lines (84 loc) · 2.43 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
package web_test
import (
"fmt"
"github.com/gocraft/web"
. "launchpad.net/gocheck"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
//
// This file is the "driver" for the test suite. We're using gocheck.
// This file will contain helpers and general things the rest of the suite needs
//
//
// gocheck: hook into "go test"
//
func Test(t *testing.T) { TestingT(t) }
// 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(c *C, rr *httptest.ResponseRecorder, body string, code int) {
c.Assert(strings.TrimSpace(string(rr.Body.Bytes())), Equals, body)
c.Assert(rr.Code, Equals, code)
}
//
// 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 web.ResponseWriter, r *web.Request, next web.NextMiddlewareFunc) {
var x, y int
fmt.Fprintln(w, x/y)
}
func (c *Context) ErrorHandler(w web.ResponseWriter, r *web.Request, err interface{}) {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "My Error")
}
func (c *Context) ErrorHandlerSecondary(w web.ResponseWriter, r *web.Request, err interface{}) {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "My Secondary Error")
}
func (c *Context) ErrorAction(w web.ResponseWriter, r *web.Request) {
var x, y int
fmt.Fprintln(w, x/y)
}
func (c *AdminContext) ErrorMiddleware(w web.ResponseWriter, r *web.Request, next web.NextMiddlewareFunc) {
var x, y int
fmt.Fprintln(w, x/y)
}
func (c *AdminContext) ErrorHandler(w web.ResponseWriter, r *web.Request, err interface{}) {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Admin Error")
}
func (c *AdminContext) ErrorAction(w web.ResponseWriter, r *web.Request) {
var x, y int
fmt.Fprintln(w, x/y)
}
func (c *ApiContext) ErrorHandler(w web.ResponseWriter, r *web.Request, err interface{}) {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Api Error")
}
func (c *ApiContext) ErrorAction(w web.ResponseWriter, r *web.Request) {
var x, y int
fmt.Fprintln(w, x/y)
}
func (c *TicketsContext) ErrorAction(w web.ResponseWriter, r *web.Request) {
var x, y int
fmt.Fprintln(w, x/y)
}