Skip to content
Myr

feat(router): add QUERY method shortcut for RFC 10008 (#4830)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* 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. ---------

authored by Jia-ben00 · committed by GitHub · parent dcaa429 · Browse files · 2 files+26 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Jia-ben00 <316397950+Jia-ben00@users.noreply.github.com>
  • Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>

Diff stats

2 files+26 added−0 removed

  • other:+26 added−0 removed

2 files+26 added−0 removed

modified:gin_test.go+19 added
1154 unmodified lines
1155 assert.Equal(t, http.StatusOK, w.Code)1156 assert.Equal(t, "ok", w.Body.String())1157 }1158}11591160func TestRouterQueryMethod(t *testing.T) {1161 router := New()1162 router.QUERY("/test", func(c *Context) {1163 c.String(http.StatusOK, "query ok")1164 })11651166 w := httptest.NewRecorder()1167 req, _ := http.NewRequest("QUERY", "/test", nil)1168 router.ServeHTTP(w, req)1169 assert.Equal(t, http.StatusOK, w.Code)1170 assert.Equal(t, "query ok", w.Body.String())11711172 // Other methods should not match1173 w2 := httptest.NewRecorder()1174 req2, _ := http.NewRequest(http.MethodGet, "/test", nil)1175 router.ServeHTTP(w2, req2)1176 assert.Equal(t, http.StatusNotFound, w2.Code)1177}
modified:routergroup.go+7 added
140 unmodified lines
141func (group *RouterGroup) HEAD(relativePath string, handlers ...HandlerFunc) IRoutes {142 return group.handle(http.MethodHead, relativePath, handlers)143}144145// QUERY is a shortcut for router.Handle("QUERY", path, handlers).146// The QUERY method is defined by RFC 10008 and is used to send a query147// in the request body, similar to GET but with a body.148func (group *RouterGroup) QUERY(relativePath string, handlers ...HandlerFunc) IRoutes {149 return group.handle("QUERY", relativePath, handlers)150}151152// Any registers a route that matches all the HTTP methods.153// GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE.154func (group *RouterGroup) Any(relativePath string, handlers ...HandlerFunc) IRoutes {155 for _, method := range anyMethods {
111 unmodified lines