-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontroller_test.go
More file actions
54 lines (49 loc) · 1.32 KB
/
controller_test.go
File metadata and controls
54 lines (49 loc) · 1.32 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
package middleware
import (
"net/http"
"net/http/httptest"
"testing"
)
var _ Controller = (*BaseController)(nil)
func Test_BaseController(t *testing.T) {
type testCase struct {
title string
method string
mws []Middleware
out string
}
cases := []testCase{
{
title: "register middleware for HTTP method",
method: http.MethodGet,
mws: []Middleware{middlewareOne},
out: "/mw1 before next/final handler/mw1 after next",
},
{
title: "add middleware to existing chain",
method: http.MethodGet,
mws: []Middleware{middlewareTwo, middlewareThree},
out: "/mw1 before next/mw2 before next/mw3 before next/final handler/mw3 after next/mw2 after next/mw1 after next",
},
{
title: "get an empty middleware chain (by default)",
method: http.MethodPost,
out: "/final handler",
},
}
t.Run("Given a BaseController", func(t *testing.T) {
controller := NewBaseController()
for _, tc := range cases {
t.Run(tc.title, func(t *testing.T) {
if len(tc.mws) > 0 {
controller.AddMiddleware(tc.method, tc.mws...)
}
w := httptest.NewRecorder()
controller.Middleware(tc.method).Then(handlerFinal).ServeHTTP(w, nil)
if w.Body.String() != tc.out {
t.Errorf("handler output is expected to be %q but was %q", tc.out, w.Body.String())
}
})
}
})
}