Skip to content

Commit 666938e

Browse files
authored
tests: error handling on closing body (#2254)
* tidy up tests
1 parent 50e7e56 commit 666938e

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

echo_test.go

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"crypto/tls"
77
"errors"
88
"fmt"
9+
"io"
910
"io/ioutil"
1011
"net"
1112
"net/http"
@@ -235,7 +236,12 @@ func TestEchoStaticRedirectIndex(t *testing.T) {
235236

236237
addr := e.ListenerAddr().String()
237238
if resp, err := http.Get("http://" + addr + "/static"); err == nil { // http.Get follows redirects by default
238-
defer resp.Body.Close()
239+
defer func(Body io.ReadCloser) {
240+
err := Body.Close()
241+
if err != nil {
242+
assert.Fail(t, err.Error())
243+
}
244+
}(resp.Body)
239245
assert.Equal(t, http.StatusOK, resp.StatusCode)
240246

241247
if body, err := ioutil.ReadAll(resp.Body); err == nil {
@@ -380,7 +386,10 @@ func TestEchoWrapHandler(t *testing.T) {
380386
c := e.NewContext(req, rec)
381387
h := WrapHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
382388
w.WriteHeader(http.StatusOK)
383-
w.Write([]byte("test"))
389+
_, err := w.Write([]byte("test"))
390+
if err != nil {
391+
assert.Fail(t, err.Error())
392+
}
384393
}))
385394
if assert.NoError(t, h(c)) {
386395
assert.Equal(t, http.StatusOK, rec.Code)
@@ -482,16 +491,16 @@ func TestEchoURL(t *testing.T) {
482491
g := e.Group("/group")
483492
g.GET("/users/:uid/files/:fid", getFile)
484493

485-
assert := assert.New(t)
494+
assertion := assert.New(t)
486495

487-
assert.Equal("/static/file", e.URL(static))
488-
assert.Equal("/users/:id", e.URL(getUser))
489-
assert.Equal("/users/1", e.URL(getUser, "1"))
490-
assert.Equal("/users/1", e.URL(getUser, "1"))
491-
assert.Equal("/documents/foo.txt", e.URL(getAny, "foo.txt"))
492-
assert.Equal("/documents/*", e.URL(getAny))
493-
assert.Equal("/group/users/1/files/:fid", e.URL(getFile, "1"))
494-
assert.Equal("/group/users/1/files/1", e.URL(getFile, "1", "1"))
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"))
495504
}
496505

497506
func TestEchoRoutes(t *testing.T) {
@@ -598,7 +607,7 @@ func TestEchoServeHTTPPathEncoding(t *testing.T) {
598607
}
599608

600609
func TestEchoHost(t *testing.T) {
601-
assert := assert.New(t)
610+
assertion := assert.New(t)
602611

603612
okHandler := func(c Context) error { return c.String(http.StatusOK, http.StatusText(http.StatusOK)) }
604613
teapotHandler := func(c Context) error { return c.String(http.StatusTeapot, http.StatusText(http.StatusTeapot)) }
@@ -694,8 +703,8 @@ func TestEchoHost(t *testing.T) {
694703

695704
e.ServeHTTP(rec, req)
696705

697-
assert.Equal(tc.expectStatus, rec.Code)
698-
assert.Equal(tc.expectBody, rec.Body.String())
706+
assertion.Equal(tc.expectStatus, rec.Code)
707+
assertion.Equal(tc.expectBody, rec.Body.String())
699708
})
700709
}
701710
}
@@ -1231,7 +1240,7 @@ func TestDefaultHTTPErrorHandler(t *testing.T) {
12311240
e := New()
12321241
e.Debug = true
12331242
e.Any("/plain", func(c Context) error {
1234-
return errors.New("An error occurred")
1243+
return errors.New("an error occurred")
12351244
})
12361245
e.Any("/badrequest", func(c Context) error {
12371246
return NewHTTPError(http.StatusBadRequest, "Invalid request")
@@ -1244,7 +1253,10 @@ func TestDefaultHTTPErrorHandler(t *testing.T) {
12441253
})
12451254
})
12461255
e.Any("/early-return", func(c Context) error {
1247-
c.String(http.StatusOK, "OK")
1256+
err := c.String(http.StatusOK, "OK")
1257+
if err != nil {
1258+
assert.Fail(t, err.Error())
1259+
}
12481260
return errors.New("ERROR")
12491261
})
12501262
e.GET("/internal-error", func(c Context) error {
@@ -1255,7 +1267,7 @@ func TestDefaultHTTPErrorHandler(t *testing.T) {
12551267
// With Debug=true plain response contains error message
12561268
c, b := request(http.MethodGet, "/plain", e)
12571269
assert.Equal(t, http.StatusInternalServerError, c)
1258-
assert.Equal(t, "{\n \"error\": \"An error occurred\",\n \"message\": \"Internal Server Error\"\n}\n", b)
1270+
assert.Equal(t, "{\n \"error\": \"an error occurred\",\n \"message\": \"Internal Server Error\"\n}\n", b)
12591271
// and special handling for HTTPError
12601272
c, b = request(http.MethodGet, "/badrequest", e)
12611273
assert.Equal(t, http.StatusBadRequest, c)
@@ -1379,7 +1391,12 @@ func TestEchoListenerNetwork(t *testing.T) {
13791391
assert.NoError(t, err)
13801392

13811393
if resp, err := http.Get(fmt.Sprintf("http://%s/ok", tt.address)); err == nil {
1382-
defer resp.Body.Close()
1394+
defer func(Body io.ReadCloser) {
1395+
err := Body.Close()
1396+
if err != nil {
1397+
assert.Fail(t, err.Error())
1398+
}
1399+
}(resp.Body)
13831400
assert.Equal(t, http.StatusOK, resp.StatusCode)
13841401

13851402
if body, err := ioutil.ReadAll(resp.Body); err == nil {

0 commit comments

Comments
 (0)