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

🩹 Fix: Fix app.Test() auto-failing when a connection is closed early #3279

Merged
merged 6 commits into from
Jan 13, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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)
}
Expand Down
19 changes: 17 additions & 2 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down
4 changes: 3 additions & 1 deletion helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading