Skip to content
Myr

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

9 files+115 added−120 removed

modified:context_test.go+2 added-2 removed
2034 unmodified lines
2035 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}20432044func TestContextWithKeysMutex(t *testing.T) {
129 unmodified lines
modified:gin_test.go+2 added-4 removed
393 unmodified lines
394 compareFunc(t, router.allNoMethod[1], middleware0)395}396397func TestRebuild404Handlers(t *testing.T) {398398}399400func TestNoMethodWithGlobalHandlers(t *testing.T) {401 var middleware0 HandlerFunc = func(c *Context) {}
87 unmodified lines
489 v2.GET("/", func(c *Context) {})490 }491492 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 lines
522 atomic.AddInt64(&handlerCounter, 1)523 })524525 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 lines
634635 assert.Nil(t, r.trustedCIDRs)636 assert.Nil(t, err)637 }639638}639640func parseCIDR(cidr string) *net.IPNet {641 _, parsedCIDR, err := net.ParseCIDR(cidr)
18 unmodified lines
modified:githubapi_test.go+5 added-5 removed
300 unmodified lines
301 c.String(http.StatusOK, "ShouldBindUri test OK")302 })303304 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 lines
323 c.String(http.StatusOK, "BindUri test OK")324 })325326 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 lines
341 assert.Error(t, c.BindUri(&m))342 })343344 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}348349func TestRaceContextCopy(t *testing.T) {
7 unmodified lines
357 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}364365func readWriteKeys(c *Context) {
22 unmodified lines
388 githubConfigRouter(router)389390 for _, route := range githubAPI {391 path, values := exampleFromPath(route.path)392 w := performRequest(router, route.method, path)392 w := PerformRequest(router, route.method, path)393394 // TEST395 assert.Contains(t, w.Body.String(), "\"status\":\"good\"")396 for _, value := range values {
78 unmodified lines
modified:logger_test.go+25 added-27 removed
29 unmodified lines
30 router.PATCH("/example", func(c *Context) {})31 router.HEAD("/example", func(c *Context) {})32 router.OPTIONS("/example", func(c *Context) {})3334 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 line
40 // 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")4849 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")5455 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")6061 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")6667 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")7273 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")7879 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 lines
94 router.PATCH("/example", func(c *Context) {})95 router.HEAD("/example", func(c *Context) {})96 router.OPTIONS("/example", func(c *Context) {})9798 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 line
104 // 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")112113 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")118119 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")124125 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")130131 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")136137 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")142143 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 lines
168 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")173174 // output test175 assert.Contains(t, buffer.String(), "[FORMATTER TEST]")176 assert.Contains(t, buffer.String(), "200")
31 unmodified lines
208 // 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")213214 // output test215 assert.Contains(t, buffer.String(), "[FORMATTER TEST]")216 assert.Contains(t, buffer.String(), "200")
10 unmodified lines
227 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)231231}232233func TestDefaultLogFormatter(t *testing.T) {234 timeStamp := time.Unix(1544173902, 0).UTC()
45 unmodified lines
280 assert.Equal(t, "[GIN] 2018/12/07 - 09:11:42 | 200 | 2743h29m3s | 20.20.20.20 | GET \"/\"\n", defaultLogFormatter(termFalseLongDurationParam))281282 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))285284}285286func TestColorForMethod(t *testing.T) {287 colorForMethod := func(method string) string {
78 unmodified lines
366 c.Error(errors.New("this is an error")) // nolint: errcheck367 c.String(http.StatusInternalServerError, "hola!")368 })369372 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())373376 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())377380 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 lines
386 router.Use(LoggerWithWriter(buffer, "/skipped"))387 router.GET("/logged", func(c *Context) {})388 router.GET("/skipped", func(c *Context) {})389392 performRequest(router, "GET", "/logged")390 PerformRequest(router, "GET", "/logged")391 assert.Contains(t, buffer.String(), "200")392393 buffer.Reset()396 performRequest(router, "GET", "/skipped")394 PerformRequest(router, "GET", "/skipped")395 assert.Contains(t, buffer.String(), "")396}397398func TestLoggerWithConfigSkippingPaths(t *testing.T) {
5 unmodified lines
404 }))405 router.GET("/logged", func(c *Context) {})406 router.GET("/skipped", func(c *Context) {})407410 performRequest(router, "GET", "/logged")408 PerformRequest(router, "GET", "/logged")409 assert.Contains(t, buffer.String(), "200")410411 buffer.Reset()414 performRequest(router, "GET", "/skipped")412 PerformRequest(router, "GET", "/skipped")413 assert.Contains(t, buffer.String(), "")414}415416func TestDisableConsoleColor(t *testing.T) {
18 unmodified lines
modified:middleware_test.go+8 added-9 removed
33 unmodified lines
34 router.NoMethod(func(c *Context) {35 signature += " XX "36 })37 // RUN38 w := performRequest(router, "GET", "/")38 w := PerformRequest(router, "GET", "/")3940 // TEST41 assert.Equal(t, http.StatusOK, w.Code)42 assert.Equal(t, "ACDB", signature)
27 unmodified lines
70 router.NoMethod(func(c *Context) {71 signature += " X "72 })73 // RUN74 w := performRequest(router, "GET", "/")74 w := PerformRequest(router, "GET", "/")7576 // TEST77 assert.Equal(t, http.StatusNotFound, w.Code)78 assert.Equal(t, "ACEGHFDB", signature)
28 unmodified lines
107 router.POST("/", func(c *Context) {108 signature += " XX "109 })110 // RUN111 w := performRequest(router, "GET", "/")111 w := PerformRequest(router, "GET", "/")112113 // TEST114 assert.Equal(t, http.StatusMethodNotAllowed, w.Code)115 assert.Equal(t, "ACEGHFDB", signature)
32 unmodified lines
148 signature += " XX "149 })150151 // RUN152 w := performRequest(router, "GET", "/")152 w := PerformRequest(router, "GET", "/")153154 // TEST155 assert.Equal(t, http.StatusNotFound, w.Code)156 assert.Equal(t, "AC X DB", signature)
17 unmodified lines
174 signature += " XX "175 })176177 // RUN178 w := performRequest(router, "GET", "/")178 w := PerformRequest(router, "GET", "/")179180 // TEST181 assert.Equal(t, http.StatusUnauthorized, w.Code)182 assert.Equal(t, "ACD", signature)
6 unmodified lines
189 signature += "A"190 c.Next()191 c.AbortWithStatus(http.StatusGone)192 signature += "B"193193 })194 router.GET("/", func(c *Context) {195 signature += "C"196 c.Next()197 })198 // RUN200 w := performRequest(router, "GET", "/")199 w := PerformRequest(router, "GET", "/")200201 // TEST202 assert.Equal(t, http.StatusGone, w.Code)203 assert.Equal(t, "ACB", signature)
14 unmodified lines
218 context.Next()219 signature += "C"220 })221 // RUN223 w := performRequest(router, "GET", "/")222 w := PerformRequest(router, "GET", "/")223224 // TEST225 assert.Equal(t, http.StatusInternalServerError, w.Code)226 assert.Equal(t, "A", signature)
18 unmodified lines
245 Data: "message",246 })247 })248250 w := performRequest(router, "GET", "/")249 w := PerformRequest(router, "GET", "/")250251 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 lines
26 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 lines
56 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 lines
6768 // 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 lines
84 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 lines
121 }122123 for errno, expectMsg := range expectMsgs {124 t.Run(expectMsg, func(t *testing.T) {125125 var buf bytes.Buffer126127 router := New()128 router.Use(RecoveryWithWriter(&buf))
6 unmodified lines
135 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 lines
156 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 lines
167168 // 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 lines
191 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 lines
202203 // 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 lines
226 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 lines
237238 // 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 lines
modified:routergroup_test.go+2 added-2 removed
78 unmodified lines
79 default:80 panic("unknown method")81 }8283 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())8687 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 lines
modified:routes_test.go+56 added-55 removed
19 unmodified lines
20 Key string21 Value string22}2324// PerformRequest for testing gin router.24func performRequest(r http.Handler, method, path string, headers ...header) *httptest.ResponseRecorder {25func 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 lines
42 r.Handle(method, "/test", func(c *Context) {43 passed = true44 })4545 w := performRequest(r, method, "/test")46 w := PerformRequest(r, method, "/test")47 assert.True(t, passed)48 assert.Equal(t, http.StatusOK, w.Code)4949 performRequest(r, method, "/test2")50 PerformRequest(r, method, "/test2")51 assert.True(t, passedAny)52}5354// TestSingleRouteOK tests that POST route is correctly invoked.
3 unmodified lines
58 router.Handle(method, "/test_2", func(c *Context) {59 passed = true60 })6161 w := performRequest(router, method, "/test")62 w := PerformRequest(router, method, "/test")6364 assert.False(t, passed)65 assert.Equal(t, http.StatusNotFound, w.Code)66}
12 unmodified lines
79 router.Handle(methodRoute, "/test", func(c *Context) {80 passed = true81 })8282 w := performRequest(router, method, "/test")83 w := PerformRequest(router, method, "/test")8485 assert.False(t, passed)86 assert.Equal(t, http.StatusMethodNotAllowed, w.Code)87}
11 unmodified lines
99 router.PUT("/hey3", func(c *Context) {100 c.String(http.StatusOK, "sup3")101 })102102 w := performRequest(router, http.MethodPut, "/hey")103 w := PerformRequest(router, http.MethodPut, "/hey")104105 assert.Equal(t, http.StatusOK, w.Code)106 assert.Equal(t, "called", w.Body.String())107}
42 unmodified lines
150 router.GET("/path2/", func(c *Context) {})151 router.POST("/path3", func(c *Context) {})152 router.PUT("/path4/", func(c *Context) {})153153 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)157157 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)161161 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)165165 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)169169 w = performRequest(router, http.MethodGet, "/path")170 w = PerformRequest(router, http.MethodGet, "/path")171 assert.Equal(t, http.StatusOK, w.Code)172172 w = performRequest(router, http.MethodGet, "/path2/")173 w = PerformRequest(router, http.MethodGet, "/path2/")174 assert.Equal(t, http.StatusOK, w.Code)175175 w = performRequest(router, http.MethodPost, "/path3")176 w = PerformRequest(router, http.MethodPost, "/path3")177 assert.Equal(t, http.StatusOK, w.Code)178178 w = performRequest(router, http.MethodPut, "/path4/")179 w = PerformRequest(router, http.MethodPut, "/path4/")180 assert.Equal(t, http.StatusOK, w.Code)181181 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)185185 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)188189 router.RedirectTrailingSlash = false190190 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}200201func TestRouteRedirectFixedPath(t *testing.T) {
5 unmodified lines
207 router.GET("/Path2", func(c *Context) {})208 router.POST("/PATH3", func(c *Context) {})209 router.POST("/Path4/", func(c *Context) {})210210 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)214214 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)218218 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)222222 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 lines
248 assert.Empty(t, wtf)249 assert.False(t, ok)250 })251251 w := performRequest(router, http.MethodGet, "/test/john/smith/is/super/great")252 w := PerformRequest(router, http.MethodGet, "/test/john/smith/is/super/great")253254 assert.Equal(t, http.StatusOK, w.Code)255 assert.Equal(t, "john", name)256 assert.Equal(t, "smith", lastName)
24 unmodified lines
281 assert.Empty(t, wtf)282 assert.False(t, ok)283 })284284 w := performRequest(router, http.MethodGet, "//test//john//smith//is//super//great")285 w := PerformRequest(router, http.MethodGet, "//test//john//smith//is//super//great")286287 assert.Equal(t, http.StatusOK, w.Code)288 assert.Equal(t, "john", name)289 assert.Equal(t, "smith", lastName)
19 unmodified lines
309 router := New()310 router.Static("/using_static", dir)311 router.StaticFile("/result", f.Name())312312 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")315316 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"))320320 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")323324 assert.Equal(t, w3, w4)325 assert.Equal(t, http.StatusOK, w3.Code)326}
2 unmodified lines
329func TestRouteStaticListingDir(t *testing.T) {330 router := New()331 router.StaticFS("/", Dir("./", true))332332 w := performRequest(router, http.MethodGet, "/")333 w := PerformRequest(router, http.MethodGet, "/")334335 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 lines
341func TestRouteStaticNoListing(t *testing.T) {342 router := New()343 router.Static("/", "./")344344 w := performRequest(router, http.MethodGet, "/")345 w := PerformRequest(router, http.MethodGet, "/")346347 assert.Equal(t, http.StatusNotFound, w.Code)348 assert.NotContains(t, w.Body.String(), "gin.go")349}
6 unmodified lines
356 c.Writer.Header().Add("X-GIN", "Gin Framework")357 })358 static.Static("/", "./")359359 w := performRequest(router, http.MethodGet, "/gin.go")360 w := PerformRequest(router, http.MethodGet, "/gin.go")361362 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 lines
372func 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)378379 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 lines
389 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}396397func 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)403404 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 lines
423 {"/../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 lines
453 {"/../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 lines
466 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)473474 // 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()))479480 // 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)485486 // Reproduction test for the bug of issue #2843487 router = New()
4 unmodified lines
492 })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}501502func TestRouterStaticFSNotFound(t *testing.T) {
2 unmodified lines
505 router.NoRoute(func(c *Context) {506 c.String(404, "non existent")507 })508508 w := performRequest(router, http.MethodGet, "/nonexistent")509 w := PerformRequest(router, http.MethodGet, "/nonexistent")510 assert.Equal(t, "non existent", w.Body.String())511511 w = performRequest(router, http.MethodHead, "/nonexistent")512 w = PerformRequest(router, http.MethodHead, "/nonexistent")513 assert.Equal(t, "non existent", w.Body.String())514}515516func TestRouterStaticFSFileNotFound(t *testing.T) {
1 unmodified line
518519 router.StaticFS("/", http.FileSystem(http.Dir(".")))520521 assert.NotPanics(t, func() {521 performRequest(router, http.MethodGet, "/nonexistent")522 PerformRequest(router, http.MethodGet, "/nonexistent")523 })524}525526// Reproduction test for the bug of issue #1805
8 unmodified lines
535536 router.StaticFS("/", http.FileSystem(http.Dir("/thisreallydoesntexist/")))537538 // First access538 performRequest(router, http.MethodGet, "/nonexistent")539 PerformRequest(router, http.MethodGet, "/nonexistent")540 assert.Equal(t, 1, middlewareCalledNum)541542 // Second access542 performRequest(router, http.MethodHead, "/nonexistent")543 PerformRequest(router, http.MethodHead, "/nonexistent")544 assert.Equal(t, 2, middlewareCalledNum)545}546547func TestRouteRawPath(t *testing.T) {
10 unmodified lines
558 assert.Equal(t, "Some/Other/Project", name)559 assert.Equal(t, "222", num)560 })561561 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}565566func TestRouteRawPathNoUnescape(t *testing.T) {
11 unmodified lines
578 assert.Equal(t, "Some%2FOther%2FProject", name)579 assert.Equal(t, "333", num)580 })581581 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}585586func TestRouteServeErrorWithWriteHeader(t *testing.T) {
2 unmodified lines
589 c.Status(421)590 c.Next()591 })592592 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 lines
623 })624 }625626 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 }630631 // Test not found
1 unmodified line
633 // For not found routes full path is empty634 assert.Equal(t, "", c.FullPath())635 })636636 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 lines
44 w.WriteHeader(http.StatusBadRequest)45 fmt.Fprint(w, "hola!")46 }))4748 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())5152 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 lines
118 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)126127 called = false128 performRequest(router, "GET", "/?foo=hola&bar=1")128 PerformRequest(router, "GET", "/?foo=hola&bar=1")129 assert.False(t, called)130131 assert.Panics(t, func() {132 Bind(&bindTestStruct{})
13 unmodified lines