From 609879bf39d86a83ee90ec9f6bc8acd298059f03 Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Sat, 16 May 2015 12:49:01 -0700 Subject: [PATCH] Refactored tests Signed-off-by: Vishal Rana --- middleware/auth_test.go | 15 ++++++++------- middleware/compress_test.go | 4 ++-- middleware/slash_test.go | 16 +++++++++++----- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/middleware/auth_test.go b/middleware/auth_test.go index 4e9b4e6f..4961cf3c 100644 --- a/middleware/auth_test.go +++ b/middleware/auth_test.go @@ -27,25 +27,26 @@ func TestBasicAuth(t *testing.T) { auth := Basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:secret")) req.Header.Set(echo.Authorization, auth) if ba(c) != nil { - t.Error("basic auth should pass") + t.Error("expected `pass`") } // Case insensitive auth = "basic " + base64.StdEncoding.EncodeToString([]byte("joe:secret")) req.Header.Set(echo.Authorization, auth) if ba(c) != nil { - t.Error("basic auth should ignore case and pass") + t.Error("expected `pass` with case insensitive header") } //--------------------- // 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) ba = BasicAuth(fn) if ba(c) == nil { - t.Error("basic auth should fail") + t.Error("expected `fail` with incorrect password") } // Invalid header @@ -53,7 +54,7 @@ func TestBasicAuth(t *testing.T) { req.Header.Set(echo.Authorization, auth) ba = BasicAuth(fn) if ba(c) == nil { - t.Error("basic auth should fail for invalid scheme") + t.Error("expected `fail` with invalid auth header") } // Invalid scheme @@ -61,13 +62,13 @@ func TestBasicAuth(t *testing.T) { req.Header.Set(echo.Authorization, auth) ba = BasicAuth(fn) if ba(c) == nil { - t.Error("basic auth should fail for invalid scheme") + t.Error("expected `fail` with invalid scheme") } // Empty auth header req.Header.Set(echo.Authorization, "") ba = BasicAuth(fn) if ba(c) == nil { - t.Error("basic auth should fail for empty auth header") + t.Error("expected `fail` with empty auth header") } } diff --git a/middleware/compress_test.go b/middleware/compress_test.go index 53aac820..867f1ea4 100644 --- a/middleware/compress_test.go +++ b/middleware/compress_test.go @@ -21,7 +21,7 @@ func TestGzip(t *testing.T) { })(c) 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) @@ -37,6 +37,6 @@ func TestGzip(t *testing.T) { s := string(b) if s != "test" { - t.Errorf(`expected "test", got "%s"`, s) + t.Errorf("expected `test`, got %s.", s) } } diff --git a/middleware/slash_test.go b/middleware/slash_test.go index 83f24552..8fa98e76 100644 --- a/middleware/slash_test.go +++ b/middleware/slash_test.go @@ -13,8 +13,9 @@ func TestStripTrailingSlash(t *testing.T) { res := &echo.Response{Writer: httptest.NewRecorder()} c := echo.NewContext(req, res, echo.New()) StripTrailingSlash()(c) - if c.Request.URL.Path != "/users" { - t.Error("it should strip the trailing slash") + p := c.Request.URL.Path + 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()} c := echo.NewContext(req, res, echo.New()) RedirectToSlash(RedirectToSlashOptions{Code: http.StatusTemporaryRedirect})(c) + + // Status code 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) } }