Documentation
¶
Overview ¶
Package fibrtest provides test helpers for fibr/Fiber apps: a fluent HTTP client over *fiber.App, response assertions, a JWT minting helper, and an in-memory Bun DB helper.
Index ¶
- func NewDB(t TB) *bun.DB
- func Token(t TB, secret string, claims jwt.MapClaims) string
- type Client
- func (c *Client) Delete(path string) *Response
- func (c *Client) Get(path string) *Response
- func (c *Client) Patch(path string, body any) *Response
- func (c *Client) Post(path string, body any) *Response
- func (c *Client) Put(path string, body any) *Response
- func (c *Client) Request(method, path string) *Request
- func (c *Client) WithBearer(token string) *Client
- func (c *Client) WithHeader(key, value string) *Client
- func (c *Client) WithTimeout(d time.Duration) *Client
- type Request
- type Response
- type TB
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client drives a Fiber app under test.
Example ¶
Drive a Fiber app under test with a few lines: build the request, assert the status, and decode the JSON response.
package main
import (
"fmt"
"testing"
"github.com/gofiber/fiber/v2"
"github.com/rahmadafandi/fibr/fibrtest"
)
func main() {
app := fiber.New()
app.Post("/greet", func(c *fiber.Ctx) error {
var in struct {
Name string `json:"name"`
}
if err := c.BodyParser(&in); err != nil {
return c.SendStatus(fiber.StatusBadRequest)
}
return c.Status(fiber.StatusCreated).JSON(fiber.Map{"hello": in.Name})
})
// In a real test this is your *testing.T.
t := &testing.T{}
c := fibrtest.New(t, app)
var out struct {
Hello string `json:"hello"`
}
c.Post("/greet", map[string]string{"name": "ada"}).ExpectStatus(201).JSON(&out)
fmt.Println(out.Hello)
}
Output: ada
func (*Client) WithBearer ¶
WithBearer returns a copy of the Client that sends "Authorization: Bearer <token>" on every request.
func (*Client) WithHeader ¶
WithHeader returns a copy of the Client that sends the given header on every request.
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
Request builds a single HTTP request against the Client's app.
func (*Request) Do ¶
Do builds the request, runs it via app.Test, and returns the Response. A transport error calls Fatalf.
func (*Request) Header ¶
Header adds a per-request header (overrides the Client default for that key).
type Response ¶
Response is an executed request's result. Body is read once so it can be inspected repeatedly.
func (*Response) ExpectStatus ¶
ExpectStatus calls Fatalf unless the status equals code. It returns the Response for chaining.