Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use http method constant #2155

Merged
merged 3 commits into from
Nov 28, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion binding/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ var (
// Default returns the appropriate Binding instance based on the HTTP method
// and the content type.
func Default(method, contentType string) Binding {
if method == "GET" {
if method == http.MethodGet {
return Form
}

Expand Down
2 changes: 1 addition & 1 deletion gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ func redirectRequest(c *Context) {
rURL := req.URL.String()

code := http.StatusMovedPermanently // Permanent redirect, request with GET method
if req.Method != "GET" {
if req.Method != http.MethodGet {
code = http.StatusTemporaryRedirect
}
debugPrint("redirecting request %d: %s --> %s", code, rPath, rURL)
Expand Down
2 changes: 1 addition & 1 deletion gin_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func TestConcurrentHandleContext(t *testing.T) {
// }

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

w := httptest.NewRecorder()
Expand Down
498 changes: 249 additions & 249 deletions githubapi_test.go

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,19 @@ func (p *LogFormatterParams) MethodColor() string {
method := p.Method

switch method {
case "GET":
case http.MethodGet:
return blue
case "POST":
case http.MethodPost:
return cyan
case "PUT":
case http.MethodPut:
return yellow
case "DELETE":
case http.MethodDelete:
return red
case "PATCH":
case http.Patch:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

http.MethodPatch?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. typo!

return green
case "HEAD":
case http.MethodHead:
return magenta
case "OPTIONS":
case http.MethodOptions:
return white
default:
return reset
Expand Down
32 changes: 16 additions & 16 deletions routergroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,51 +95,51 @@ func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers ...Ha

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

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

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

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

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

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

// HEAD is a shortcut for router.Handle("HEAD", path, handle).
func (group *RouterGroup) HEAD(relativePath string, handlers ...HandlerFunc) IRoutes {
return group.handle("HEAD", relativePath, handlers)
return group.handle(http.MethodHead, 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 {
group.handle("GET", relativePath, handlers)
group.handle("POST", relativePath, handlers)
group.handle("PUT", relativePath, handlers)
group.handle("PATCH", relativePath, handlers)
group.handle("HEAD", relativePath, handlers)
group.handle("OPTIONS", relativePath, handlers)
group.handle("DELETE", relativePath, handlers)
group.handle("CONNECT", relativePath, handlers)
group.handle("TRACE", relativePath, handlers)
group.handle(http.MethodGet, relativePath, handlers)
group.handle(http.MethodPost, relativePath, handlers)
group.handle(http.MethodPut, relativePath, handlers)
group.handle(http.MethodPatch, relativePath, handlers)
group.handle(http.MethodHead, relativePath, handlers)
group.handle(http.MethodOptions, relativePath, handlers)
group.handle(http.MethodDelete, relativePath, handlers)
group.handle(http.MethodConnect, relativePath, handlers)
group.handle(http.MethodTrace, relativePath, handlers)
return group.returnObj()
}

Expand Down
32 changes: 16 additions & 16 deletions routergroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ func TestRouterGroupBasic(t *testing.T) {
}

func TestRouterGroupBasicHandle(t *testing.T) {
performRequestInGroup(t, "GET")
performRequestInGroup(t, "POST")
performRequestInGroup(t, "PUT")
performRequestInGroup(t, "PATCH")
performRequestInGroup(t, "DELETE")
performRequestInGroup(t, "HEAD")
performRequestInGroup(t, "OPTIONS")
performRequestInGroup(t, http.MethodGet)
performRequestInGroup(t, http.MethodPost)
performRequestInGroup(t, http.MethodPut)
performRequestInGroup(t, http.MethodPatch)
performRequestInGroup(t, http.MethodDelete)
performRequestInGroup(t, http.MethodHead)
performRequestInGroup(t, http.MethodOptions)
}

func performRequestInGroup(t *testing.T, method string) {
Expand All @@ -55,25 +55,25 @@ func performRequestInGroup(t *testing.T, method string) {
}

switch method {
case "GET":
case http.MethodGet:
v1.GET("/test", handler)
login.GET("/test", handler)
case "POST":
case http.MethodPost:
v1.POST("/test", handler)
login.POST("/test", handler)
case "PUT":
case http.MethodPut:
v1.PUT("/test", handler)
login.PUT("/test", handler)
case "PATCH":
case http.MethodPatch:
v1.PATCH("/test", handler)
login.PATCH("/test", handler)
case "DELETE":
case http.MethodDelete:
v1.DELETE("/test", handler)
login.DELETE("/test", handler)
case "HEAD":
case http.MethodHead:
v1.HEAD("/test", handler)
login.HEAD("/test", handler)
case "OPTIONS":
case http.MethodOptions:
v1.OPTIONS("/test", handler)
login.OPTIONS("/test", handler)
default:
Expand Down Expand Up @@ -128,7 +128,7 @@ func TestRouterGroupTooManyHandlers(t *testing.T) {
func TestRouterGroupBadMethod(t *testing.T) {
router := New()
assert.Panics(t, func() {
router.Handle("get", "/")
router.Handle(http.MethodGet, "/")
})
assert.Panics(t, func() {
router.Handle(" GET", "/")
Expand Down Expand Up @@ -162,7 +162,7 @@ func testRoutesInterface(t *testing.T, r IRoutes) {
handler := func(c *Context) {}
assert.Equal(t, r, r.Use(handler))

assert.Equal(t, r, r.Handle("GET", "/handler", handler))
assert.Equal(t, r, r.Handle(http.MethodGet, "/handler", handler))
assert.Equal(t, r, r.Any("/any", handler))
assert.Equal(t, r, r.GET("/", handler))
assert.Equal(t, r, r.POST("/", handler))
Expand Down
Loading