@@ -6,7 +6,41 @@ import (
6
6
"testing"
7
7
)
8
8
9
- func TestRouter_Use (t * testing.T ) {
9
+ func TestMiddlewareFunc_Middleware (t * testing.T ) {
10
+ // create a mock handler
11
+ mockHandler := func (ctx * Context ) error { return nil }
12
+
13
+ // create a middleware that adds a value to the context
14
+ middleware := MiddlewareFunc (func (handler Handler ) Handler {
15
+ return func (ctx * Context ) error {
16
+ ctx .SetValue ("key" , "value" )
17
+ return handler (ctx )
18
+ }
19
+ })
20
+
21
+ // call the Middleware method with the mock handler
22
+ newHandler := middleware .Middleware (mockHandler )
23
+
24
+ // create a new context instance
25
+ req := httptest .NewRequest (http .MethodGet , "/test" , nil )
26
+ w := httptest .NewRecorder ()
27
+ ctx := NewContext (w , req )
28
+
29
+ // call the new handler with the context
30
+ err := newHandler (ctx )
31
+
32
+ // check if the context value was set correctly
33
+ if val := ctx .GetValue ("key" ); val != "value" {
34
+ t .Errorf ("Expected context value for key \" key\" to be \" value\" , but got %v" , val )
35
+ }
36
+
37
+ // check if the original handler was called with the context
38
+ if err != nil {
39
+ t .Errorf ("Expected err to be nil, but got %v" , err )
40
+ }
41
+ }
42
+
43
+ func TestMiddleware_Use (t * testing.T ) {
10
44
// Create a new router.
11
45
r := NewRouter ()
12
46
@@ -75,3 +109,34 @@ func TestCORSMiddleware(t *testing.T) {
75
109
t .Errorf ("Expected handler to return no error, but got %v" , err )
76
110
}
77
111
}
112
+
113
+ func TestMiddlewareFunc_Handle (t * testing.T ) {
114
+ req := httptest .NewRequest (http .MethodGet , "/test" , nil )
115
+ w := httptest .NewRecorder ()
116
+
117
+ ctx := NewContext (w , req )
118
+
119
+ // create a mock handler
120
+ mockHandler := func (ctx * Context ) error { return nil }
121
+
122
+ // create a middleware that adds a value to the context
123
+ middleware := MiddlewareFunc (func (handler Handler ) Handler {
124
+ return func (ctx * Context ) error {
125
+ ctx .SetValue ("key" , "value" )
126
+ return handler (ctx )
127
+ }
128
+ })
129
+
130
+ // call the Handle method with the mock handler as the next handler
131
+ err := middleware .Handle (ctx , mockHandler )
132
+
133
+ // check if the context value was set correctly
134
+ if val := ctx .GetValue ("key" ); val != "value" {
135
+ t .Errorf ("Expected context value for key \" key\" to be \" value\" , but got %v" , val )
136
+ }
137
+
138
+ // check if the next handler was called with the original context
139
+ if err != nil {
140
+ t .Errorf ("Expected err to be nil, but got %v" , err )
141
+ }
142
+ }
0 commit comments