Skip to content

Commit 7cb8756

Browse files
Add docs from gofiber/fiber@f8b490f
1 parent 8e1f463 commit 7cb8756

File tree

2 files changed

+91
-3
lines changed

2 files changed

+91
-3
lines changed

docs/core/api/app.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,10 +641,10 @@ 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 `-1` as the second argument.
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"
647-
func (app *App) Test(req *http.Request, msTimeout ...int) (*http.Response, error)
647+
func (app *App) Test(req *http.Request, config ...TestConfig) (*http.Response, error)
648648
```
649649

650650
```go title="Example"
@@ -685,6 +685,31 @@ func main() {
685685
}
686686
```
687687

688+
If not provided, TestConfig is set to the following defaults:
689+
690+
```go title="Default TestConfig"
691+
config := fiber.TestConfig{
692+
Timeout: time.Second(),
693+
FailOnTimeout: true,
694+
}
695+
```
696+
697+
:::caution
698+
699+
This is **not** the same as supplying an empty `TestConfig{}` to
700+
`app.Test(), but rather be the equivalent of supplying:
701+
702+
```go title="Empty TestConfig"
703+
cfg := fiber.TestConfig{
704+
Timeout: 0,
705+
FailOnTimeout: false,
706+
}
707+
```
708+
709+
This would make a Test that has no timeout.
710+
711+
:::
712+
688713
## Hooks
689714

690715
`Hooks` is a method to return the [hooks](./hooks.md) property.

docs/core/whats_new.md

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ We have made several changes to the Fiber app, including:
7575

7676
### Methods changes
7777

78-
- Test -> timeout changed to 1 second
78+
- 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

@@ -184,6 +185,68 @@ To enable the routing changes above we had to slightly adjust the signature of t
184185
+ Add(methods []string, path string, handler Handler, middleware ...Handler) Router
185186
```
186187

188+
### Test Config
189+
190+
The `app.Test()` method now allows users to customize their test configurations:
191+
192+
<details>
193+
<summary>Example</summary>
194+
195+
```go
196+
// Create a test app with a handler to test
197+
app := fiber.New()
198+
app.Get("/", func(c fiber.Ctx) {
199+
return c.SendString("hello world")
200+
})
201+
202+
// Define the HTTP request and custom TestConfig to test the handler
203+
req := httptest.NewRequest(MethodGet, "/", nil)
204+
testConfig := fiber.TestConfig{
205+
Timeout: 0,
206+
FailOnTimeout: false,
207+
}
208+
209+
// Test the handler using the request and testConfig
210+
resp, err := app.Test(req, testConfig)
211+
```
212+
213+
</details>
214+
215+
To provide configurable testing capabilities, we had to change
216+
the signature of the `Test` method.
217+
218+
```diff
219+
- Test(req *http.Request, timeout ...time.Duration) (*http.Response, error)
220+
+ Test(req *http.Request, config ...fiber.TestConfig) (*http.Response, error)
221+
```
222+
223+
The `TestConfig` struct provides the following configuration options:
224+
225+
- `Timeout`: The duration to wait before timing out the test. Use 0 for no timeout.
226+
- `FailOnTimeout`: Controls the behavior when a timeout occurs:
227+
- When true, the test will return an `os.ErrDeadlineExceeded` if the test exceeds the `Timeout` duration.
228+
- When false, the test will return the partial response received before timing out.
229+
230+
If a custom `TestConfig` isn't provided, then the following will be used:
231+
232+
```go
233+
testConfig := fiber.TestConfig{
234+
Timeout: time.Second,
235+
FailOnTimeout: true,
236+
}
237+
```
238+
239+
**Note:** Using this default is **NOT** the same as providing an empty `TestConfig` as an argument to `app.Test()`.
240+
241+
An empty `TestConfig` is the equivalent of:
242+
243+
```go
244+
testConfig := fiber.TestConfig{
245+
Timeout: 0,
246+
FailOnTimeout: false,
247+
}
248+
```
249+
187250
---
188251

189252
## 🧠 Context

0 commit comments

Comments
 (0)