1
0
mirror of https://github.com/labstack/echo.git synced 2025-06-15 00:14:57 +02:00

Total coverage for middleware

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana
2015-05-30 10:54:55 -07:00
parent b526a0df5b
commit a9e49e2430
13 changed files with 228 additions and 235 deletions

View File

@ -6,33 +6,22 @@ import (
"testing"
"github.com/labstack/echo"
"github.com/stretchr/testify/assert"
)
func TestStripTrailingSlash(t *testing.T) {
req, _ := http.NewRequest(echo.GET, "/users/", nil)
res := echo.NewResponse(httptest.NewRecorder())
c := echo.NewContext(req, res, echo.New())
rec := httptest.NewRecorder()
c := echo.NewContext(req, echo.NewResponse(rec), echo.New())
StripTrailingSlash()(c)
p := c.Request().URL.Path
if p != "/users" {
t.Errorf("expected path `/users` got, %s.", p)
}
assert.Equal(t, "/users", c.Request().URL.Path)
}
func TestRedirectToSlash(t *testing.T) {
req, _ := http.NewRequest(echo.GET, "/users", nil)
res := echo.NewResponse(httptest.NewRecorder())
c := echo.NewContext(req, res, echo.New())
rec := httptest.NewRecorder()
c := echo.NewContext(req, echo.NewResponse(rec), echo.New())
RedirectToSlash(RedirectToSlashOptions{Code: http.StatusTemporaryRedirect})(c)
// Status code
if res.Status() != http.StatusTemporaryRedirect {
t.Errorf("expected status `307`, got %d.", res.Status())
}
// Location header
l := c.Response().Header().Get("Location")
if l != "/users/" {
t.Errorf("expected Location header `/users/`, got %s.", l)
}
assert.Equal(t, http.StatusTemporaryRedirect, rec.Code)
assert.Equal(t, "/users/", c.Response().Header().Get("Location"))
}