2016-03-31 21:17:18 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2016-04-13 07:39:29 +02:00
|
|
|
"net/http"
|
2016-09-23 07:53:44 +02:00
|
|
|
"net/http/httptest"
|
2016-03-31 21:17:18 +02:00
|
|
|
"testing"
|
|
|
|
|
2019-01-30 12:56:56 +02:00
|
|
|
"github.com/labstack/echo/v4"
|
2016-03-31 21:17:18 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAddTrailingSlash(t *testing.T) {
|
2019-02-07 19:49:51 +02:00
|
|
|
is := assert.New(t)
|
2016-03-31 21:17:18 +02:00
|
|
|
e := echo.New()
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/add-slash", nil)
|
2016-09-23 07:53:44 +02:00
|
|
|
rec := httptest.NewRecorder()
|
2016-04-24 19:21:23 +02:00
|
|
|
c := e.NewContext(req, rec)
|
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
|
|
|
})
|
2019-02-07 19:49:51 +02:00
|
|
|
is.NoError(h(c))
|
|
|
|
is.Equal("/add-slash/", req.URL.Path)
|
|
|
|
is.Equal("/add-slash/", req.RequestURI)
|
2018-10-14 09:18:44 +02:00
|
|
|
|
2019-02-07 19:49:51 +02:00
|
|
|
// Method Connect must not fail:
|
|
|
|
req = httptest.NewRequest(http.MethodConnect, "", nil)
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = e.NewContext(req, rec)
|
|
|
|
h = AddTrailingSlash()(func(c echo.Context) error {
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
is.NoError(h(c))
|
|
|
|
is.Equal("/", req.URL.Path)
|
|
|
|
is.Equal("/", req.RequestURI)
|
2016-04-13 07:39:29 +02:00
|
|
|
|
|
|
|
// With config
|
2018-10-14 17:16:58 +02:00
|
|
|
req = httptest.NewRequest(http.MethodGet, "/add-slash?key=value", nil)
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-04-24 19:21:23 +02:00
|
|
|
c = e.NewContext(req, rec)
|
2016-04-13 22:28:13 +02:00
|
|
|
h = AddTrailingSlashWithConfig(TrailingSlashConfig{
|
|
|
|
RedirectCode: http.StatusMovedPermanently,
|
|
|
|
})(func(c echo.Context) error {
|
2016-04-13 07:39:29 +02:00
|
|
|
return nil
|
|
|
|
})
|
2019-02-07 19:49:51 +02:00
|
|
|
is.NoError(h(c))
|
|
|
|
is.Equal(http.StatusMovedPermanently, rec.Code)
|
|
|
|
is.Equal("/add-slash/?key=value", rec.Header().Get(echo.HeaderLocation))
|
2016-03-31 21:17:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveTrailingSlash(t *testing.T) {
|
2019-02-07 19:49:51 +02:00
|
|
|
is := assert.New(t)
|
2016-03-31 21:17:18 +02:00
|
|
|
e := echo.New()
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/remove-slash/", nil)
|
2016-09-23 07:53:44 +02:00
|
|
|
rec := httptest.NewRecorder()
|
2016-04-24 19:21:23 +02:00
|
|
|
c := e.NewContext(req, rec)
|
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
|
|
|
})
|
2019-02-07 19:49:51 +02:00
|
|
|
is.NoError(h(c))
|
|
|
|
is.Equal("/remove-slash", req.URL.Path)
|
|
|
|
is.Equal("/remove-slash", req.RequestURI)
|
2018-10-14 09:18:44 +02:00
|
|
|
|
2019-02-07 19:49:51 +02:00
|
|
|
// Method Connect must not fail:
|
|
|
|
req = httptest.NewRequest(http.MethodConnect, "", nil)
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = e.NewContext(req, rec)
|
|
|
|
h = RemoveTrailingSlash()(func(c echo.Context) error {
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
is.NoError(h(c))
|
|
|
|
is.Equal("", req.URL.Path)
|
|
|
|
is.Equal("", req.RequestURI)
|
2016-04-13 07:39:29 +02:00
|
|
|
|
|
|
|
// With config
|
2018-10-14 17:16:58 +02:00
|
|
|
req = httptest.NewRequest(http.MethodGet, "/remove-slash/?key=value", nil)
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-04-24 19:21:23 +02:00
|
|
|
c = e.NewContext(req, rec)
|
2016-04-13 22:28:13 +02:00
|
|
|
h = RemoveTrailingSlashWithConfig(TrailingSlashConfig{
|
|
|
|
RedirectCode: http.StatusMovedPermanently,
|
|
|
|
})(func(c echo.Context) error {
|
2016-04-13 07:39:29 +02:00
|
|
|
return nil
|
|
|
|
})
|
2019-02-07 19:49:51 +02:00
|
|
|
is.NoError(h(c))
|
|
|
|
is.Equal(http.StatusMovedPermanently, rec.Code)
|
|
|
|
is.Equal("/remove-slash?key=value", rec.Header().Get(echo.HeaderLocation))
|
2016-06-01 04:10:34 +02:00
|
|
|
|
|
|
|
// With bare URL
|
2018-10-14 17:16:58 +02:00
|
|
|
req = httptest.NewRequest(http.MethodGet, "http://localhost", nil)
|
2016-09-23 07:53:44 +02:00
|
|
|
rec = httptest.NewRecorder()
|
2016-06-01 04:10:34 +02:00
|
|
|
c = e.NewContext(req, rec)
|
|
|
|
h = RemoveTrailingSlash()(func(c echo.Context) error {
|
|
|
|
return nil
|
|
|
|
})
|
2019-02-07 19:49:51 +02:00
|
|
|
is.NoError(h(c))
|
|
|
|
is.Equal("", req.URL.Path)
|
2016-03-31 21:17:18 +02:00
|
|
|
}
|