+78
-78
@@ -70,10 +70,10 @@ func testRouteNotOK2(method string, t *testing.T) {
|
||||
router := New()
|
||||
router.HandleMethodNotAllowed = true
|
||||
var methodRoute string
|
||||
if method == "POST" {
|
||||
methodRoute = "GET"
|
||||
if method == http.MethodPost {
|
||||
methodRoute = http.MethodGet
|
||||
} else {
|
||||
methodRoute = "POST"
|
||||
methodRoute = http.MethodPost
|
||||
}
|
||||
router.Handle(methodRoute, "/test", func(c *Context) {
|
||||
passed = true
|
||||
@@ -99,46 +99,46 @@ func TestRouterMethod(t *testing.T) {
|
||||
c.String(http.StatusOK, "sup3")
|
||||
})
|
||||
|
||||
w := performRequest(router, "PUT", "/hey")
|
||||
w := performRequest(router, http.MethodPut, "/hey")
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Equal(t, "called", w.Body.String())
|
||||
}
|
||||
|
||||
func TestRouterGroupRouteOK(t *testing.T) {
|
||||
testRouteOK("GET", t)
|
||||
testRouteOK("POST", t)
|
||||
testRouteOK("PUT", t)
|
||||
testRouteOK("PATCH", t)
|
||||
testRouteOK("HEAD", t)
|
||||
testRouteOK("OPTIONS", t)
|
||||
testRouteOK("DELETE", t)
|
||||
testRouteOK("CONNECT", t)
|
||||
testRouteOK("TRACE", t)
|
||||
testRouteOK(http.MethodGet, t)
|
||||
testRouteOK(http.MethodPost, t)
|
||||
testRouteOK(http.MethodPut, t)
|
||||
testRouteOK(http.MethodPatch, t)
|
||||
testRouteOK(http.MethodHead, t)
|
||||
testRouteOK(http.MethodOptions, t)
|
||||
testRouteOK(http.MethodDelete, t)
|
||||
testRouteOK(http.MethodConnect, t)
|
||||
testRouteOK(http.MethodTrace, t)
|
||||
}
|
||||
|
||||
func TestRouteNotOK(t *testing.T) {
|
||||
testRouteNotOK("GET", t)
|
||||
testRouteNotOK("POST", t)
|
||||
testRouteNotOK("PUT", t)
|
||||
testRouteNotOK("PATCH", t)
|
||||
testRouteNotOK("HEAD", t)
|
||||
testRouteNotOK("OPTIONS", t)
|
||||
testRouteNotOK("DELETE", t)
|
||||
testRouteNotOK("CONNECT", t)
|
||||
testRouteNotOK("TRACE", t)
|
||||
testRouteNotOK(http.MethodGet, t)
|
||||
testRouteNotOK(http.MethodPost, t)
|
||||
testRouteNotOK(http.MethodPut, t)
|
||||
testRouteNotOK(http.MethodPatch, t)
|
||||
testRouteNotOK(http.MethodHead, t)
|
||||
testRouteNotOK(http.MethodOptions, t)
|
||||
testRouteNotOK(http.MethodDelete, t)
|
||||
testRouteNotOK(http.MethodConnect, t)
|
||||
testRouteNotOK(http.MethodTrace, t)
|
||||
}
|
||||
|
||||
func TestRouteNotOK2(t *testing.T) {
|
||||
testRouteNotOK2("GET", t)
|
||||
testRouteNotOK2("POST", t)
|
||||
testRouteNotOK2("PUT", t)
|
||||
testRouteNotOK2("PATCH", t)
|
||||
testRouteNotOK2("HEAD", t)
|
||||
testRouteNotOK2("OPTIONS", t)
|
||||
testRouteNotOK2("DELETE", t)
|
||||
testRouteNotOK2("CONNECT", t)
|
||||
testRouteNotOK2("TRACE", t)
|
||||
testRouteNotOK2(http.MethodGet, t)
|
||||
testRouteNotOK2(http.MethodPost, t)
|
||||
testRouteNotOK2(http.MethodPut, t)
|
||||
testRouteNotOK2(http.MethodPatch, t)
|
||||
testRouteNotOK2(http.MethodHead, t)
|
||||
testRouteNotOK2(http.MethodOptions, t)
|
||||
testRouteNotOK2(http.MethodDelete, t)
|
||||
testRouteNotOK2(http.MethodConnect, t)
|
||||
testRouteNotOK2(http.MethodTrace, t)
|
||||
}
|
||||
|
||||
func TestRouteRedirectTrailingSlash(t *testing.T) {
|
||||
@@ -150,50 +150,50 @@ func TestRouteRedirectTrailingSlash(t *testing.T) {
|
||||
router.POST("/path3", func(c *Context) {})
|
||||
router.PUT("/path4/", func(c *Context) {})
|
||||
|
||||
w := performRequest(router, "GET", "/path/")
|
||||
w := performRequest(router, http.MethodGet, "/path/")
|
||||
assert.Equal(t, "/path", w.Header().Get("Location"))
|
||||
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
||||
|
||||
w = performRequest(router, "GET", "/path2")
|
||||
w = performRequest(router, http.MethodGet, "/path2")
|
||||
assert.Equal(t, "/path2/", w.Header().Get("Location"))
|
||||
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
||||
|
||||
w = performRequest(router, "POST", "/path3/")
|
||||
w = performRequest(router, http.MethodPost, "/path3/")
|
||||
assert.Equal(t, "/path3", w.Header().Get("Location"))
|
||||
assert.Equal(t, http.StatusTemporaryRedirect, w.Code)
|
||||
|
||||
w = performRequest(router, "PUT", "/path4")
|
||||
w = performRequest(router, http.MethodPut, "/path4")
|
||||
assert.Equal(t, "/path4/", w.Header().Get("Location"))
|
||||
assert.Equal(t, http.StatusTemporaryRedirect, w.Code)
|
||||
|
||||
w = performRequest(router, "GET", "/path")
|
||||
w = performRequest(router, http.MethodGet, "/path")
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
|
||||
w = performRequest(router, "GET", "/path2/")
|
||||
w = performRequest(router, http.MethodGet, "/path2/")
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
|
||||
w = performRequest(router, "POST", "/path3")
|
||||
w = performRequest(router, http.MethodPost, "/path3")
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
|
||||
w = performRequest(router, "PUT", "/path4/")
|
||||
w = performRequest(router, http.MethodPut, "/path4/")
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
|
||||
w = performRequest(router, "GET", "/path2", header{Key: "X-Forwarded-Prefix", Value: "/api"})
|
||||
w = performRequest(router, http.MethodGet, "/path2", header{Key: "X-Forwarded-Prefix", Value: "/api"})
|
||||
assert.Equal(t, "/api/path2/", w.Header().Get("Location"))
|
||||
assert.Equal(t, 301, w.Code)
|
||||
|
||||
w = performRequest(router, "GET", "/path2/", header{Key: "X-Forwarded-Prefix", Value: "/api/"})
|
||||
w = performRequest(router, http.MethodGet, "/path2/", header{Key: "X-Forwarded-Prefix", Value: "/api/"})
|
||||
assert.Equal(t, 200, w.Code)
|
||||
|
||||
router.RedirectTrailingSlash = false
|
||||
|
||||
w = performRequest(router, "GET", "/path/")
|
||||
w = performRequest(router, http.MethodGet, "/path/")
|
||||
assert.Equal(t, http.StatusNotFound, w.Code)
|
||||
w = performRequest(router, "GET", "/path2")
|
||||
w = performRequest(router, http.MethodGet, "/path2")
|
||||
assert.Equal(t, http.StatusNotFound, w.Code)
|
||||
w = performRequest(router, "POST", "/path3/")
|
||||
w = performRequest(router, http.MethodPost, "/path3/")
|
||||
assert.Equal(t, http.StatusNotFound, w.Code)
|
||||
w = performRequest(router, "PUT", "/path4")
|
||||
w = performRequest(router, http.MethodPut, "/path4")
|
||||
assert.Equal(t, http.StatusNotFound, w.Code)
|
||||
}
|
||||
|
||||
@@ -207,19 +207,19 @@ func TestRouteRedirectFixedPath(t *testing.T) {
|
||||
router.POST("/PATH3", func(c *Context) {})
|
||||
router.POST("/Path4/", func(c *Context) {})
|
||||
|
||||
w := performRequest(router, "GET", "/PATH")
|
||||
w := performRequest(router, http.MethodGet, "/PATH")
|
||||
assert.Equal(t, "/path", w.Header().Get("Location"))
|
||||
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
||||
|
||||
w = performRequest(router, "GET", "/path2")
|
||||
w = performRequest(router, http.MethodGet, "/path2")
|
||||
assert.Equal(t, "/Path2", w.Header().Get("Location"))
|
||||
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
||||
|
||||
w = performRequest(router, "POST", "/path3")
|
||||
w = performRequest(router, http.MethodPost, "/path3")
|
||||
assert.Equal(t, "/PATH3", w.Header().Get("Location"))
|
||||
assert.Equal(t, http.StatusTemporaryRedirect, w.Code)
|
||||
|
||||
w = performRequest(router, "POST", "/path4")
|
||||
w = performRequest(router, http.MethodPost, "/path4")
|
||||
assert.Equal(t, "/Path4/", w.Header().Get("Location"))
|
||||
assert.Equal(t, http.StatusTemporaryRedirect, w.Code)
|
||||
}
|
||||
@@ -249,7 +249,7 @@ func TestRouteParamsByName(t *testing.T) {
|
||||
assert.False(t, ok)
|
||||
})
|
||||
|
||||
w := performRequest(router, "GET", "/test/john/smith/is/super/great")
|
||||
w := performRequest(router, http.MethodGet, "/test/john/smith/is/super/great")
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Equal(t, "john", name)
|
||||
@@ -283,7 +283,7 @@ func TestRouteParamsByNameWithExtraSlash(t *testing.T) {
|
||||
assert.False(t, ok)
|
||||
})
|
||||
|
||||
w := performRequest(router, "GET", "//test//john//smith//is//super//great")
|
||||
w := performRequest(router, http.MethodGet, "//test//john//smith//is//super//great")
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Equal(t, "john", name)
|
||||
@@ -311,16 +311,16 @@ func TestRouteStaticFile(t *testing.T) {
|
||||
router.Static("/using_static", dir)
|
||||
router.StaticFile("/result", f.Name())
|
||||
|
||||
w := performRequest(router, "GET", "/using_static/"+filename)
|
||||
w2 := performRequest(router, "GET", "/result")
|
||||
w := performRequest(router, http.MethodGet, "/using_static/"+filename)
|
||||
w2 := performRequest(router, http.MethodGet, "/result")
|
||||
|
||||
assert.Equal(t, w, w2)
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Equal(t, "Gin Web Framework", w.Body.String())
|
||||
assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))
|
||||
|
||||
w3 := performRequest(router, "HEAD", "/using_static/"+filename)
|
||||
w4 := performRequest(router, "HEAD", "/result")
|
||||
w3 := performRequest(router, http.MethodHead, "/using_static/"+filename)
|
||||
w4 := performRequest(router, http.MethodHead, "/result")
|
||||
|
||||
assert.Equal(t, w3, w4)
|
||||
assert.Equal(t, http.StatusOK, w3.Code)
|
||||
@@ -331,7 +331,7 @@ func TestRouteStaticListingDir(t *testing.T) {
|
||||
router := New()
|
||||
router.StaticFS("/", Dir("./", true))
|
||||
|
||||
w := performRequest(router, "GET", "/")
|
||||
w := performRequest(router, http.MethodGet, "/")
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Contains(t, w.Body.String(), "gin.go")
|
||||
@@ -343,7 +343,7 @@ func TestRouteStaticNoListing(t *testing.T) {
|
||||
router := New()
|
||||
router.Static("/", "./")
|
||||
|
||||
w := performRequest(router, "GET", "/")
|
||||
w := performRequest(router, http.MethodGet, "/")
|
||||
|
||||
assert.Equal(t, http.StatusNotFound, w.Code)
|
||||
assert.NotContains(t, w.Body.String(), "gin.go")
|
||||
@@ -358,7 +358,7 @@ func TestRouterMiddlewareAndStatic(t *testing.T) {
|
||||
})
|
||||
static.Static("/", "./")
|
||||
|
||||
w := performRequest(router, "GET", "/gin.go")
|
||||
w := performRequest(router, http.MethodGet, "/gin.go")
|
||||
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Contains(t, w.Body.String(), "package gin")
|
||||
@@ -372,13 +372,13 @@ func TestRouteNotAllowedEnabled(t *testing.T) {
|
||||
router := New()
|
||||
router.HandleMethodNotAllowed = true
|
||||
router.POST("/path", func(c *Context) {})
|
||||
w := performRequest(router, "GET", "/path")
|
||||
w := performRequest(router, http.MethodGet, "/path")
|
||||
assert.Equal(t, http.StatusMethodNotAllowed, w.Code)
|
||||
|
||||
router.NoMethod(func(c *Context) {
|
||||
c.String(http.StatusTeapot, "responseText")
|
||||
})
|
||||
w = performRequest(router, "GET", "/path")
|
||||
w = performRequest(router, http.MethodGet, "/path")
|
||||
assert.Equal(t, "responseText", w.Body.String())
|
||||
assert.Equal(t, http.StatusTeapot, w.Code)
|
||||
}
|
||||
@@ -387,9 +387,9 @@ func TestRouteNotAllowedEnabled2(t *testing.T) {
|
||||
router := New()
|
||||
router.HandleMethodNotAllowed = true
|
||||
// add one methodTree to trees
|
||||
router.addRoute("POST", "/", HandlersChain{func(_ *Context) {}})
|
||||
router.addRoute(http.MethodPost, "/", HandlersChain{func(_ *Context) {}})
|
||||
router.GET("/path2", func(c *Context) {})
|
||||
w := performRequest(router, "POST", "/path2")
|
||||
w := performRequest(router, http.MethodPost, "/path2")
|
||||
assert.Equal(t, http.StatusMethodNotAllowed, w.Code)
|
||||
}
|
||||
|
||||
@@ -397,13 +397,13 @@ func TestRouteNotAllowedDisabled(t *testing.T) {
|
||||
router := New()
|
||||
router.HandleMethodNotAllowed = false
|
||||
router.POST("/path", func(c *Context) {})
|
||||
w := performRequest(router, "GET", "/path")
|
||||
w := performRequest(router, http.MethodGet, "/path")
|
||||
assert.Equal(t, http.StatusNotFound, w.Code)
|
||||
|
||||
router.NoMethod(func(c *Context) {
|
||||
c.String(http.StatusTeapot, "responseText")
|
||||
})
|
||||
w = performRequest(router, "GET", "/path")
|
||||
w = performRequest(router, http.MethodGet, "/path")
|
||||
assert.Equal(t, "404 page not found", w.Body.String())
|
||||
assert.Equal(t, http.StatusNotFound, w.Code)
|
||||
}
|
||||
@@ -453,7 +453,7 @@ func TestRouterNotFound(t *testing.T) {
|
||||
{"/nope", http.StatusNotFound, ""}, // NotFound
|
||||
}
|
||||
for _, tr := range testRoutes {
|
||||
w := performRequest(router, "GET", tr.route)
|
||||
w := performRequest(router, http.MethodGet, tr.route)
|
||||
assert.Equal(t, tr.code, w.Code)
|
||||
if w.Code != http.StatusNotFound {
|
||||
assert.Equal(t, tr.location, fmt.Sprint(w.Header().Get("Location")))
|
||||
@@ -466,20 +466,20 @@ func TestRouterNotFound(t *testing.T) {
|
||||
c.AbortWithStatus(http.StatusNotFound)
|
||||
notFound = true
|
||||
})
|
||||
w := performRequest(router, "GET", "/nope")
|
||||
w := performRequest(router, http.MethodGet, "/nope")
|
||||
assert.Equal(t, http.StatusNotFound, w.Code)
|
||||
assert.True(t, notFound)
|
||||
|
||||
// Test other method than GET (want 307 instead of 301)
|
||||
router.PATCH("/path", func(c *Context) {})
|
||||
w = performRequest(router, "PATCH", "/path/")
|
||||
w = performRequest(router, http.MethodPatch, "/path/")
|
||||
assert.Equal(t, http.StatusTemporaryRedirect, w.Code)
|
||||
assert.Equal(t, "map[Location:[/path]]", fmt.Sprint(w.Header()))
|
||||
|
||||
// Test special case where no node for the prefix "/" exists
|
||||
router = New()
|
||||
router.GET("/a", func(c *Context) {})
|
||||
w = performRequest(router, "GET", "/")
|
||||
w = performRequest(router, http.MethodGet, "/")
|
||||
assert.Equal(t, http.StatusNotFound, w.Code)
|
||||
}
|
||||
|
||||
@@ -490,10 +490,10 @@ func TestRouterStaticFSNotFound(t *testing.T) {
|
||||
c.String(404, "non existent")
|
||||
})
|
||||
|
||||
w := performRequest(router, "GET", "/nonexistent")
|
||||
w := performRequest(router, http.MethodGet, "/nonexistent")
|
||||
assert.Equal(t, "non existent", w.Body.String())
|
||||
|
||||
w = performRequest(router, "HEAD", "/nonexistent")
|
||||
w = performRequest(router, http.MethodHead, "/nonexistent")
|
||||
assert.Equal(t, "non existent", w.Body.String())
|
||||
}
|
||||
|
||||
@@ -503,7 +503,7 @@ func TestRouterStaticFSFileNotFound(t *testing.T) {
|
||||
router.StaticFS("/", http.FileSystem(http.Dir(".")))
|
||||
|
||||
assert.NotPanics(t, func() {
|
||||
performRequest(router, "GET", "/nonexistent")
|
||||
performRequest(router, http.MethodGet, "/nonexistent")
|
||||
})
|
||||
}
|
||||
|
||||
@@ -520,11 +520,11 @@ func TestMiddlewareCalledOnceByRouterStaticFSNotFound(t *testing.T) {
|
||||
router.StaticFS("/", http.FileSystem(http.Dir("/thisreallydoesntexist/")))
|
||||
|
||||
// First access
|
||||
performRequest(router, "GET", "/nonexistent")
|
||||
performRequest(router, http.MethodGet, "/nonexistent")
|
||||
assert.Equal(t, 1, middlewareCalledNum)
|
||||
|
||||
// Second access
|
||||
performRequest(router, "HEAD", "/nonexistent")
|
||||
performRequest(router, http.MethodHead, "/nonexistent")
|
||||
assert.Equal(t, 2, middlewareCalledNum)
|
||||
}
|
||||
|
||||
@@ -543,7 +543,7 @@ func TestRouteRawPath(t *testing.T) {
|
||||
assert.Equal(t, "222", num)
|
||||
})
|
||||
|
||||
w := performRequest(route, "POST", "/project/Some%2FOther%2FProject/build/222")
|
||||
w := performRequest(route, http.MethodPost, "/project/Some%2FOther%2FProject/build/222")
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
@@ -563,7 +563,7 @@ func TestRouteRawPathNoUnescape(t *testing.T) {
|
||||
assert.Equal(t, "333", num)
|
||||
})
|
||||
|
||||
w := performRequest(route, "POST", "/project/Some%2FOther%2FProject/build/333")
|
||||
w := performRequest(route, http.MethodPost, "/project/Some%2FOther%2FProject/build/333")
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
@@ -574,7 +574,7 @@ func TestRouteServeErrorWithWriteHeader(t *testing.T) {
|
||||
c.Next()
|
||||
})
|
||||
|
||||
w := performRequest(route, "GET", "/NotFound")
|
||||
w := performRequest(route, http.MethodGet, "/NotFound")
|
||||
assert.Equal(t, 421, w.Code)
|
||||
assert.Equal(t, 0, w.Body.Len())
|
||||
}
|
||||
@@ -605,7 +605,7 @@ func TestRouteContextHoldsFullPath(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, route := range routes {
|
||||
w := performRequest(router, "GET", route)
|
||||
w := performRequest(router, http.MethodGet, route)
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
@@ -615,6 +615,6 @@ func TestRouteContextHoldsFullPath(t *testing.T) {
|
||||
assert.Equal(t, "", c.FullPath())
|
||||
})
|
||||
|
||||
w := performRequest(router, "GET", "/not-found")
|
||||
w := performRequest(router, http.MethodGet, "/not-found")
|
||||
assert.Equal(t, http.StatusNotFound, w.Code)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user