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

🚀 v3 Feature: Make app.Test accept a time.Duration timeout #2269

Merged
merged 1 commit into from
Dec 12, 2022
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
14 changes: 7 additions & 7 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -865,11 +865,11 @@ func (app *App) Hooks() *Hooks {

// Test is used for internal debugging by passing a *http.Request.
// Timeout is optional and defaults to 1s, -1 will disable it completely.
func (app *App) Test(req *http.Request, msTimeout ...int) (resp *http.Response, err error) {
func (app *App) Test(req *http.Request, timeout ...time.Duration) (resp *http.Response, err error) {
// Set timeout
timeout := 1000
if len(msTimeout) > 0 {
timeout = msTimeout[0]
to := 1 * time.Second
if len(timeout) > 0 {
to = timeout[0]
}

// Add Content-Length if not provided with body
Expand Down Expand Up @@ -908,12 +908,12 @@ func (app *App) Test(req *http.Request, msTimeout ...int) (resp *http.Response,
}()

// Wait for callback
if timeout >= 0 {
if to >= 0 {
// With timeout
select {
case err = <-channel:
case <-time.After(time.Duration(timeout) * time.Millisecond):
return nil, fmt.Errorf("test: timeout error %vms", timeout)
case <-time.After(to):
return nil, fmt.Errorf("test: timeout error after %s", to)
}
} else {
// Without timeout
Expand Down
2 changes: 1 addition & 1 deletion app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,7 @@ func Test_Test_Timeout(t *testing.T) {
return nil
})

_, err = app.Test(httptest.NewRequest(MethodGet, "/timeout", nil), 20)
_, err = app.Test(httptest.NewRequest(MethodGet, "/timeout", nil), 20*time.Millisecond)
require.True(t, err != nil, "app.Test(req)")
}

Expand Down
3 changes: 2 additions & 1 deletion middleware/compress/compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http/httptest"
"os"
"testing"
"time"

"github.com/gofiber/fiber/v3"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -112,7 +113,7 @@ func Test_Compress_Brotli(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
req.Header.Set("Accept-Encoding", "br")

resp, err := app.Test(req, 10000)
resp, err := app.Test(req, 10*time.Second)
require.NoError(t, err, "app.Test(req)")
require.Equal(t, 200, resp.StatusCode, "Status code")
require.Equal(t, "br", resp.Header.Get(fiber.HeaderContentEncoding))
Expand Down
5 changes: 3 additions & 2 deletions middleware/pprof/pprof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"net/http/httptest"
"testing"
"time"

"github.com/gofiber/fiber/v3"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -104,7 +105,7 @@ func Test_Pprof_Subs(t *testing.T) {
if sub == "profile" {
target += "?seconds=1"
}
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, target, nil), 5000)
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, target, nil), 5*time.Second)
require.NoError(t, err)
require.Equal(t, 200, resp.StatusCode)
})
Expand All @@ -131,7 +132,7 @@ func Test_Pprof_Subs_WithPrefix(t *testing.T) {
if sub == "profile" {
target += "?seconds=1"
}
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, target, nil), 5000)
resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, target, nil), 5*time.Second)
require.NoError(t, err)
require.Equal(t, 200, resp.StatusCode)
})
Expand Down
8 changes: 4 additions & 4 deletions middleware/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func Test_Proxy(t *testing.T) {
func(c fiber.Ctx) error { return c.SendStatus(fiber.StatusTeapot) }, t,
)

resp, err := target.Test(httptest.NewRequest("GET", "/", nil), 2000)
resp, err := target.Test(httptest.NewRequest("GET", "/", nil), 2*time.Second)
require.NoError(t, err)
require.Equal(t, fiber.StatusTeapot, resp.StatusCode)

Expand Down Expand Up @@ -297,7 +297,7 @@ func Test_Proxy_Timeout_Slow_Server(t *testing.T) {
Timeout: 3 * time.Second,
}))

resp, err := app.Test(httptest.NewRequest("GET", "/", nil), 5000)
resp, err := app.Test(httptest.NewRequest("GET", "/", nil), 5*time.Second)
require.NoError(t, err)
require.Equal(t, fiber.StatusOK, resp.StatusCode)

Expand All @@ -321,7 +321,7 @@ func Test_Proxy_With_Timeout(t *testing.T) {
Timeout: 100 * time.Millisecond,
}))

resp, err := app.Test(httptest.NewRequest("GET", "/", nil), 2000)
resp, err := app.Test(httptest.NewRequest("GET", "/", nil), 2*time.Second)
require.NoError(t, err)
require.Equal(t, fiber.StatusInternalServerError, resp.StatusCode)

Expand Down Expand Up @@ -475,7 +475,7 @@ func Test_ProxyBalancer_Custom_Client(t *testing.T) {
func(c fiber.Ctx) error { return c.SendStatus(fiber.StatusTeapot) }, t,
)

resp, err := target.Test(httptest.NewRequest("GET", "/", nil), 2000)
resp, err := target.Test(httptest.NewRequest("GET", "/", nil), 2*time.Second)
require.NoError(t, err)
require.Equal(t, fiber.StatusTeapot, resp.StatusCode)

Expand Down