Skip to content

Commit 1d5f335

Browse files
authored
refactor assertions (#2301)
1 parent 4c44305 commit 1d5f335

File tree

7 files changed

+71
-91
lines changed

7 files changed

+71
-91
lines changed

echo_test.go

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -491,16 +491,14 @@ func TestEchoURL(t *testing.T) {
491491
g := e.Group("/group")
492492
g.GET("/users/:uid/files/:fid", getFile)
493493

494-
assertion := assert.New(t)
495-
496-
assertion.Equal("/static/file", e.URL(static))
497-
assertion.Equal("/users/:id", e.URL(getUser))
498-
assertion.Equal("/users/1", e.URL(getUser, "1"))
499-
assertion.Equal("/users/1", e.URL(getUser, "1"))
500-
assertion.Equal("/documents/foo.txt", e.URL(getAny, "foo.txt"))
501-
assertion.Equal("/documents/*", e.URL(getAny))
502-
assertion.Equal("/group/users/1/files/:fid", e.URL(getFile, "1"))
503-
assertion.Equal("/group/users/1/files/1", e.URL(getFile, "1", "1"))
494+
assert.Equal(t, "/static/file", e.URL(static))
495+
assert.Equal(t, "/users/:id", e.URL(getUser))
496+
assert.Equal(t, "/users/1", e.URL(getUser, "1"))
497+
assert.Equal(t, "/users/1", e.URL(getUser, "1"))
498+
assert.Equal(t, "/documents/foo.txt", e.URL(getAny, "foo.txt"))
499+
assert.Equal(t, "/documents/*", e.URL(getAny))
500+
assert.Equal(t, "/group/users/1/files/:fid", e.URL(getFile, "1"))
501+
assert.Equal(t, "/group/users/1/files/1", e.URL(getFile, "1", "1"))
504502
}
505503

506504
func TestEchoRoutes(t *testing.T) {
@@ -607,8 +605,6 @@ func TestEchoServeHTTPPathEncoding(t *testing.T) {
607605
}
608606

609607
func TestEchoHost(t *testing.T) {
610-
assertion := assert.New(t)
611-
612608
okHandler := func(c Context) error { return c.String(http.StatusOK, http.StatusText(http.StatusOK)) }
613609
teapotHandler := func(c Context) error { return c.String(http.StatusTeapot, http.StatusText(http.StatusTeapot)) }
614610
acceptHandler := func(c Context) error { return c.String(http.StatusAccepted, http.StatusText(http.StatusAccepted)) }
@@ -703,8 +699,8 @@ func TestEchoHost(t *testing.T) {
703699

704700
e.ServeHTTP(rec, req)
705701

706-
assertion.Equal(tc.expectStatus, rec.Code)
707-
assertion.Equal(tc.expectBody, rec.Body.String())
702+
assert.Equal(t, tc.expectStatus, rec.Code)
703+
assert.Equal(t, tc.expectBody, rec.Body.String())
708704
})
709705
}
710706
}
@@ -1429,8 +1425,6 @@ func TestEchoListenerNetworkInvalid(t *testing.T) {
14291425
}
14301426

