| 147 | } |
| 148 | |
| 149 | func TestRequestPattern(t *testing.T) { |
| 150 | cases := []struct { |
| 151 | Name string |
| 152 | Method string |
| 153 | Pattern string |
| 154 | URL string |
| 155 | Expected string |
| 156 | }{ |
| 157 | { |
| 158 | Name: "simple", |
| 159 | Method: "GET", |
| 160 | Pattern: "/users", |
| 161 | URL: "/users", |
| 162 | Expected: "GET /users", |
| 163 | }, |
| 164 | { |
| 165 | Name: "with segment", |
| 166 | Method: "POST", |
| 167 | Pattern: "/users/{id}", |
| 168 | URL: "/users/123", |
| 169 | Expected: "POST /users/{id}", |
| 170 | }, |
| 171 | { |
| 172 | Name: "with wildcard", |
| 173 | Method: "GET", |
| 174 | Pattern: "/files/{*path}", |
| 175 | URL: "/files/a/b/c", |
| 176 | Expected: "GET /files/{*path}", |
| 177 | }, |
| 178 | } |
| 179 | |
| 180 | for _, c := range cases { |
| 181 | t.Run(c.Name, func(t *testing.T) { |
| 182 | var called bool |
| 183 | mux := NewMuxer() |
| 184 | mux.Handle(c.Method, c.Pattern, func(_ http.ResponseWriter, r *http.Request) { |
| 185 | assert.Equal(t, c.Expected, r.Pattern) |
| 186 | called = true |
| 187 | }) |
| 188 | req, _ := http.NewRequest(c.Method, c.URL, nil) |
| 189 | w := httptest.NewRecorder() |
| 190 | mux.ServeHTTP(w, req) |
| 191 | assert.True(t, called) |
| 192 | }) |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // TestRequestPatternInMiddleware verifies that r.Pattern is set by the |
| 197 | // built-in middleware before user-registered middlewares run. This is |