Skip to content

Commit

Permalink
♻ Refactor: Change app.Test() and all uses to accept 0 as no timeout …
Browse files Browse the repository at this point in the history
…instead of -1
  • Loading branch information
grivera64 committed Nov 17, 2024
1 parent 6b6674f commit 334dbe8
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 12 deletions.
4 changes: 2 additions & 2 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ type TestConfig struct {

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

// Wait for callback
if cfg.Timeout >= 0 {
if cfg.Timeout > 0 {
// With timeout
select {
case err = <-channel:
Expand Down
6 changes: 2 additions & 4 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1126,8 +1126,7 @@ func Test_Test_Timeout(t *testing.T) {
app.Get("/", testEmptyHandler)

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

req := httptest.NewRequest(MethodGet, "/", nil)
_, err = app.Test(req, TestConfig{
Timeout: -1,
FailOnTimeout: true,
Timeout: 0,
})
}()

Expand Down
5 changes: 2 additions & 3 deletions docs/api/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ func (app *App) SetTLSHandler(tlsHandler *TLSHandler)

## Test

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`.
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`.

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

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

:::

Expand Down
3 changes: 2 additions & 1 deletion docs/whats_new.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ We have made several changes to the Fiber app, including:
### Methods changes

- Test -> Replaced timeout with a config parameter
- -1 represents no timeout -> 0 represents no timeout
- Listen -> has a config parameter
- Listener -> has a config parameter

Expand Down Expand Up @@ -201,7 +202,7 @@ app.Get("/", func(c fiber.Ctx) {
// Define the HTTP request and custom TestConfig to test the handler
req := httptest.NewRequest(MethodGet, "/", nil)
testConfig := fiber.TestConfig{
Timeout: -1,
Timeout: 0,
FailOnTimeout: false,
}

Expand Down
3 changes: 1 addition & 2 deletions middleware/keyauth/keyauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import (
const CorrectKey = "specials: !$%,.#\"!?~`<>@$^*(){}[]|/\\123"

var testConfig = fiber.TestConfig{
Timeout: -1,
FailOnTimeout: false,
Timeout: 0,
}

func Test_AuthSources(t *testing.T) {
Expand Down

0 comments on commit 334dbe8

Please sign in to comment.