Skip to content

Commit 8a4c82c

Browse files
dylanhittEwenQuim
authored andcommitted
WIP: possible test?
1 parent 5c0ac13 commit 8a4c82c

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

mux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func Register[T any, B any, Contexted ctx[B]](s *Server, method string, path str
8888
func register[T any, B any](s *Server, method string, path string, controller http.Handler, middlewares ...func(http.Handler) http.Handler) Route[T, B] {
8989
fullPath := method + " " + s.basePath + path
9090

91-
allMiddlewares := append(middlewares, s.middlewares...)
91+
allMiddlewares := append(s.middlewares, middlewares...)
9292
s.Mux.Handle(fullPath, withMiddlewares(controller, allMiddlewares...))
9393

9494
if s.DisableOpenapi {

mux_test.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,101 @@ func dummyMiddleware(handler http.Handler) http.Handler {
2020
})
2121
}
2222

23+
// orderMiddleware sets the X-Test-Order Header on the request and
24+
// X-Test-Response header on the response. It is
25+
// used to test the order execution of our middleware
26+
func orderMiddleware(s string) func(http.Handler) http.Handler {
27+
return func(handler http.Handler) http.Handler {
28+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
29+
orderValue := strings.Split(r.Header.Get("X-Test-Order"), ",")
30+
orderValue = append(orderValue, s)
31+
32+
r.Header.Set("X-Test-Order", strings.Join(orderValue, ","))
33+
w.Header().Set("X-Test-Response", "response")
34+
handler.ServeHTTP(w, r)
35+
})
36+
}
37+
}
38+
39+
// TestUse is used to mainly test the ordering of middleware execution
40+
func TestUse(t *testing.T) {
41+
t.Run("base", func(t *testing.T) {
42+
s := NewServer()
43+
Use(s, orderMiddleware("First!"))
44+
Get(s, "/test", func(ctx *ContextNoBody) (string, error) {
45+
return "test", nil
46+
})
47+
48+
r := httptest.NewRequest(http.MethodGet, "/test", nil)
49+
r.Header.Set("X-Test-Order", "Start!")
50+
w := httptest.NewRecorder()
51+
52+
s.Mux.ServeHTTP(w, r)
53+
54+
orderValue := strings.Split(r.Header.Get("X-Test-Order"), ",")
55+
require.Equal(t, []string{"Start!", "First!"}, orderValue)
56+
})
57+
58+
t.Run("multiple uses of Use", func(t *testing.T) {
59+
s := NewServer()
60+
Use(s, orderMiddleware("First!"))
61+
Use(s, orderMiddleware("Second!"))
62+
Get(s, "/test", func(ctx *ContextNoBody) (string, error) {
63+
return "test", nil
64+
})
65+
66+
r := httptest.NewRequest(http.MethodGet, "/test", nil)
67+
r.Header.Set("X-Test-Order", "Start!")
68+
w := httptest.NewRecorder()
69+
70+
s.Mux.ServeHTTP(w, r)
71+
72+
orderValue := strings.Split(r.Header.Get("X-Test-Order"), ",")
73+
require.Equal(t, []string{"Start!", "First!", "Second!"}, orderValue)
74+
})
75+
76+
t.Run("variadic use of Use", func(t *testing.T) {
77+
s := NewServer()
78+
Use(s, orderMiddleware("First!"))
79+
Use(s, orderMiddleware("Second!"), orderMiddleware("Third!"))
80+
Get(s, "/test", func(ctx *ContextNoBody) (string, error) {
81+
return "test", nil
82+
})
83+
84+
r := httptest.NewRequest(http.MethodGet, "/test", nil)
85+
r.Header.Set("X-Test-Order", "Start!")
86+
w := httptest.NewRecorder()
87+
88+
s.Mux.ServeHTTP(w, r)
89+
90+
orderValue := strings.Split(r.Header.Get("X-Test-Order"), ",")
91+
require.Equal(t, []string{"Start!", "First!", "Second!", "Third!"}, orderValue)
92+
})
93+
94+
t.Run("variadic use of Route Get", func(t *testing.T) {
95+
s := NewServer()
96+
Use(s, orderMiddleware("First!"))
97+
Use(s, orderMiddleware("Second!"), orderMiddleware("Third!"))
98+
Get(s, "/test", func(ctx *ContextNoBody) (string, error) {
99+
return "test", nil
100+
}, orderMiddleware("Fourth!"), orderMiddleware("Fifth!"))
101+
102+
r := httptest.NewRequest(http.MethodGet, "/test", nil)
103+
r.Header.Set("X-Test-Order", "Start!")
104+
w := httptest.NewRecorder()
105+
106+
s.Mux.ServeHTTP(w, r)
107+
108+
orderValue := strings.Split(r.Header.Get("X-Test-Order"), ",")
109+
require.Equal(
110+
t, []string{
111+
"Start!", "First!", "Second!", "Third!", "Fourth!", "Fifth!",
112+
},
113+
orderValue,
114+
)
115+
})
116+
}
117+
23118
func TestUseStd(t *testing.T) {
24119
s := NewServer()
25120
UseStd(s, dummyMiddleware)

0 commit comments

Comments
 (0)