From d5f12ac6d7ba14b9bc85fa7d0ae486289d52bdf1 Mon Sep 17 00:00:00 2001 From: thinkerou Date: Fri, 29 Nov 2019 07:50:49 +0800 Subject: [PATCH] use http method constant (#2155) * use http method constant * fix typo --- binding/binding.go | 2 +- gin.go | 2 +- gin_integration_test.go | 2 +- githubapi_test.go | 498 ++++++++++++++++++++-------------------- logger.go | 14 +- routergroup.go | 32 +-- routergroup_test.go | 32 +-- routes_test.go | 156 ++++++------- 8 files changed, 369 insertions(+), 369 deletions(-) diff --git a/binding/binding.go b/binding/binding.go index 6d58c3cd..f578aa55 100644 --- a/binding/binding.go +++ b/binding/binding.go @@ -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 } diff --git a/gin.go b/gin.go index f0cd09e4..71f3fd5c 100644 --- a/gin.go +++ b/gin.go @@ -490,7 +490,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) diff --git a/gin_integration_test.go b/gin_integration_test.go index d86f610b..f29d1fc1 100644 --- a/gin_integration_test.go +++ b/gin_integration_test.go @@ -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() diff --git a/githubapi_test.go b/githubapi_test.go index fb74d659..925c5a14 100644 --- a/githubapi_test.go +++ b/githubapi_test.go @@ -24,265 +24,265 @@ type route struct { // http://developer.github.com/v3/ var githubAPI = []route{ // OAuth Authorizations - {"GET", "/authorizations"}, - {"GET", "/authorizations/:id"}, - {"POST", "/authorizations"}, - //{"PUT", "/authorizations/clients/:client_id"}, - //{"PATCH", "/authorizations/:id"}, - {"DELETE", "/authorizations/:id"}, - {"GET", "/applications/:client_id/tokens/:access_token"}, - {"DELETE", "/applications/:client_id/tokens"}, - {"DELETE", "/applications/:client_id/tokens/:access_token"}, + {http.MethodGet, "/authorizations"}, + {http.MethodGet, "/authorizations/:id"}, + {http.MethodPost, "/authorizations"}, + //{http.MethodPut, "/authorizations/clients/:client_id"}, + //{http.MethodPatch, "/authorizations/:id"}, + {http.MethodDelete, "/authorizations/:id"}, + {http.MethodGet, "/applications/:client_id/tokens/:access_token"}, + {http.MethodDelete, "/applications/:client_id/tokens"}, + {http.MethodDelete, "/applications/:client_id/tokens/:access_token"}, // Activity - {"GET", "/events"}, - {"GET", "/repos/:owner/:repo/events"}, - {"GET", "/networks/:owner/:repo/events"}, - {"GET", "/orgs/:org/events"}, - {"GET", "/users/:user/received_events"}, - {"GET", "/users/:user/received_events/public"}, - {"GET", "/users/:user/events"}, - {"GET", "/users/:user/events/public"}, - {"GET", "/users/:user/events/orgs/:org"}, - {"GET", "/feeds"}, - {"GET", "/notifications"}, - {"GET", "/repos/:owner/:repo/notifications"}, - {"PUT", "/notifications"}, - {"PUT", "/repos/:owner/:repo/notifications"}, - {"GET", "/notifications/threads/:id"}, - //{"PATCH", "/notifications/threads/:id"}, - {"GET", "/notifications/threads/:id/subscription"}, - {"PUT", "/notifications/threads/:id/subscription"}, - {"DELETE", "/notifications/threads/:id/subscription"}, - {"GET", "/repos/:owner/:repo/stargazers"}, - {"GET", "/users/:user/starred"}, - {"GET", "/user/starred"}, - {"GET", "/user/starred/:owner/:repo"}, - {"PUT", "/user/starred/:owner/:repo"}, - {"DELETE", "/user/starred/:owner/:repo"}, - {"GET", "/repos/:owner/:repo/subscribers"}, - {"GET", "/users/:user/subscriptions"}, - {"GET", "/user/subscriptions"}, - {"GET", "/repos/:owner/:repo/subscription"}, - {"PUT", "/repos/:owner/:repo/subscription"}, - {"DELETE", "/repos/:owner/:repo/subscription"}, - {"GET", "/user/subscriptions/:owner/:repo"}, - {"PUT", "/user/subscriptions/:owner/:repo"}, - {"DELETE", "/user/subscriptions/:owner/:repo"}, + {http.MethodGet, "/events"}, + {http.MethodGet, "/repos/:owner/:repo/events"}, + {http.MethodGet, "/networks/:owner/:repo/events"}, + {http.MethodGet, "/orgs/:org/events"}, + {http.MethodGet, "/users/:user/received_events"}, + {http.MethodGet, "/users/:user/received_events/public"}, + {http.MethodGet, "/users/:user/events"}, + {http.MethodGet, "/users/:user/events/public"}, + {http.MethodGet, "/users/:user/events/orgs/:org"}, + {http.MethodGet, "/feeds"}, + {http.MethodGet, "/notifications"}, + {http.MethodGet, "/repos/:owner/:repo/notifications"}, + {http.MethodPut, "/notifications"}, + {http.MethodPut, "/repos/:owner/:repo/notifications"}, + {http.MethodGet, "/notifications/threads/:id"}, + //{http.MethodPatch, "/notifications/threads/:id"}, + {http.MethodGet, "/notifications/threads/:id/subscription"}, + {http.MethodPut, "/notifications/threads/:id/subscription"}, + {http.MethodDelete, "/notifications/threads/:id/subscription"}, + {http.MethodGet, "/repos/:owner/:repo/stargazers"}, + {http.MethodGet, "/users/:user/starred"}, + {http.MethodGet, "/user/starred"}, + {http.MethodGet, "/user/starred/:owner/:repo"}, + {http.MethodPut, "/user/starred/:owner/:repo"}, + {http.MethodDelete, "/user/starred/:owner/:repo"}, + {http.MethodGet, "/repos/:owner/:repo/subscribers"}, + {http.MethodGet, "/users/:user/subscriptions"}, + {http.MethodGet, "/user/subscriptions"}, + {http.MethodGet, "/repos/:owner/:repo/subscription"}, + {http.MethodPut, "/repos/:owner/:repo/subscription"}, + {http.MethodDelete, "/repos/:owner/:repo/subscription"}, + {http.MethodGet, "/user/subscriptions/:owner/:repo"}, + {http.MethodPut, "/user/subscriptions/:owner/:repo"}, + {http.MethodDelete, "/user/subscriptions/:owner/:repo"}, // Gists - {"GET", "/users/:user/gists"}, - {"GET", "/gists"}, - //{"GET", "/gists/public"}, - //{"GET", "/gists/starred"}, - {"GET", "/gists/:id"}, - {"POST", "/gists"}, - //{"PATCH", "/gists/:id"}, - {"PUT", "/gists/:id/star"}, - {"DELETE", "/gists/:id/star"}, - {"GET", "/gists/:id/star"}, - {"POST", "/gists/:id/forks"}, - {"DELETE", "/gists/:id"}, + {http.MethodGet, "/users/:user/gists"}, + {http.MethodGet, "/gists"}, + //{http.MethodGet, "/gists/public"}, + //{http.MethodGet, "/gists/starred"}, + {http.MethodGet, "/gists/:id"}, + {http.MethodPost, "/gists"}, + //{http.MethodPatch, "/gists/:id"}, + {http.MethodPut, "/gists/:id/star"}, + {http.MethodDelete, "/gists/:id/star"}, + {http.MethodGet, "/gists/:id/star"}, + {http.MethodPost, "/gists/:id/forks"}, + {http.MethodDelete, "/gists/:id"}, // Git Data - {"GET", "/repos/:owner/:repo/git/blobs/:sha"}, - {"POST", "/repos/:owner/:repo/git/blobs"}, - {"GET", "/repos/:owner/:repo/git/commits/:sha"}, - {"POST", "/repos/:owner/:repo/git/commits"}, - //{"GET", "/repos/:owner/:repo/git/refs/*ref"}, - {"GET", "/repos/:owner/:repo/git/refs"}, - {"POST", "/repos/:owner/:repo/git/refs"}, - //{"PATCH", "/repos/:owner/:repo/git/refs/*ref"}, - //{"DELETE", "/repos/:owner/:repo/git/refs/*ref"}, - {"GET", "/repos/:owner/:repo/git/tags/:sha"}, - {"POST", "/repos/:owner/:repo/git/tags"}, - {"GET", "/repos/:owner/:repo/git/trees/:sha"}, - {"POST", "/repos/:owner/:repo/git/trees"}, + {http.MethodGet, "/repos/:owner/:repo/git/blobs/:sha"}, + {http.MethodPost, "/repos/:owner/:repo/git/blobs"}, + {http.MethodGet, "/repos/:owner/:repo/git/commits/:sha"}, + {http.MethodPost, "/repos/:owner/:repo/git/commits"}, + //{http.MethodGet, "/repos/:owner/:repo/git/refs/*ref"}, + {http.MethodGet, "/repos/:owner/:repo/git/refs"}, + {http.MethodPost, "/repos/:owner/:repo/git/refs"}, + //{http.MethodPatch, "/repos/:owner/:repo/git/refs/*ref"}, + //{http.MethodDelete, "/repos/:owner/:repo/git/refs/*ref"}, + {http.MethodGet, "/repos/:owner/:repo/git/tags/:sha"}, + {http.MethodPost, "/repos/:owner/:repo/git/tags"}, + {http.MethodGet, "/repos/:owner/:repo/git/trees/:sha"}, + {http.MethodPost, "/repos/:owner/:repo/git/trees"}, // Issues - {"GET", "/issues"}, - {"GET", "/user/issues"}, - {"GET", "/orgs/:org/issues"}, - {"GET", "/repos/:owner/:repo/issues"}, - {"GET", "/repos/:owner/:repo/issues/:number"}, - {"POST", "/repos/:owner/:repo/issues"}, - //{"PATCH", "/repos/:owner/:repo/issues/:number"}, - {"GET", "/repos/:owner/:repo/assignees"}, - {"GET", "/repos/:owner/:repo/assignees/:assignee"}, - {"GET", "/repos/:owner/:repo/issues/:number/comments"}, - //{"GET", "/repos/:owner/:repo/issues/comments"}, - //{"GET", "/repos/:owner/:repo/issues/comments/:id"}, - {"POST", "/repos/:owner/:repo/issues/:number/comments"}, - //{"PATCH", "/repos/:owner/:repo/issues/comments/:id"}, - //{"DELETE", "/repos/:owner/:repo/issues/comments/:id"}, - {"GET", "/repos/:owner/:repo/issues/:number/events"}, - //{"GET", "/repos/:owner/:repo/issues/events"}, - //{"GET", "/repos/:owner/:repo/issues/events/:id"}, - {"GET", "/repos/:owner/:repo/labels"}, - {"GET", "/repos/:owner/:repo/labels/:name"}, - {"POST", "/repos/:owner/:repo/labels"}, - //{"PATCH", "/repos/:owner/:repo/labels/:name"}, - {"DELETE", "/repos/:owner/:repo/labels/:name"}, - {"GET", "/repos/:owner/:repo/issues/:number/labels"}, - {"POST", "/repos/:owner/:repo/issues/:number/labels"}, - {"DELETE", "/repos/:owner/:repo/issues/:number/labels/:name"}, - {"PUT", "/repos/:owner/:repo/issues/:number/labels"}, - {"DELETE", "/repos/:owner/:repo/issues/:number/labels"}, - {"GET", "/repos/:owner/:repo/milestones/:number/labels"}, - {"GET", "/repos/:owner/:repo/milestones"}, - {"GET", "/repos/:owner/:repo/milestones/:number"}, - {"POST", "/repos/:owner/:repo/milestones"}, - //{"PATCH", "/repos/:owner/:repo/milestones/:number"}, - {"DELETE", "/repos/:owner/:repo/milestones/:number"}, + {http.MethodGet, "/issues"}, + {http.MethodGet, "/user/issues"}, + {http.MethodGet, "/orgs/:org/issues"}, + {http.MethodGet, "/repos/:owner/:repo/issues"}, + {http.MethodGet, "/repos/:owner/:repo/issues/:number"}, + {http.MethodPost, "/repos/:owner/:repo/issues"}, + //{http.MethodPatch, "/repos/:owner/:repo/issues/:number"}, + {http.MethodGet, "/repos/:owner/:repo/assignees"}, + {http.MethodGet, "/repos/:owner/:repo/assignees/:assignee"}, + {http.MethodGet, "/repos/:owner/:repo/issues/:number/comments"}, + //{http.MethodGet, "/repos/:owner/:repo/issues/comments"}, + //{http.MethodGet, "/repos/:owner/:repo/issues/comments/:id"}, + {http.MethodPost, "/repos/:owner/:repo/issues/:number/comments"}, + //{http.MethodPatch, "/repos/:owner/:repo/issues/comments/:id"}, + //{http.MethodDelete, "/repos/:owner/:repo/issues/comments/:id"}, + {http.MethodGet, "/repos/:owner/:repo/issues/:number/events"}, + //{http.MethodGet, "/repos/:owner/:repo/issues/events"}, + //{http.MethodGet, "/repos/:owner/:repo/issues/events/:id"}, + {http.MethodGet, "/repos/:owner/:repo/labels"}, + {http.MethodGet, "/repos/:owner/:repo/labels/:name"}, + {http.MethodPost, "/repos/:owner/:repo/labels"}, + //{http.MethodPatch, "/repos/:owner/:repo/labels/:name"}, + {http.MethodDelete, "/repos/:owner/:repo/labels/:name"}, + {http.MethodGet, "/repos/:owner/:repo/issues/:number/labels"}, + {http.MethodPost, "/repos/:owner/:repo/issues/:number/labels"}, + {http.MethodDelete, "/repos/:owner/:repo/issues/:number/labels/:name"}, + {http.MethodPut, "/repos/:owner/:repo/issues/:number/labels"}, + {http.MethodDelete, "/repos/:owner/:repo/issues/:number/labels"}, + {http.MethodGet, "/repos/:owner/:repo/milestones/:number/labels"}, + {http.MethodGet, "/repos/:owner/:repo/milestones"}, + {http.MethodGet, "/repos/:owner/:repo/milestones/:number"}, + {http.MethodPost, "/repos/:owner/:repo/milestones"}, + //{http.MethodPatch, "/repos/:owner/:repo/milestones/:number"}, + {http.MethodDelete, "/repos/:owner/:repo/milestones/:number"}, // Miscellaneous - {"GET", "/emojis"}, - {"GET", "/gitignore/templates"}, - {"GET", "/gitignore/templates/:name"}, - {"POST", "/markdown"}, - {"POST", "/markdown/raw"}, - {"GET", "/meta"}, - {"GET", "/rate_limit"}, + {http.MethodGet, "/emojis"}, + {http.MethodGet, "/gitignore/templates"}, + {http.MethodGet, "/gitignore/templates/:name"}, + {http.MethodPost, "/markdown"}, + {http.MethodPost, "/markdown/raw"}, + {http.MethodGet, "/meta"}, + {http.MethodGet, "/rate_limit"}, // Organizations - {"GET", "/users/:user/orgs"}, - {"GET", "/user/orgs"}, - {"GET", "/orgs/:org"}, - //{"PATCH", "/orgs/:org"}, - {"GET", "/orgs/:org/members"}, - {"GET", "/orgs/:org/members/:user"}, - {"DELETE", "/orgs/:org/members/:user"}, - {"GET", "/orgs/:org/public_members"}, - {"GET", "/orgs/:org/public_members/:user"}, - {"PUT", "/orgs/:org/public_members/:user"}, - {"DELETE", "/orgs/:org/public_members/:user"}, - {"GET", "/orgs/:org/teams"}, - {"GET", "/teams/:id"}, - {"POST", "/orgs/:org/teams"}, - //{"PATCH", "/teams/:id"}, - {"DELETE", "/teams/:id"}, - {"GET", "/teams/:id/members"}, - {"GET", "/teams/:id/members/:user"}, - {"PUT", "/teams/:id/members/:user"}, - {"DELETE", "/teams/:id/members/:user"}, - {"GET", "/teams/:id/repos"}, - {"GET", "/teams/:id/repos/:owner/:repo"}, - {"PUT", "/teams/:id/repos/:owner/:repo"}, - {"DELETE", "/teams/:id/repos/:owner/:repo"}, - {"GET", "/user/teams"}, + {http.MethodGet, "/users/:user/orgs"}, + {http.MethodGet, "/user/orgs"}, + {http.MethodGet, "/orgs/:org"}, + //{http.MethodPatch, "/orgs/:org"}, + {http.MethodGet, "/orgs/:org/members"}, + {http.MethodGet, "/orgs/:org/members/:user"}, + {http.MethodDelete, "/orgs/:org/members/:user"}, + {http.MethodGet, "/orgs/:org/public_members"}, + {http.MethodGet, "/orgs/:org/public_members/:user"}, + {http.MethodPut, "/orgs/:org/public_members/:user"}, + {http.MethodDelete, "/orgs/:org/public_members/:user"}, + {http.MethodGet, "/orgs/:org/teams"}, + {http.MethodGet, "/teams/:id"}, + {http.MethodPost, "/orgs/:org/teams"}, + //{http.MethodPatch, "/teams/:id"}, + {http.MethodDelete, "/teams/:id"}, + {http.MethodGet, "/teams/:id/members"}, + {http.MethodGet, "/teams/:id/members/:user"}, + {http.MethodPut, "/teams/:id/members/:user"}, + {http.MethodDelete, "/teams/:id/members/:user"}, + {http.MethodGet, "/teams/:id/repos"}, + {http.MethodGet, "/teams/:id/repos/:owner/:repo"}, + {http.MethodPut, "/teams/:id/repos/:owner/:repo"}, + {http.MethodDelete, "/teams/:id/repos/:owner/:repo"}, + {http.MethodGet, "/user/teams"}, // Pull Requests - {"GET", "/repos/:owner/:repo/pulls"}, - {"GET", "/repos/:owner/:repo/pulls/:number"}, - {"POST", "/repos/:owner/:repo/pulls"}, - //{"PATCH", "/repos/:owner/:repo/pulls/:number"}, - {"GET", "/repos/:owner/:repo/pulls/:number/commits"}, - {"GET", "/repos/:owner/:repo/pulls/:number/files"}, - {"GET", "/repos/:owner/:repo/pulls/:number/merge"}, - {"PUT", "/repos/:owner/:repo/pulls/:number/merge"}, - {"GET", "/repos/:owner/:repo/pulls/:number/comments"}, - //{"GET", "/repos/:owner/:repo/pulls/comments"}, - //{"GET", "/repos/:owner/:repo/pulls/comments/:number"}, - {"PUT", "/repos/:owner/:repo/pulls/:number/comments"}, - //{"PATCH", "/repos/:owner/:repo/pulls/comments/:number"}, - //{"DELETE", "/repos/:owner/:repo/pulls/comments/:number"}, + {http.MethodGet, "/repos/:owner/:repo/pulls"}, + {http.MethodGet, "/repos/:owner/:repo/pulls/:number"}, + {http.MethodPost, "/repos/:owner/:repo/pulls"}, + //{http.MethodPatch, "/repos/:owner/:repo/pulls/:number"}, + {http.MethodGet, "/repos/:owner/:repo/pulls/:number/commits"}, + {http.MethodGet, "/repos/:owner/:repo/pulls/:number/files"}, + {http.MethodGet, "/repos/:owner/:repo/pulls/:number/merge"}, + {http.MethodPut, "/repos/:owner/:repo/pulls/:number/merge"}, + {http.MethodGet, "/repos/:owner/:repo/pulls/:number/comments"}, + //{http.MethodGet, "/repos/:owner/:repo/pulls/comments"}, + //{http.MethodGet, "/repos/:owner/:repo/pulls/comments/:number"}, + {http.MethodPut, "/repos/:owner/:repo/pulls/:number/comments"}, + //{http.MethodPatch, "/repos/:owner/:repo/pulls/comments/:number"}, + //{http.MethodDelete, "/repos/:owner/:repo/pulls/comments/:number"}, // Repositories - {"GET", "/user/repos"}, - {"GET", "/users/:user/repos"}, - {"GET", "/orgs/:org/repos"}, - {"GET", "/repositories"}, - {"POST", "/user/repos"}, - {"POST", "/orgs/:org/repos"}, - {"GET", "/repos/:owner/:repo"}, - //{"PATCH", "/repos/:owner/:repo"}, - {"GET", "/repos/:owner/:repo/contributors"}, - {"GET", "/repos/:owner/:repo/languages"}, - {"GET", "/repos/:owner/:repo/teams"}, - {"GET", "/repos/:owner/:repo/tags"}, - {"GET", "/repos/:owner/:repo/branches"}, - {"GET", "/repos/:owner/:repo/branches/:branch"}, - {"DELETE", "/repos/:owner/:repo"}, - {"GET", "/repos/:owner/:repo/collaborators"}, - {"GET", "/repos/:owner/:repo/collaborators/:user"}, - {"PUT", "/repos/:owner/:repo/collaborators/:user"}, - {"DELETE", "/repos/:owner/:repo/collaborators/:user"}, - {"GET", "/repos/:owner/:repo/comments"}, - {"GET", "/repos/:owner/:repo/commits/:sha/comments"}, - {"POST", "/repos/:owner/:repo/commits/:sha/comments"}, - {"GET", "/repos/:owner/:repo/comments/:id"}, - //{"PATCH", "/repos/:owner/:repo/comments/:id"}, - {"DELETE", "/repos/:owner/:repo/comments/:id"}, - {"GET", "/repos/:owner/:repo/commits"}, - {"GET", "/repos/:owner/:repo/commits/:sha"}, - {"GET", "/repos/:owner/:repo/readme"}, - //{"GET", "/repos/:owner/:repo/contents/*path"}, - //{"PUT", "/repos/:owner/:repo/contents/*path"}, - //{"DELETE", "/repos/:owner/:repo/contents/*path"}, - //{"GET", "/repos/:owner/:repo/:archive_format/:ref"}, - {"GET", "/repos/:owner/:repo/keys"}, - {"GET", "/repos/:owner/:repo/keys/:id"}, - {"POST", "/repos/:owner/:repo/keys"}, - //{"PATCH", "/repos/:owner/:repo/keys/:id"}, - {"DELETE", "/repos/:owner/:repo/keys/:id"}, - {"GET", "/repos/:owner/:repo/downloads"}, - {"GET", "/repos/:owner/:repo/downloads/:id"}, - {"DELETE", "/repos/:owner/:repo/downloads/:id"}, - {"GET", "/repos/:owner/:repo/forks"}, - {"POST", "/repos/:owner/:repo/forks"}, - {"GET", "/repos/:owner/:repo/hooks"}, - {"GET", "/repos/:owner/:repo/hooks/:id"}, - {"POST", "/repos/:owner/:repo/hooks"}, - //{"PATCH", "/repos/:owner/:repo/hooks/:id"}, - {"POST", "/repos/:owner/:repo/hooks/:id/tests"}, - {"DELETE", "/repos/:owner/:repo/hooks/:id"}, - {"POST", "/repos/:owner/:repo/merges"}, - {"GET", "/repos/:owner/:repo/releases"}, - {"GET", "/repos/:owner/:repo/releases/:id"}, - {"POST", "/repos/:owner/:repo/releases"}, - //{"PATCH", "/repos/:owner/:repo/releases/:id"}, - {"DELETE", "/repos/:owner/:repo/releases/:id"}, - {"GET", "/repos/:owner/:repo/releases/:id/assets"}, - {"GET", "/repos/:owner/:repo/stats/contributors"}, - {"GET", "/repos/:owner/:repo/stats/commit_activity"}, - {"GET", "/repos/:owner/:repo/stats/code_frequency"}, - {"GET", "/repos/:owner/:repo/stats/participation"}, - {"GET", "/repos/:owner/:repo/stats/punch_card"}, - {"GET", "/repos/:owner/:repo/statuses/:ref"}, - {"POST", "/repos/:owner/:repo/statuses/:ref"}, + {http.MethodGet, "/user/repos"}, + {http.MethodGet, "/users/:user/repos"}, + {http.MethodGet, "/orgs/:org/repos"}, + {http.MethodGet, "/repositories"}, + {http.MethodPost, "/user/repos"}, + {http.MethodPost, "/orgs/:org/repos"}, + {http.MethodGet, "/repos/:owner/:repo"}, + //{http.MethodPatch, "/repos/:owner/:repo"}, + {http.MethodGet, "/repos/:owner/:repo/contributors"}, + {http.MethodGet, "/repos/:owner/:repo/languages"}, + {http.MethodGet, "/repos/:owner/:repo/teams"}, + {http.MethodGet, "/repos/:owner/:repo/tags"}, + {http.MethodGet, "/repos/:owner/:repo/branches"}, + {http.MethodGet, "/repos/:owner/:repo/branches/:branch"}, + {http.MethodDelete, "/repos/:owner/:repo"}, + {http.MethodGet, "/repos/:owner/:repo/collaborators"}, + {http.MethodGet, "/repos/:owner/:repo/collaborators/:user"}, + {http.MethodPut, "/repos/:owner/:repo/collaborators/:user"}, + {http.MethodDelete, "/repos/:owner/:repo/collaborators/:user"}, + {http.MethodGet, "/repos/:owner/:repo/comments"}, + {http.MethodGet, "/repos/:owner/:repo/commits/:sha/comments"}, + {http.MethodPost, "/repos/:owner/:repo/commits/:sha/comments"}, + {http.MethodGet, "/repos/:owner/:repo/comments/:id"}, + //{http.MethodPatch, "/repos/:owner/:repo/comments/:id"}, + {http.MethodDelete, "/repos/:owner/:repo/comments/:id"}, + {http.MethodGet, "/repos/:owner/:repo/commits"}, + {http.MethodGet, "/repos/:owner/:repo/commits/:sha"}, + {http.MethodGet, "/repos/:owner/:repo/readme"}, + //{http.MethodGet, "/repos/:owner/:repo/contents/*path"}, + //{http.MethodPut, "/repos/:owner/:repo/contents/*path"}, + //{http.MethodDelete, "/repos/:owner/:repo/contents/*path"}, + //{http.MethodGet, "/repos/:owner/:repo/:archive_format/:ref"}, + {http.MethodGet, "/repos/:owner/:repo/keys"}, + {http.MethodGet, "/repos/:owner/:repo/keys/:id"}, + {http.MethodPost, "/repos/:owner/:repo/keys"}, + //{http.MethodPatch, "/repos/:owner/:repo/keys/:id"}, + {http.MethodDelete, "/repos/:owner/:repo/keys/:id"}, + {http.MethodGet, "/repos/:owner/:repo/downloads"}, + {http.MethodGet, "/repos/:owner/:repo/downloads/:id"}, + {http.MethodDelete, "/repos/:owner/:repo/downloads/:id"}, + {http.MethodGet, "/repos/:owner/:repo/forks"}, + {http.MethodPost, "/repos/:owner/:repo/forks"}, + {http.MethodGet, "/repos/:owner/:repo/hooks"}, + {http.MethodGet, "/repos/:owner/:repo/hooks/:id"}, + {http.MethodPost, "/repos/:owner/:repo/hooks"}, + //{http.MethodPatch, "/repos/:owner/:repo/hooks/:id"}, + {http.MethodPost, "/repos/:owner/:repo/hooks/:id/tests"}, + {http.MethodDelete, "/repos/:owner/:repo/hooks/:id"}, + {http.MethodPost, "/repos/:owner/:repo/merges"}, + {http.MethodGet, "/repos/:owner/:repo/releases"}, + {http.MethodGet, "/repos/:owner/:repo/releases/:id"}, + {http.MethodPost, "/repos/:owner/:repo/releases"}, + //{http.MethodPatch, "/repos/:owner/:repo/releases/:id"}, + {http.MethodDelete, "/repos/:owner/:repo/releases/:id"}, + {http.MethodGet, "/repos/:owner/:repo/releases/:id/assets"}, + {http.MethodGet, "/repos/:owner/:repo/stats/contributors"}, + {http.MethodGet, "/repos/:owner/:repo/stats/commit_activity"}, + {http.MethodGet, "/repos/:owner/:repo/stats/code_frequency"}, + {http.MethodGet, "/repos/:owner/:repo/stats/participation"}, + {http.MethodGet, "/repos/:owner/:repo/stats/punch_card"}, + {http.MethodGet, "/repos/:owner/:repo/statuses/:ref"}, + {http.MethodPost, "/repos/:owner/:repo/statuses/:ref"}, // Search - {"GET", "/search/repositories"}, - {"GET", "/search/code"}, - {"GET", "/search/issues"}, - {"GET", "/search/users"}, - {"GET", "/legacy/issues/search/:owner/:repository/:state/:keyword"}, - {"GET", "/legacy/repos/search/:keyword"}, - {"GET", "/legacy/user/search/:keyword"}, - {"GET", "/legacy/user/email/:email"}, + {http.MethodGet, "/search/repositories"}, + {http.MethodGet, "/search/code"}, + {http.MethodGet, "/search/issues"}, + {http.MethodGet, "/search/users"}, + {http.MethodGet, "/legacy/issues/search/:owner/:repository/:state/:keyword"}, + {http.MethodGet, "/legacy/repos/search/:keyword"}, + {http.MethodGet, "/legacy/user/search/:keyword"}, + {http.MethodGet, "/legacy/user/email/:email"}, // Users - {"GET", "/users/:user"}, - {"GET", "/user"}, - //{"PATCH", "/user"}, - {"GET", "/users"}, - {"GET", "/user/emails"}, - {"POST", "/user/emails"}, - {"DELETE", "/user/emails"}, - {"GET", "/users/:user/followers"}, - {"GET", "/user/followers"}, - {"GET", "/users/:user/following"}, - {"GET", "/user/following"}, - {"GET", "/user/following/:user"}, - {"GET", "/users/:user/following/:target_user"}, - {"PUT", "/user/following/:user"}, - {"DELETE", "/user/following/:user"}, - {"GET", "/users/:user/keys"}, - {"GET", "/user/keys"}, - {"GET", "/user/keys/:id"}, - {"POST", "/user/keys"}, - //{"PATCH", "/user/keys/:id"}, - {"DELETE", "/user/keys/:id"}, + {http.MethodGet, "/users/:user"}, + {http.MethodGet, "/user"}, + //{http.MethodPatch, "/user"}, + {http.MethodGet, "/users"}, + {http.MethodGet, "/user/emails"}, + {http.MethodPost, "/user/emails"}, + {http.MethodDelete, "/user/emails"}, + {http.MethodGet, "/users/:user/followers"}, + {http.MethodGet, "/user/followers"}, + {http.MethodGet, "/users/:user/following"}, + {http.MethodGet, "/user/following"}, + {http.MethodGet, "/user/following/:user"}, + {http.MethodGet, "/users/:user/following/:target_user"}, + {http.MethodPut, "/user/following/:user"}, + {http.MethodDelete, "/user/following/:user"}, + {http.MethodGet, "/users/:user/keys"}, + {http.MethodGet, "/user/keys"}, + {http.MethodGet, "/user/keys/:id"}, + {http.MethodPost, "/user/keys"}, + //{http.MethodPatch, "/user/keys/:id"}, + {http.MethodDelete, "/user/keys/:id"}, } func TestShouldBindUri(t *testing.T) { @@ -293,7 +293,7 @@ func TestShouldBindUri(t *testing.T) { Name string `uri:"name" binding:"required"` Id string `uri:"id" binding:"required"` } - router.Handle("GET", "/rest/:name/:id", func(c *Context) { + router.Handle(http.MethodGet, "/rest/:name/:id", func(c *Context) { var person Person assert.NoError(t, c.ShouldBindUri(&person)) assert.True(t, "" != person.Name) @@ -302,7 +302,7 @@ func TestShouldBindUri(t *testing.T) { }) path, _ := exampleFromPath("/rest/:name/:id") - w := performRequest(router, "GET", path) + w := performRequest(router, http.MethodGet, path) assert.Equal(t, "ShouldBindUri test OK", w.Body.String()) assert.Equal(t, http.StatusOK, w.Code) } @@ -315,7 +315,7 @@ func TestBindUri(t *testing.T) { Name string `uri:"name" binding:"required"` Id string `uri:"id" binding:"required"` } - router.Handle("GET", "/rest/:name/:id", func(c *Context) { + router.Handle(http.MethodGet, "/rest/:name/:id", func(c *Context) { var person Person assert.NoError(t, c.BindUri(&person)) assert.True(t, "" != person.Name) @@ -324,7 +324,7 @@ func TestBindUri(t *testing.T) { }) path, _ := exampleFromPath("/rest/:name/:id") - w := performRequest(router, "GET", path) + w := performRequest(router, http.MethodGet, path) assert.Equal(t, "BindUri test OK", w.Body.String()) assert.Equal(t, http.StatusOK, w.Code) } @@ -336,13 +336,13 @@ func TestBindUriError(t *testing.T) { type Member struct { Number string `uri:"num" binding:"required,uuid"` } - router.Handle("GET", "/new/rest/:num", func(c *Context) { + router.Handle(http.MethodGet, "/new/rest/:num", func(c *Context) { var m Member assert.Error(t, c.BindUri(&m)) }) path1, _ := exampleFromPath("/new/rest/:num") - w1 := performRequest(router, "GET", path1) + w1 := performRequest(router, http.MethodGet, path1) assert.Equal(t, http.StatusBadRequest, w1.Code) } @@ -358,7 +358,7 @@ func TestRaceContextCopy(t *testing.T) { go readWriteKeys(c.Copy()) c.String(http.StatusOK, "run OK, no panics") }) - w := performRequest(router, "GET", "/test/copy/race") + w := performRequest(router, http.MethodGet, "/test/copy/race") assert.Equal(t, "run OK, no panics", w.Body.String()) } @@ -438,7 +438,7 @@ func exampleFromPath(path string) (string, Params) { func BenchmarkGithub(b *testing.B) { router := New() githubConfigRouter(router) - runRequest(b, router, "GET", "/legacy/issues/search/:owner/:repository/:state/:keyword") + runRequest(b, router, http.MethodGet, "/legacy/issues/search/:owner/:repository/:state/:keyword") } func BenchmarkParallelGithub(b *testing.B) { @@ -446,7 +446,7 @@ func BenchmarkParallelGithub(b *testing.B) { router := New() githubConfigRouter(router) - req, _ := http.NewRequest("POST", "/repos/manucorporat/sse/git/blobs", nil) + req, _ := http.NewRequest(http.MethodPost, "/repos/manucorporat/sse/git/blobs", nil) b.RunParallel(func(pb *testing.PB) { // Each goroutine has its own bytes.Buffer. @@ -462,7 +462,7 @@ func BenchmarkParallelGithubDefault(b *testing.B) { router := New() githubConfigRouter(router) - req, _ := http.NewRequest("POST", "/repos/manucorporat/sse/git/blobs", nil) + req, _ := http.NewRequest(http.MethodPost, "/repos/manucorporat/sse/git/blobs", nil) b.RunParallel(func(pb *testing.PB) { // Each goroutine has its own bytes.Buffer. diff --git a/logger.go b/logger.go index fcf90c25..d5b96b3e 100644 --- a/logger.go +++ b/logger.go @@ -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.MethodPatch: return green - case "HEAD": + case http.MethodHead: return magenta - case "OPTIONS": + case http.MethodOptions: return white default: return reset diff --git a/routergroup.go b/routergroup.go index 2e7a5b90..9ff7c038 100644 --- a/routergroup.go +++ b/routergroup.go @@ -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() } diff --git a/routergroup_test.go b/routergroup_test.go index ce3d54a2..0e49d65b 100644 --- a/routergroup_test.go +++ b/routergroup_test.go @@ -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) { @@ -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: @@ -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", "/") @@ -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)) diff --git a/routes_test.go b/routes_test.go index d1ddbe91..ee6ea823 100644 --- a/routes_test.go +++ b/routes_test.go @@ -70,10 +70,10 @@ func testRouteNotOK2(method string, t *testing.T) { router := New() router.HandleMethodNotAllowed = true var methodRoute string - if method == "POST" { - methodRoute = "GET" + if method == http.MethodPost { + methodRoute = http.MethodGet } else { - methodRoute = "POST" + methodRoute = http.MethodPost } router.Handle(methodRoute, "/test", func(c *Context) { passed = true @@ -99,46 +99,46 @@ func TestRouterMethod(t *testing.T) { c.String(http.StatusOK, "sup3") }) - w := performRequest(router, "PUT", "/hey") + w := performRequest(router, http.MethodPut, "/hey") assert.Equal(t, http.StatusOK, w.Code) assert.Equal(t, "called", w.Body.String()) } func TestRouterGroupRouteOK(t *testing.T) { - testRouteOK("GET", t) - testRouteOK("POST", t) - testRouteOK("PUT", t) - testRouteOK("PATCH", t) - testRouteOK("HEAD", t) - testRouteOK("OPTIONS", t) - testRouteOK("DELETE", t) - testRouteOK("CONNECT", t) - testRouteOK("TRACE", t) + testRouteOK(http.MethodGet, t) + testRouteOK(http.MethodPost, t) + testRouteOK(http.MethodPut, t) + testRouteOK(http.MethodPatch, t) + testRouteOK(http.MethodHead, t) + testRouteOK(http.MethodOptions, t) + testRouteOK(http.MethodDelete, t) + testRouteOK(http.MethodConnect, t) + testRouteOK(http.MethodTrace, t) } func TestRouteNotOK(t *testing.T) { - testRouteNotOK("GET", t) - testRouteNotOK("POST", t) - testRouteNotOK("PUT", t) - testRouteNotOK("PATCH", t) - testRouteNotOK("HEAD", t) - testRouteNotOK("OPTIONS", t) - testRouteNotOK("DELETE", t) - testRouteNotOK("CONNECT", t) - testRouteNotOK("TRACE", t) + testRouteNotOK(http.MethodGet, t) + testRouteNotOK(http.MethodPost, t) + testRouteNotOK(http.MethodPut, t) + testRouteNotOK(http.MethodPatch, t) + testRouteNotOK(http.MethodHead, t) + testRouteNotOK(http.MethodOptions, t) + testRouteNotOK(http.MethodDelete, t) + testRouteNotOK(http.MethodConnect, t) + testRouteNotOK(http.MethodTrace, t) } func TestRouteNotOK2(t *testing.T) { - testRouteNotOK2("GET", t) - testRouteNotOK2("POST", t) - testRouteNotOK2("PUT", t) - testRouteNotOK2("PATCH", t) - testRouteNotOK2("HEAD", t) - testRouteNotOK2("OPTIONS", t) - testRouteNotOK2("DELETE", t) - testRouteNotOK2("CONNECT", t) - testRouteNotOK2("TRACE", t) + testRouteNotOK2(http.MethodGet, t) + testRouteNotOK2(http.MethodPost, t) + testRouteNotOK2(http.MethodPut, t) + testRouteNotOK2(http.MethodPatch, t) + testRouteNotOK2(http.MethodHead, t) + testRouteNotOK2(http.MethodOptions, t) + testRouteNotOK2(http.MethodDelete, t) + testRouteNotOK2(http.MethodConnect, t) + testRouteNotOK2(http.MethodTrace, t) } func TestRouteRedirectTrailingSlash(t *testing.T) { @@ -150,50 +150,50 @@ func TestRouteRedirectTrailingSlash(t *testing.T) { router.POST("/path3", func(c *Context) {}) router.PUT("/path4/", func(c *Context) {}) - w := performRequest(router, "GET", "/path/") + w := performRequest(router, http.MethodGet, "/path/") assert.Equal(t, "/path", w.Header().Get("Location")) assert.Equal(t, http.StatusMovedPermanently, w.Code) - w = performRequest(router, "GET", "/path2") + w = performRequest(router, http.MethodGet, "/path2") assert.Equal(t, "/path2/", w.Header().Get("Location")) assert.Equal(t, http.StatusMovedPermanently, w.Code) - w = performRequest(router, "POST", "/path3/") + w = performRequest(router, http.MethodPost, "/path3/") assert.Equal(t, "/path3", w.Header().Get("Location")) assert.Equal(t, http.StatusTemporaryRedirect, w.Code) - w = performRequest(router, "PUT", "/path4") + w = performRequest(router, http.MethodPut, "/path4") assert.Equal(t, "/path4/", w.Header().Get("Location")) assert.Equal(t, http.StatusTemporaryRedirect, w.Code) - w = performRequest(router, "GET", "/path") + w = performRequest(router, http.MethodGet, "/path") assert.Equal(t, http.StatusOK, w.Code) - w = performRequest(router, "GET", "/path2/") + w = performRequest(router, http.MethodGet, "/path2/") assert.Equal(t, http.StatusOK, w.Code) - w = performRequest(router, "POST", "/path3") + w = performRequest(router, http.MethodPost, "/path3") assert.Equal(t, http.StatusOK, w.Code) - w = performRequest(router, "PUT", "/path4/") + w = performRequest(router, http.MethodPut, "/path4/") assert.Equal(t, http.StatusOK, w.Code) - w = performRequest(router, "GET", "/path2", header{Key: "X-Forwarded-Prefix", Value: "/api"}) + w = performRequest(router, http.MethodGet, "/path2", header{Key: "X-Forwarded-Prefix", Value: "/api"}) assert.Equal(t, "/api/path2/", w.Header().Get("Location")) assert.Equal(t, 301, w.Code) - w = performRequest(router, "GET", "/path2/", header{Key: "X-Forwarded-Prefix", Value: "/api/"}) + w = performRequest(router, http.MethodGet, "/path2/", header{Key: "X-Forwarded-Prefix", Value: "/api/"}) assert.Equal(t, 200, w.Code) router.RedirectTrailingSlash = false - w = performRequest(router, "GET", "/path/") + w = performRequest(router, http.MethodGet, "/path/") assert.Equal(t, http.StatusNotFound, w.Code) - w = performRequest(router, "GET", "/path2") + w = performRequest(router, http.MethodGet, "/path2") assert.Equal(t, http.StatusNotFound, w.Code) - w = performRequest(router, "POST", "/path3/") + w = performRequest(router, http.MethodPost, "/path3/") assert.Equal(t, http.StatusNotFound, w.Code) - w = performRequest(router, "PUT", "/path4") + w = performRequest(router, http.MethodPut, "/path4") assert.Equal(t, http.StatusNotFound, w.Code) } @@ -207,19 +207,19 @@ func TestRouteRedirectFixedPath(t *testing.T) { router.POST("/PATH3", func(c *Context) {}) router.POST("/Path4/", func(c *Context) {}) - w := performRequest(router, "GET", "/PATH") + w := performRequest(router, http.MethodGet, "/PATH") assert.Equal(t, "/path", w.Header().Get("Location")) assert.Equal(t, http.StatusMovedPermanently, w.Code) - w = performRequest(router, "GET", "/path2") + w = performRequest(router, http.MethodGet, "/path2") assert.Equal(t, "/Path2", w.Header().Get("Location")) assert.Equal(t, http.StatusMovedPermanently, w.Code) - w = performRequest(router, "POST", "/path3") + w = performRequest(router, http.MethodPost, "/path3") assert.Equal(t, "/PATH3", w.Header().Get("Location")) assert.Equal(t, http.StatusTemporaryRedirect, w.Code) - w = performRequest(router, "POST", "/path4") + w = performRequest(router, http.MethodPost, "/path4") assert.Equal(t, "/Path4/", w.Header().Get("Location")) assert.Equal(t, http.StatusTemporaryRedirect, w.Code) } @@ -249,7 +249,7 @@ func TestRouteParamsByName(t *testing.T) { assert.False(t, ok) }) - w := performRequest(router, "GET", "/test/john/smith/is/super/great") + w := performRequest(router, http.MethodGet, "/test/john/smith/is/super/great") assert.Equal(t, http.StatusOK, w.Code) assert.Equal(t, "john", name) @@ -283,7 +283,7 @@ func TestRouteParamsByNameWithExtraSlash(t *testing.T) { assert.False(t, ok) }) - w := performRequest(router, "GET", "//test//john//smith//is//super//great") + w := performRequest(router, http.MethodGet, "//test//john//smith//is//super//great") assert.Equal(t, http.StatusOK, w.Code) assert.Equal(t, "john", name) @@ -311,16 +311,16 @@ func TestRouteStaticFile(t *testing.T) { router.Static("/using_static", dir) router.StaticFile("/result", f.Name()) - w := performRequest(router, "GET", "/using_static/"+filename) - w2 := performRequest(router, "GET", "/result") + w := performRequest(router, http.MethodGet, "/using_static/"+filename) + w2 := performRequest(router, http.MethodGet, "/result") assert.Equal(t, w, w2) assert.Equal(t, http.StatusOK, w.Code) assert.Equal(t, "Gin Web Framework", w.Body.String()) assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type")) - w3 := performRequest(router, "HEAD", "/using_static/"+filename) - w4 := performRequest(router, "HEAD", "/result") + w3 := performRequest(router, http.MethodHead, "/using_static/"+filename) + w4 := performRequest(router, http.MethodHead, "/result") assert.Equal(t, w3, w4) assert.Equal(t, http.StatusOK, w3.Code) @@ -331,7 +331,7 @@ func TestRouteStaticListingDir(t *testing.T) { router := New() router.StaticFS("/", Dir("./", true)) - w := performRequest(router, "GET", "/") + w := performRequest(router, http.MethodGet, "/") assert.Equal(t, http.StatusOK, w.Code) assert.Contains(t, w.Body.String(), "gin.go") @@ -343,7 +343,7 @@ func TestRouteStaticNoListing(t *testing.T) { router := New() router.Static("/", "./") - w := performRequest(router, "GET", "/") + w := performRequest(router, http.MethodGet, "/") assert.Equal(t, http.StatusNotFound, w.Code) assert.NotContains(t, w.Body.String(), "gin.go") @@ -358,7 +358,7 @@ func TestRouterMiddlewareAndStatic(t *testing.T) { }) static.Static("/", "./") - w := performRequest(router, "GET", "/gin.go") + w := performRequest(router, http.MethodGet, "/gin.go") assert.Equal(t, http.StatusOK, w.Code) assert.Contains(t, w.Body.String(), "package gin") @@ -372,13 +372,13 @@ func TestRouteNotAllowedEnabled(t *testing.T) { router := New() router.HandleMethodNotAllowed = true router.POST("/path", func(c *Context) {}) - w := performRequest(router, "GET", "/path") + w := performRequest(router, http.MethodGet, "/path") assert.Equal(t, http.StatusMethodNotAllowed, w.Code) router.NoMethod(func(c *Context) { c.String(http.StatusTeapot, "responseText") }) - w = performRequest(router, "GET", "/path") + w = performRequest(router, http.MethodGet, "/path") assert.Equal(t, "responseText", w.Body.String()) assert.Equal(t, http.StatusTeapot, w.Code) } @@ -387,9 +387,9 @@ func TestRouteNotAllowedEnabled2(t *testing.T) { router := New() router.HandleMethodNotAllowed = true // add one methodTree to trees - router.addRoute("POST", "/", HandlersChain{func(_ *Context) {}}) + router.addRoute(http.MethodPost, "/", HandlersChain{func(_ *Context) {}}) router.GET("/path2", func(c *Context) {}) - w := performRequest(router, "POST", "/path2") + w := performRequest(router, http.MethodPost, "/path2") assert.Equal(t, http.StatusMethodNotAllowed, w.Code) } @@ -397,13 +397,13 @@ func TestRouteNotAllowedDisabled(t *testing.T) { router := New() router.HandleMethodNotAllowed = false router.POST("/path", func(c *Context) {}) - w := performRequest(router, "GET", "/path") + w := performRequest(router, http.MethodGet, "/path") assert.Equal(t, http.StatusNotFound, w.Code) router.NoMethod(func(c *Context) { c.String(http.StatusTeapot, "responseText") }) - w = performRequest(router, "GET", "/path") + w = performRequest(router, http.MethodGet, "/path") assert.Equal(t, "404 page not found", w.Body.String()) assert.Equal(t, http.StatusNotFound, w.Code) } @@ -453,7 +453,7 @@ func TestRouterNotFound(t *testing.T) { {"/nope", http.StatusNotFound, ""}, // NotFound } for _, tr := range testRoutes { - w := performRequest(router, "GET", tr.route) + w := performRequest(router, http.MethodGet, tr.route) assert.Equal(t, tr.code, w.Code) if w.Code != http.StatusNotFound { assert.Equal(t, tr.location, fmt.Sprint(w.Header().Get("Location"))) @@ -466,20 +466,20 @@ func TestRouterNotFound(t *testing.T) { c.AbortWithStatus(http.StatusNotFound) notFound = true }) - w := performRequest(router, "GET", "/nope") + w := performRequest(router, http.MethodGet, "/nope") assert.Equal(t, http.StatusNotFound, w.Code) assert.True(t, notFound) // Test other method than GET (want 307 instead of 301) router.PATCH("/path", func(c *Context) {}) - w = performRequest(router, "PATCH", "/path/") + w = performRequest(router, http.MethodPatch, "/path/") assert.Equal(t, http.StatusTemporaryRedirect, w.Code) assert.Equal(t, "map[Location:[/path]]", fmt.Sprint(w.Header())) // Test special case where no node for the prefix "/" exists router = New() router.GET("/a", func(c *Context) {}) - w = performRequest(router, "GET", "/") + w = performRequest(router, http.MethodGet, "/") assert.Equal(t, http.StatusNotFound, w.Code) } @@ -490,10 +490,10 @@ func TestRouterStaticFSNotFound(t *testing.T) { c.String(404, "non existent") }) - w := performRequest(router, "GET", "/nonexistent") + w := performRequest(router, http.MethodGet, "/nonexistent") assert.Equal(t, "non existent", w.Body.String()) - w = performRequest(router, "HEAD", "/nonexistent") + w = performRequest(router, http.MethodHead, "/nonexistent") assert.Equal(t, "non existent", w.Body.String()) } @@ -503,7 +503,7 @@ func TestRouterStaticFSFileNotFound(t *testing.T) { router.StaticFS("/", http.FileSystem(http.Dir("."))) assert.NotPanics(t, func() { - performRequest(router, "GET", "/nonexistent") + performRequest(router, http.MethodGet, "/nonexistent") }) } @@ -520,11 +520,11 @@ func TestMiddlewareCalledOnceByRouterStaticFSNotFound(t *testing.T) { router.StaticFS("/", http.FileSystem(http.Dir("/thisreallydoesntexist/"))) // First access - performRequest(router, "GET", "/nonexistent") + performRequest(router, http.MethodGet, "/nonexistent") assert.Equal(t, 1, middlewareCalledNum) // Second access - performRequest(router, "HEAD", "/nonexistent") + performRequest(router, http.MethodHead, "/nonexistent") assert.Equal(t, 2, middlewareCalledNum) } @@ -543,7 +543,7 @@ func TestRouteRawPath(t *testing.T) { assert.Equal(t, "222", num) }) - w := performRequest(route, "POST", "/project/Some%2FOther%2FProject/build/222") + w := performRequest(route, http.MethodPost, "/project/Some%2FOther%2FProject/build/222") assert.Equal(t, http.StatusOK, w.Code) } @@ -563,7 +563,7 @@ func TestRouteRawPathNoUnescape(t *testing.T) { assert.Equal(t, "333", num) }) - w := performRequest(route, "POST", "/project/Some%2FOther%2FProject/build/333") + w := performRequest(route, http.MethodPost, "/project/Some%2FOther%2FProject/build/333") assert.Equal(t, http.StatusOK, w.Code) } @@ -574,7 +574,7 @@ func TestRouteServeErrorWithWriteHeader(t *testing.T) { c.Next() }) - w := performRequest(route, "GET", "/NotFound") + w := performRequest(route, http.MethodGet, "/NotFound") assert.Equal(t, 421, w.Code) assert.Equal(t, 0, w.Body.Len()) } @@ -605,7 +605,7 @@ func TestRouteContextHoldsFullPath(t *testing.T) { } for _, route := range routes { - w := performRequest(router, "GET", route) + w := performRequest(router, http.MethodGet, route) assert.Equal(t, http.StatusOK, w.Code) } @@ -615,6 +615,6 @@ func TestRouteContextHoldsFullPath(t *testing.T) { assert.Equal(t, "", c.FullPath()) }) - w := performRequest(router, "GET", "/not-found") + w := performRequest(router, http.MethodGet, "/not-found") assert.Equal(t, http.StatusNotFound, w.Code) }