From cd2266da045ec4d4ce4f48db0dbc470787d8c809 Mon Sep 17 00:00:00 2001 From: Kamandlou Date: Fri, 19 Aug 2022 21:36:40 +0430 Subject: [PATCH 1/6] replace GET constance with stdlib constance --- bind_test.go | 6 +++--- context_test.go | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/bind_test.go b/bind_test.go index 4ed8dbb50..a7801da92 100644 --- a/bind_test.go +++ b/bind_test.go @@ -330,7 +330,7 @@ func TestBindUnmarshalParam(t *testing.T) { func TestBindUnmarshalText(t *testing.T) { e := New() - req := httptest.NewRequest(GET, "/?ts=2016-12-06T19:09:05Z&sa=one,two,three&ta=2016-12-06T19:09:05Z&ta=2016-12-06T19:09:05Z&ST=baz", nil) + req := httptest.NewRequest(http.MethodGet, "/?ts=2016-12-06T19:09:05Z&sa=one,two,three&ta=2016-12-06T19:09:05Z&ta=2016-12-06T19:09:05Z&ST=baz", nil) rec := httptest.NewRecorder() c := e.NewContext(req, rec) result := struct { @@ -406,7 +406,7 @@ func TestBindUnmarshalParamAnonymousFieldPtrCustomTag(t *testing.T) { func TestBindUnmarshalTextPtr(t *testing.T) { e := New() - req := httptest.NewRequest(GET, "/?ts=2016-12-06T19:09:05Z", nil) + req := httptest.NewRequest(http.MethodGet, "/?ts=2016-12-06T19:09:05Z", nil) rec := httptest.NewRecorder() c := e.NewContext(req, rec) result := struct { @@ -462,7 +462,7 @@ func TestBindbindData(t *testing.T) { func TestBindParam(t *testing.T) { e := New() - req := httptest.NewRequest(GET, "/", nil) + req := httptest.NewRequest(http.MethodGet, "/", nil) rec := httptest.NewRecorder() c := e.NewContext(req, rec) c.SetPath("/users/:id/:name") diff --git a/context_test.go b/context_test.go index a8b9a9946..377a740e4 100644 --- a/context_test.go +++ b/context_test.go @@ -728,7 +728,7 @@ func TestContext_QueryString(t *testing.T) { queryString := "query=string&var=val" - req := httptest.NewRequest(GET, "/?"+queryString, nil) + req := httptest.NewRequest(http.MethodGet, "/?"+queryString, nil) c := e.NewContext(req, nil) testify.Equal(t, queryString, c.QueryString()) @@ -739,7 +739,7 @@ func TestContext_Request(t *testing.T) { testify.Nil(t, c.Request()) - req := httptest.NewRequest(GET, "/path", nil) + req := httptest.NewRequest(http.MethodGet, "/path", nil) c.SetRequest(req) testify.Equal(t, req, c.Request()) From 4835633cf7cb66c7e07eafddcbb552261c951bb4 Mon Sep 17 00:00:00 2001 From: Kamandlou Date: Fri, 19 Aug 2022 21:38:38 +0430 Subject: [PATCH 2/6] replace POST constance with stdlib constance --- bind_test.go | 2 +- context_test.go | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/bind_test.go b/bind_test.go index a7801da92..822008153 100644 --- a/bind_test.go +++ b/bind_test.go @@ -492,7 +492,7 @@ func TestBindParam(t *testing.T) { // Bind something with param and post data payload body := bytes.NewBufferString(`{ "name": "Jon Snow" }`) e2 := New() - req2 := httptest.NewRequest(POST, "/", body) + req2 := httptest.NewRequest(http.MethodPost, "/", body) req2.Header.Set(HeaderContentType, MIMEApplicationJSON) rec2 := httptest.NewRecorder() diff --git a/context_test.go b/context_test.go index 377a740e4..b25e11c60 100644 --- a/context_test.go +++ b/context_test.go @@ -32,7 +32,7 @@ var testUser = user{1, "Jon Snow"} func BenchmarkAllocJSONP(b *testing.B) { e := New() - req := httptest.NewRequest(POST, "/", strings.NewReader(userJSON)) + req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON)) rec := httptest.NewRecorder() c := e.NewContext(req, rec).(*context) @@ -46,7 +46,7 @@ func BenchmarkAllocJSONP(b *testing.B) { func BenchmarkAllocJSON(b *testing.B) { e := New() - req := httptest.NewRequest(POST, "/", strings.NewReader(userJSON)) + req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON)) rec := httptest.NewRecorder() c := e.NewContext(req, rec).(*context) @@ -60,7 +60,7 @@ func BenchmarkAllocJSON(b *testing.B) { func BenchmarkAllocXML(b *testing.B) { e := New() - req := httptest.NewRequest(POST, "/", strings.NewReader(userJSON)) + req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON)) rec := httptest.NewRecorder() c := e.NewContext(req, rec).(*context) @@ -849,7 +849,7 @@ func TestContext_IsWebSocket(t *testing.T) { func TestContext_Bind(t *testing.T) { e := New() - req := httptest.NewRequest(POST, "/", strings.NewReader(userJSON)) + req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userJSON)) c := e.NewContext(req, nil) u := new(user) From 78f90e88fb1eb60b39fbdfa92f7307fd46170b5d Mon Sep 17 00:00:00 2001 From: Kamandlou Date: Thu, 25 Aug 2022 23:56:50 +0430 Subject: [PATCH 3/6] error handling in closing body --- echo_test.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/echo_test.go b/echo_test.go index 64796b3b5..e58f630a0 100644 --- a/echo_test.go +++ b/echo_test.go @@ -6,6 +6,7 @@ import ( "crypto/tls" "errors" "fmt" + "io" "io/ioutil" "net" "net/http" @@ -235,7 +236,12 @@ func TestEchoStaticRedirectIndex(t *testing.T) { addr := e.ListenerAddr().String() if resp, err := http.Get("http://" + addr + "/static"); err == nil { // http.Get follows redirects by default - defer resp.Body.Close() + defer func(Body io.ReadCloser) { + err := Body.Close() + if err != nil { + assert.Fail(t, err.Error()) + } + }(resp.Body) assert.Equal(t, http.StatusOK, resp.StatusCode) if body, err := ioutil.ReadAll(resp.Body); err == nil { @@ -1231,7 +1237,7 @@ func TestDefaultHTTPErrorHandler(t *testing.T) { e := New() e.Debug = true e.Any("/plain", func(c Context) error { - return errors.New("An error occurred") + return errors.New("an error occurred") }) e.Any("/badrequest", func(c Context) error { return NewHTTPError(http.StatusBadRequest, "Invalid request") @@ -1255,7 +1261,7 @@ func TestDefaultHTTPErrorHandler(t *testing.T) { // With Debug=true plain response contains error message c, b := request(http.MethodGet, "/plain", e) assert.Equal(t, http.StatusInternalServerError, c) - assert.Equal(t, "{\n \"error\": \"An error occurred\",\n \"message\": \"Internal Server Error\"\n}\n", b) + assert.Equal(t, "{\n \"error\": \"an error occurred\",\n \"message\": \"Internal Server Error\"\n}\n", b) // and special handling for HTTPError c, b = request(http.MethodGet, "/badrequest", e) assert.Equal(t, http.StatusBadRequest, c) From 68d0189fe651a9bdb15eb936e74ac88ce8673dd2 Mon Sep 17 00:00:00 2001 From: Kamandlou Date: Thu, 25 Aug 2022 23:58:21 +0430 Subject: [PATCH 4/6] error handling in closing body --- echo_test.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/echo_test.go b/echo_test.go index e58f630a0..7612af797 100644 --- a/echo_test.go +++ b/echo_test.go @@ -1385,7 +1385,12 @@ func TestEchoListenerNetwork(t *testing.T) { assert.NoError(t, err) if resp, err := http.Get(fmt.Sprintf("http://%s/ok", tt.address)); err == nil { - defer resp.Body.Close() + defer func(Body io.ReadCloser) { + err := Body.Close() + if err != nil { + assert.Fail(t, err.Error()) + } + }(resp.Body) assert.Equal(t, http.StatusOK, resp.StatusCode) if body, err := ioutil.ReadAll(resp.Body); err == nil { From 676df0fabc0b63e774f898d3c27bd463d58882b2 Mon Sep 17 00:00:00 2001 From: Kamandlou Date: Fri, 26 Aug 2022 00:14:33 +0430 Subject: [PATCH 5/6] rename variable because collide with the imported package name --- echo_test.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/echo_test.go b/echo_test.go index 7612af797..116531fb9 100644 --- a/echo_test.go +++ b/echo_test.go @@ -488,16 +488,16 @@ func TestEchoURL(t *testing.T) { g := e.Group("/group") g.GET("/users/:uid/files/:fid", getFile) - assert := assert.New(t) - - assert.Equal("/static/file", e.URL(static)) - assert.Equal("/users/:id", e.URL(getUser)) - assert.Equal("/users/1", e.URL(getUser, "1")) - assert.Equal("/users/1", e.URL(getUser, "1")) - assert.Equal("/documents/foo.txt", e.URL(getAny, "foo.txt")) - assert.Equal("/documents/*", e.URL(getAny)) - assert.Equal("/group/users/1/files/:fid", e.URL(getFile, "1")) - assert.Equal("/group/users/1/files/1", e.URL(getFile, "1", "1")) + assertion := assert.New(t) + + assertion.Equal("/static/file", e.URL(static)) + assertion.Equal("/users/:id", e.URL(getUser)) + assertion.Equal("/users/1", e.URL(getUser, "1")) + assertion.Equal("/users/1", e.URL(getUser, "1")) + assertion.Equal("/documents/foo.txt", e.URL(getAny, "foo.txt")) + assertion.Equal("/documents/*", e.URL(getAny)) + assertion.Equal("/group/users/1/files/:fid", e.URL(getFile, "1")) + assertion.Equal("/group/users/1/files/1", e.URL(getFile, "1", "1")) } func TestEchoRoutes(t *testing.T) { @@ -604,7 +604,7 @@ func TestEchoServeHTTPPathEncoding(t *testing.T) { } func TestEchoHost(t *testing.T) { - assert := assert.New(t) + assertion := assert.New(t) okHandler := func(c Context) error { return c.String(http.StatusOK, http.StatusText(http.StatusOK)) } teapotHandler := func(c Context) error { return c.String(http.StatusTeapot, http.StatusText(http.StatusTeapot)) } @@ -700,8 +700,8 @@ func TestEchoHost(t *testing.T) { e.ServeHTTP(rec, req) - assert.Equal(tc.expectStatus, rec.Code) - assert.Equal(tc.expectBody, rec.Body.String()) + assertion.Equal(tc.expectStatus, rec.Code) + assertion.Equal(tc.expectBody, rec.Body.String()) }) } } From eb618061287742bb72a74cc32e3feefa1a970fb3 Mon Sep 17 00:00:00 2001 From: Kamandlou Date: Fri, 26 Aug 2022 00:19:47 +0430 Subject: [PATCH 6/6] fix unhandled errors --- echo_test.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/echo_test.go b/echo_test.go index 116531fb9..7fd77c836 100644 --- a/echo_test.go +++ b/echo_test.go @@ -386,7 +386,10 @@ func TestEchoWrapHandler(t *testing.T) { c := e.NewContext(req, rec) h := WrapHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) - w.Write([]byte("test")) + _, err := w.Write([]byte("test")) + if err != nil { + assert.Fail(t, err.Error()) + } })) if assert.NoError(t, h(c)) { assert.Equal(t, http.StatusOK, rec.Code) @@ -1250,7 +1253,10 @@ func TestDefaultHTTPErrorHandler(t *testing.T) { }) }) e.Any("/early-return", func(c Context) error { - c.String(http.StatusOK, "OK") + err := c.String(http.StatusOK, "OK") + if err != nil { + assert.Fail(t, err.Error()) + } return errors.New("ERROR") }) e.GET("/internal-error", func(c Context) error {