feat(routergroup): include HTTP QUERY method in Any (#4878)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by adam magued · committed by GitHub · parent 43fe48e · Browse files · 4 files·+31 added−12 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Signed-off-by: AdamMagued <adamismailmageud@gmail.com>
Diff stats
4 files+31 added−12 removed
- other:+31 added−12 removed
Provenance
- Commit
e8f2496- Tree
4348feb- Author
adam magued <adamismailmageud@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
43fe48e
4 files changed+31 added−12 removed
modified:docs/doc.md+1 added-2 removed
165 unmodified lines166 ```go167 req := httptest.NewRequest(gin.MethodQuery, "/search", body)168 ```169 170-Two things to keep in mind:170+Keep in mind:171 172-- `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.172 - `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.173 174 ```go175 if q, ok := routes.(gin.IQueryRoutes); ok {2532 unmodified linesmodified:routergroup_test.go+22 added
219 unmodified lines220 result := group.combineHandlers(HandlersChain{})221 assert.NotNil(t, result, "result should not be nil even with empty handlers")222 assert.Empty(t, result, "empty handlers should return empty chain")223 }224+225+func TestRouterGroupAny(t *testing.T) {226+ router := New()227+ router.Any("/any", func(c *Context) {228+ c.String(http.StatusOK, c.Request.Method)229+ })230+231+ v1 := router.Group("/v1")232+ v1.Any("/any", func(c *Context) {233+ c.String(http.StatusOK, "v1:"+c.Request.Method)234+ })235+236+ for _, method := range anyMethods {237+ w := PerformRequest(router, method, "/any")238+ assert.Equal(t, http.StatusOK, w.Code)239+ assert.Equal(t, method, w.Body.String())240+241+ w = PerformRequest(router, method, "/v1/any")242+ assert.Equal(t, http.StatusOK, w.Code)243+ assert.Equal(t, "v1:"+method, w.Body.String())244+ }245+}modified:routergroup.go+2 added-6 removed
22 unmodified lines23 // anyMethods for RouterGroup Any method24 anyMethods = []string{25 http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch,26 http.MethodHead, http.MethodOptions, http.MethodDelete, http.MethodConnect,27- http.MethodTrace,27+ http.MethodTrace, MethodQuery,28 }29 )30 31 // IRouter defines all router handle interface includes single and group router.145 unmodified lines177 return group.handle(MethodQuery, relativePath, handlers)178 }179 180 // Any registers a route that matches all the HTTP methods.181-// GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE.182-//183-// QUERY is not part of that list, so that Any keeps registering exactly the184-// same routes as before. Register it with QUERY, or with185-// Match([]string{gin.MethodQuery, ...}, ...) to combine it with other methods.181+// GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE, QUERY.182 func (group *RouterGroup) Any(relativePath string, handlers ...HandlerFunc) IRoutes {183 for _, method := range anyMethods {184 group.handle(method, relativePath, handlers)185 }110 unmodified linesmodified:routes_test.go+6 added-4 removed
108 unmodified lines109 assert.Equal(t, http.StatusOK, w.Code)110 assert.Equal(t, "gin", w.Body.String())111 }112 113-// TestRouteQueryNotRegisteredByAny documents that Any does not register QUERY,114-// so that existing Any routes keep matching exactly the methods they used to.115-func TestRouteQueryNotRegisteredByAny(t *testing.T) {113+// TestRouteQueryRegisteredByAny documents that Any registers QUERY.114+func TestRouteQueryRegisteredByAny(t *testing.T) {115 router := New()116 router.Any("/test", func(c *Context) {117 c.Status(http.StatusOK)118 })119 120 w := PerformRequest(router, MethodQuery, "/test")121 123- assert.Equal(t, http.StatusNotFound, w.Code)122+ assert.Equal(t, http.StatusOK, w.Code)123 }124 125 // TestQueryRoutesInterface tests that a router reached through IRoutes can126 // still register a QUERY route via the IQueryRoutes assertion.42 unmodified lines169 testRouteOK(http.MethodOptions, t)170 testRouteOK(http.MethodDelete, t)171 testRouteOK(http.MethodConnect, t)172 testRouteOK(http.MethodTrace, t)173+ testRouteOK(MethodQuery, t)174 }175 176 func TestRouteNotOK(t *testing.T) {177 testRouteNotOK(http.MethodGet, t)4 unmodified lines182 testRouteNotOK(http.MethodOptions, t)183 testRouteNotOK(http.MethodDelete, t)184 testRouteNotOK(http.MethodConnect, t)185 testRouteNotOK(http.MethodTrace, t)186+ testRouteNotOK(MethodQuery, t)187 }188 189 func TestRouteNotOK2(t *testing.T) {190 testRouteNotOK2(http.MethodGet, t)4 unmodified lines195 testRouteNotOK2(http.MethodOptions, t)196 testRouteNotOK2(http.MethodDelete, t)197 testRouteNotOK2(http.MethodConnect, t)198 testRouteNotOK2(http.MethodTrace, t)199+ testRouteNotOK2(MethodQuery, t)200 }201 202 func TestRouteRedirectTrailingSlash(t *testing.T) {203 router := New()742 unmodified lines