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

🐛 [Bug]: Fix app.Test() auto-failing when a connection is closed early #3278

Closed
3 tasks done
grivera64 opened this issue Jan 11, 2025 · 0 comments · Fixed by #3279
Closed
3 tasks done

🐛 [Bug]: Fix app.Test() auto-failing when a connection is closed early #3278

grivera64 opened this issue Jan 11, 2025 · 0 comments · Fixed by #3279
Assignees

Comments

@grivera64
Copy link
Member

grivera64 commented Jan 11, 2025

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

  1. Create a handler that only has return c.Drop()
  2. Call app.Test() for that handler.
  3. 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant