diff --git a/gin_test.go b/gin_test.go index a9cf1755..b488c446 100644 --- a/gin_test.go +++ b/gin_test.go @@ -1156,3 +1156,22 @@ func TestUpdateRouteTreesCalledOnce(t *testing.T) { assert.Equal(t, "ok", w.Body.String()) } } + +func TestRouterQueryMethod(t *testing.T) { + router := New() + router.QUERY("/test", func(c *Context) { + c.String(http.StatusOK, "query ok") + }) + + w := httptest.NewRecorder() + req, _ := http.NewRequest("QUERY", "/test", nil) + router.ServeHTTP(w, req) + assert.Equal(t, http.StatusOK, w.Code) + assert.Equal(t, "query ok", w.Body.String()) + + // Other methods should not match + w2 := httptest.NewRecorder() + req2, _ := http.NewRequest(http.MethodGet, "/test", nil) + router.ServeHTTP(w2, req2) + assert.Equal(t, http.StatusNotFound, w2.Code) +} diff --git a/routergroup.go b/routergroup.go index c01b917e..1ae0b7b4 100644 --- a/routergroup.go +++ b/routergroup.go @@ -142,6 +142,13 @@ func (group *RouterGroup) HEAD(relativePath string, handlers ...HandlerFunc) IRo return group.handle(http.MethodHead, relativePath, handlers) } +// QUERY is a shortcut for router.Handle("QUERY", path, handlers). +// The QUERY method is defined by RFC 10008 and is used to send a query +// in the request body, similar to GET but with a body. +func (group *RouterGroup) QUERY(relativePath string, handlers ...HandlerFunc) IRoutes { + return group.handle("QUERY", relativePath, handlers) +} + // Any registers a route that matches all the HTTP methods. // GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE. func (group *RouterGroup) Any(relativePath string, handlers ...HandlerFunc) IRoutes {