2016-03-31 21:17:18 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2016-04-13 07:39:29 +02:00
|
|
|
"net/http"
|
2016-03-31 21:17:18 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
"github.com/labstack/echo/test"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAddTrailingSlash(t *testing.T) {
|
|
|
|
e := echo.New()
|
|
|
|
rq := test.NewRequest(echo.GET, "/add-slash", nil)
|
|
|
|
rc := test.NewResponseRecorder()
|
|
|
|
c := echo.NewContext(rq, rc, e)
|
2016-04-02 23:19:39 +02:00
|
|
|
h := AddTrailingSlash()(func(c echo.Context) error {
|
2016-03-31 21:17:18 +02:00
|
|
|
return nil
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
|
|
|
h(c)
|
2016-03-31 21:17:18 +02:00
|
|
|
assert.Equal(t, "/add-slash/", rq.URL().Path())
|
2016-04-13 07:39:29 +02:00
|
|
|
|
|
|
|
// With config
|
|
|
|
rq = test.NewRequest(echo.GET, "/add-slash?key=value", nil)
|
|
|
|
rc = test.NewResponseRecorder()
|
|
|
|
c = echo.NewContext(rq, rc, e)
|
|
|
|
h = AddTrailingSlashWithConfig(TrailingSlashConfig{RedirectCode: http.StatusMovedPermanently})(func(c echo.Context) error {
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
h(c)
|
|
|
|
assert.Equal(t, http.StatusMovedPermanently, rc.Status())
|
|
|
|
assert.Equal(t, "/add-slash/?key=value", rc.Header().Get(echo.HeaderLocation))
|
2016-03-31 21:17:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveTrailingSlash(t *testing.T) {
|
|
|
|
e := echo.New()
|
|
|
|
rq := test.NewRequest(echo.GET, "/remove-slash/", nil)
|
|
|
|
rc := test.NewResponseRecorder()
|
|
|
|
c := echo.NewContext(rq, rc, e)
|
2016-04-02 23:19:39 +02:00
|
|
|
h := RemoveTrailingSlash()(func(c echo.Context) error {
|
2016-03-31 21:17:18 +02:00
|
|
|
return nil
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
|
|
|
h(c)
|
2016-03-31 21:17:18 +02:00
|
|
|
assert.Equal(t, "/remove-slash", rq.URL().Path())
|
2016-04-13 07:39:29 +02:00
|
|
|
|
|
|
|
// With config
|
|
|
|
rq = test.NewRequest(echo.GET, "/remove-slash/?key=value", nil)
|
|
|
|
rc = test.NewResponseRecorder()
|
|
|
|
c = echo.NewContext(rq, rc, e)
|
|
|
|
h = RemoveTrailingSlashWithConfig(TrailingSlashConfig{RedirectCode: http.StatusMovedPermanently})(func(c echo.Context) error {
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
h(c)
|
|
|
|
assert.Equal(t, http.StatusMovedPermanently, rc.Status())
|
|
|
|
assert.Equal(t, "/remove-slash?key=value", rc.Header().Get(echo.HeaderLocation))
|
2016-03-31 21:17:18 +02:00
|
|
|
}
|