2015-05-14 08:07:03 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStripTrailingSlash(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(echo.GET, "/users/", nil)
|
2015-05-20 23:38:51 +02:00
|
|
|
res := echo.NewResponse(httptest.NewRecorder())
|
2015-05-14 08:07:03 +02:00
|
|
|
c := echo.NewContext(req, res, echo.New())
|
|
|
|
StripTrailingSlash()(c)
|
2015-05-16 21:49:01 +02:00
|
|
|
p := c.Request.URL.Path
|
|
|
|
if p != "/users" {
|
|
|
|
t.Errorf("expected path `/users` got, %s.", p)
|
2015-05-14 08:07:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRedirectToSlash(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(echo.GET, "/users", nil)
|
2015-05-20 23:38:51 +02:00
|
|
|
res := echo.NewResponse(httptest.NewRecorder())
|
2015-05-14 08:07:03 +02:00
|
|
|
c := echo.NewContext(req, res, echo.New())
|
2015-05-15 01:25:49 +02:00
|
|
|
RedirectToSlash(RedirectToSlashOptions{Code: http.StatusTemporaryRedirect})(c)
|
2015-05-16 21:49:01 +02:00
|
|
|
|
|
|
|
// Status code
|
2015-05-15 01:25:49 +02:00
|
|
|
if res.Status() != http.StatusTemporaryRedirect {
|
2015-05-16 21:49:01 +02:00
|
|
|
t.Errorf("expected status `307`, got %d.", res.Status())
|
2015-05-14 08:07:03 +02:00
|
|
|
}
|
2015-05-16 21:49:01 +02:00
|
|
|
|
|
|
|
// Location header
|
|
|
|
l := c.Response.Header().Get("Location")
|
|
|
|
if l != "/users/" {
|
|
|
|
t.Errorf("expected Location header `/users/`, got %s.", l)
|
2015-05-14 08:07:03 +02:00
|
|
|
}
|
|
|
|
}
|