From d4dec77afa9b8948dc09cbf7c8c057af24b48528 Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Sat, 4 Jul 2015 14:13:25 +0200 Subject: [PATCH] Adds unit tests for RedirectTrailingSlash & RedirectFixedPath --- routes_test.go | 78 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 2 deletions(-) diff --git a/routes_test.go b/routes_test.go index 2210e13b..32f00983 100644 --- a/routes_test.go +++ b/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 := ""