test: expose performRequest func (#3012)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Bo-Yi Wu · committed by GitHub · parent 0136319 · Browse files · 9 files·+115 added−120 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+115 added−120 removed
- other:+115 added−120 removed
Provenance
- Commit
94153d1- Tree
dba0030- Author
Bo-Yi Wu <appleboy.tw@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
0136319
9 files changed+115 added−120 removed
modified:context_test.go+2 added-2 removed
2034 unmodified lines2035 assert.Equal(t, c.Param("name"), param)2036 }(c.Copy(), c.Param("name"))2037 })2038 }2039- performRequest(router, "GET", "/name1/api")2040- performRequest(router, "GET", "/name2/api")2039+ PerformRequest(router, "GET", "/name1/api")2040+ PerformRequest(router, "GET", "/name2/api")2041 wg.Wait()2042 }2043 2044 func TestContextWithKeysMutex(t *testing.T) {129 unmodified linesmodified:gin_test.go+2 added-4 removed
393 unmodified lines394 compareFunc(t, router.allNoMethod[1], middleware0)395 }396 397 func TestRebuild404Handlers(t *testing.T) {398-398 }399 400 func TestNoMethodWithGlobalHandlers(t *testing.T) {401 var middleware0 HandlerFunc = func(c *Context) {}87 unmodified lines489 v2.GET("/", func(c *Context) {})490 }491 492 assert.NotPanics(t, func() {494- w := performRequest(r, "GET", "/")493+ w := PerformRequest(r, "GET", "/")494 assert.Equal(t, 301, w.Code)495 })496 }497 24 unmodified lines522 atomic.AddInt64(&handlerCounter, 1)523 })524 525 assert.NotPanics(t, func() {527- w := performRequest(r, "GET", "/"+strconv.Itoa(expectValue-1)) // include 0 value526+ w := PerformRequest(r, "GET", "/"+strconv.Itoa(expectValue-1)) // include 0 value527 assert.Equal(t, 200, w.Code)528 assert.Equal(t, expectValue, w.Body.Len())529 })530 103 unmodified lines634 635 assert.Nil(t, r.trustedCIDRs)636 assert.Nil(t, err)637 }639-638 }639 640 func parseCIDR(cidr string) *net.IPNet {641 _, parsedCIDR, err := net.ParseCIDR(cidr)18 unmodified linesmodified:githubapi_test.go+5 added-5 removed
300 unmodified lines301 c.String(http.StatusOK, "ShouldBindUri test OK")302 })303 304 path, _ := exampleFromPath("/rest/:name/:id")305- w := performRequest(router, http.MethodGet, path)305+ w := PerformRequest(router, http.MethodGet, path)306 assert.Equal(t, "ShouldBindUri test OK", w.Body.String())307 assert.Equal(t, http.StatusOK, w.Code)308 }309 13 unmodified lines323 c.String(http.StatusOK, "BindUri test OK")324 })325 326 path, _ := exampleFromPath("/rest/:name/:id")327- w := performRequest(router, http.MethodGet, path)327+ w := PerformRequest(router, http.MethodGet, path)328 assert.Equal(t, "BindUri test OK", w.Body.String())329 assert.Equal(t, http.StatusOK, w.Code)330 }331 9 unmodified lines341 assert.Error(t, c.BindUri(&m))342 })343 344 path1, _ := exampleFromPath("/new/rest/:num")345- w1 := performRequest(router, http.MethodGet, path1)345+ w1 := PerformRequest(router, http.MethodGet, path1)346 assert.Equal(t, http.StatusBadRequest, w1.Code)347 }348 349 func TestRaceContextCopy(t *testing.T) {7 unmodified lines357 go readWriteKeys(c.Copy())358 go readWriteKeys(c.Copy())359 c.String(http.StatusOK, "run OK, no panics")360 })361- w := performRequest(router, http.MethodGet, "/test/copy/race")361+ w := PerformRequest(router, http.MethodGet, "/test/copy/race")362 assert.Equal(t, "run OK, no panics", w.Body.String())363 }364 365 func readWriteKeys(c *Context) {22 unmodified lines388 githubConfigRouter(router)389 390 for _, route := range githubAPI {391 path, values := exampleFromPath(route.path)392- w := performRequest(router, route.method, path)392+ w := PerformRequest(router, route.method, path)393 394 // TEST395 assert.Contains(t, w.Body.String(), "\"status\":\"good\"")396 for _, value := range values {78 unmodified linesmodified:logger_test.go+25 added-27 removed
29 unmodified lines30 router.PATCH("/example", func(c *Context) {})31 router.HEAD("/example", func(c *Context) {})32 router.OPTIONS("/example", func(c *Context) {})33 34- performRequest(router, "GET", "/example?a=100")34+ PerformRequest(router, "GET", "/example?a=100")35 assert.Contains(t, buffer.String(), "200")36 assert.Contains(t, buffer.String(), "GET")37 assert.Contains(t, buffer.String(), "/example")38 assert.Contains(t, buffer.String(), "a=100")1 unmodified line40 // I wrote these first (extending the above) but then realized they are more41 // like integration tests because they test the whole logging process rather42 // than individual functions. Im not sure where these should go.43 buffer.Reset()44- performRequest(router, "POST", "/example")44+ PerformRequest(router, "POST", "/example")45 assert.Contains(t, buffer.String(), "200")46 assert.Contains(t, buffer.String(), "POST")47 assert.Contains(t, buffer.String(), "/example")48 49 buffer.Reset()50- performRequest(router, "PUT", "/example")50+ PerformRequest(router, "PUT", "/example")51 assert.Contains(t, buffer.String(), "200")52 assert.Contains(t, buffer.String(), "PUT")53 assert.Contains(t, buffer.String(), "/example")54 55 buffer.Reset()56- performRequest(router, "DELETE", "/example")56+ PerformRequest(router, "DELETE", "/example")57 assert.Contains(t, buffer.String(), "200")58 assert.Contains(t, buffer.String(), "DELETE")59 assert.Contains(t, buffer.String(), "/example")60 61 buffer.Reset()62- performRequest(router, "PATCH", "/example")62+ PerformRequest(router, "PATCH", "/example")63 assert.Contains(t, buffer.String(), "200")64 assert.Contains(t, buffer.String(), "PATCH")65 assert.Contains(t, buffer.String(), "/example")66 67 buffer.Reset()68- performRequest(router, "HEAD", "/example")68+ PerformRequest(router, "HEAD", "/example")69 assert.Contains(t, buffer.String(), "200")70 assert.Contains(t, buffer.String(), "HEAD")71 assert.Contains(t, buffer.String(), "/example")72 73 buffer.Reset()74- performRequest(router, "OPTIONS", "/example")74+ PerformRequest(router, "OPTIONS", "/example")75 assert.Contains(t, buffer.String(), "200")76 assert.Contains(t, buffer.String(), "OPTIONS")77 assert.Contains(t, buffer.String(), "/example")78 79 buffer.Reset()80- performRequest(router, "GET", "/notfound")80+ PerformRequest(router, "GET", "/notfound")81 assert.Contains(t, buffer.String(), "404")82 assert.Contains(t, buffer.String(), "GET")83 assert.Contains(t, buffer.String(), "/notfound")84 }9 unmodified lines94 router.PATCH("/example", func(c *Context) {})95 router.HEAD("/example", func(c *Context) {})96 router.OPTIONS("/example", func(c *Context) {})97 98- performRequest(router, "GET", "/example?a=100")98+ PerformRequest(router, "GET", "/example?a=100")99 assert.Contains(t, buffer.String(), "200")100 assert.Contains(t, buffer.String(), "GET")101 assert.Contains(t, buffer.String(), "/example")102 assert.Contains(t, buffer.String(), "a=100")1 unmodified line104 // I wrote these first (extending the above) but then realized they are more105 // like integration tests because they test the whole logging process rather106 // than individual functions. Im not sure where these should go.107 buffer.Reset()108- performRequest(router, "POST", "/example")108+ PerformRequest(router, "POST", "/example")109 assert.Contains(t, buffer.String(), "200")110 assert.Contains(t, buffer.String(), "POST")111 assert.Contains(t, buffer.String(), "/example")112 113 buffer.Reset()114- performRequest(router, "PUT", "/example")114+ PerformRequest(router, "PUT", "/example")115 assert.Contains(t, buffer.String(), "200")116 assert.Contains(t, buffer.String(), "PUT")117 assert.Contains(t, buffer.String(), "/example")118 119 buffer.Reset()120- performRequest(router, "DELETE", "/example")120+ PerformRequest(router, "DELETE", "/example")121 assert.Contains(t, buffer.String(), "200")122 assert.Contains(t, buffer.String(), "DELETE")123 assert.Contains(t, buffer.String(), "/example")124 125 buffer.Reset()126- performRequest(router, "PATCH", "/example")126+ PerformRequest(router, "PATCH", "/example")127 assert.Contains(t, buffer.String(), "200")128 assert.Contains(t, buffer.String(), "PATCH")129 assert.Contains(t, buffer.String(), "/example")130 131 buffer.Reset()132- performRequest(router, "HEAD", "/example")132+ PerformRequest(router, "HEAD", "/example")133 assert.Contains(t, buffer.String(), "200")134 assert.Contains(t, buffer.String(), "HEAD")135 assert.Contains(t, buffer.String(), "/example")136 137 buffer.Reset()138- performRequest(router, "OPTIONS", "/example")138+ PerformRequest(router, "OPTIONS", "/example")139 assert.Contains(t, buffer.String(), "200")140 assert.Contains(t, buffer.String(), "OPTIONS")141 assert.Contains(t, buffer.String(), "/example")142 143 buffer.Reset()144- performRequest(router, "GET", "/notfound")144+ PerformRequest(router, "GET", "/notfound")145 assert.Contains(t, buffer.String(), "404")146 assert.Contains(t, buffer.String(), "GET")147 assert.Contains(t, buffer.String(), "/notfound")148 }19 unmodified lines168 param.ErrorMessage,169 )170 }))171 router.GET("/example", func(c *Context) {})172- performRequest(router, "GET", "/example?a=100")172+ PerformRequest(router, "GET", "/example?a=100")173 174 // output test175 assert.Contains(t, buffer.String(), "[FORMATTER TEST]")176 assert.Contains(t, buffer.String(), "200")31 unmodified lines208 // set dummy ClientIP209 c.Request.Header.Set("X-Forwarded-For", "20.20.20.20")210 gotKeys = c.Keys211 })212- performRequest(router, "GET", "/example?a=100")212+ PerformRequest(router, "GET", "/example?a=100")213 214 // output test215 assert.Contains(t, buffer.String(), "[FORMATTER TEST]")216 assert.Contains(t, buffer.String(), "200")10 unmodified lines227 assert.Equal(t, "GET", gotParam.Method)228 assert.Equal(t, "/example?a=100", gotParam.Path)229 assert.Empty(t, gotParam.ErrorMessage)230 assert.Equal(t, gotKeys, gotParam.Keys)231-231 }232 233 func TestDefaultLogFormatter(t *testing.T) {234 timeStamp := time.Unix(1544173902, 0).UTC()45 unmodified lines280 assert.Equal(t, "[GIN] 2018/12/07 - 09:11:42 | 200 | 2743h29m3s | 20.20.20.20 | GET \"/\"\n", defaultLogFormatter(termFalseLongDurationParam))281 282 assert.Equal(t, "[GIN] 2018/12/07 - 09:11:42 |\x1b[97;42m 200 \x1b[0m| 5s | 20.20.20.20 |\x1b[97;44m GET \x1b[0m \"/\"\n", defaultLogFormatter(termTrueParam))283 assert.Equal(t, "[GIN] 2018/12/07 - 09:11:42 |\x1b[97;42m 200 \x1b[0m| 2743h29m3s | 20.20.20.20 |\x1b[97;44m GET \x1b[0m \"/\"\n", defaultLogFormatter(termTrueLongDurationParam))285-284 }285 286 func TestColorForMethod(t *testing.T) {287 colorForMethod := func(method string) string {78 unmodified lines366 c.Error(errors.New("this is an error")) // nolint: errcheck367 c.String(http.StatusInternalServerError, "hola!")368 })369 372- w := performRequest(router, "GET", "/error")370+ w := PerformRequest(router, "GET", "/error")371 assert.Equal(t, http.StatusOK, w.Code)372 assert.Equal(t, "{\"error\":\"this is an error\"}", w.Body.String())373 376- w = performRequest(router, "GET", "/abort")374+ w = PerformRequest(router, "GET", "/abort")375 assert.Equal(t, http.StatusUnauthorized, w.Code)376 assert.Equal(t, "{\"error\":\"no authorized\"}", w.Body.String())377 380- w = performRequest(router, "GET", "/print")378+ w = PerformRequest(router, "GET", "/print")379 assert.Equal(t, http.StatusInternalServerError, w.Code)380 assert.Equal(t, "hola!{\"error\":\"this is an error\"}", w.Body.String())381 }382 3 unmodified lines386 router.Use(LoggerWithWriter(buffer, "/skipped"))387 router.GET("/logged", func(c *Context) {})388 router.GET("/skipped", func(c *Context) {})389 392- performRequest(router, "GET", "/logged")390+ PerformRequest(router, "GET", "/logged")391 assert.Contains(t, buffer.String(), "200")392 393 buffer.Reset()396- performRequest(router, "GET", "/skipped")394+ PerformRequest(router, "GET", "/skipped")395 assert.Contains(t, buffer.String(), "")396 }397 398 func TestLoggerWithConfigSkippingPaths(t *testing.T) {5 unmodified lines404 }))405 router.GET("/logged", func(c *Context) {})406 router.GET("/skipped", func(c *Context) {})407 410- performRequest(router, "GET", "/logged")408+ PerformRequest(router, "GET", "/logged")409 assert.Contains(t, buffer.String(), "200")410 411 buffer.Reset()414- performRequest(router, "GET", "/skipped")412+ PerformRequest(router, "GET", "/skipped")413 assert.Contains(t, buffer.String(), "")414 }415 416 func TestDisableConsoleColor(t *testing.T) {18 unmodified linesmodified:middleware_test.go+8 added-9 removed
33 unmodified lines34 router.NoMethod(func(c *Context) {35 signature += " XX "36 })37 // RUN38- w := performRequest(router, "GET", "/")38+ w := PerformRequest(router, "GET", "/")39 40 // TEST41 assert.Equal(t, http.StatusOK, w.Code)42 assert.Equal(t, "ACDB", signature)27 unmodified lines70 router.NoMethod(func(c *Context) {71 signature += " X "72 })73 // RUN74- w := performRequest(router, "GET", "/")74+ w := PerformRequest(router, "GET", "/")75 76 // TEST77 assert.Equal(t, http.StatusNotFound, w.Code)78 assert.Equal(t, "ACEGHFDB", signature)28 unmodified lines107 router.POST("/", func(c *Context) {108 signature += " XX "109 })110 // RUN111- w := performRequest(router, "GET", "/")111+ w := PerformRequest(router, "GET", "/")112 113 // TEST114 assert.Equal(t, http.StatusMethodNotAllowed, w.Code)115 assert.Equal(t, "ACEGHFDB", signature)32 unmodified lines148 signature += " XX "149 })150 151 // RUN152- w := performRequest(router, "GET", "/")152+ w := PerformRequest(router, "GET", "/")153 154 // TEST155 assert.Equal(t, http.StatusNotFound, w.Code)156 assert.Equal(t, "AC X DB", signature)17 unmodified lines174 signature += " XX "175 })176 177 // RUN178- w := performRequest(router, "GET", "/")178+ w := PerformRequest(router, "GET", "/")179 180 // TEST181 assert.Equal(t, http.StatusUnauthorized, w.Code)182 assert.Equal(t, "ACD", signature)6 unmodified lines189 signature += "A"190 c.Next()191 c.AbortWithStatus(http.StatusGone)192 signature += "B"193-193 })194 router.GET("/", func(c *Context) {195 signature += "C"196 c.Next()197 })198 // RUN200- w := performRequest(router, "GET", "/")199+ w := PerformRequest(router, "GET", "/")200 201 // TEST202 assert.Equal(t, http.StatusGone, w.Code)203 assert.Equal(t, "ACB", signature)14 unmodified lines218 context.Next()219 signature += "C"220 })221 // RUN223- w := performRequest(router, "GET", "/")222+ w := PerformRequest(router, "GET", "/")223 224 // TEST225 assert.Equal(t, http.StatusInternalServerError, w.Code)226 assert.Equal(t, "A", signature)18 unmodified lines245 Data: "message",246 })247 })248 250- w := performRequest(router, "GET", "/")249+ w := PerformRequest(router, "GET", "/")250 251 assert.Equal(t, http.StatusBadRequest, w.Code)252 assert.Equal(t, strings.Replace("hola\n<map><foo>bar</foo></map>{\"foo\":\"bar\"}{\"foo\":\"bar\"}event:test\ndata:message\n\n", " ", "", -1), strings.Replace(w.Body.String(), " ", "", -1))253 }modified:recovery_test.go+11 added-12 removed
25 unmodified lines26 c.AbortWithStatus(http.StatusBadRequest)27 panic("Oupps, Houston, we have a problem")28 })29 // RUN30- w := performRequest(router, "GET", "/recovery",30+ w := PerformRequest(router, "GET", "/recovery",31 header{32 Key: "Host",33 Value: "www.google.com",34 },21 unmodified lines56 router.GET("/recovery", func(_ *Context) {57 panic("Oupps, Houston, we have a problem")58 })59 // RUN60- w := performRequest(router, "GET", "/recovery")60+ w := PerformRequest(router, "GET", "/recovery")61 // TEST62 assert.Equal(t, http.StatusInternalServerError, w.Code)63 assert.Contains(t, buffer.String(), "panic recovered")64 assert.Contains(t, buffer.String(), "Oupps, Houston, we have a problem")2 unmodified lines67 68 // Debug mode prints the request69 SetMode(DebugMode)70 // RUN71- w = performRequest(router, "GET", "/recovery")71+ w = PerformRequest(router, "GET", "/recovery")72 // TEST73 assert.Equal(t, http.StatusInternalServerError, w.Code)74 assert.Contains(t, buffer.String(), "GET /recovery")75 8 unmodified lines84 c.AbortWithStatus(http.StatusBadRequest)85 panic("Oupps, Houston, we have a problem")86 })87 // RUN88- w := performRequest(router, "GET", "/recovery")88+ w := PerformRequest(router, "GET", "/recovery")89 // TEST90 assert.Equal(t, http.StatusBadRequest, w.Code)91 }92 28 unmodified lines121 }122 123 for errno, expectMsg := range expectMsgs {124 t.Run(expectMsg, func(t *testing.T) {125-125 var buf bytes.Buffer126 127 router := New()128 router.Use(RecoveryWithWriter(&buf))6 unmodified lines135 e := &net.OpError{Err: &os.SyscallError{Err: errno}}136 panic(e)137 })138 // RUN140- w := performRequest(router, "GET", "/recovery")139+ w := PerformRequest(router, "GET", "/recovery")140 // TEST141 assert.Equal(t, expectCode, w.Code)142 assert.Contains(t, strings.ToLower(buf.String()), expectMsg)143 })12 unmodified lines156 router.GET("/recovery", func(_ *Context) {157 panic("Oupps, Houston, we have a problem")158 })159 // RUN161- w := performRequest(router, "GET", "/recovery")160+ w := PerformRequest(router, "GET", "/recovery")161 // TEST162 assert.Equal(t, http.StatusBadRequest, w.Code)163 assert.Contains(t, buffer.String(), "panic recovered")164 assert.Contains(t, buffer.String(), "Oupps, Houston, we have a problem")2 unmodified lines167 168 // Debug mode prints the request169 SetMode(DebugMode)170 // RUN172- w = performRequest(router, "GET", "/recovery")171+ w = PerformRequest(router, "GET", "/recovery")172 // TEST173 assert.Equal(t, http.StatusBadRequest, w.Code)174 assert.Contains(t, buffer.String(), "GET /recovery")175 15 unmodified lines191 router.GET("/recovery", func(_ *Context) {192 panic("Oupps, Houston, we have a problem")193 })194 // RUN196- w := performRequest(router, "GET", "/recovery")195+ w := PerformRequest(router, "GET", "/recovery")196 // TEST197 assert.Equal(t, http.StatusBadRequest, w.Code)198 assert.Contains(t, buffer.String(), "panic recovered")199 assert.Contains(t, buffer.String(), "Oupps, Houston, we have a problem")2 unmodified lines202 203 // Debug mode prints the request204 SetMode(DebugMode)205 // RUN207- w = performRequest(router, "GET", "/recovery")206+ w = PerformRequest(router, "GET", "/recovery")207 // TEST208 assert.Equal(t, http.StatusBadRequest, w.Code)209 assert.Contains(t, buffer.String(), "GET /recovery")210 15 unmodified lines226 router.GET("/recovery", func(_ *Context) {227 panic("Oupps, Houston, we have a problem")228 })229 // RUN231- w := performRequest(router, "GET", "/recovery")230+ w := PerformRequest(router, "GET", "/recovery")231 // TEST232 assert.Equal(t, http.StatusBadRequest, w.Code)233 assert.Contains(t, buffer.String(), "panic recovered")234 assert.Contains(t, buffer.String(), "Oupps, Houston, we have a problem")2 unmodified lines237 238 // Debug mode prints the request239 SetMode(DebugMode)240 // RUN242- w = performRequest(router, "GET", "/recovery")241+ w = PerformRequest(router, "GET", "/recovery")242 // TEST243 assert.Equal(t, http.StatusBadRequest, w.Code)244 assert.Contains(t, buffer.String(), "GET /recovery")245 4 unmodified linesmodified:routergroup_test.go+2 added-2 removed
78 unmodified lines79 default:80 panic("unknown method")81 }82 83- w := performRequest(router, method, "/v1/login/test")83+ w := PerformRequest(router, method, "/v1/login/test")84 assert.Equal(t, http.StatusBadRequest, w.Code)85 assert.Equal(t, "the method was "+method+" and index 3", w.Body.String())86 87- w = performRequest(router, method, "/v1/test")87+ w = PerformRequest(router, method, "/v1/test")88 assert.Equal(t, http.StatusBadRequest, w.Code)89 assert.Equal(t, "the method was "+method+" and index 1", w.Body.String())90 }91 91 unmodified linesmodified:routes_test.go+56 added-55 removed
19 unmodified lines20 Key string21 Value string22 }23 24+// PerformRequest for testing gin router.24-func performRequest(r http.Handler, method, path string, headers ...header) *httptest.ResponseRecorder {25+func PerformRequest(r http.Handler, method, path string, headers ...header) *httptest.ResponseRecorder {26 req := httptest.NewRequest(method, path, nil)27 for _, h := range headers {28 req.Header.Add(h.Key, h.Value)29 }12 unmodified lines42 r.Handle(method, "/test", func(c *Context) {43 passed = true44 })45 45- w := performRequest(r, method, "/test")46+ w := PerformRequest(r, method, "/test")47 assert.True(t, passed)48 assert.Equal(t, http.StatusOK, w.Code)49 49- performRequest(r, method, "/test2")50+ PerformRequest(r, method, "/test2")51 assert.True(t, passedAny)52 }53 54 // TestSingleRouteOK tests that POST route is correctly invoked.3 unmodified lines58 router.Handle(method, "/test_2", func(c *Context) {59 passed = true60 })61 61- w := performRequest(router, method, "/test")62+ w := PerformRequest(router, method, "/test")63 64 assert.False(t, passed)65 assert.Equal(t, http.StatusNotFound, w.Code)66 }12 unmodified lines79 router.Handle(methodRoute, "/test", func(c *Context) {80 passed = true81 })82 82- w := performRequest(router, method, "/test")83+ w := PerformRequest(router, method, "/test")84 85 assert.False(t, passed)86 assert.Equal(t, http.StatusMethodNotAllowed, w.Code)87 }11 unmodified lines99 router.PUT("/hey3", func(c *Context) {100 c.String(http.StatusOK, "sup3")101 })102 102- w := performRequest(router, http.MethodPut, "/hey")103+ w := PerformRequest(router, http.MethodPut, "/hey")104 105 assert.Equal(t, http.StatusOK, w.Code)106 assert.Equal(t, "called", w.Body.String())107 }42 unmodified lines150 router.GET("/path2/", func(c *Context) {})151 router.POST("/path3", func(c *Context) {})152 router.PUT("/path4/", func(c *Context) {})153 153- w := performRequest(router, http.MethodGet, "/path/")154+ w := PerformRequest(router, http.MethodGet, "/path/")155 assert.Equal(t, "/path", w.Header().Get("Location"))156 assert.Equal(t, http.StatusMovedPermanently, w.Code)157 157- w = performRequest(router, http.MethodGet, "/path2")158+ w = PerformRequest(router, http.MethodGet, "/path2")159 assert.Equal(t, "/path2/", w.Header().Get("Location"))160 assert.Equal(t, http.StatusMovedPermanently, w.Code)161 161- w = performRequest(router, http.MethodPost, "/path3/")162+ w = PerformRequest(router, http.MethodPost, "/path3/")163 assert.Equal(t, "/path3", w.Header().Get("Location"))164 assert.Equal(t, http.StatusTemporaryRedirect, w.Code)165 165- w = performRequest(router, http.MethodPut, "/path4")166+ w = PerformRequest(router, http.MethodPut, "/path4")167 assert.Equal(t, "/path4/", w.Header().Get("Location"))168 assert.Equal(t, http.StatusTemporaryRedirect, w.Code)169 169- w = performRequest(router, http.MethodGet, "/path")170+ w = PerformRequest(router, http.MethodGet, "/path")171 assert.Equal(t, http.StatusOK, w.Code)172 172- w = performRequest(router, http.MethodGet, "/path2/")173+ w = PerformRequest(router, http.MethodGet, "/path2/")174 assert.Equal(t, http.StatusOK, w.Code)175 175- w = performRequest(router, http.MethodPost, "/path3")176+ w = PerformRequest(router, http.MethodPost, "/path3")177 assert.Equal(t, http.StatusOK, w.Code)178 178- w = performRequest(router, http.MethodPut, "/path4/")179+ w = PerformRequest(router, http.MethodPut, "/path4/")180 assert.Equal(t, http.StatusOK, w.Code)181 181- w = performRequest(router, http.MethodGet, "/path2", header{Key: "X-Forwarded-Prefix", Value: "/api"})182+ w = PerformRequest(router, http.MethodGet, "/path2", header{Key: "X-Forwarded-Prefix", Value: "/api"})183 assert.Equal(t, "/api/path2/", w.Header().Get("Location"))184 assert.Equal(t, 301, w.Code)185 185- w = performRequest(router, http.MethodGet, "/path2/", header{Key: "X-Forwarded-Prefix", Value: "/api/"})186+ w = PerformRequest(router, http.MethodGet, "/path2/", header{Key: "X-Forwarded-Prefix", Value: "/api/"})187 assert.Equal(t, 200, w.Code)188 189 router.RedirectTrailingSlash = false190 190- w = performRequest(router, http.MethodGet, "/path/")191+ w = PerformRequest(router, http.MethodGet, "/path/")192 assert.Equal(t, http.StatusNotFound, w.Code)192- w = performRequest(router, http.MethodGet, "/path2")193+ w = PerformRequest(router, http.MethodGet, "/path2")194 assert.Equal(t, http.StatusNotFound, w.Code)194- w = performRequest(router, http.MethodPost, "/path3/")195+ w = PerformRequest(router, http.MethodPost, "/path3/")196 assert.Equal(t, http.StatusNotFound, w.Code)196- w = performRequest(router, http.MethodPut, "/path4")197+ w = PerformRequest(router, http.MethodPut, "/path4")198 assert.Equal(t, http.StatusNotFound, w.Code)199 }200 201 func TestRouteRedirectFixedPath(t *testing.T) {5 unmodified lines207 router.GET("/Path2", func(c *Context) {})208 router.POST("/PATH3", func(c *Context) {})209 router.POST("/Path4/", func(c *Context) {})210 210- w := performRequest(router, http.MethodGet, "/PATH")211+ w := PerformRequest(router, http.MethodGet, "/PATH")212 assert.Equal(t, "/path", w.Header().Get("Location"))213 assert.Equal(t, http.StatusMovedPermanently, w.Code)214 214- w = performRequest(router, http.MethodGet, "/path2")215+ w = PerformRequest(router, http.MethodGet, "/path2")216 assert.Equal(t, "/Path2", w.Header().Get("Location"))217 assert.Equal(t, http.StatusMovedPermanently, w.Code)218 218- w = performRequest(router, http.MethodPost, "/path3")219+ w = PerformRequest(router, http.MethodPost, "/path3")220 assert.Equal(t, "/PATH3", w.Header().Get("Location"))221 assert.Equal(t, http.StatusTemporaryRedirect, w.Code)222 222- w = performRequest(router, http.MethodPost, "/path4")223+ w = PerformRequest(router, http.MethodPost, "/path4")224 assert.Equal(t, "/Path4/", w.Header().Get("Location"))225 assert.Equal(t, http.StatusTemporaryRedirect, w.Code)226 }227 20 unmodified lines248 assert.Empty(t, wtf)249 assert.False(t, ok)250 })251 251- w := performRequest(router, http.MethodGet, "/test/john/smith/is/super/great")252+ w := PerformRequest(router, http.MethodGet, "/test/john/smith/is/super/great")253 254 assert.Equal(t, http.StatusOK, w.Code)255 assert.Equal(t, "john", name)256 assert.Equal(t, "smith", lastName)24 unmodified lines281 assert.Empty(t, wtf)282 assert.False(t, ok)283 })284 284- w := performRequest(router, http.MethodGet, "//test//john//smith//is//super//great")285+ w := PerformRequest(router, http.MethodGet, "//test//john//smith//is//super//great")286 287 assert.Equal(t, http.StatusOK, w.Code)288 assert.Equal(t, "john", name)289 assert.Equal(t, "smith", lastName)19 unmodified lines309 router := New()310 router.Static("/using_static", dir)311 router.StaticFile("/result", f.Name())312 312- w := performRequest(router, http.MethodGet, "/using_static/"+filename)313- w2 := performRequest(router, http.MethodGet, "/result")313+ w := PerformRequest(router, http.MethodGet, "/using_static/"+filename)314+ w2 := PerformRequest(router, http.MethodGet, "/result")315 316 assert.Equal(t, w, w2)317 assert.Equal(t, http.StatusOK, w.Code)318 assert.Equal(t, "Gin Web Framework", w.Body.String())319 assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))320 320- w3 := performRequest(router, http.MethodHead, "/using_static/"+filename)321- w4 := performRequest(router, http.MethodHead, "/result")321+ w3 := PerformRequest(router, http.MethodHead, "/using_static/"+filename)322+ w4 := PerformRequest(router, http.MethodHead, "/result")323 324 assert.Equal(t, w3, w4)325 assert.Equal(t, http.StatusOK, w3.Code)326 }2 unmodified lines329 func TestRouteStaticListingDir(t *testing.T) {330 router := New()331 router.StaticFS("/", Dir("./", true))332 332- w := performRequest(router, http.MethodGet, "/")333+ w := PerformRequest(router, http.MethodGet, "/")334 335 assert.Equal(t, http.StatusOK, w.Code)336 assert.Contains(t, w.Body.String(), "gin.go")337 assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))3 unmodified lines341 func TestRouteStaticNoListing(t *testing.T) {342 router := New()343 router.Static("/", "./")344 344- w := performRequest(router, http.MethodGet, "/")345+ w := PerformRequest(router, http.MethodGet, "/")346 347 assert.Equal(t, http.StatusNotFound, w.Code)348 assert.NotContains(t, w.Body.String(), "gin.go")349 }6 unmodified lines356 c.Writer.Header().Add("X-GIN", "Gin Framework")357 })358 static.Static("/", "./")359 359- w := performRequest(router, http.MethodGet, "/gin.go")360+ w := PerformRequest(router, http.MethodGet, "/gin.go")361 362 assert.Equal(t, http.StatusOK, w.Code)363 assert.Contains(t, w.Body.String(), "package gin")364 // Content-Type='text/plain; charset=utf-8' when go version <= 1.16,7 unmodified lines372 func TestRouteNotAllowedEnabled(t *testing.T) {373 router := New()374 router.HandleMethodNotAllowed = true375 router.POST("/path", func(c *Context) {})375- w := performRequest(router, http.MethodGet, "/path")376+ w := PerformRequest(router, http.MethodGet, "/path")377 assert.Equal(t, http.StatusMethodNotAllowed, w.Code)378 379 router.NoMethod(func(c *Context) {380 c.String(http.StatusTeapot, "responseText")381 })381- w = performRequest(router, http.MethodGet, "/path")382+ w = PerformRequest(router, http.MethodGet, "/path")383 assert.Equal(t, "responseText", w.Body.String())384 assert.Equal(t, http.StatusTeapot, w.Code)385 }386 2 unmodified lines389 router.HandleMethodNotAllowed = true390 // add one methodTree to trees391 router.addRoute(http.MethodPost, "/", HandlersChain{func(_ *Context) {}})392 router.GET("/path2", func(c *Context) {})392- w := performRequest(router, http.MethodPost, "/path2")393+ w := PerformRequest(router, http.MethodPost, "/path2")394 assert.Equal(t, http.StatusMethodNotAllowed, w.Code)395 }396 397 func TestRouteNotAllowedDisabled(t *testing.T) {398 router := New()399 router.HandleMethodNotAllowed = false400 router.POST("/path", func(c *Context) {})400- w := performRequest(router, http.MethodGet, "/path")401+ w := PerformRequest(router, http.MethodGet, "/path")402 assert.Equal(t, http.StatusNotFound, w.Code)403 404 router.NoMethod(func(c *Context) {405 c.String(http.StatusTeapot, "responseText")406 })406- w = performRequest(router, http.MethodGet, "/path")407+ w = PerformRequest(router, http.MethodGet, "/path")408 assert.Equal(t, "404 page not found", w.Body.String())409 assert.Equal(t, http.StatusNotFound, w.Code)410 }411 11 unmodified lines423 {"/../path", http.StatusOK, ""}, // CleanPath424 {"/nope", http.StatusNotFound, ""}, // NotFound425 }426 for _, tr := range testRoutes {426- w := performRequest(router, "GET", tr.route)427+ w := PerformRequest(router, "GET", tr.route)428 assert.Equal(t, tr.code, w.Code)429 if w.Code != http.StatusNotFound {430 assert.Equal(t, tr.location, fmt.Sprint(w.Header().Get("Location")))431 }21 unmodified lines453 {"/../path", http.StatusMovedPermanently, "/path"}, // Without CleanPath454 {"/nope", http.StatusNotFound, ""}, // NotFound455 }456 for _, tr := range testRoutes {456- w := performRequest(router, http.MethodGet, tr.route)457+ w := PerformRequest(router, http.MethodGet, tr.route)458 assert.Equal(t, tr.code, w.Code)459 if w.Code != http.StatusNotFound {460 assert.Equal(t, tr.location, fmt.Sprint(w.Header().Get("Location")))461 }4 unmodified lines466 router.NoRoute(func(c *Context) {467 c.AbortWithStatus(http.StatusNotFound)468 notFound = true469 })469- w := performRequest(router, http.MethodGet, "/nope")470+ w := PerformRequest(router, http.MethodGet, "/nope")471 assert.Equal(t, http.StatusNotFound, w.Code)472 assert.True(t, notFound)473 474 // Test other method than GET (want 307 instead of 301)475 router.PATCH("/path", func(c *Context) {})475- w = performRequest(router, http.MethodPatch, "/path/")476+ w = PerformRequest(router, http.MethodPatch, "/path/")477 assert.Equal(t, http.StatusTemporaryRedirect, w.Code)478 assert.Equal(t, "map[Location:[/path]]", fmt.Sprint(w.Header()))479 480 // Test special case where no node for the prefix "/" exists481 router = New()482 router.GET("/a", func(c *Context) {})482- w = performRequest(router, http.MethodGet, "/")483+ w = PerformRequest(router, http.MethodGet, "/")484 assert.Equal(t, http.StatusNotFound, w.Code)485 486 // Reproduction test for the bug of issue #2843487 router = New()4 unmodified lines492 })493 router.GET("/logout", func(c *Context) {494 c.String(200, "logout")495 })495- w = performRequest(router, http.MethodGet, "/login")496+ w = PerformRequest(router, http.MethodGet, "/login")497 assert.Equal(t, "login", w.Body.String())497- w = performRequest(router, http.MethodGet, "/logout")498+ w = PerformRequest(router, http.MethodGet, "/logout")499 assert.Equal(t, "logout", w.Body.String())500 }501 502 func TestRouterStaticFSNotFound(t *testing.T) {2 unmodified lines505 router.NoRoute(func(c *Context) {506 c.String(404, "non existent")507 })508 508- w := performRequest(router, http.MethodGet, "/nonexistent")509+ w := PerformRequest(router, http.MethodGet, "/nonexistent")510 assert.Equal(t, "non existent", w.Body.String())511 511- w = performRequest(router, http.MethodHead, "/nonexistent")512+ w = PerformRequest(router, http.MethodHead, "/nonexistent")513 assert.Equal(t, "non existent", w.Body.String())514 }515 516 func TestRouterStaticFSFileNotFound(t *testing.T) {1 unmodified line518 519 router.StaticFS("/", http.FileSystem(http.Dir(".")))520 521 assert.NotPanics(t, func() {521- performRequest(router, http.MethodGet, "/nonexistent")522+ PerformRequest(router, http.MethodGet, "/nonexistent")523 })524 }525 526 // Reproduction test for the bug of issue #18058 unmodified lines535 536 router.StaticFS("/", http.FileSystem(http.Dir("/thisreallydoesntexist/")))537 538 // First access538- performRequest(router, http.MethodGet, "/nonexistent")539+ PerformRequest(router, http.MethodGet, "/nonexistent")540 assert.Equal(t, 1, middlewareCalledNum)541 542 // Second access542- performRequest(router, http.MethodHead, "/nonexistent")543+ PerformRequest(router, http.MethodHead, "/nonexistent")544 assert.Equal(t, 2, middlewareCalledNum)545 }546 547 func TestRouteRawPath(t *testing.T) {10 unmodified lines558 assert.Equal(t, "Some/Other/Project", name)559 assert.Equal(t, "222", num)560 })561 561- w := performRequest(route, http.MethodPost, "/project/Some%2FOther%2FProject/build/222")562+ w := PerformRequest(route, http.MethodPost, "/project/Some%2FOther%2FProject/build/222")563 assert.Equal(t, http.StatusOK, w.Code)564 }565 566 func TestRouteRawPathNoUnescape(t *testing.T) {11 unmodified lines578 assert.Equal(t, "Some%2FOther%2FProject", name)579 assert.Equal(t, "333", num)580 })581 581- w := performRequest(route, http.MethodPost, "/project/Some%2FOther%2FProject/build/333")582+ w := PerformRequest(route, http.MethodPost, "/project/Some%2FOther%2FProject/build/333")583 assert.Equal(t, http.StatusOK, w.Code)584 }585 586 func TestRouteServeErrorWithWriteHeader(t *testing.T) {2 unmodified lines589 c.Status(421)590 c.Next()591 })592 592- w := performRequest(route, http.MethodGet, "/NotFound")593+ w := PerformRequest(route, http.MethodGet, "/NotFound")594 assert.Equal(t, 421, w.Code)595 assert.Equal(t, 0, w.Body.Len())596 }597 25 unmodified lines623 })624 }625 626 for _, route := range routes {626- w := performRequest(router, http.MethodGet, route)627+ w := PerformRequest(router, http.MethodGet, route)628 assert.Equal(t, http.StatusOK, w.Code)629 }630 631 // Test not found1 unmodified line633 // For not found routes full path is empty634 assert.Equal(t, "", c.FullPath())635 })636 636- w := performRequest(router, http.MethodGet, "/not-found")637+ w := PerformRequest(router, http.MethodGet, "/not-found")638 assert.Equal(t, http.StatusNotFound, w.Code)639 }modified:utils_test.go+4 added-4 removed
43 unmodified lines44 w.WriteHeader(http.StatusBadRequest)45 fmt.Fprint(w, "hola!")46 }))47 48- w := performRequest(router, "POST", "/path")48+ w := PerformRequest(router, "POST", "/path")49 assert.Equal(t, http.StatusInternalServerError, w.Code)50 assert.Equal(t, "hello", w.Body.String())51 52- w = performRequest(router, "GET", "/path2")52+ w = PerformRequest(router, "GET", "/path2")53 assert.Equal(t, http.StatusBadRequest, w.Code)54 assert.Equal(t, "hola!", w.Body.String())55 }56 61 unmodified lines118 router.GET("/", Bind(bindTestStruct{}), func(c *Context) {119 called = true120 value = c.MustGet(BindKey).(*bindTestStruct)121 })122- performRequest(router, "GET", "/?foo=hola&bar=10")122+ PerformRequest(router, "GET", "/?foo=hola&bar=10")123 assert.True(t, called)124 assert.Equal(t, "hola", value.Foo)125 assert.Equal(t, 10, value.Bar)126 127 called = false128- performRequest(router, "GET", "/?foo=hola&bar=1")128+ PerformRequest(router, "GET", "/?foo=hola&bar=1")129 assert.False(t, called)130 131 assert.Panics(t, func() {132 Bind(&bindTestStruct{})13 unmodified lines