forked from labstack/echo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Vishal Rana <[email protected]>
- Loading branch information
Showing
4 changed files
with
79 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package middleware | ||
|
||
import "github.com/labstack/echo" | ||
|
||
// StripTrailingSlash removes trailing slash from request path. | ||
func StripTrailingSlash() echo.HandlerFunc { | ||
return func(c *echo.Context) *echo.HTTPError { | ||
p := c.Request.URL.Path | ||
l := len(p) | ||
if p[l-1] == '/' { | ||
c.Request.URL.Path = p[:l-1] | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
// RedirectToSlash redirects requests without trailing slash path to trailing slash | ||
// path with status code. | ||
func RedirectToSlash(code int) echo.HandlerFunc { | ||
return func(c *echo.Context) (he *echo.HTTPError) { | ||
p := c.Request.URL.Path | ||
l := len(p) | ||
if p[l-1] != '/' { | ||
c.Redirect(code, p+"/") | ||
} | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/labstack/echo" | ||
) | ||
|
||
func TestStripTrailingSlash(t *testing.T) { | ||
req, _ := http.NewRequest(echo.GET, "/users/", nil) | ||
res := &echo.Response{Writer: httptest.NewRecorder()} | ||
c := echo.NewContext(req, res, echo.New()) | ||
StripTrailingSlash()(c) | ||
if c.Request.URL.Path != "/users" { | ||
t.Error("it should strip the trailing slash") | ||
} | ||
} | ||
|
||
func TestRedirectToSlash(t *testing.T) { | ||
req, _ := http.NewRequest(echo.GET, "/users", nil) | ||
res := &echo.Response{Writer: httptest.NewRecorder()} | ||
c := echo.NewContext(req, res, echo.New()) | ||
RedirectToSlash(301)(c) | ||
println(c.Response.Header().Get("Location")) | ||
if res.Status() != 301 { | ||
t.Errorf("status code should be 301, found %d", res.Status()) | ||
} | ||
if c.Response.Header().Get("Location") != "/users/" { | ||
t.Error("Location header should be /users/") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters