mirror of https://github.com/gofiber/fiber.git
🩹 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 typepull/3280/head
parent
bc37f209bf
commit
4e5fea1d7a
6
app.go
6
app.go
|
@ -941,6 +941,8 @@ func (app *App) Hooks() *Hooks {
|
||||||
return app.hooks
|
return app.hooks
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var ErrTestGotEmptyResponse = errors.New("test: got empty response")
|
||||||
|
|
||||||
// TestConfig is a struct holding Test settings
|
// TestConfig is a struct holding Test settings
|
||||||
type TestConfig struct {
|
type TestConfig struct {
|
||||||
// Timeout defines the maximum duration a
|
// 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
|
// 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
|
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)
|
res, err := http.ReadResponse(buffer, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, io.ErrUnexpectedEOF) {
|
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)
|
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,
|
Timeout: 100 * time.Millisecond,
|
||||||
FailOnTimeout: true,
|
FailOnTimeout: true,
|
||||||
})
|
})
|
||||||
require.Equal(t, os.ErrDeadlineExceeded, err)
|
require.ErrorIs(t, err, os.ErrDeadlineExceeded)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_App_Test_timeout_empty_response(t *testing.T) {
|
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,
|
Timeout: 100 * time.Millisecond,
|
||||||
FailOnTimeout: false,
|
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) {
|
func Test_App_SetTLSHandler(t *testing.T) {
|
||||||
|
|
|
@ -5896,7 +5896,7 @@ func Test_Ctx_Drop(t *testing.T) {
|
||||||
|
|
||||||
// Test the Drop method
|
// Test the Drop method
|
||||||
resp, err := app.Test(httptest.NewRequest(MethodGet, "/block-me", nil))
|
resp, err := app.Test(httptest.NewRequest(MethodGet, "/block-me", nil))
|
||||||
require.Error(t, err)
|
require.ErrorIs(t, err, ErrTestGotEmptyResponse)
|
||||||
require.Nil(t, resp)
|
require.Nil(t, resp)
|
||||||
|
|
||||||
// Test the no-response handler
|
// Test the no-response handler
|
||||||
|
@ -5927,7 +5927,7 @@ func Test_Ctx_DropWithMiddleware(t *testing.T) {
|
||||||
|
|
||||||
// Test the Drop method
|
// Test the Drop method
|
||||||
resp, err := app.Test(httptest.NewRequest(MethodGet, "/block-me", nil))
|
resp, err := app.Test(httptest.NewRequest(MethodGet, "/block-me", nil))
|
||||||
require.Error(t, err)
|
require.ErrorIs(t, err, ErrTestGotEmptyResponse)
|
||||||
require.Nil(t, resp)
|
require.Nil(t, resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -612,6 +612,8 @@ func isNoCache(cacheControl string) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var errTestConnClosed = errors.New("testConn is closed")
|
||||||
|
|
||||||
type testConn struct {
|
type testConn struct {
|
||||||
r bytes.Buffer
|
r bytes.Buffer
|
||||||
w bytes.Buffer
|
w bytes.Buffer
|
||||||
|
@ -631,7 +633,7 @@ func (c *testConn) Write(b []byte) (int, error) {
|
||||||
defer c.Unlock()
|
defer c.Unlock()
|
||||||
|
|
||||||
if c.isClosed {
|
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
|
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
|
// Close early, write should fail
|
||||||
conn.Close() //nolint:errcheck, revive // It is fine to ignore the error here
|
conn.Close() //nolint:errcheck, revive // It is fine to ignore the error here
|
||||||
_, err = conn.Write([]byte("Response 2\n"))
|
_, err = conn.Write([]byte("Response 2\n"))
|
||||||
require.Error(t, err)
|
require.ErrorIs(t, err, errTestConnClosed)
|
||||||
|
|
||||||
res := make([]byte, 11)
|
res := make([]byte, 11)
|
||||||
_, err = conn.w.Read(res)
|
_, err = conn.w.Read(res)
|
||||||
|
|
Loading…
Reference in New Issue