githubapi_test.go
This file
Path
githubapi_test.go
476 lines · 17.0 KiB · Go
Last changed by
ci(lint): enable perfsprint linter (#4090)Matthieu MORELbefore adoption02c1144
History of this fileTake it
The walk stopped after 200 commits: lines older than 55e27f1 point at it.
fix(engine): missing route params for CreateTestContext (#2778) (#2803)RoCrybefore adoption55e27f1
lines 1–12why, one step back1// Copyright 2014 Manu Martinez-Almeida. All rights reserved.2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45package gin67import (8 "fmt"9 "math/rand"10 "net/http"11 "net/http/httptest"12 "os"ci(lint): enable perfsprint linter (#4090)Matthieu MORELbefore adoption02c1144
line 13why, one step back13 "strconv"Replace bytes.Buffer with strings.Builder where appropriate (#3347)hopehookbefore adoptionb2d4185
line 14why, one step back14 "strings"fix(engine): missing route params for CreateTestContext (#2778) (#2803)RoCrybefore adoption55e27f1
lines 15–17why, one step back15 "testing"1617 "github.com/stretchr/testify/assert"ci(lint): enable testifylint linter (#4010)Matthieu MORELbefore adoption5f55c6a
line 18why, one step back18 "github.com/stretchr/testify/require"fix(engine): missing route params for CreateTestContext (#2778) (#2803)RoCrybefore adoption55e27f1
lines 19–299why, one step back19)2021type route struct {22 method string23 path string24}2526// http://developer.github.com/v3/27var githubAPI = []route{28 // OAuth Authorizations29 {http.MethodGet, "/authorizations"},30 {http.MethodGet, "/authorizations/:id"},31 {http.MethodPost, "/authorizations"},32 //{http.MethodPut, "/authorizations/clients/:client_id"},33 //{http.MethodPatch, "/authorizations/:id"},34 {http.MethodDelete, "/authorizations/:id"},35 {http.MethodGet, "/applications/:client_id/tokens/:access_token"},36 {http.MethodDelete, "/applications/:client_id/tokens"},37 {http.MethodDelete, "/applications/:client_id/tokens/:access_token"},3839 // Activity40 {http.MethodGet, "/events"},41 {http.MethodGet, "/repos/:owner/:repo/events"},42 {http.MethodGet, "/networks/:owner/:repo/events"},43 {http.MethodGet, "/orgs/:org/events"},44 {http.MethodGet, "/users/:user/received_events"},45 {http.MethodGet, "/users/:user/received_events/public"},46 {http.MethodGet, "/users/:user/events"},47 {http.MethodGet, "/users/:user/events/public"},48 {http.MethodGet, "/users/:user/events/orgs/:org"},49 {http.MethodGet, "/feeds"},50 {http.MethodGet, "/notifications"},51 {http.MethodGet, "/repos/:owner/:repo/notifications"},52 {http.MethodPut, "/notifications"},53 {http.MethodPut, "/repos/:owner/:repo/notifications"},54 {http.MethodGet, "/notifications/threads/:id"},55 //{http.MethodPatch, "/notifications/threads/:id"},56 {http.MethodGet, "/notifications/threads/:id/subscription"},57 {http.MethodPut, "/notifications/threads/:id/subscription"},58 {http.MethodDelete, "/notifications/threads/:id/subscription"},59 {http.MethodGet, "/repos/:owner/:repo/stargazers"},60 {http.MethodGet, "/users/:user/starred"},61 {http.MethodGet, "/user/starred"},62 {http.MethodGet, "/user/starred/:owner/:repo"},63 {http.MethodPut, "/user/starred/:owner/:repo"},64 {http.MethodDelete, "/user/starred/:owner/:repo"},65 {http.MethodGet, "/repos/:owner/:repo/subscribers"},66 {http.MethodGet, "/users/:user/subscriptions"},67 {http.MethodGet, "/user/subscriptions"},68 {http.MethodGet, "/repos/:owner/:repo/subscription"},69 {http.MethodPut, "/repos/:owner/:repo/subscription"},70 {http.MethodDelete, "/repos/:owner/:repo/subscription"},71 {http.MethodGet, "/user/subscriptions/:owner/:repo"},72 {http.MethodPut, "/user/subscriptions/:owner/:repo"},73 {http.MethodDelete, "/user/subscriptions/:owner/:repo"},7475 // Gists76 {http.MethodGet, "/users/:user/gists"},77 {http.MethodGet, "/gists"},78 //{http.MethodGet, "/gists/public"},79 //{http.MethodGet, "/gists/starred"},80 {http.MethodGet, "/gists/:id"},81 {http.MethodPost, "/gists"},82 //{http.MethodPatch, "/gists/:id"},83 {http.MethodPut, "/gists/:id/star"},84 {http.MethodDelete, "/gists/:id/star"},85 {http.MethodGet, "/gists/:id/star"},86 {http.MethodPost, "/gists/:id/forks"},87 {http.MethodDelete, "/gists/:id"},8889 // Git Data90 {http.MethodGet, "/repos/:owner/:repo/git/blobs/:sha"},91 {http.MethodPost, "/repos/:owner/:repo/git/blobs"},92 {http.MethodGet, "/repos/:owner/:repo/git/commits/:sha"},93 {http.MethodPost, "/repos/:owner/:repo/git/commits"},94 //{http.MethodGet, "/repos/:owner/:repo/git/refs/*ref"},95 {http.MethodGet, "/repos/:owner/:repo/git/refs"},96 {http.MethodPost, "/repos/:owner/:repo/git/refs"},97 //{http.MethodPatch, "/repos/:owner/:repo/git/refs/*ref"},98 //{http.MethodDelete, "/repos/:owner/:repo/git/refs/*ref"},99 {http.MethodGet, "/repos/:owner/:repo/git/tags/:sha"},100 {http.MethodPost, "/repos/:owner/:repo/git/tags"},101 {http.MethodGet, "/repos/:owner/:repo/git/trees/:sha"},102 {http.MethodPost, "/repos/:owner/:repo/git/trees"},103104 // Issues105 {http.MethodGet, "/issues"},106 {http.MethodGet, "/user/issues"},107 {http.MethodGet, "/orgs/:org/issues"},108 {http.MethodGet, "/repos/:owner/:repo/issues"},109 {http.MethodGet, "/repos/:owner/:repo/issues/:number"},110 {http.MethodPost, "/repos/:owner/:repo/issues"},111 //{http.MethodPatch, "/repos/:owner/:repo/issues/:number"},112 {http.MethodGet, "/repos/:owner/:repo/assignees"},113 {http.MethodGet, "/repos/:owner/:repo/assignees/:assignee"},114 {http.MethodGet, "/repos/:owner/:repo/issues/:number/comments"},115 //{http.MethodGet, "/repos/:owner/:repo/issues/comments"},116 //{http.MethodGet, "/repos/:owner/:repo/issues/comments/:id"},117 {http.MethodPost, "/repos/:owner/:repo/issues/:number/comments"},118 //{http.MethodPatch, "/repos/:owner/:repo/issues/comments/:id"},119 //{http.MethodDelete, "/repos/:owner/:repo/issues/comments/:id"},120 {http.MethodGet, "/repos/:owner/:repo/issues/:number/events"},121 //{http.MethodGet, "/repos/:owner/:repo/issues/events"},122 //{http.MethodGet, "/repos/:owner/:repo/issues/events/:id"},123 {http.MethodGet, "/repos/:owner/:repo/labels"},124 {http.MethodGet, "/repos/:owner/:repo/labels/:name"},125 {http.MethodPost, "/repos/:owner/:repo/labels"},126 //{http.MethodPatch, "/repos/:owner/:repo/labels/:name"},127 {http.MethodDelete, "/repos/:owner/:repo/labels/:name"},128 {http.MethodGet, "/repos/:owner/:repo/issues/:number/labels"},129 {http.MethodPost, "/repos/:owner/:repo/issues/:number/labels"},130 {http.MethodDelete, "/repos/:owner/:repo/issues/:number/labels/:name"},131 {http.MethodPut, "/repos/:owner/:repo/issues/:number/labels"},132 {http.MethodDelete, "/repos/:owner/:repo/issues/:number/labels"},133 {http.MethodGet, "/repos/:owner/:repo/milestones/:number/labels"},134 {http.MethodGet, "/repos/:owner/:repo/milestones"},135 {http.MethodGet, "/repos/:owner/:repo/milestones/:number"},136 {http.MethodPost, "/repos/:owner/:repo/milestones"},137 //{http.MethodPatch, "/repos/:owner/:repo/milestones/:number"},138 {http.MethodDelete, "/repos/:owner/:repo/milestones/:number"},139140 // Miscellaneous141 {http.MethodGet, "/emojis"},142 {http.MethodGet, "/gitignore/templates"},143 {http.MethodGet, "/gitignore/templates/:name"},144 {http.MethodPost, "/markdown"},145 {http.MethodPost, "/markdown/raw"},146 {http.MethodGet, "/meta"},147 {http.MethodGet, "/rate_limit"},148149 // Organizations150 {http.MethodGet, "/users/:user/orgs"},151 {http.MethodGet, "/user/orgs"},152 {http.MethodGet, "/orgs/:org"},153 //{http.MethodPatch, "/orgs/:org"},154 {http.MethodGet, "/orgs/:org/members"},155 {http.MethodGet, "/orgs/:org/members/:user"},156 {http.MethodDelete, "/orgs/:org/members/:user"},157 {http.MethodGet, "/orgs/:org/public_members"},158 {http.MethodGet, "/orgs/:org/public_members/:user"},159 {http.MethodPut, "/orgs/:org/public_members/:user"},160 {http.MethodDelete, "/orgs/:org/public_members/:user"},161 {http.MethodGet, "/orgs/:org/teams"},162 {http.MethodGet, "/teams/:id"},163 {http.MethodPost, "/orgs/:org/teams"},164 //{http.MethodPatch, "/teams/:id"},165 {http.MethodDelete, "/teams/:id"},166 {http.MethodGet, "/teams/:id/members"},167 {http.MethodGet, "/teams/:id/members/:user"},168 {http.MethodPut, "/teams/:id/members/:user"},169 {http.MethodDelete, "/teams/:id/members/:user"},170 {http.MethodGet, "/teams/:id/repos"},171 {http.MethodGet, "/teams/:id/repos/:owner/:repo"},172 {http.MethodPut, "/teams/:id/repos/:owner/:repo"},173 {http.MethodDelete, "/teams/:id/repos/:owner/:repo"},174 {http.MethodGet, "/user/teams"},175176 // Pull Requests177 {http.MethodGet, "/repos/:owner/:repo/pulls"},178 {http.MethodGet, "/repos/:owner/:repo/pulls/:number"},179 {http.MethodPost, "/repos/:owner/:repo/pulls"},180 //{http.MethodPatch, "/repos/:owner/:repo/pulls/:number"},181 {http.MethodGet, "/repos/:owner/:repo/pulls/:number/commits"},182 {http.MethodGet, "/repos/:owner/:repo/pulls/:number/files"},183 {http.MethodGet, "/repos/:owner/:repo/pulls/:number/merge"},184 {http.MethodPut, "/repos/:owner/:repo/pulls/:number/merge"},185 {http.MethodGet, "/repos/:owner/:repo/pulls/:number/comments"},186 //{http.MethodGet, "/repos/:owner/:repo/pulls/comments"},187 //{http.MethodGet, "/repos/:owner/:repo/pulls/comments/:number"},188 {http.MethodPut, "/repos/:owner/:repo/pulls/:number/comments"},189 //{http.MethodPatch, "/repos/:owner/:repo/pulls/comments/:number"},190 //{http.MethodDelete, "/repos/:owner/:repo/pulls/comments/:number"},191192 // Repositories193 {http.MethodGet, "/user/repos"},194 {http.MethodGet, "/users/:user/repos"},195 {http.MethodGet, "/orgs/:org/repos"},196 {http.MethodGet, "/repositories"},197 {http.MethodPost, "/user/repos"},198 {http.MethodPost, "/orgs/:org/repos"},199 {http.MethodGet, "/repos/:owner/:repo"},200 //{http.MethodPatch, "/repos/:owner/:repo"},201 {http.MethodGet, "/repos/:owner/:repo/contributors"},202 {http.MethodGet, "/repos/:owner/:repo/languages"},203 {http.MethodGet, "/repos/:owner/:repo/teams"},204 {http.MethodGet, "/repos/:owner/:repo/tags"},205 {http.MethodGet, "/repos/:owner/:repo/branches"},206 {http.MethodGet, "/repos/:owner/:repo/branches/:branch"},207 {http.MethodDelete, "/repos/:owner/:repo"},208 {http.MethodGet, "/repos/:owner/:repo/collaborators"},209 {http.MethodGet, "/repos/:owner/:repo/collaborators/:user"},210 {http.MethodPut, "/repos/:owner/:repo/collaborators/:user"},211 {http.MethodDelete, "/repos/:owner/:repo/collaborators/:user"},212 {http.MethodGet, "/repos/:owner/:repo/comments"},213 {http.MethodGet, "/repos/:owner/:repo/commits/:sha/comments"},214 {http.MethodPost, "/repos/:owner/:repo/commits/:sha/comments"},215 {http.MethodGet, "/repos/:owner/:repo/comments/:id"},216 //{http.MethodPatch, "/repos/:owner/:repo/comments/:id"},217 {http.MethodDelete, "/repos/:owner/:repo/comments/:id"},218 {http.MethodGet, "/repos/:owner/:repo/commits"},219 {http.MethodGet, "/repos/:owner/:repo/commits/:sha"},220 {http.MethodGet, "/repos/:owner/:repo/readme"},221 //{http.MethodGet, "/repos/:owner/:repo/contents/*path"},222 //{http.MethodPut, "/repos/:owner/:repo/contents/*path"},223 //{http.MethodDelete, "/repos/:owner/:repo/contents/*path"},224 //{http.MethodGet, "/repos/:owner/:repo/:archive_format/:ref"},225 {http.MethodGet, "/repos/:owner/:repo/keys"},226 {http.MethodGet, "/repos/:owner/:repo/keys/:id"},227 {http.MethodPost, "/repos/:owner/:repo/keys"},228 //{http.MethodPatch, "/repos/:owner/:repo/keys/:id"},229 {http.MethodDelete, "/repos/:owner/:repo/keys/:id"},230 {http.MethodGet, "/repos/:owner/:repo/downloads"},231 {http.MethodGet, "/repos/:owner/:repo/downloads/:id"},232 {http.MethodDelete, "/repos/:owner/:repo/downloads/:id"},233 {http.MethodGet, "/repos/:owner/:repo/forks"},234 {http.MethodPost, "/repos/:owner/:repo/forks"},235 {http.MethodGet, "/repos/:owner/:repo/hooks"},236 {http.MethodGet, "/repos/:owner/:repo/hooks/:id"},237 {http.MethodPost, "/repos/:owner/:repo/hooks"},238 //{http.MethodPatch, "/repos/:owner/:repo/hooks/:id"},239 {http.MethodPost, "/repos/:owner/:repo/hooks/:id/tests"},240 {http.MethodDelete, "/repos/:owner/:repo/hooks/:id"},241 {http.MethodPost, "/repos/:owner/:repo/merges"},242 {http.MethodGet, "/repos/:owner/:repo/releases"},243 {http.MethodGet, "/repos/:owner/:repo/releases/:id"},244 {http.MethodPost, "/repos/:owner/:repo/releases"},245 //{http.MethodPatch, "/repos/:owner/:repo/releases/:id"},246 {http.MethodDelete, "/repos/:owner/:repo/releases/:id"},247 {http.MethodGet, "/repos/:owner/:repo/releases/:id/assets"},248 {http.MethodGet, "/repos/:owner/:repo/stats/contributors"},249 {http.MethodGet, "/repos/:owner/:repo/stats/commit_activity"},250 {http.MethodGet, "/repos/:owner/:repo/stats/code_frequency"},251 {http.MethodGet, "/repos/:owner/:repo/stats/participation"},252 {http.MethodGet, "/repos/:owner/:repo/stats/punch_card"},253 {http.MethodGet, "/repos/:owner/:repo/statuses/:ref"},254 {http.MethodPost, "/repos/:owner/:repo/statuses/:ref"},255256 // Search257 {http.MethodGet, "/search/repositories"},258 {http.MethodGet, "/search/code"},259 {http.MethodGet, "/search/issues"},260 {http.MethodGet, "/search/users"},261 {http.MethodGet, "/legacy/issues/search/:owner/:repository/:state/:keyword"},262 {http.MethodGet, "/legacy/repos/search/:keyword"},263 {http.MethodGet, "/legacy/user/search/:keyword"},264 {http.MethodGet, "/legacy/user/email/:email"},265266 // Users267 {http.MethodGet, "/users/:user"},268 {http.MethodGet, "/user"},269 //{http.MethodPatch, "/user"},270 {http.MethodGet, "/users"},271 {http.MethodGet, "/user/emails"},272 {http.MethodPost, "/user/emails"},273 {http.MethodDelete, "/user/emails"},274 {http.MethodGet, "/users/:user/followers"},275 {http.MethodGet, "/user/followers"},276 {http.MethodGet, "/users/:user/following"},277 {http.MethodGet, "/user/following"},278 {http.MethodGet, "/user/following/:user"},279 {http.MethodGet, "/users/:user/following/:target_user"},280 {http.MethodPut, "/user/following/:user"},281 {http.MethodDelete, "/user/following/:user"},282 {http.MethodGet, "/users/:user/keys"},283 {http.MethodGet, "/user/keys"},284 {http.MethodGet, "/user/keys/:id"},285 {http.MethodPost, "/user/keys"},286 //{http.MethodPatch, "/user/keys/:id"},287 {http.MethodDelete, "/user/keys/:id"},288}289290func TestShouldBindUri(t *testing.T) {291 DefaultWriter = os.Stdout292 router := New()293294 type Person struct {295 Name string `uri:"name" binding:"required"`296 ID string `uri:"id" binding:"required"`297 }298 router.Handle(http.MethodGet, "/rest/:name/:id", func(c *Context) {299 var person Personci(lint): enable testifylint linter (#4010)Matthieu MORELbefore adoption5f55c6a
lines 300–302why, one step back300 require.NoError(t, c.ShouldBindUri(&person))301 assert.NotEqual(t, "", person.Name)302 assert.NotEqual(t, "", person.ID)fix(engine): missing route params for CreateTestContext (#2778) (#2803)RoCrybefore adoption55e27f1
lines 303–321why, one step back303 c.String(http.StatusOK, "ShouldBindUri test OK")304 })305306 path, _ := exampleFromPath("/rest/:name/:id")307 w := PerformRequest(router, http.MethodGet, path)308 assert.Equal(t, "ShouldBindUri test OK", w.Body.String())309 assert.Equal(t, http.StatusOK, w.Code)310}311312func TestBindUri(t *testing.T) {313 DefaultWriter = os.Stdout314 router := New()315316 type Person struct {317 Name string `uri:"name" binding:"required"`318 ID string `uri:"id" binding:"required"`319 }320 router.Handle(http.MethodGet, "/rest/:name/:id", func(c *Context) {321 var person Personci(lint): enable testifylint linter (#4010)Matthieu MORELbefore adoption5f55c6a
lines 322–324why, one step back322 require.NoError(t, c.BindUri(&person))323 assert.NotEqual(t, "", person.Name)324 assert.NotEqual(t, "", person.ID)fix(engine): missing route params for CreateTestContext (#2778) (#2803)RoCrybefore adoption55e27f1
lines 325–342why, one step back325 c.String(http.StatusOK, "BindUri test OK")326 })327328 path, _ := exampleFromPath("/rest/:name/:id")329 w := PerformRequest(router, http.MethodGet, path)330 assert.Equal(t, "BindUri test OK", w.Body.String())331 assert.Equal(t, http.StatusOK, w.Code)332}333334func TestBindUriError(t *testing.T) {335 DefaultWriter = os.Stdout336 router := New()337338 type Member struct {339 Number string `uri:"num" binding:"required,uuid"`340 }341 router.Handle(http.MethodGet, "/new/rest/:num", func(c *Context) {342 var m Memberci(lint): enable testifylint linter (#4010)Matthieu MORELbefore adoption5f55c6a
line 343why, one step back343 require.Error(t, c.BindUri(&m))fix(engine): missing route params for CreateTestContext (#2778) (#2803)RoCrybefore adoption55e27f1
lines 344–405why, one step back344 })345346 path1, _ := exampleFromPath("/new/rest/:num")347 w1 := PerformRequest(router, http.MethodGet, path1)348 assert.Equal(t, http.StatusBadRequest, w1.Code)349}350351func TestRaceContextCopy(t *testing.T) {352 DefaultWriter = os.Stdout353 router := Default()354 router.GET("/test/copy/race", func(c *Context) {355 c.Set("1", 0)356 c.Set("2", 0)357358 // Sending a copy of the Context to two separate routines359 go readWriteKeys(c.Copy())360 go readWriteKeys(c.Copy())361 c.String(http.StatusOK, "run OK, no panics")362 })363 w := PerformRequest(router, http.MethodGet, "/test/copy/race")364 assert.Equal(t, "run OK, no panics", w.Body.String())365}366367func readWriteKeys(c *Context) {368 for {369 c.Set("1", rand.Int())370 c.Set("2", c.Value("1"))371 }372}373374func githubConfigRouter(router *Engine) {375 for _, route := range githubAPI {376 router.Handle(route.method, route.path, func(c *Context) {377 output := make(map[string]string, len(c.Params)+1)378 output["status"] = "good"379 for _, param := range c.Params {380 output[param.Key] = param.Value381 }382 c.JSON(http.StatusOK, output)383 })384 }385}386387func TestGithubAPI(t *testing.T) {388 DefaultWriter = os.Stdout389 router := New()390 githubConfigRouter(router)391392 for _, route := range githubAPI {393 path, values := exampleFromPath(route.path)394 w := PerformRequest(router, route.method, path)395396 // TEST397 assert.Contains(t, w.Body.String(), "\"status\":\"good\"")398 for _, value := range values {399 str := fmt.Sprintf("\"%s\":\"%s\"", value.Key, value.Value)400 assert.Contains(t, w.Body.String(), str)401 }402 }403}404405func exampleFromPath(path string) (string, Params) {Replace bytes.Buffer with strings.Builder where appropriate (#3347)hopehookbefore adoptionb2d4185
line 406why, one step back406 output := new(strings.Builder)fix(engine): missing route params for CreateTestContext (#2778) (#2803)RoCrybefore adoption55e27f1
lines 407–414why, one step back407 params := make(Params, 0, 6)408 start := -1409 for i, c := range path {410 if c == ':' {411 start = i + 1412 }413 if start >= 0 {414 if c == '/' {ci(lint): enable perfsprint linter (#4090)Matthieu MORELbefore adoption02c1144
line 415why, one step back415 value := strconv.Itoa(rand.Intn(100000))fix(engine): missing route params for CreateTestContext (#2778) (#2803)RoCrybefore adoption55e27f1
lines 416–428why, one step back416 params = append(params, Param{417 Key: path[start:i],418 Value: value,419 })420 output.WriteString(value)421 output.WriteRune(c)422 start = -1423 }424 } else {425 output.WriteRune(c)426 }427 }428 if start >= 0 {ci(lint): enable perfsprint linter (#4090)Matthieu MORELbefore adoption02c1144
line 429why, one step back429 value := strconv.Itoa(rand.Intn(100000))fix(engine): missing route params for CreateTestContext (#2778) (#2803)RoCrybefore adoption55e27f1
lines 430–476why, one step back430 params = append(params, Param{431 Key: path[start:],432 Value: value,433 })434 output.WriteString(value)435 }436437 return output.String(), params438}439440func BenchmarkGithub(b *testing.B) {441 router := New()442 githubConfigRouter(router)443 runRequest(b, router, http.MethodGet, "/legacy/issues/search/:owner/:repository/:state/:keyword")444}445446func BenchmarkParallelGithub(b *testing.B) {447 DefaultWriter = os.Stdout448 router := New()449 githubConfigRouter(router)450451 req, _ := http.NewRequest(http.MethodPost, "/repos/manucorporat/sse/git/blobs", nil)452453 b.RunParallel(func(pb *testing.PB) {454 // Each goroutine has its own bytes.Buffer.455 for pb.Next() {456 w := httptest.NewRecorder()457 router.ServeHTTP(w, req)458 }459 })460}461462func BenchmarkParallelGithubDefault(b *testing.B) {463 DefaultWriter = os.Stdout464 router := New()465 githubConfigRouter(router)466467 req, _ := http.NewRequest(http.MethodPost, "/repos/manucorporat/sse/git/blobs", nil)468469 b.RunParallel(func(pb *testing.PB) {470 // Each goroutine has its own bytes.Buffer.471 for pb.Next() {472 w := httptest.NewRecorder()473 router.ServeHTTP(w, req)474 }475 })476}