Issue #1143
(t *testing.T)
| 173 | |
| 174 | // Issue #1143 |
| 175 | func TestRewriteWithConfigPreMiddleware_Issue1143(t *testing.T) { |
| 176 | e := echo.New() |
| 177 | |
| 178 | // middlewares added with `Pre()` are executed before routing is done and therefore change which handler matches |
| 179 | e.Pre(RewriteWithConfig(RewriteConfig{ |
| 180 | Rules: map[string]string{ |
| 181 | "/api/*/mgmt/proj/*/agt": "/api/$1/hosts/$2", |
| 182 | "/api/*/mgmt/proj": "/api/$1/eng", |
| 183 | }, |
| 184 | })) |
| 185 | |
| 186 | e.Add(http.MethodGet, "/api/:version/hosts/:name", func(c *echo.Context) error { |
| 187 | return c.String(http.StatusOK, "hosts") |
| 188 | }) |
| 189 | e.Add(http.MethodGet, "/api/:version/eng", func(c *echo.Context) error { |
| 190 | return c.String(http.StatusOK, "eng") |
| 191 | }) |
| 192 | |
| 193 | for range 100 { |
| 194 | req := httptest.NewRequest(http.MethodGet, "/api/v1/mgmt/proj/test/agt", nil) |
| 195 | rec := httptest.NewRecorder() |
| 196 | e.ServeHTTP(rec, req) |
| 197 | assert.Equal(t, "/api/v1/hosts/test", req.URL.EscapedPath()) |
| 198 | assert.Equal(t, http.StatusOK, rec.Code) |
| 199 | |
| 200 | defer rec.Result().Body.Close() |
| 201 | bodyBytes, _ := io.ReadAll(rec.Result().Body) |
| 202 | assert.Equal(t, "hosts", string(bodyBytes)) |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | // Issue #1573 |
| 207 | func TestEchoRewriteWithCaret(t *testing.T) { |