@@ -26,10 +26,7 @@ func dummyMiddleware(handler http.Handler) http.Handler {
26
26
func orderMiddleware (s string ) func (http.Handler ) http.Handler {
27
27
return func (handler http.Handler ) http.Handler {
28
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 , "," ))
29
+ r .Header .Add ("X-Test-Order" , s )
33
30
w .Header ().Set ("X-Test-Response" , "response" )
34
31
handler .ServeHTTP (w , r )
35
32
})
@@ -51,8 +48,7 @@ func TestUse(t *testing.T) {
51
48
52
49
s .Mux .ServeHTTP (w , r )
53
50
54
- orderValue := strings .Split (r .Header .Get ("X-Test-Order" ), "," )
55
- require .Equal (t , []string {"Start!" , "First!" }, orderValue )
51
+ require .Equal (t , []string {"Start!" , "First!" }, r .Header ["X-Test-Order" ])
56
52
})
57
53
58
54
t .Run ("multiple uses of Use" , func (t * testing.T ) {
@@ -69,8 +65,7 @@ func TestUse(t *testing.T) {
69
65
70
66
s .Mux .ServeHTTP (w , r )
71
67
72
- orderValue := strings .Split (r .Header .Get ("X-Test-Order" ), "," )
73
- require .Equal (t , []string {"Start!" , "First!" , "Second!" }, orderValue )
68
+ require .Equal (t , []string {"Start!" , "First!" , "Second!" }, r .Header ["X-Test-Order" ])
74
69
})
75
70
76
71
t .Run ("variadic use of Use" , func (t * testing.T ) {
@@ -87,8 +82,7 @@ func TestUse(t *testing.T) {
87
82
88
83
s .Mux .ServeHTTP (w , r )
89
84
90
- orderValue := strings .Split (r .Header .Get ("X-Test-Order" ), "," )
91
- require .Equal (t , []string {"Start!" , "First!" , "Second!" , "Third!" }, orderValue )
85
+ require .Equal (t , []string {"Start!" , "First!" , "Second!" , "Third!" }, r .Header ["X-Test-Order" ])
92
86
})
93
87
94
88
t .Run ("variadic use of Route Get" , func (t * testing.T ) {
@@ -105,13 +99,26 @@ func TestUse(t *testing.T) {
105
99
106
100
s .Mux .ServeHTTP (w , r )
107
101
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
- )
102
+ require .Equal (t , []string {"Start!" , "First!" , "Second!" , "Third!" , "Fourth!" , "Fifth!" }, r .Header ["X-Test-Order" ])
103
+ })
104
+
105
+ t .Run ("group middlewares" , func (t * testing.T ) {
106
+ s := NewServer ()
107
+ Use (s , orderMiddleware ("First!" ))
108
+ group := Group (s , "/group" )
109
+ Use (group , orderMiddleware ("Second!" ))
110
+ Use (group , orderMiddleware ("Third!" ))
111
+ Get (group , "/test" , func (ctx * ContextNoBody ) (string , error ) {
112
+ return "test" , nil
113
+ })
114
+
115
+ r := httptest .NewRequest (http .MethodGet , "/group/test" , nil )
116
+ r .Header .Set ("X-Test-Order" , "Start!" )
117
+ w := httptest .NewRecorder ()
118
+
119
+ s .Mux .ServeHTTP (w , r )
120
+
121
+ require .Equal (t , []string {"Start!" , "First!" , "Second!" , "Third!" }, r .Header ["X-Test-Order" ])
115
122
})
116
123
}
117
124
0 commit comments