Skip to content

Commit

Permalink
Add docs from gofiber/fiber@f8b490f
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Nov 22, 2024
1 parent 8e1f463 commit 7cb8756
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 3 deletions.
29 changes: 27 additions & 2 deletions docs/core/api/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down
65 changes: 64 additions & 1 deletion docs/core/whats_new.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:

<details>
<summary>Example</summary>

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

</details>

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
Expand Down

0 comments on commit 7cb8756

Please sign in to comment.