-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Description
Bug Description
While working on #3276, I found an issue with app.Test() when a handler closes the underlying connection before returning from the handler. Since app.server.ServeConn(conn) fails to write to the connection when the connection is closed early, an error is instantly returned.
This issue sometimes occurs when using c.Drop() in a handler (especially in middlewares). Since c.Drop() closes the underlying connection before app.server.ServeConn() can write to it, it instantly returns an error.
This is also an issue for testing future features like manual response flushing via c.End() (#3276) and Early hints (#3211), where data is sent during the life of a handler. Since app.Test() assumes that an early connection is always an unrecoverable error, it won't allow proper testing of these features.
How to Reproduce
- Create a handler that only has
return c.Drop() - Call
app.Test()for that handler. - You get an error
"testConn is closed".
Expected Behavior
app.Test() should only return an error if the handler's response is empty or if the test failed on timeout.
If the closed connection results in an empty response (e.g. from c.Drop()), the error should be "test: got empty response" instead of the closed connection error.
Fiber Version
v3.0.0-beta.4
Code Snippet (optional)
package mypackage
import (
"testing"
"github.com/gofiber/fiber/v3"
"github.com/stretchr/testify/require"
)
// go test -run Test_MyTest
func Test_MyTest(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,
})
require.ErrorIs(t, err, errors.New("test: got empty response")) // fail: Expected ..., but got: &errors.errorString{s:"testConn is closed"}
}Checklist:
- I agree to follow Fiber's Code of Conduct.
- I have checked for existing issues that describe my problem prior to opening this one.
- I understand that improperly formatted bug reports may be closed without explanation.