14311427
func TestEchoReverse(t *testing.T) {
1432-
assert := assert.New(t)
1433-
14341428
e := New()
14351429
dummyHandler := func(Context) error { return nil }
14361430

@@ -1440,33 +1434,31 @@ func TestEchoReverse(t *testing.T) {
14401434
e.GET("/params/:foo/bar/:qux", dummyHandler).Name = "/params/:foo/bar/:qux"
14411435
e.GET("/params/:foo/bar/:qux/*", dummyHandler).Name = "/params/:foo/bar/:qux/*"
14421436

1443-
assert.Equal("/static", e.Reverse("/static"))
1444-
assert.Equal("/static", e.Reverse("/static", "missing param"))
1445-
assert.Equal("/static/*", e.Reverse("/static/*"))
1446-
assert.Equal("/static/foo.txt", e.Reverse("/static/*", "foo.txt"))
1447-
1448-
assert.Equal("/params/:foo", e.Reverse("/params/:foo"))
1449-
assert.Equal("/params/one", e.Reverse("/params/:foo", "one"))
1450-
assert.Equal("/params/:foo/bar/:qux", e.Reverse("/params/:foo/bar/:qux"))
1451-
assert.Equal("/params/one/bar/:qux", e.Reverse("/params/:foo/bar/:qux", "one"))
1452-
assert.Equal("/params/one/bar/two", e.Reverse("/params/:foo/bar/:qux", "one", "two"))
1453-
assert.Equal("/params/one/bar/two/three", e.Reverse("/params/:foo/bar/:qux/*", "one", "two", "three"))
1437+
assert.Equal(t, "/static", e.Reverse("/static"))
1438+
assert.Equal(t, "/static", e.Reverse("/static", "missing param"))
1439+
assert.Equal(t, "/static/*", e.Reverse("/static/*"))
1440+
assert.Equal(t, "/static/foo.txt", e.Reverse("/static/*", "foo.txt"))
1441+
1442+
assert.Equal(t, "/params/:foo", e.Reverse("/params/:foo"))
1443+
assert.Equal(t, "/params/one", e.Reverse("/params/:foo", "one"))
1444+
assert.Equal(t, "/params/:foo/bar/:qux", e.Reverse("/params/:foo/bar/:qux"))
1445+
assert.Equal(t, "/params/one/bar/:qux", e.Reverse("/params/:foo/bar/:qux", "one"))
1446+
assert.Equal(t, "/params/one/bar/two", e.Reverse("/params/:foo/bar/:qux", "one", "two"))
1447+
assert.Equal(t, "/params/one/bar/two/three", e.Reverse("/params/:foo/bar/:qux/*", "one", "two", "three"))
14541448
}
14551449

14561450
func TestEchoReverseHandleHostProperly(t *testing.T) {
1457-
assert := assert.New(t)
1458-
14591451
dummyHandler := func(Context) error { return nil }
14601452

14611453
e := New()
14621454
h := e.Host("the_host")
14631455
h.GET("/static", dummyHandler).Name = "/static"
14641456
h.GET("/static/*", dummyHandler).Name = "/static/*"
14651457

1466-
assert.Equal("/static", e.Reverse("/static"))
1467-
assert.Equal("/static", e.Reverse("/static", "missing param"))
1468-
assert.Equal("/static/*", e.Reverse("/static/*"))
1469-
assert.Equal("/static/foo.txt", e.Reverse("/static/*", "foo.txt"))
1458+
assert.Equal(t, "/static", e.Reverse("/static"))
1459+
assert.Equal(t, "/static", e.Reverse("/static", "missing param"))
1460+
assert.Equal(t, "/static/*", e.Reverse("/static/*"))
1461+
assert.Equal(t, "/static/foo.txt", e.Reverse("/static/*", "foo.txt"))
14701462
}
14711463

14721464
func TestEcho_ListenerAddr(t *testing.T) {

middleware/basic_auth_test.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,10 @@ func TestBasicAuth(t *testing.T) {
2626
return c.String(http.StatusOK, "test")
2727
})
2828

29-
assert := assert.New(t)
30-
3129
// Valid credentials
3230
auth := basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:secret"))
3331
req.Header.Set(echo.HeaderAuthorization, auth)
34-
assert.NoError(h(c))
32+
assert.NoError(t, h(c))
3533

3634
h = BasicAuthWithConfig(BasicAuthConfig{
3735
Skipper: nil,
@@ -44,34 +42,34 @@ func TestBasicAuth(t *testing.T) {
4442
// Valid credentials
4543
auth = basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:secret"))
4644
req.Header.Set(echo.HeaderAuthorization, auth)
47-
assert.NoError(h(c))
45+
assert.NoError(t, h(c))
4846

4947
// Case-insensitive header scheme
5048
auth = strings.ToUpper(basic) + " " + base64.StdEncoding.EncodeToString([]byte("joe:secret"))
5149
req.Header.Set(echo.HeaderAuthorization, auth)
52-
assert.NoError(h(c))
50+
assert.NoError(t, h(c))
5351

5452
// Invalid credentials
5553
auth = basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:invalid-password"))
5654
req.Header.Set(echo.HeaderAuthorization, auth)
5755
he := h(c).(*echo.HTTPError)
58-
assert.Equal(http.StatusUnauthorized, he.Code)
59-
assert.Equal(basic+` realm="someRealm"`, res.Header().Get(echo.HeaderWWWAuthenticate))
56+
assert.Equal(t, http.StatusUnauthorized, he.Code)
57+
assert.Equal(t, basic+` realm="someRealm"`, res.Header().Get(echo.HeaderWWWAuthenticate))
6058

6159
// Invalid base64 string
6260
auth = basic + " invalidString"
6361
req.Header.Set(echo.HeaderAuthorization, auth)
6462
he = h(c).(*echo.HTTPError)
65-
assert.Equal(http.StatusBadRequest, he.Code)
63+
assert.Equal(t, http.StatusBadRequest, he.Code)
6664

6765
// Missing Authorization header
6866
req.Header.Del(echo.HeaderAuthorization)
6967
he = h(c).(*echo.HTTPError)
70-
assert.Equal(http.StatusUnauthorized, he.Code)
68+
assert.Equal(t, http.StatusUnauthorized, he.Code)
7169

7270
// Invalid Authorization header
7371
auth = base64.StdEncoding.EncodeToString([]byte("invalid"))
7472
req.Header.Set(echo.HeaderAuthorization, auth)
7573
he = h(c).(*echo.HTTPError)
76-
assert.Equal(http.StatusUnauthorized, he.Code)
74+
assert.Equal(t, http.StatusUnauthorized, he.Code)
7775
}

middleware/body_dump_test.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,11 @@ func TestBodyDump(t *testing.T) {
3333
responseBody = string(resBody)
3434
})
3535

36-
assert := assert.New(t)
37-
38-
if assert.NoError(mw(h)(c)) {
39-
assert.Equal(requestBody, hw)
40-
assert.Equal(responseBody, hw)
41-
assert.Equal(http.StatusOK, rec.Code)
42-
assert.Equal(hw, rec.Body.String())
36+
if assert.NoError(t, mw(h)(c)) {
37+
assert.Equal(t, requestBody, hw)
38+
assert.Equal(t, responseBody, hw)
39+
assert.Equal(t, http.StatusOK, rec.Code)
40+
assert.Equal(t, hw, rec.Body.String())
4341
}
4442

4543
// Must set default skipper

middleware/body_limit_test.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,24 @@ func TestBodyLimit(t *testing.T) {
2525
return c.String(http.StatusOK, string(body))
2626
}
2727

28-
assert := assert.New(t)
29-
3028
// Based on content length (within limit)
31-
if assert.NoError(BodyLimit("2M")(h)(c)) {
32-
assert.Equal(http.StatusOK, rec.Code)
33-
assert.Equal(hw, rec.Body.Bytes())
29+
if assert.NoError(t, BodyLimit("2M")(h)(c)) {
30+
assert.Equal(t, http.StatusOK, rec.Code)
31+
assert.Equal(t, hw, rec.Body.Bytes())
3432
}
3533

3634
// Based on content length (overlimit)
3735
he := BodyLimit("2B")(h)(c).(*echo.HTTPError)
38-
assert.Equal(http.StatusRequestEntityTooLarge, he.Code)
36+
assert.Equal(t, http.StatusRequestEntityTooLarge, he.Code)
3937

4038
// Based on content read (within limit)
4139
req = httptest.NewRequest(http.MethodPost, "/", bytes.NewReader(hw))
4240
req.ContentLength = -1
4341
rec = httptest.NewRecorder()
4442
c = e.NewContext(req, rec)
45-
if assert.NoError(BodyLimit("2M")(h)(c)) {
46-
assert.Equal(http.StatusOK, rec.Code)
47-
assert.Equal("Hello, World!", rec.Body.String())
43+
if assert.NoError(t, BodyLimit("2M")(h)(c)) {
44+
assert.Equal(t, http.StatusOK, rec.Code)
45+
assert.Equal(t, "Hello, World!", rec.Body.String())
4846
}
4947

5048
// Based on content read (overlimit)
@@ -53,7 +51,7 @@ func TestBodyLimit(t *testing.T) {
5351
rec = httptest.NewRecorder()
5452
c = e.NewContext(req, rec)
5553
he = BodyLimit("2B")(h)(c).(*echo.HTTPError)
56-
assert.Equal(http.StatusRequestEntityTooLarge, he.Code)
54+
assert.Equal(t, http.StatusRequestEntityTooLarge, he.Code)
5755
}
5856

5957
func TestBodyLimitReader(t *testing.T) {

middleware/compress_test.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,22 @@ func TestGzip(t *testing.T) {
2626
})
2727
h(c)
2828

29-
assert := assert.New(t)
30-
31-
assert.Equal("test", rec.Body.String())
29+
assert.Equal(t, "test", rec.Body.String())
3230

3331
// Gzip
3432
req = httptest.NewRequest(http.MethodGet, "/", nil)
3533
req.Header.Set(echo.HeaderAcceptEncoding, gzipScheme)
3634
rec = httptest.NewRecorder()
3735
c = e.NewContext(req, rec)
3836
h(c)
39-
assert.Equal(gzipScheme, rec.Header().Get(echo.HeaderContentEncoding))
40-
assert.Contains(rec.Header().Get(echo.HeaderContentType), echo.MIMETextPlain)
37+
assert.Equal(t, gzipScheme, rec.Header().Get(echo.HeaderContentEncoding))
38+
assert.Contains(t, rec.Header().Get(echo.HeaderContentType), echo.MIMETextPlain)
4139
r, err := gzip.NewReader(rec.Body)
42-
if assert.NoError(err) {
40+
if assert.NoError(t, err) {
4341
buf := new(bytes.Buffer)
4442
defer r.Close()
4543
buf.ReadFrom(r)
46-
assert.Equal("test", buf.String())
44+
assert.Equal(t, "test", buf.String())
4745
}
4846

4947
chunkBuf := make([]byte, 5)
@@ -63,21 +61,21 @@ func TestGzip(t *testing.T) {
6361
c.Response().Flush()
6462

6563
// Read the first part of the data
66-
assert.True(rec.Flushed)
67-
assert.Equal(gzipScheme, rec.Header().Get(echo.HeaderContentEncoding))
64+
assert.True(t, rec.Flushed)
65+
assert.Equal(t, gzipScheme, rec.Header().Get(echo.HeaderContentEncoding))
6866
r.Reset(rec.Body)
6967

7068
_, err = io.ReadFull(r, chunkBuf)
71-
assert.NoError(err)
72-
assert.Equal("test\n", string(chunkBuf))
69+
assert.NoError(t, err)
70+
assert.Equal(t, "test\n", string(chunkBuf))
7371

7472
// Write and flush the second part of the data
7573
c.Response().Write([]byte("test\n"))
7674
c.Response().Flush()
7775

7876
_, err = io.ReadFull(r, chunkBuf)
79-
assert.NoError(err)
80-
assert.Equal("test\n", string(chunkBuf))
77+
assert.NoError(t, err)
78+
assert.Equal(t, "test\n", string(chunkBuf))
8179

8280
// Write the final part of the data and return
8381
c.Response().Write([]byte("test"))
@@ -87,7 +85,7 @@ func TestGzip(t *testing.T) {
8785
buf := new(bytes.Buffer)
8886
defer r.Close()
8987
buf.ReadFrom(r)
90-
assert.Equal("test", buf.String())
88+
assert.Equal(t, "test", buf.String())
9189
}
9290

9391
func TestGzipNoContent(t *testing.T) {

middleware/decompress_test.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ func TestDecompress(t *testing.T) {
2828
})
2929
h(c)
3030

31-
assert := assert.New(t)
32-
assert.Equal("test", rec.Body.String())
31+
assert.Equal(t, "test", rec.Body.String())
3332

3433
// Decompress
3534
body := `{"name": "echo"}`
@@ -39,10 +38,10 @@ func TestDecompress(t *testing.T) {
3938
rec = httptest.NewRecorder()
4039
c = e.NewContext(req, rec)
4140
h(c)
42-
assert.Equal(GZIPEncoding, req.Header.Get(echo.HeaderContentEncoding))
41+
assert.Equal(t, GZIPEncoding, req.Header.Get(echo.HeaderContentEncoding))
4342
b, err := ioutil.ReadAll(req.Body)
44-
assert.NoError(err)
45-
assert.Equal(body, string(b))
43+
assert.NoError(t, err)
44+
assert.Equal(t, body, string(b))
4645
}
4746

4847
func TestDecompressDefaultConfig(t *testing.T) {
@@ -57,8 +56,7 @@ func TestDecompressDefaultConfig(t *testing.T) {
5756
})
5857
h(c)
5958

60-
assert := assert.New(t)
61-
assert.Equal("test", rec.Body.String())
59+
assert.Equal(t, "test", rec.Body.String())
6260

6361
// Decompress
6462
body := `{"name": "echo"}`
@@ -68,10 +66,10 @@ func TestDecompressDefaultConfig(t *testing.T) {
6866
rec = httptest.NewRecorder()
6967
c = e.NewContext(req, rec)
7068
h(c)
71-
assert.Equal(GZIPEncoding, req.Header.Get(echo.HeaderContentEncoding))
69+
assert.Equal(t, GZIPEncoding, req.Header.Get(echo.HeaderContentEncoding))
7270
b, err := ioutil.ReadAll(req.Body)
73-
assert.NoError(err)
74-
assert.Equal(body, string(b))
71+
assert.NoError(t, err)
72+
assert.Equal(t, body, string(b))
7573
}
7674

7775
func TestCompressRequestWithoutDecompressMiddleware(t *testing.T) {

middleware/jwt_test.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,6 @@ func TestJWTConfig(t *testing.T) {
348348
}
349349

350350
func TestJWTwithKID(t *testing.T) {
351-
test := assert.New(t)
352-
353351
e := echo.New()
354352
handler := func(c echo.Context) error {
355353
return c.String(http.StatusOK, "test")
@@ -417,19 +415,19 @@ func TestJWTwithKID(t *testing.T) {
417415
if tc.expErrCode != 0 {
418416
h := JWTWithConfig(tc.config)(handler)
419417
he := h(c).(*echo.HTTPError)
420-
test.Equal(tc.expErrCode, he.Code, tc.info)
418+
assert.Equal(t, tc.expErrCode, he.Code, tc.info)
421419
continue
422420
}
423421

424422
h := JWTWithConfig(tc.config)(handler)
425-
if test.NoError(h(c), tc.info) {
423+
if assert.NoError(t, h(c), tc.info) {
426424
user := c.Get("user").(*jwt.Token)
427425
switch claims := user.Claims.(type) {
428426
case jwt.MapClaims:
429-
test.Equal(claims["name"], "John Doe", tc.info)
427+
assert.Equal(t, claims["name"], "John Doe", tc.info)
430428
case *jwtCustomClaims:
431-
test.Equal(claims.Name, "John Doe", tc.info)
432-
test.Equal(claims.Admin, true, tc.info)
429+
assert.Equal(t, claims.Name, "John Doe", tc.info)
430+
assert.Equal(t, claims.Admin, true, tc.info)
433431
default:
434432
panic("unexpected type of claims")
435433
}

0 commit comments

Comments
 (0)