mirror of
https://github.com/gin-gonic/gin.git
synced 2026-11-21 10:03:40 +08:00
feat(router): add QUERY method shortcut for RFC 10008 (#4830)
* feat(router): add QUERY method shortcut for RFC 10008
Add a QUERY() router method as a shortcut for Handle("QUERY", path, handlers),
following the same pattern as GET, POST, HEAD and other HTTP method shortcuts.
The QUERY method is defined by RFC 10008 and allows sending a query in the
request body, similar to GET but with a body. Go's net/http does not yet
expose an http.MethodQuery constant, so the literal string "QUERY" is used.
Closes #4721
* style(router): format QUERY method test
- Remove an extra blank line to satisfy gofmt.
---------
Co-authored-by: Jia-ben00 <316397950+Jia-ben00@users.noreply.github.com>
Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
parent
dcaa4296d1
commit
2a1a14ff37
19
gin_test.go
19
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)
|
||||
}
|
||||
|
||||
@ -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 {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user