1
0
mirror of https://github.com/labstack/echo.git synced 2025-07-05 00:58:47 +02:00

Simplify code of Add/Remove trailing slash and fix bug (#1275)

* Simplify code of Add/Remove trailing slash

- simplify code (more informative / understanding)
- assert collides with imported package name (in tests)
- fix unhandled errors

* add tests for https://github.com/labstack/echo/pull/1275#issuecomment-460467700
This commit is contained in:
Evgeniy Kulikov
2019-02-07 20:49:51 +03:00
committed by Vishal Rana
parent bc28fceaf3
commit 88965757af
2 changed files with 40 additions and 19 deletions

View File

@ -1,6 +1,8 @@
package middleware
import (
"strings"
"github.com/labstack/echo/v4"
)
@ -49,7 +51,7 @@ func AddTrailingSlashWithConfig(config TrailingSlashConfig) echo.MiddlewareFunc
url := req.URL
path := url.Path
qs := c.QueryString()
if path != "/" && path[len(path)-1] != '/' {
if !strings.HasSuffix(path, "/") {
path += "/"
uri := path
if qs != "" {
@ -97,7 +99,7 @@ func RemoveTrailingSlashWithConfig(config TrailingSlashConfig) echo.MiddlewareFu
path := url.Path
qs := c.QueryString()
l := len(path) - 1
if l >= 0 && path != "/" && path[l] == '/' {
if l > 0 && strings.HasSuffix(path, "/") {
path = path[:l]
uri := path
if qs != "" {