mirror of
https://github.com/gofiber/fiber.git
synced 2025-02-06 11:02:01 +00:00
🩹 Fix: Fix app.Test() auto-failing when a connection is closed early (#3279)
* ♻️ 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
This commit is contained in:
parent
bc37f209bf
commit
4e5fea1d7a
6
app.go
6
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)
|
||||
}
|
||||
|
19
app_test.go
19
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) {
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user