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"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAddTrailingSlash(t *testing.T) {
|
|
|
|
e := echo.New()
|
2017-02-23 01:57:12 +02:00
|
|
|
req := httptest.NewRequest(echo.GET, "/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
|
|
|
})
|
|
|
|
h(c)
|
2016-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, "/add-slash/", req.URL.Path)
|
|
|
|
assert.Equal(t, "/add-slash/", req.RequestURI)
|
2016-04-13 07:39:29 +02:00
|
|
|
|
|
|
|
// With config
|
2017-05-14 00:44:27 +02:00
|
|
|
req = httptest.NewRequest(echo.GET, "/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
|
|
|
|
})
|
|
|
|
h(c)
|
2016-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, http.StatusMovedPermanently, rec.Code)
|
2016-04-24 19:21:23 +02:00
|
|
|
assert.Equal(t, "/add-slash/?key=value", rec.Header().Get(echo.HeaderLocation))
|
2016-03-31 21:17:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveTrailingSlash(t *testing.T) {
|
|
|
|
e := echo.New()
|
2017-02-23 01:57:12 +02:00
|
|
|
req := httptest.NewRequest(echo.GET, "/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
|
|
|
})
|
|
|
|
h(c)
|
2016-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, "/remove-slash", req.URL.Path)
|
|
|
|
assert.Equal(t, "/remove-slash", req.RequestURI)
|
2016-04-13 07:39:29 +02:00
|
|
|
|
|
|
|
// With config
|
2017-05-14 00:44:27 +02:00
|
|
|
req = httptest.NewRequest(echo.GET, "/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
|
|
|
|
})
|
|
|
|
h(c)
|
2016-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, http.StatusMovedPermanently, rec.Code)
|
2016-04-24 19:21:23 +02:00
|
|
|
assert.Equal(t, "/remove-slash?key=value", rec.Header().Get(echo.HeaderLocation))
|
2016-06-01 04:10:34 +02:00
|
|
|
|
|
|
|
// With bare URL
|
2017-05-14 00:44:27 +02:00
|
|
|
req = httptest.NewRequest(echo.GET, "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
|
|
|
|
})
|
|
|
|
h(c)
|
2016-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, "", req.URL.Path)
|
2016-03-31 21:17:18 +02:00
|
|
|
}
|