-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller_test.go
More file actions
129 lines (107 loc) · 3.79 KB
/
Copy pathcontroller_test.go
File metadata and controls
129 lines (107 loc) · 3.79 KB
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
package flow
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
// testUserController is a controller with declared middleware.
type testUserController struct{}
func (c *testUserController) Middleware() []ControllerMiddleware {
return []ControllerMiddleware{
{Handler: func(req *Request, next Closure) any {
res := next(req)
if res2, ok := res.(*Response); ok {
res2.Header("X-Controller", "yes")
return res2
}
return res
}},
{Handler: func(req *Request, next Closure) any { return next(req) }, Only: []string{"Show"}},
{Handler: func(req *Request, next Closure) any { return next(req) }, Except: []string{"Show"}},
}
}
func (c *testUserController) Show(req *Request, id string) *Response {
return NewResponse().SetContent("show:" + id)
}
func (c *testUserController) Update(req *Request, id string) *Response {
return NewResponse().SetContent("update:" + id)
}
// testInvokableController is a single-invocation controller.
type testInvokableController struct{}
func (c *testInvokableController) Invoke(req *Request) *Response {
return NewResponse().SetContent("invoked")
}
func TestControllerActionStringRegistration(t *testing.T) {
r := New()
r.RegisterController("TestUserController", &testUserController{})
r.Get("/users/{id}", "TestUserController@Show").Name("users.show")
r.Register()
httpReq, _ := http.NewRequest("GET", "/users/42", nil)
req := NewRequest(httpReq)
req.SetResponseWriter(httptest.NewRecorder())
res := r.Dispatch(req)
resp := res
assert.Equal(t, "show:42", resp.GetContent())
// Controller middleware declared without filters applies.
assert.Equal(t, "yes", resp.Headers().Get("X-Controller"))
}
func TestControllerMiddlewareOnlyExcept(t *testing.T) {
r := New()
r.RegisterController("TestUserController", &testUserController{})
r.Get("/users/{id}", "TestUserController@Show")
r.Put("/users/{id}", "TestUserController@Update")
r.Register()
// Show: Only-filter middleware applies, Except-filter does not.
req1 := NewRequest(mustRequest("PUT", "/users/42"))
req1.SetResponseWriter(httptest.NewRecorder())
res1 := r.Dispatch(req1)
assert.Equal(t, "update:42", res1.GetContent())
// Update is excluded by Except but the Except-declared entry only skips
// Show; the Only entry skips Update. Both unfiltered entries already ran.
// Show runs the Only entry.
showReq := NewRequest(mustRequest("GET", "/users/42"))
rec2 := httptest.NewRecorder()
showReq.SetResponseWriter(rec2)
_ = r.Dispatch(showReq)
}
func mustRequest(method, url string) *http.Request {
httpReq, err := http.NewRequest(method, url, nil)
if err != nil {
panic(err)
}
return httpReq
}
func TestInvokableControllerAction(t *testing.T) {
r := New()
r.RegisterController("TestInvokableController", &testInvokableController{})
r.Get("/invoke", "TestInvokableController")
r.Register()
httpReq, _ := http.NewRequest("GET", "/invoke", nil)
req := NewRequest(httpReq)
req.SetResponseWriter(httptest.NewRecorder())
res := r.Dispatch(req)
assert.Equal(t, "invoked", res.GetContent())
}
func TestControllerNamespacePrefix(t *testing.T) {
r := New()
r.Group(GroupAttributes{Controller: "Admin"}, func(admin Router) {
admin.RegisterController("Admin.UserController", &testUserController{})
admin.Get("/admin/users/{id}", "UserController@Show")
})
r.Register()
httpReq, _ := http.NewRequest("GET", "/admin/users/7", nil)
req := NewRequest(httpReq)
req.SetResponseWriter(httptest.NewRecorder())
res := r.Dispatch(req)
assert.Equal(t, "show:7", res.GetContent())
}
func TestUnregisteredControllerErrors(t *testing.T) {
r := New()
r.Get("/broken", "Missing@Show")
r.Register()
httpReq, _ := http.NewRequest("GET", "/broken", nil)
req := NewRequest(httpReq)
req.SetResponseWriter(httptest.NewRecorder())
assert.Panics(t, func() { r.Dispatch(req) })
}