2015-05-13 23:07:03 -07:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
2015-05-30 10:54:55 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-05-13 23:07:03 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStripTrailingSlash(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(echo.GET, "/users/", nil)
|
2015-05-30 10:54:55 -07:00
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := echo.NewContext(req, echo.NewResponse(rec), echo.New())
|
2015-05-13 23:07:03 -07:00
|
|
|
StripTrailingSlash()(c)
|
2015-05-30 10:54:55 -07:00
|
|
|
assert.Equal(t, "/users", c.Request().URL.Path)
|
2015-05-13 23:07:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRedirectToSlash(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(echo.GET, "/users", nil)
|
2015-05-30 10:54:55 -07:00
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := echo.NewContext(req, echo.NewResponse(rec), echo.New())
|
2015-05-14 16:25:49 -07:00
|
|
|
RedirectToSlash(RedirectToSlashOptions{Code: http.StatusTemporaryRedirect})(c)
|
2015-05-30 10:54:55 -07:00
|
|
|
assert.Equal(t, http.StatusTemporaryRedirect, rec.Code)
|
|
|
|
assert.Equal(t, "/users/", c.Response().Header().Get("Location"))
|
2015-05-13 23:07:03 -07:00
|
|
|
}
|