Skip to content
Myr

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

4 files+31 added−12 removed

modified:docs/doc.md+1 added-2 removed
165 unmodified lines
166```go167req := httptest.NewRequest(gin.MethodQuery, "/search", body)168```169170Two things to keep in mind:170Keep in mind:171172- `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.173174```go175if q, ok := routes.(gin.IQueryRoutes); ok {
2532 unmodified lines
modified:routergroup_test.go+22 added
219 unmodified lines
220 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}224225func TestRouterGroupAny(t *testing.T) {226 router := New()227 router.Any("/any", func(c *Context) {228 c.String(http.StatusOK, c.Request.Method)229 })230231 v1 := router.Group("/v1")232 v1.Any("/any", func(c *Context) {233 c.String(http.StatusOK, "v1:"+c.Request.Method)234 })235236 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())240241 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 lines
23 // 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)3031// IRouter defines all router handle interface includes single and group router.
145 unmodified lines
177 return group.handle(MethodQuery, relativePath, handlers)178}179180// 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.182func (group *RouterGroup) Any(relativePath string, handlers ...HandlerFunc) IRoutes {183 for _, method := range anyMethods {184 group.handle(method, relativePath, handlers)185 }
110 unmodified lines
modified:routes_test.go+6 added-4 removed
108 unmodified lines
109 assert.Equal(t, http.StatusOK, w.Code)110 assert.Equal(t, "gin", w.Body.String())111}112113// TestRouteQueryNotRegisteredByAny documents that Any does not register QUERY,114// so that existing Any routes keep matching exactly the methods they used to.115func TestRouteQueryNotRegisteredByAny(t *testing.T) {113// TestRouteQueryRegisteredByAny documents that Any registers QUERY.114func TestRouteQueryRegisteredByAny(t *testing.T) {115 router := New()116 router.Any("/test", func(c *Context) {117 c.Status(http.StatusOK)118 })119120 w := PerformRequest(router, MethodQuery, "/test")121123 assert.Equal(t, http.StatusNotFound, w.Code)122 assert.Equal(t, http.StatusOK, w.Code)123}124125// TestQueryRoutesInterface tests that a router reached through IRoutes can126// still register a QUERY route via the IQueryRoutes assertion.
42 unmodified lines
169 testRouteOK(http.MethodOptions, t)170 testRouteOK(http.MethodDelete, t)171 testRouteOK(http.MethodConnect, t)172 testRouteOK(http.MethodTrace, t)173 testRouteOK(MethodQuery, t)174}175176func TestRouteNotOK(t *testing.T) {177 testRouteNotOK(http.MethodGet, t)
4 unmodified lines
182 testRouteNotOK(http.MethodOptions, t)183 testRouteNotOK(http.MethodDelete, t)184 testRouteNotOK(http.MethodConnect, t)185 testRouteNotOK(http.MethodTrace, t)186 testRouteNotOK(MethodQuery, t)187}188189func TestRouteNotOK2(t *testing.T) {190 testRouteNotOK2(http.MethodGet, t)
4 unmodified lines
195 testRouteNotOK2(http.MethodOptions, t)196 testRouteNotOK2(http.MethodDelete, t)197 testRouteNotOK2(http.MethodConnect, t)198 testRouteNotOK2(http.MethodTrace, t)199 testRouteNotOK2(MethodQuery, t)200}201202func TestRouteRedirectTrailingSlash(t *testing.T) {203 router := New()
742 unmodified lines