Skip to content

Commit d5f12ac

Browse files
authored
use http method constant (gin-gonic#2155)
* use http method constant * fix typo
1 parent 352d69c commit d5f12ac

File tree

8 files changed

+369
-369
lines changed

8 files changed

+369
-369
lines changed

binding/binding.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ var (
8484
// Default returns the appropriate Binding instance based on the HTTP method
8585
// and the content type.
8686
func Default(method, contentType string) Binding {
87-
if method == "GET" {
87+
if method == http.MethodGet {
8888
return Form
8989
}
9090

gin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ func redirectRequest(c *Context) {
490490
rURL := req.URL.String()
491491

492492
code := http.StatusMovedPermanently // Permanent redirect, request with GET method
493-
if req.Method != "GET" {
493+
if req.Method != http.MethodGet {
494494
code = http.StatusTemporaryRedirect
495495
}
496496
debugPrint("redirecting request %d: %s --> %s", code, rPath, rURL)

gin_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ func TestConcurrentHandleContext(t *testing.T) {
291291
// }
292292

293293
func testGetRequestHandler(t *testing.T, h http.Handler, url string) {
294-
req, err := http.NewRequest("GET", url, nil)
294+
req, err := http.NewRequest(http.MethodGet, url, nil)
295295
assert.NoError(t, err)
296296

297297
w := httptest.NewRecorder()

githubapi_test.go

Lines changed: 249 additions & 249 deletions
Large diffs are not rendered by default.

logger.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,19 @@ func (p *LogFormatterParams) MethodColor() string {
9999
method := p.Method
100100

101101
switch method {
102-
case "GET":
102+
case http.MethodGet:
103103
return blue
104-
case "POST":
104+
case http.MethodPost:
105105
return cyan
106-
case "PUT":
106+
case http.MethodPut:
107107
return yellow
108-
case "DELETE":
108+
case http.MethodDelete:
109109
return red
110-
case "PATCH":
110+
case http.MethodPatch:
111111
return green
112-
case "HEAD":
112+
case http.MethodHead:
113113
return magenta
114-
case "OPTIONS":
114+
case http.MethodOptions:
115115
return white
116116
default:
117117
return reset

routergroup.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -95,51 +95,51 @@ func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers ...Ha
9595

9696
// POST is a shortcut for router.Handle("POST", path, handle).
9797
func (group *RouterGroup) POST(relativePath string, handlers ...HandlerFunc) IRoutes {
98-
return group.handle("POST", relativePath, handlers)
98+
return group.handle(http.MethodPost, relativePath, handlers)
9999
}
100100

101101
// GET is a shortcut for router.Handle("GET", path, handle).
102102
func (group *RouterGroup) GET(relativePath string, handlers ...HandlerFunc) IRoutes {
103-
return group.handle("GET", relativePath, handlers)
103+
return group.handle(http.MethodGet, relativePath, handlers)
104104
}
105105

106106
// DELETE is a shortcut for router.Handle("DELETE", path, handle).
107107
func (group *RouterGroup) DELETE(relativePath string, handlers ...HandlerFunc) IRoutes {
108-
return group.handle("DELETE", relativePath, handlers)
108+
return group.handle(http.MethodDelete, relativePath, handlers)
109109
}
110110

111111
// PATCH is a shortcut for router.Handle("PATCH", path, handle).
112112
func (group *RouterGroup) PATCH(relativePath string, handlers ...HandlerFunc) IRoutes {
113-
return group.handle("PATCH", relativePath, handlers)
113+
return group.handle(http.MethodPatch, relativePath, handlers)
114114
}
115115

116116
// PUT is a shortcut for router.Handle("PUT", path, handle).
117117
func (group *RouterGroup) PUT(relativePath string, handlers ...HandlerFunc) IRoutes {
118-
return group.handle("PUT", relativePath, handlers)
118+
return group.handle(http.MethodPut, relativePath, handlers)
119119
}
120120

121121
// OPTIONS is a shortcut for router.Handle("OPTIONS", path, handle).
122122
func (group *RouterGroup) OPTIONS(relativePath string, handlers ...HandlerFunc) IRoutes {
123-
return group.handle("OPTIONS", relativePath, handlers)
123+
return group.handle(http.MethodOptions, relativePath, handlers)
124124
}
125125

126126
// HEAD is a shortcut for router.Handle("HEAD", path, handle).
127127
func (group *RouterGroup) HEAD(relativePath string, handlers ...HandlerFunc) IRoutes {
128-
return group.handle("HEAD", relativePath, handlers)
128+
return group.handle(http.MethodHead, relativePath, handlers)
129129
}
130130

131131
// Any registers a route that matches all the HTTP methods.
132132
// GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE.
133133
func (group *RouterGroup) Any(relativePath string, handlers ...HandlerFunc) IRoutes {
134-
group.handle("GET", relativePath, handlers)
135-
group.handle("POST", relativePath, handlers)
136-
group.handle("PUT", relativePath, handlers)
137-
group.handle("PATCH", relativePath, handlers)
138-
group.handle("HEAD", relativePath, handlers)
139-
group.handle("OPTIONS", relativePath, handlers)
140-
group.handle("DELETE", relativePath, handlers)
141-
group.handle("CONNECT", relativePath, handlers)
142-
group.handle("TRACE", relativePath, handlers)
134+
group.handle(http.MethodGet, relativePath, handlers)
135+
group.handle(http.MethodPost, relativePath, handlers)
136+
group.handle(http.MethodPut, relativePath, handlers)
137+
group.handle(http.MethodPatch, relativePath, handlers)
138+
group.handle(http.MethodHead, relativePath, handlers)
139+
group.handle(http.MethodOptions, relativePath, handlers)
140+
group.handle(http.MethodDelete, relativePath, handlers)
141+
group.handle(http.MethodConnect, relativePath, handlers)
142+
group.handle(http.MethodTrace, relativePath, handlers)
143143
return group.returnObj()
144144
}
145145

routergroup_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ func TestRouterGroupBasic(t *testing.T) {
3333
}
3434

3535
func TestRouterGroupBasicHandle(t *testing.T) {
36-
performRequestInGroup(t, "GET")
37-
performRequestInGroup(t, "POST")
38-
performRequestInGroup(t, "PUT")
39-
performRequestInGroup(t, "PATCH")
40-
performRequestInGroup(t, "DELETE")
41-
performRequestInGroup(t, "HEAD")
42-
performRequestInGroup(t, "OPTIONS")
36+
performRequestInGroup(t, http.MethodGet)
37+
performRequestInGroup(t, http.MethodPost)
38+
performRequestInGroup(t, http.MethodPut)
39+
performRequestInGroup(t, http.MethodPatch)
40+
performRequestInGroup(t, http.MethodDelete)
41+
performRequestInGroup(t, http.MethodHead)
42+
performRequestInGroup(t, http.MethodOptions)
4343
}
4444

4545
func performRequestInGroup(t *testing.T, method string) {
@@ -55,25 +55,25 @@ func performRequestInGroup(t *testing.T, method string) {
5555
}
5656

5757
switch method {
58-
case "GET":
58+
case http.MethodGet:
5959
v1.GET("/test", handler)
6060
login.GET("/test", handler)
61-
case "POST":
61+
case http.MethodPost:
6262
v1.POST("/test", handler)
6363
login.POST("/test", handler)
64-
case "PUT":
64+
case http.MethodPut:
6565
v1.PUT("/test", handler)
6666
login.PUT("/test", handler)
67-
case "PATCH":
67+
case http.MethodPatch:
6868
v1.PATCH("/test", handler)
6969
login.PATCH("/test", handler)
70-
case "DELETE":
70+
case http.MethodDelete:
7171
v1.DELETE("/test", handler)
7272
login.DELETE("/test", handler)
73-
case "HEAD":
73+
case http.MethodHead:
7474
v1.HEAD("/test", handler)
7575
login.HEAD("/test", handler)
76-
case "OPTIONS":
76+
case http.MethodOptions:
7777
v1.OPTIONS("/test", handler)
7878
login.OPTIONS("/test", handler)
7979
default:
@@ -128,7 +128,7 @@ func TestRouterGroupTooManyHandlers(t *testing.T) {
128128
func TestRouterGroupBadMethod(t *testing.T) {
129129
router := New()
130130
assert.Panics(t, func() {
131-
router.Handle("get", "/")
131+
router.Handle(http.MethodGet, "/")
132132
})
133133
assert.Panics(t, func() {
134134
router.Handle(" GET", "/")
@@ -162,7 +162,7 @@ func testRoutesInterface(t *testing.T, r IRoutes) {
162162
handler := func(c *Context) {}
163163
assert.Equal(t, r, r.Use(handler))
164164

165-
assert.Equal(t, r, r.Handle("GET", "/handler", handler))
165+
assert.Equal(t, r, r.Handle(http.MethodGet, "/handler", handler))
166166
assert.Equal(t, r, r.Any("/any", handler))
167167
assert.Equal(t, r, r.GET("/", handler))
168168
assert.Equal(t, r, r.POST("/", handler))

0 commit comments

Comments
 (0)