Parcourir la source

Adds unit tests for RedirectTrailingSlash & RedirectFixedPath

Manu Mtz-Almeida il y a 10 ans
Parent
commit
d4dec77afa
1 fichiers modifiés avec 76 ajouts et 2 suppressions
  1. 76 2
      routes_test.go

+ 76 - 2
routes_test.go

@@ -109,7 +109,6 @@ func TestRouterGroupRouteOK(t *testing.T) {
 	testRouteOK("TRACE", t)
 }
 
-// TestSingleRouteOK tests that POST route is correctly invoked.
 func TestRouteNotOK(t *testing.T) {
 	testRouteNotOK("GET", t)
 	testRouteNotOK("POST", t)
@@ -122,7 +121,6 @@ func TestRouteNotOK(t *testing.T) {
 	testRouteNotOK("TRACE", t)
 }
 
-// TestSingleRouteOK tests that POST route is correctly invoked.
 func TestRouteNotOK2(t *testing.T) {
 	testRouteNotOK2("GET", t)
 	testRouteNotOK2("POST", t)
@@ -135,6 +133,82 @@ func TestRouteNotOK2(t *testing.T) {
 	testRouteNotOK2("TRACE", t)
 }
 
+func TestRouteRedirectTrailingSlash(t *testing.T) {
+	router := New()
+	router.RedirectFixedPath = false
+	router.RedirectTrailingSlash = true
+	router.GET("/path", func(c *Context) {})
+	router.GET("/path2/", func(c *Context) {})
+	router.POST("/path3", func(c *Context) {})
+	router.PUT("/path4/", func(c *Context) {})
+
+	w := performRequest(router, "GET", "/path/")
+	assert.Equal(t, w.Header().Get("Location"), "/path")
+	assert.Equal(t, w.Code, 301)
+
+	w = performRequest(router, "GET", "/path2")
+	assert.Equal(t, w.Header().Get("Location"), "/path2/")
+	assert.Equal(t, w.Code, 301)
+
+	w = performRequest(router, "POST", "/path3/")
+	assert.Equal(t, w.Header().Get("Location"), "/path3")
+	assert.Equal(t, w.Code, 307)
+
+	w = performRequest(router, "PUT", "/path4")
+	assert.Equal(t, w.Header().Get("Location"), "/path4/")
+	assert.Equal(t, w.Code, 307)
+
+	w = performRequest(router, "GET", "/path")
+	assert.Equal(t, w.Code, 200)
+
+	w = performRequest(router, "GET", "/path2/")
+	assert.Equal(t, w.Code, 200)
+
+	w = performRequest(router, "POST", "/path3")
+	assert.Equal(t, w.Code, 200)
+
+	w = performRequest(router, "PUT", "/path4/")
+	assert.Equal(t, w.Code, 200)
+
+	router.RedirectTrailingSlash = false
+
+	w = performRequest(router, "GET", "/path/")
+	assert.Equal(t, w.Code, 404)
+	w = performRequest(router, "GET", "/path2")
+	assert.Equal(t, w.Code, 404)
+	w = performRequest(router, "POST", "/path3/")
+	assert.Equal(t, w.Code, 404)
+	w = performRequest(router, "PUT", "/path4")
+	assert.Equal(t, w.Code, 404)
+}
+
+func TestRouteRedirectFixedPath(t *testing.T) {
+	router := New()
+	router.RedirectFixedPath = true
+	router.RedirectTrailingSlash = false
+
+	router.GET("/path", func(c *Context) {})
+	router.GET("/Path2", func(c *Context) {})
+	router.POST("/PATH3", func(c *Context) {})
+	router.POST("/Path4/", func(c *Context) {})
+
+	w := performRequest(router, "GET", "/PATH")
+	assert.Equal(t, w.Header().Get("Location"), "/path")
+	assert.Equal(t, w.Code, 301)
+
+	w = performRequest(router, "GET", "/path2")
+	assert.Equal(t, w.Header().Get("Location"), "/Path2")
+	assert.Equal(t, w.Code, 301)
+
+	w = performRequest(router, "POST", "/path3")
+	assert.Equal(t, w.Header().Get("Location"), "/PATH3")
+	assert.Equal(t, w.Code, 307)
+
+	w = performRequest(router, "POST", "/path4")
+	assert.Equal(t, w.Header().Get("Location"), "/Path4/")
+	assert.Equal(t, w.Code, 307)
+}
+
 // TestContextParamsGet tests that a parameter can be parsed from the URL.
 func TestRouteParamsByName(t *testing.T) {
 	name := ""