diff --git a/docs/doc.md b/docs/doc.md index 16102841..366b249b 100644 --- a/docs/doc.md +++ b/docs/doc.md @@ -167,9 +167,8 @@ Use the `gin.MethodQuery` constant wherever an HTTP method name is expected, for req := httptest.NewRequest(gin.MethodQuery, "/search", body) ``` -Two things to keep in mind: +Keep in mind: -- `router.Any` does not register `QUERY`, so existing catch-all routes keep matching exactly the methods they matched before. Use `router.QUERY` or `router.Match([]string{gin.MethodQuery, http.MethodPost}, ...)` when you want it. - `QUERY` is not part of the `gin.IRoutes` interface. `*gin.Engine` and `*gin.RouterGroup` expose it directly; if you only hold an `IRoutes` value, assert to `gin.IQueryRoutes` first. ```go diff --git a/routergroup.go b/routergroup.go index d85a8baf..47eeee58 100644 --- a/routergroup.go +++ b/routergroup.go @@ -24,7 +24,7 @@ var ( anyMethods = []string{ http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodHead, http.MethodOptions, http.MethodDelete, http.MethodConnect, - http.MethodTrace, + http.MethodTrace, MethodQuery, } ) @@ -178,11 +178,7 @@ func (group *RouterGroup) QUERY(relativePath string, handlers ...HandlerFunc) IR } // Any registers a route that matches all the HTTP methods. -// GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE. -// -// QUERY is not part of that list, so that Any keeps registering exactly the -// same routes as before. Register it with QUERY, or with -// Match([]string{gin.MethodQuery, ...}, ...) to combine it with other methods. +// GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE, QUERY. func (group *RouterGroup) Any(relativePath string, handlers ...HandlerFunc) IRoutes { for _, method := range anyMethods { group.handle(method, relativePath, handlers) diff --git a/routergroup_test.go b/routergroup_test.go index be5c7819..c3859a35 100644 --- a/routergroup_test.go +++ b/routergroup_test.go @@ -221,3 +221,25 @@ func TestRouterGroupCombineHandlersEmptySliceNotNil(t *testing.T) { assert.NotNil(t, result, "result should not be nil even with empty handlers") assert.Empty(t, result, "empty handlers should return empty chain") } + +func TestRouterGroupAny(t *testing.T) { + router := New() + router.Any("/any", func(c *Context) { + c.String(http.StatusOK, c.Request.Method) + }) + + v1 := router.Group("/v1") + v1.Any("/any", func(c *Context) { + c.String(http.StatusOK, "v1:"+c.Request.Method) + }) + + for _, method := range anyMethods { + w := PerformRequest(router, method, "/any") + assert.Equal(t, http.StatusOK, w.Code) + assert.Equal(t, method, w.Body.String()) + + w = PerformRequest(router, method, "/v1/any") + assert.Equal(t, http.StatusOK, w.Code) + assert.Equal(t, "v1:"+method, w.Body.String()) + } +} diff --git a/routes_test.go b/routes_test.go index dbfaad5c..3306233f 100644 --- a/routes_test.go +++ b/routes_test.go @@ -110,9 +110,8 @@ func TestRouteQuery(t *testing.T) { assert.Equal(t, "gin", w.Body.String()) } -// TestRouteQueryNotRegisteredByAny documents that Any does not register QUERY, -// so that existing Any routes keep matching exactly the methods they used to. -func TestRouteQueryNotRegisteredByAny(t *testing.T) { +// TestRouteQueryRegisteredByAny documents that Any registers QUERY. +func TestRouteQueryRegisteredByAny(t *testing.T) { router := New() router.Any("/test", func(c *Context) { c.Status(http.StatusOK) @@ -120,7 +119,7 @@ func TestRouteQueryNotRegisteredByAny(t *testing.T) { w := PerformRequest(router, MethodQuery, "/test") - assert.Equal(t, http.StatusNotFound, w.Code) + assert.Equal(t, http.StatusOK, w.Code) } // TestQueryRoutesInterface tests that a router reached through IRoutes can @@ -171,6 +170,7 @@ func TestRouterGroupRouteOK(t *testing.T) { testRouteOK(http.MethodDelete, t) testRouteOK(http.MethodConnect, t) testRouteOK(http.MethodTrace, t) + testRouteOK(MethodQuery, t) } func TestRouteNotOK(t *testing.T) { @@ -183,6 +183,7 @@ func TestRouteNotOK(t *testing.T) { testRouteNotOK(http.MethodDelete, t) testRouteNotOK(http.MethodConnect, t) testRouteNotOK(http.MethodTrace, t) + testRouteNotOK(MethodQuery, t) } func TestRouteNotOK2(t *testing.T) { @@ -195,6 +196,7 @@ func TestRouteNotOK2(t *testing.T) { testRouteNotOK2(http.MethodDelete, t) testRouteNotOK2(http.MethodConnect, t) testRouteNotOK2(http.MethodTrace, t) + testRouteNotOK2(MethodQuery, t) } func TestRouteRedirectTrailingSlash(t *testing.T) {