Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error handling on closing body #2254

Merged
merged 6 commits into from Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions bind_test.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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()
Expand Down
12 changes: 6 additions & 6 deletions context_test.go
Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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())
Expand All @@ -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())
Expand Down Expand Up @@ -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)

Expand Down
53 changes: 35 additions & 18 deletions echo_test.go
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/tls"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -380,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)
Expand Down Expand Up @@ -482,16 +491,16 @@ func TestEchoURL(t *testing.T) {
g := e.Group("/group")
g.GET("/users/:uid/files/:fid", getFile)

assert := assert.New(t)
assertion := 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.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) {
Expand Down Expand Up @@ -598,7 +607,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)) }
Expand Down Expand Up @@ -694,8 +703,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())
})
}
}
Expand Down Expand Up @@ -1231,7 +1240,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")
Expand All @@ -1244,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 {
Expand All @@ -1255,7 +1267,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)
Expand Down Expand Up @@ -1379,7 +1391,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 {
Expand Down