From cd2266da045ec4d4ce4f48db0dbc470787d8c809 Mon Sep 17 00:00:00 2001 From: Kamandlou Date: Fri, 19 Aug 2022 21:36:40 +0430 Subject: [PATCH 01/12] 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 02/12] 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 03/12] 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 04/12] 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 05/12] 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 06/12] 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 { From 8c61f91c51aa619574edcac9f50ab8b14ab41cc0 Mon Sep 17 00:00:00 2001 From: Kamandlou Date: Fri, 26 Aug 2022 00:37:58 +0430 Subject: [PATCH 07/12] fix unhandled error --- context.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/context.go b/context.go index a4ecfadfc..eaa5c992c 100644 --- a/context.go +++ b/context.go @@ -181,7 +181,7 @@ type ( // Logger returns the `Logger` instance. Logger() Logger - // Set the logger + // SetLogger Set the logger SetLogger(l Logger) // Echo returns the `Echo` instance. @@ -389,7 +389,10 @@ func (c *context) FormFile(name string) (*multipart.FileHeader, error) { if err != nil { return nil, err } - f.Close() + err = f.Close() + if err != nil { + return nil, err + } return fh, nil } From 33474d28d6263dfab2bf94cbf5ff0e6d895ab825 Mon Sep 17 00:00:00 2001 From: Kamandlou Date: Fri, 16 Sep 2022 13:54:19 +0430 Subject: [PATCH 08/12] rename variable because collides with imported package name --- bind_test.go | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/bind_test.go b/bind_test.go index 822008153..86a7adf11 100644 --- a/bind_test.go +++ b/bind_test.go @@ -190,13 +190,13 @@ func TestToMultipleFields(t *testing.T) { } func TestBindJSON(t *testing.T) { - assert := assert.New(t) - testBindOkay(assert, strings.NewReader(userJSON), nil, MIMEApplicationJSON) - testBindOkay(assert, strings.NewReader(userJSON), dummyQuery, MIMEApplicationJSON) - testBindArrayOkay(assert, strings.NewReader(usersJSON), nil, MIMEApplicationJSON) - testBindArrayOkay(assert, strings.NewReader(usersJSON), dummyQuery, MIMEApplicationJSON) - testBindError(assert, strings.NewReader(invalidContent), MIMEApplicationJSON, &json.SyntaxError{}) - testBindError(assert, strings.NewReader(userJSONInvalidType), MIMEApplicationJSON, &json.UnmarshalTypeError{}) + assertion := assert.New(t) + testBindOkay(assertion, strings.NewReader(userJSON), nil, MIMEApplicationJSON) + testBindOkay(assertion, strings.NewReader(userJSON), dummyQuery, MIMEApplicationJSON) + testBindArrayOkay(assertion, strings.NewReader(usersJSON), nil, MIMEApplicationJSON) + testBindArrayOkay(assertion, strings.NewReader(usersJSON), dummyQuery, MIMEApplicationJSON) + testBindError(assertion, strings.NewReader(invalidContent), MIMEApplicationJSON, &json.SyntaxError{}) + testBindError(assertion, strings.NewReader(userJSONInvalidType), MIMEApplicationJSON, &json.UnmarshalTypeError{}) } func TestBindXML(t *testing.T) { @@ -217,17 +217,17 @@ func TestBindXML(t *testing.T) { } func TestBindForm(t *testing.T) { - assert := assert.New(t) + assertion := assert.New(t) - testBindOkay(assert, strings.NewReader(userForm), nil, MIMEApplicationForm) - testBindOkay(assert, strings.NewReader(userForm), dummyQuery, MIMEApplicationForm) + testBindOkay(assertion, strings.NewReader(userForm), nil, MIMEApplicationForm) + testBindOkay(assertion, strings.NewReader(userForm), dummyQuery, MIMEApplicationForm) e := New() req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userForm)) rec := httptest.NewRecorder() c := e.NewContext(req, rec) req.Header.Set(HeaderContentType, MIMEApplicationForm) err := c.Bind(&[]struct{ Field string }{}) - assert.Error(err) + assertion.Error(err) } func TestBindQueryParams(t *testing.T) { @@ -317,14 +317,14 @@ func TestBindUnmarshalParam(t *testing.T) { err := c.Bind(&result) ts := Timestamp(time.Date(2016, 12, 6, 19, 9, 5, 0, time.UTC)) - assert := assert.New(t) - if assert.NoError(err) { + assertion := assert.New(t) + if assertion.NoError(err) { // assert.Equal( Timestamp(reflect.TypeOf(&Timestamp{}), time.Date(2016, 12, 6, 19, 9, 5, 0, time.UTC)), result.T) - assert.Equal(ts, result.T) - assert.Equal(StringArray([]string{"one", "two", "three"}), result.SA) - assert.Equal([]Timestamp{ts, ts}, result.TA) - assert.Equal(Struct{""}, result.ST) // child struct does not have a field with matching tag - assert.Equal("baz", result.StWithTag.Foo) // child struct has field with matching tag + assertion.Equal(ts, result.T) + assertion.Equal(StringArray([]string{"one", "two", "three"}), result.SA) + assertion.Equal([]Timestamp{ts, ts}, result.TA) + assertion.Equal(Struct{""}, result.ST) // child struct does not have a field with matching tag + assertion.Equal("baz", result.StWithTag.Foo) // child struct has field with matching tag } } From e75e02b5b4abc6d40f026272d52dd3b4e4e11ea0 Mon Sep 17 00:00:00 2001 From: Kamandlou Date: Sat, 1 Oct 2022 09:55:35 +0330 Subject: [PATCH 09/12] revert "context.go" --- context.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/context.go b/context.go index eaa5c992c..5567100b9 100644 --- a/context.go +++ b/context.go @@ -389,10 +389,7 @@ func (c *context) FormFile(name string) (*multipart.FileHeader, error) { if err != nil { return nil, err } - err = f.Close() - if err != nil { - return nil, err - } + f.Close() return fh, nil } From a8ce89695122a82182d0391cda6a0ca4861e80de Mon Sep 17 00:00:00 2001 From: Kamandlou Date: Thu, 6 Oct 2022 10:15:10 +0330 Subject: [PATCH 10/12] update bind_test.go --- bind_test.go | 94 ++++++++++++++++++++++++---------------------------- 1 file changed, 44 insertions(+), 50 deletions(-) diff --git a/bind_test.go b/bind_test.go index 86a7adf11..064992dc5 100644 --- a/bind_test.go +++ b/bind_test.go @@ -190,44 +190,40 @@ func TestToMultipleFields(t *testing.T) { } func TestBindJSON(t *testing.T) { - assertion := assert.New(t) - testBindOkay(assertion, strings.NewReader(userJSON), nil, MIMEApplicationJSON) - testBindOkay(assertion, strings.NewReader(userJSON), dummyQuery, MIMEApplicationJSON) - testBindArrayOkay(assertion, strings.NewReader(usersJSON), nil, MIMEApplicationJSON) - testBindArrayOkay(assertion, strings.NewReader(usersJSON), dummyQuery, MIMEApplicationJSON) - testBindError(assertion, strings.NewReader(invalidContent), MIMEApplicationJSON, &json.SyntaxError{}) - testBindError(assertion, strings.NewReader(userJSONInvalidType), MIMEApplicationJSON, &json.UnmarshalTypeError{}) + testBindOkay(t, strings.NewReader(userJSON), nil, MIMEApplicationJSON) + testBindOkay(t, strings.NewReader(userJSON), dummyQuery, MIMEApplicationJSON) + testBindArrayOkay(t, strings.NewReader(usersJSON), nil, MIMEApplicationJSON) + testBindArrayOkay(t, strings.NewReader(usersJSON), dummyQuery, MIMEApplicationJSON) + testBindError(t, strings.NewReader(invalidContent), MIMEApplicationJSON, &json.SyntaxError{}) + testBindError(t, strings.NewReader(userJSONInvalidType), MIMEApplicationJSON, &json.UnmarshalTypeError{}) } func TestBindXML(t *testing.T) { - assert := assert.New(t) - - testBindOkay(assert, strings.NewReader(userXML), nil, MIMEApplicationXML) - testBindOkay(assert, strings.NewReader(userXML), dummyQuery, MIMEApplicationXML) - testBindArrayOkay(assert, strings.NewReader(userXML), nil, MIMEApplicationXML) - testBindArrayOkay(assert, strings.NewReader(userXML), dummyQuery, MIMEApplicationXML) - testBindError(assert, strings.NewReader(invalidContent), MIMEApplicationXML, errors.New("")) - testBindError(assert, strings.NewReader(userXMLConvertNumberError), MIMEApplicationXML, &strconv.NumError{}) - testBindError(assert, strings.NewReader(userXMLUnsupportedTypeError), MIMEApplicationXML, &xml.SyntaxError{}) - testBindOkay(assert, strings.NewReader(userXML), nil, MIMETextXML) - testBindOkay(assert, strings.NewReader(userXML), dummyQuery, MIMETextXML) - testBindError(assert, strings.NewReader(invalidContent), MIMETextXML, errors.New("")) - testBindError(assert, strings.NewReader(userXMLConvertNumberError), MIMETextXML, &strconv.NumError{}) - testBindError(assert, strings.NewReader(userXMLUnsupportedTypeError), MIMETextXML, &xml.SyntaxError{}) + testBindOkay(t, strings.NewReader(userXML), nil, MIMEApplicationXML) + testBindOkay(t, strings.NewReader(userXML), dummyQuery, MIMEApplicationXML) + testBindArrayOkay(t, strings.NewReader(userXML), nil, MIMEApplicationXML) + testBindArrayOkay(t, strings.NewReader(userXML), dummyQuery, MIMEApplicationXML) + testBindError(t, strings.NewReader(invalidContent), MIMEApplicationXML, errors.New("")) + testBindError(t, strings.NewReader(userXMLConvertNumberError), MIMEApplicationXML, &strconv.NumError{}) + testBindError(t, strings.NewReader(userXMLUnsupportedTypeError), MIMEApplicationXML, &xml.SyntaxError{}) + testBindOkay(t, strings.NewReader(userXML), nil, MIMETextXML) + testBindOkay(t, strings.NewReader(userXML), dummyQuery, MIMETextXML) + testBindError(t, strings.NewReader(invalidContent), MIMETextXML, errors.New("")) + testBindError(t, strings.NewReader(userXMLConvertNumberError), MIMETextXML, &strconv.NumError{}) + testBindError(t, strings.NewReader(userXMLUnsupportedTypeError), MIMETextXML, &xml.SyntaxError{}) } func TestBindForm(t *testing.T) { - assertion := assert.New(t) - testBindOkay(assertion, strings.NewReader(userForm), nil, MIMEApplicationForm) - testBindOkay(assertion, strings.NewReader(userForm), dummyQuery, MIMEApplicationForm) + testBindOkay(t, strings.NewReader(userForm), nil, MIMEApplicationForm) + testBindOkay(t, strings.NewReader(userForm), dummyQuery, MIMEApplicationForm) e := New() req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(userForm)) rec := httptest.NewRecorder() c := e.NewContext(req, rec) req.Header.Set(HeaderContentType, MIMEApplicationForm) err := c.Bind(&[]struct{ Field string }{}) - assertion.Error(err) + assert.Error(t, err) } func TestBindQueryParams(t *testing.T) { @@ -426,14 +422,12 @@ func TestBindMultipartForm(t *testing.T) { mw.Close() body := bodyBuffer.Bytes() - assert := assert.New(t) - testBindOkay(assert, bytes.NewReader(body), nil, mw.FormDataContentType()) - testBindOkay(assert, bytes.NewReader(body), dummyQuery, mw.FormDataContentType()) + testBindOkay(t, bytes.NewReader(body), nil, mw.FormDataContentType()) + testBindOkay(t, bytes.NewReader(body), dummyQuery, mw.FormDataContentType()) } func TestBindUnsupportedMediaType(t *testing.T) { - assert := assert.New(t) - testBindError(assert, strings.NewReader(invalidContent), MIMEApplicationJSON, &json.SyntaxError{}) + testBindError(t, strings.NewReader(invalidContent), MIMEApplicationJSON, &json.SyntaxError{}) } func TestBindbindData(t *testing.T) { @@ -528,7 +522,6 @@ func TestBindUnmarshalTypeError(t *testing.T) { } func TestBindSetWithProperType(t *testing.T) { - assert := assert.New(t) ts := new(bindTestStruct) typ := reflect.TypeOf(ts).Elem() val := reflect.ValueOf(ts).Elem() @@ -543,9 +536,10 @@ func TestBindSetWithProperType(t *testing.T) { } val := values[typeField.Name][0] err := setWithProperType(typeField.Type.Kind(), val, structField) - assert.NoError(err) + assert.NoError(t, err) } - assertBindTestStruct(assert, ts) + assertion := assert.New(t) + assertBindTestStruct(assertion, ts) type foo struct { Bar bytes.Buffer @@ -553,7 +547,7 @@ func TestBindSetWithProperType(t *testing.T) { v := &foo{} typ = reflect.TypeOf(v).Elem() val = reflect.ValueOf(v).Elem() - assert.Error(setWithProperType(typ.Field(0).Type.Kind(), "5", val.Field(0))) + assert.Error(t, setWithProperType(typ.Field(0).Type.Kind(), "5", val.Field(0))) } func TestBindSetFields(t *testing.T) { @@ -632,7 +626,7 @@ func assertBindTestStruct(a *assert.Assertions, ts *bindTestStruct) { a.Equal("", ts.GetCantSet()) } -func testBindOkay(assert *assert.Assertions, r io.Reader, query url.Values, ctype string) { +func testBindOkay(t *testing.T, r io.Reader, query url.Values, ctype string) { e := New() path := "/" if len(query) > 0 { @@ -644,13 +638,13 @@ func testBindOkay(assert *assert.Assertions, r io.Reader, query url.Values, ctyp req.Header.Set(HeaderContentType, ctype) u := new(user) err := c.Bind(u) - if assert.NoError(err) { - assert.Equal(1, u.ID) - assert.Equal("Jon Snow", u.Name) + if assert.Equal(t, nil, err) { + assert.Equal(t, 1, u.ID) + assert.Equal(t, "Jon Snow", u.Name) } } -func testBindArrayOkay(assert *assert.Assertions, r io.Reader, query url.Values, ctype string) { +func testBindArrayOkay(t *testing.T, r io.Reader, query url.Values, ctype string) { e := New() path := "/" if len(query) > 0 { @@ -662,14 +656,14 @@ func testBindArrayOkay(assert *assert.Assertions, r io.Reader, query url.Values, req.Header.Set(HeaderContentType, ctype) u := []user{} err := c.Bind(&u) - if assert.NoError(err) { - assert.Equal(1, len(u)) - assert.Equal(1, u[0].ID) - assert.Equal("Jon Snow", u[0].Name) + if assert.NoError(t, err) { + assert.Equal(t, 1, len(u)) + assert.Equal(t, 1, u[0].ID) + assert.Equal(t, "Jon Snow", u[0].Name) } } -func testBindError(assert *assert.Assertions, r io.Reader, ctype string, expectedInternal error) { +func testBindError(t *testing.T, r io.Reader, ctype string, expectedInternal error) { e := New() req := httptest.NewRequest(http.MethodPost, "/", r) rec := httptest.NewRecorder() @@ -681,14 +675,14 @@ func testBindError(assert *assert.Assertions, r io.Reader, ctype string, expecte switch { case strings.HasPrefix(ctype, MIMEApplicationJSON), strings.HasPrefix(ctype, MIMEApplicationXML), strings.HasPrefix(ctype, MIMETextXML), strings.HasPrefix(ctype, MIMEApplicationForm), strings.HasPrefix(ctype, MIMEMultipartForm): - if assert.IsType(new(HTTPError), err) { - assert.Equal(http.StatusBadRequest, err.(*HTTPError).Code) - assert.IsType(expectedInternal, err.(*HTTPError).Internal) + if assert.IsType(t, new(HTTPError), err) { + assert.Equal(t, http.StatusBadRequest, err.(*HTTPError).Code) + assert.IsType(t, expectedInternal, err.(*HTTPError).Internal) } default: - if assert.IsType(new(HTTPError), err) { - assert.Equal(ErrUnsupportedMediaType, err) - assert.IsType(expectedInternal, err.(*HTTPError).Internal) + if assert.IsType(t, new(HTTPError), err) { + assert.Equal(t, ErrUnsupportedMediaType, err) + assert.IsType(t, expectedInternal, err.(*HTTPError).Internal) } } } From 6c14ba7c9aac1dce2e752107975c276323edc090 Mon Sep 17 00:00:00 2001 From: Kamandlou Date: Thu, 6 Oct 2022 11:24:59 +0330 Subject: [PATCH 11/12] update bind_test.go --- bind_test.go | 93 +++++++++++++++++++++++++--------------------------- 1 file changed, 45 insertions(+), 48 deletions(-) diff --git a/bind_test.go b/bind_test.go index 064992dc5..0d49f0bc6 100644 --- a/bind_test.go +++ b/bind_test.go @@ -313,14 +313,13 @@ func TestBindUnmarshalParam(t *testing.T) { err := c.Bind(&result) ts := Timestamp(time.Date(2016, 12, 6, 19, 9, 5, 0, time.UTC)) - assertion := assert.New(t) - if assertion.NoError(err) { + if assert.NoError(t, err) { // assert.Equal( Timestamp(reflect.TypeOf(&Timestamp{}), time.Date(2016, 12, 6, 19, 9, 5, 0, time.UTC)), result.T) - assertion.Equal(ts, result.T) - assertion.Equal(StringArray([]string{"one", "two", "three"}), result.SA) - assertion.Equal([]Timestamp{ts, ts}, result.TA) - assertion.Equal(Struct{""}, result.ST) // child struct does not have a field with matching tag - assertion.Equal("baz", result.StWithTag.Foo) // child struct has field with matching tag + assert.Equal(t, ts, result.T) + assert.Equal(t, StringArray([]string{"one", "two", "three"}), result.SA) + assert.Equal(t, []Timestamp{ts, ts}, result.TA) + assert.Equal(t, Struct{""}, result.ST) // child struct does not have a field with matching tag + assert.Equal(t, "baz", result.StWithTag.Foo) // child struct has field with matching tag } } @@ -431,27 +430,26 @@ func TestBindUnsupportedMediaType(t *testing.T) { } func TestBindbindData(t *testing.T) { - a := assert.New(t) ts := new(bindTestStruct) b := new(DefaultBinder) err := b.bindData(ts, values, "form") - a.NoError(err) - - a.Equal(0, ts.I) - a.Equal(int8(0), ts.I8) - a.Equal(int16(0), ts.I16) - a.Equal(int32(0), ts.I32) - a.Equal(int64(0), ts.I64) - a.Equal(uint(0), ts.UI) - a.Equal(uint8(0), ts.UI8) - a.Equal(uint16(0), ts.UI16) - a.Equal(uint32(0), ts.UI32) - a.Equal(uint64(0), ts.UI64) - a.Equal(false, ts.B) - a.Equal(float32(0), ts.F32) - a.Equal(float64(0), ts.F64) - a.Equal("", ts.S) - a.Equal("", ts.cantSet) + assert.NoError(t, err) + + assert.Equal(t, 0, ts.I) + assert.Equal(t, int8(0), ts.I8) + assert.Equal(t, int16(0), ts.I16) + assert.Equal(t, int32(0), ts.I32) + assert.Equal(t, int64(0), ts.I64) + assert.Equal(t, uint(0), ts.UI) + assert.Equal(t, uint8(0), ts.UI8) + assert.Equal(t, uint16(0), ts.UI16) + assert.Equal(t, uint32(0), ts.UI32) + assert.Equal(t, uint64(0), ts.UI64) + assert.Equal(t, false, ts.B) + assert.Equal(t, float32(0), ts.F32) + assert.Equal(t, float64(0), ts.F64) + assert.Equal(t, "", ts.S) + assert.Equal(t, "", ts.cantSet) } func TestBindParam(t *testing.T) { @@ -551,52 +549,51 @@ func TestBindSetWithProperType(t *testing.T) { } func TestBindSetFields(t *testing.T) { - assert := assert.New(t) ts := new(bindTestStruct) val := reflect.ValueOf(ts).Elem() // Int - if assert.NoError(setIntField("5", 0, val.FieldByName("I"))) { - assert.Equal(5, ts.I) + if assert.NoError(t, setIntField("5", 0, val.FieldByName("I"))) { + assert.Equal(t, 5, ts.I) } - if assert.NoError(setIntField("", 0, val.FieldByName("I"))) { - assert.Equal(0, ts.I) + if assert.NoError(t, setIntField("", 0, val.FieldByName("I"))) { + assert.Equal(t, 0, ts.I) } // Uint - if assert.NoError(setUintField("10", 0, val.FieldByName("UI"))) { - assert.Equal(uint(10), ts.UI) + if assert.NoError(t, setUintField("10", 0, val.FieldByName("UI"))) { + assert.Equal(t, uint(10), ts.UI) } - if assert.NoError(setUintField("", 0, val.FieldByName("UI"))) { - assert.Equal(uint(0), ts.UI) + if assert.NoError(t, setUintField("", 0, val.FieldByName("UI"))) { + assert.Equal(t, uint(0), ts.UI) } // Float - if assert.NoError(setFloatField("15.5", 0, val.FieldByName("F32"))) { - assert.Equal(float32(15.5), ts.F32) + if assert.NoError(t, setFloatField("15.5", 0, val.FieldByName("F32"))) { + assert.Equal(t, float32(15.5), ts.F32) } - if assert.NoError(setFloatField("", 0, val.FieldByName("F32"))) { - assert.Equal(float32(0.0), ts.F32) + if assert.NoError(t, setFloatField("", 0, val.FieldByName("F32"))) { + assert.Equal(t, float32(0.0), ts.F32) } // Bool - if assert.NoError(setBoolField("true", val.FieldByName("B"))) { - assert.Equal(true, ts.B) + if assert.NoError(t, setBoolField("true", val.FieldByName("B"))) { + assert.Equal(t, true, ts.B) } - if assert.NoError(setBoolField("", val.FieldByName("B"))) { - assert.Equal(false, ts.B) + if assert.NoError(t, setBoolField("", val.FieldByName("B"))) { + assert.Equal(t, false, ts.B) } ok, err := unmarshalFieldNonPtr("2016-12-06T19:09:05Z", val.FieldByName("T")) - if assert.NoError(err) { - assert.Equal(ok, true) - assert.Equal(Timestamp(time.Date(2016, 12, 6, 19, 9, 5, 0, time.UTC)), ts.T) + if assert.NoError(t, err) { + assert.Equal(t, ok, true) + assert.Equal(t, Timestamp(time.Date(2016, 12, 6, 19, 9, 5, 0, time.UTC)), ts.T) } } func BenchmarkBindbindDataWithTags(b *testing.B) { b.ReportAllocs() - assert := assert.New(b) + assertion := assert.New(b) ts := new(bindTestStructWithTags) binder := new(DefaultBinder) var err error @@ -604,8 +601,8 @@ func BenchmarkBindbindDataWithTags(b *testing.B) { for i := 0; i < b.N; i++ { err = binder.bindData(ts, values, "form") } - assert.NoError(err) - assertBindTestStruct(assert, (*bindTestStruct)(ts)) + assertion.NoError(err) + assertBindTestStruct(assertion, (*bindTestStruct)(ts)) } func assertBindTestStruct(a *assert.Assertions, ts *bindTestStruct) { From 8f7019e2d37291547ce7db9c90ea179c74b8b318 Mon Sep 17 00:00:00 2001 From: Kamandlou Date: Thu, 6 Oct 2022 11:43:15 +0330 Subject: [PATCH 12/12] update bind_test.go --- bind_test.go | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/bind_test.go b/bind_test.go index 0d49f0bc6..c35283dcf 100644 --- a/bind_test.go +++ b/bind_test.go @@ -536,8 +536,7 @@ func TestBindSetWithProperType(t *testing.T) { err := setWithProperType(typeField.Type.Kind(), val, structField) assert.NoError(t, err) } - assertion := assert.New(t) - assertBindTestStruct(assertion, ts) + assertBindTestStruct(t, ts) type foo struct { Bar bytes.Buffer @@ -593,7 +592,6 @@ func TestBindSetFields(t *testing.T) { func BenchmarkBindbindDataWithTags(b *testing.B) { b.ReportAllocs() - assertion := assert.New(b) ts := new(bindTestStructWithTags) binder := new(DefaultBinder) var err error @@ -601,26 +599,26 @@ func BenchmarkBindbindDataWithTags(b *testing.B) { for i := 0; i < b.N; i++ { err = binder.bindData(ts, values, "form") } - assertion.NoError(err) - assertBindTestStruct(assertion, (*bindTestStruct)(ts)) + assert.NoError(b, err) + assertBindTestStruct(b, (*bindTestStruct)(ts)) } -func assertBindTestStruct(a *assert.Assertions, ts *bindTestStruct) { - a.Equal(0, ts.I) - a.Equal(int8(8), ts.I8) - a.Equal(int16(16), ts.I16) - a.Equal(int32(32), ts.I32) - a.Equal(int64(64), ts.I64) - a.Equal(uint(0), ts.UI) - a.Equal(uint8(8), ts.UI8) - a.Equal(uint16(16), ts.UI16) - a.Equal(uint32(32), ts.UI32) - a.Equal(uint64(64), ts.UI64) - a.Equal(true, ts.B) - a.Equal(float32(32.5), ts.F32) - a.Equal(float64(64.5), ts.F64) - a.Equal("test", ts.S) - a.Equal("", ts.GetCantSet()) +func assertBindTestStruct(tb testing.TB, ts *bindTestStruct) { + assert.Equal(tb, 0, ts.I) + assert.Equal(tb, int8(8), ts.I8) + assert.Equal(tb, int16(16), ts.I16) + assert.Equal(tb, int32(32), ts.I32) + assert.Equal(tb, int64(64), ts.I64) + assert.Equal(tb, uint(0), ts.UI) + assert.Equal(tb, uint8(8), ts.UI8) + assert.Equal(tb, uint16(16), ts.UI16) + assert.Equal(tb, uint32(32), ts.UI32) + assert.Equal(tb, uint64(64), ts.UI64) + assert.Equal(tb, true, ts.B) + assert.Equal(tb, float32(32.5), ts.F32) + assert.Equal(tb, float64(64.5), ts.F64) + assert.Equal(tb, "test", ts.S) + assert.Equal(tb, "", ts.GetCantSet()) } func testBindOkay(t *testing.T, r io.Reader, query url.Values, ctype string) {