diff --git a/docs/core/api/app.md b/docs/core/api/app.md index 8c7f8979bca..6582159c0f8 100644 --- a/docs/core/api/app.md +++ b/docs/core/api/app.md @@ -641,10 +641,10 @@ 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 `-1` as the second argument. +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, msTimeout ...int) (*http.Response, error) +func (app *App) Test(req *http.Request, config ...TestConfig) (*http.Response, error) ``` ```go title="Example" @@ -685,6 +685,31 @@ func main() { } ``` +If not provided, TestConfig is set to the following defaults: + +```go title="Default TestConfig" +config := fiber.TestConfig{ + Timeout: time.Second(), + FailOnTimeout: true, +} +``` + +:::caution + +This is **not** the same as supplying an empty `TestConfig{}` to +`app.Test(), but rather be the equivalent of supplying: + +```go title="Empty TestConfig" +cfg := fiber.TestConfig{ + Timeout: 0, + FailOnTimeout: false, +} +``` + +This would make a Test that has no timeout. + +::: + ## Hooks `Hooks` is a method to return the [hooks](./hooks.md) property. diff --git a/docs/core/whats_new.md b/docs/core/whats_new.md index 0a47dca4912..8677a0080f8 100644 --- a/docs/core/whats_new.md +++ b/docs/core/whats_new.md @@ -75,7 +75,8 @@ We have made several changes to the Fiber app, including: ### Methods changes -- Test -> timeout changed to 1 second +- 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 @@ -184,6 +185,68 @@ To enable the routing changes above we had to slightly adjust the signature of t + Add(methods []string, path string, handler Handler, middleware ...Handler) Router ``` +### Test Config + +The `app.Test()` method now allows users to customize their test configurations: + +
+Example + +```go +// Create a test app with a handler to test +app := fiber.New() +app.Get("/", func(c fiber.Ctx) { + return c.SendString("hello world") +}) + +// Define the HTTP request and custom TestConfig to test the handler +req := httptest.NewRequest(MethodGet, "/", nil) +testConfig := fiber.TestConfig{ + Timeout: 0, + FailOnTimeout: false, +} + +// Test the handler using the request and testConfig +resp, err := app.Test(req, testConfig) +``` + +
+ +To provide configurable testing capabilities, we had to change +the signature of the `Test` method. + +```diff +- Test(req *http.Request, timeout ...time.Duration) (*http.Response, error) ++ Test(req *http.Request, config ...fiber.TestConfig) (*http.Response, error) +``` + +The `TestConfig` struct provides the following configuration options: + +- `Timeout`: The duration to wait before timing out the test. Use 0 for no timeout. +- `FailOnTimeout`: Controls the behavior when a timeout occurs: + - When true, the test will return an `os.ErrDeadlineExceeded` if the test exceeds the `Timeout` duration. + - When false, the test will return the partial response received before timing out. + +If a custom `TestConfig` isn't provided, then the following will be used: + +```go +testConfig := fiber.TestConfig{ + Timeout: time.Second, + FailOnTimeout: true, +} +``` + +**Note:** Using this default is **NOT** the same as providing an empty `TestConfig` as an argument to `app.Test()`. + +An empty `TestConfig` is the equivalent of: + +```go +testConfig := fiber.TestConfig{ + Timeout: 0, + FailOnTimeout: false, +} +``` + --- ## 🧠 Context