Skip to content

Commit 334dbe8

Browse files
committed
♻ Refactor: Change app.Test() and all uses to accept 0 as no timeout instead of -1
1 parent 6b6674f commit 334dbe8

File tree

5 files changed

+9
-12
lines changed

5 files changed

+9
-12
lines changed

Diff for: app.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,7 @@ type TestConfig struct {
919919

920920
// Test is used for internal debugging by passing a *http.Request.
921921
// Config is optional and defaults to a 1s error on timeout,
922-
// -1 timeout will disable it completely.
922+
// 0 timeout will disable it completely.
923923
func (app *App) Test(req *http.Request, config ...TestConfig) (*http.Response, error) {
924924
// Default config
925925
cfg := TestConfig{
@@ -968,7 +968,7 @@ func (app *App) Test(req *http.Request, config ...TestConfig) (*http.Response, e
968968
}()
969969

970970
// Wait for callback
971-
if cfg.Timeout >= 0 {
971+
if cfg.Timeout > 0 {
972972
// With timeout
973973
select {
974974
case err = <-channel:

Diff for: app_test.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -1126,8 +1126,7 @@ func Test_Test_Timeout(t *testing.T) {
11261126
app.Get("/", testEmptyHandler)
11271127

11281128
resp, err := app.Test(httptest.NewRequest(MethodGet, "/", nil), TestConfig{
1129-
Timeout: -1,
1130-
FailOnTimeout: false,
1129+
Timeout: 0,
11311130
})
11321131
require.NoError(t, err, "app.Test(req)")
11331132
require.Equal(t, 200, resp.StatusCode, "Status code")
@@ -1440,8 +1439,7 @@ func Test_App_Test_no_timeout_infinitely(t *testing.T) {
14401439

14411440
req := httptest.NewRequest(MethodGet, "/", nil)
14421441
_, err = app.Test(req, TestConfig{
1443-
Timeout: -1,
1444-
FailOnTimeout: true,
1442+
Timeout: 0,
14451443
})
14461444
}()
14471445

Diff for: docs/api/app.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ func (app *App) SetTLSHandler(tlsHandler *TLSHandler)
641641

642642
## Test
643643

644-
Testing your application is done with the `Test` method. Use this method for creating `_test.go` files or when you need to debug your routing logic. The default timeout is `1s`; to disable a timeout altogether, pass a `TestConfig` struct with `Timeout: -1`.
644+
Testing your application is done with the `Test` method. Use this method for creating `_test.go` files or when you need to debug your routing logic. The default timeout is `1s`; to disable a timeout altogether, pass a `TestConfig` struct with `Timeout: 0`.
645645

646646
```go title="Signature"
647647
func (app *App) Test(req *http.Request, config ...TestConfig) (*http.Response, error)
@@ -706,8 +706,7 @@ cfg := fiber.TestConfig{
706706
}
707707
```
708708

709-
This would make a Test that instantly times out,
710-
which would always result in a "test: empty response" error.
709+
This would make a Test that has no timeout.
711710

712711
:::
713712

Diff for: docs/whats_new.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ We have made several changes to the Fiber app, including:
7676
### Methods changes
7777

7878
- Test -> Replaced timeout with a config parameter
79+
- -1 represents no timeout -> 0 represents no timeout
7980
- Listen -> has a config parameter
8081
- Listener -> has a config parameter
8182

@@ -201,7 +202,7 @@ app.Get("/", func(c fiber.Ctx) {
201202
// Define the HTTP request and custom TestConfig to test the handler
202203
req := httptest.NewRequest(MethodGet, "/", nil)
203204
testConfig := fiber.TestConfig{
204-
Timeout: -1,
205+
Timeout: 0,
205206
FailOnTimeout: false,
206207
}
207208

Diff for: middleware/keyauth/keyauth_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ import (
1515
const CorrectKey = "specials: !$%,.#\"!?~`<>@$^*(){}[]|/\\123"
1616

1717
var testConfig = fiber.TestConfig{
18-
Timeout: -1,
19-
FailOnTimeout: false,
18+
Timeout: 0,
2019
}
2120

2221
func Test_AuthSources(t *testing.T) {

0 commit comments

Comments
 (0)