1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00

Refactored tests

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-05-16 12:49:01 -07:00
parent d90395cf2b
commit 609879bf39
3 changed files with 21 additions and 14 deletions

View File

@ -27,25 +27,26 @@ func TestBasicAuth(t *testing.T) {
auth := Basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:secret")) auth := Basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:secret"))
req.Header.Set(echo.Authorization, auth) req.Header.Set(echo.Authorization, auth)
if ba(c) != nil { if ba(c) != nil {
t.Error("basic auth should pass") t.Error("expected `pass`")
} }
// Case insensitive // Case insensitive
auth = "basic " + base64.StdEncoding.EncodeToString([]byte("joe:secret")) auth = "basic " + base64.StdEncoding.EncodeToString([]byte("joe:secret"))
req.Header.Set(echo.Authorization, auth) req.Header.Set(echo.Authorization, auth)
if ba(c) != nil { if ba(c) != nil {
t.Error("basic auth should ignore case and pass") t.Error("expected `pass` with case insensitive header")
} }
//--------------------- //---------------------
// Invalid credentials // Invalid credentials
//--------------------- //---------------------
auth = Basic + " " + base64.StdEncoding.EncodeToString([]byte(" joe: secret")) // Incorrect password
auth = Basic + " " + base64.StdEncoding.EncodeToString([]byte("joe: password"))
req.Header.Set(echo.Authorization, auth) req.Header.Set(echo.Authorization, auth)
ba = BasicAuth(fn) ba = BasicAuth(fn)
if ba(c) == nil { if ba(c) == nil {
t.Error("basic auth should fail") t.Error("expected `fail` with incorrect password")
} }
// Invalid header // Invalid header
@ -53,7 +54,7 @@ func TestBasicAuth(t *testing.T) {
req.Header.Set(echo.Authorization, auth) req.Header.Set(echo.Authorization, auth)
ba = BasicAuth(fn) ba = BasicAuth(fn)
if ba(c) == nil { if ba(c) == nil {
t.Error("basic auth should fail for invalid scheme") t.Error("expected `fail` with invalid auth header")
} }
// Invalid scheme // Invalid scheme
@ -61,13 +62,13 @@ func TestBasicAuth(t *testing.T) {
req.Header.Set(echo.Authorization, auth) req.Header.Set(echo.Authorization, auth)
ba = BasicAuth(fn) ba = BasicAuth(fn)
if ba(c) == nil { if ba(c) == nil {
t.Error("basic auth should fail for invalid scheme") t.Error("expected `fail` with invalid scheme")
} }
// Empty auth header // Empty auth header
req.Header.Set(echo.Authorization, "") req.Header.Set(echo.Authorization, "")
ba = BasicAuth(fn) ba = BasicAuth(fn)
if ba(c) == nil { if ba(c) == nil {
t.Error("basic auth should fail for empty auth header") t.Error("expected `fail` with empty auth header")
} }
} }

View File

@ -21,7 +21,7 @@ func TestGzip(t *testing.T) {
})(c) })(c)
if w.Header().Get(echo.ContentEncoding) != "gzip" { if w.Header().Get(echo.ContentEncoding) != "gzip" {
t.Errorf("expected Content-Encoding: gzip, got %d", w.Header().Get(echo.ContentEncoding)) t.Errorf("expected Content-Encoding header `gzip`, got %d.", w.Header().Get(echo.ContentEncoding))
} }
r, err := gzip.NewReader(w.Body) r, err := gzip.NewReader(w.Body)
@ -37,6 +37,6 @@ func TestGzip(t *testing.T) {
s := string(b) s := string(b)
if s != "test" { if s != "test" {
t.Errorf(`expected "test", got "%s"`, s) t.Errorf("expected `test`, got %s.", s)
} }
} }

View File

@ -13,8 +13,9 @@ func TestStripTrailingSlash(t *testing.T) {
res := &echo.Response{Writer: httptest.NewRecorder()} res := &echo.Response{Writer: httptest.NewRecorder()}
c := echo.NewContext(req, res, echo.New()) c := echo.NewContext(req, res, echo.New())
StripTrailingSlash()(c) StripTrailingSlash()(c)
if c.Request.URL.Path != "/users" { p := c.Request.URL.Path
t.Error("it should strip the trailing slash") if p != "/users" {
t.Errorf("expected path `/users` got, %s.", p)
} }
} }
@ -23,10 +24,15 @@ func TestRedirectToSlash(t *testing.T) {
res := &echo.Response{Writer: httptest.NewRecorder()} res := &echo.Response{Writer: httptest.NewRecorder()}
c := echo.NewContext(req, res, echo.New()) c := echo.NewContext(req, res, echo.New())
RedirectToSlash(RedirectToSlashOptions{Code: http.StatusTemporaryRedirect})(c) RedirectToSlash(RedirectToSlashOptions{Code: http.StatusTemporaryRedirect})(c)
// Status code
if res.Status() != http.StatusTemporaryRedirect { if res.Status() != http.StatusTemporaryRedirect {
t.Errorf("status code should be 307, found %d", res.Status()) t.Errorf("expected status `307`, got %d.", res.Status())
} }
if c.Response.Header().Get("Location") != "/users/" {
t.Error("Location header should be /users/") // Location header
l := c.Response.Header().Get("Location")
if l != "/users/" {
t.Errorf("expected Location header `/users/`, got %s.", l)
} }
} }