Skip to content

Commit 609879b

Browse files
committed
Refactored tests
Signed-off-by: Vishal Rana <vr@labstack.com>
1 parent d90395c commit 609879b

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

middleware/auth_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,47 +27,48 @@ func TestBasicAuth(t *testing.T) {
2727
auth := Basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:secret"))
2828
req.Header.Set(echo.Authorization, auth)
2929
if ba(c) != nil {
30-
t.Error("basic auth should pass")
30+
t.Error("expected `pass`")
3131
}
3232

3333
// Case insensitive
3434
auth = "basic " + base64.StdEncoding.EncodeToString([]byte("joe:secret"))
3535
req.Header.Set(echo.Authorization, auth)
3636
if ba(c) != nil {
37-
t.Error("basic auth should ignore case and pass")
37+
t.Error("expected `pass` with case insensitive header")
3838
}
3939

4040
//---------------------
4141
// Invalid credentials
4242
//---------------------
4343

44-
auth = Basic + " " + base64.StdEncoding.EncodeToString([]byte(" joe: secret"))
44+
// Incorrect password
45+
auth = Basic + " " + base64.StdEncoding.EncodeToString([]byte("joe: password"))
4546
req.Header.Set(echo.Authorization, auth)
4647
ba = BasicAuth(fn)
4748
if ba(c) == nil {
48-
t.Error("basic auth should fail")
49+
t.Error("expected `fail` with incorrect password")
4950
}
5051

5152
// Invalid header
5253
auth = base64.StdEncoding.EncodeToString([]byte(" :secret"))
5354
req.Header.Set(echo.Authorization, auth)
5455
ba = BasicAuth(fn)
5556
if ba(c) == nil {
56-
t.Error("basic auth should fail for invalid scheme")
57+
t.Error("expected `fail` with invalid auth header")
5758
}
5859

5960
// Invalid scheme
6061
auth = "Base " + base64.StdEncoding.EncodeToString([]byte(" :secret"))
6162
req.Header.Set(echo.Authorization, auth)
6263
ba = BasicAuth(fn)
6364
if ba(c) == nil {
64-
t.Error("basic auth should fail for invalid scheme")
65+
t.Error("expected `fail` with invalid scheme")
6566
}
6667

6768
// Empty auth header
6869
req.Header.Set(echo.Authorization, "")
6970
ba = BasicAuth(fn)
7071
if ba(c) == nil {
71-
t.Error("basic auth should fail for empty auth header")
72+
t.Error("expected `fail` with empty auth header")
7273
}
7374
}

middleware/compress_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestGzip(t *testing.T) {
2121
})(c)
2222

2323
if w.Header().Get(echo.ContentEncoding) != "gzip" {
24-
t.Errorf("expected Content-Encoding: gzip, got %d", w.Header().Get(echo.ContentEncoding))
24+
t.Errorf("expected Content-Encoding header `gzip`, got %d.", w.Header().Get(echo.ContentEncoding))
2525
}
2626

2727
r, err := gzip.NewReader(w.Body)
@@ -37,6 +37,6 @@ func TestGzip(t *testing.T) {
3737
s := string(b)
3838

3939
if s != "test" {
40-
t.Errorf(`expected "test", got "%s"`, s)
40+
t.Errorf("expected `test`, got %s.", s)
4141
}
4242
}

middleware/slash_test.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ func TestStripTrailingSlash(t *testing.T) {
1313
res := &echo.Response{Writer: httptest.NewRecorder()}
1414
c := echo.NewContext(req, res, echo.New())
1515
StripTrailingSlash()(c)
16-
if c.Request.URL.Path != "/users" {
17-
t.Error("it should strip the trailing slash")
16+
p := c.Request.URL.Path
17+
if p != "/users" {
18+
t.Errorf("expected path `/users` got, %s.", p)
1819
}
1920
}
2021

@@ -23,10 +24,15 @@ func TestRedirectToSlash(t *testing.T) {
2324
res := &echo.Response{Writer: httptest.NewRecorder()}
2425
c := echo.NewContext(req, res, echo.New())
2526
RedirectToSlash(RedirectToSlashOptions{Code: http.StatusTemporaryRedirect})(c)
27+
28+
// Status code
2629
if res.Status() != http.StatusTemporaryRedirect {
27-
t.Errorf("status code should be 307, found %d", res.Status())
30+
t.Errorf("expected status `307`, got %d.", res.Status())
2831
}
29-
if c.Response.Header().Get("Location") != "/users/" {
30-
t.Error("Location header should be /users/")
32+
33+
// Location header
34+
l := c.Response.Header().Get("Location")
35+
if l != "/users/" {
36+
t.Errorf("expected Location header `/users/`, got %s.", l)
3137
}
3238
}

0 commit comments

Comments
 (0)