From 4e5fea1d7a830a52cbe736d2da88660a2e959549 Mon Sep 17 00:00:00 2001 From: Giovanni Rivera Date: Mon, 13 Jan 2025 05:18:03 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=A9=B9=20Fix:=20Fix=20app.Test()=20auto-f?= =?UTF-8?q?ailing=20when=20a=20connection=20is=20closed=20early=20(#3279)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ♻️ Refactor: Extract testConn err to variable * ♻️ Refactor: Extract ErrTestGotEmptyResponse from app.Test() * 🩹 Fix: Fix `app.Test()` auto-failing when testConn is closed * 🩹 Fix(app_test.go): Use tab for indent instead of spaces * 🩹 Fix(app_test.go): Fix to respect gofmt linter * ♻️ Refactor: Update Drop tests to verify error type --- app.go | 6 ++++-- app_test.go | 19 +++++++++++++++++-- ctx_test.go | 4 ++-- helpers.go | 4 +++- helpers_test.go | 2 +- 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/app.go b/app.go index e96642aed2..dca7efed00 100644 --- a/app.go +++ b/app.go @@ -941,6 +941,8 @@ func (app *App) Hooks() *Hooks { return app.hooks } +var ErrTestGotEmptyResponse = errors.New("test: got empty response") + // TestConfig is a struct holding Test settings type TestConfig struct { // Timeout defines the maximum duration a @@ -1022,7 +1024,7 @@ func (app *App) Test(req *http.Request, config ...TestConfig) (*http.Response, e } // Check for errors - if err != nil && !errors.Is(err, fasthttp.ErrGetOnly) { + if err != nil && !errors.Is(err, fasthttp.ErrGetOnly) && !errors.Is(err, errTestConnClosed) { return nil, err } @@ -1033,7 +1035,7 @@ func (app *App) Test(req *http.Request, config ...TestConfig) (*http.Response, e res, err := http.ReadResponse(buffer, req) if err != nil { if errors.Is(err, io.ErrUnexpectedEOF) { - return nil, errors.New("test: got empty response") + return nil, ErrTestGotEmptyResponse } return nil, fmt.Errorf("failed to read response: %w", err) } diff --git a/app_test.go b/app_test.go index 8455ded86e..1b2b7a40d9 100644 --- a/app_test.go +++ b/app_test.go @@ -1491,7 +1491,7 @@ func Test_App_Test_timeout(t *testing.T) { Timeout: 100 * time.Millisecond, FailOnTimeout: true, }) - require.Equal(t, os.ErrDeadlineExceeded, err) + require.ErrorIs(t, err, os.ErrDeadlineExceeded) } func Test_App_Test_timeout_empty_response(t *testing.T) { @@ -1507,7 +1507,22 @@ func Test_App_Test_timeout_empty_response(t *testing.T) { Timeout: 100 * time.Millisecond, FailOnTimeout: false, }) - require.Equal(t, errors.New("test: got empty response"), err) + require.ErrorIs(t, err, ErrTestGotEmptyResponse) +} + +func Test_App_Test_drop_empty_response(t *testing.T) { + t.Parallel() + + app := New() + app.Get("/", func(c Ctx) error { + return c.Drop() + }) + + _, err := app.Test(httptest.NewRequest(MethodGet, "/", nil), TestConfig{ + Timeout: 0, + FailOnTimeout: false, + }) + require.ErrorIs(t, err, ErrTestGotEmptyResponse) } func Test_App_SetTLSHandler(t *testing.T) { diff --git a/ctx_test.go b/ctx_test.go index eb81876e37..f094a2c494 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -5896,7 +5896,7 @@ func Test_Ctx_Drop(t *testing.T) { // Test the Drop method resp, err := app.Test(httptest.NewRequest(MethodGet, "/block-me", nil)) - require.Error(t, err) + require.ErrorIs(t, err, ErrTestGotEmptyResponse) require.Nil(t, resp) // Test the no-response handler @@ -5927,7 +5927,7 @@ func Test_Ctx_DropWithMiddleware(t *testing.T) { // Test the Drop method resp, err := app.Test(httptest.NewRequest(MethodGet, "/block-me", nil)) - require.Error(t, err) + require.ErrorIs(t, err, ErrTestGotEmptyResponse) require.Nil(t, resp) } diff --git a/helpers.go b/helpers.go index 526074032a..04a1da6907 100644 --- a/helpers.go +++ b/helpers.go @@ -612,6 +612,8 @@ func isNoCache(cacheControl string) bool { return true } +var errTestConnClosed = errors.New("testConn is closed") + type testConn struct { r bytes.Buffer w bytes.Buffer @@ -631,7 +633,7 @@ func (c *testConn) Write(b []byte) (int, error) { defer c.Unlock() if c.isClosed { - return 0, errors.New("testConn is closed") + return 0, errTestConnClosed } return c.w.Write(b) //nolint:wrapcheck // This must not be wrapped } diff --git a/helpers_test.go b/helpers_test.go index 28a5df2ae7..a3f631bbfd 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -548,7 +548,7 @@ func Test_Utils_TestConn_Closed_Write(t *testing.T) { // Close early, write should fail conn.Close() //nolint:errcheck, revive // It is fine to ignore the error here _, err = conn.Write([]byte("Response 2\n")) - require.Error(t, err) + require.ErrorIs(t, err, errTestConnClosed) res := make([]byte, 11) _, err = conn.w.Read(res)