fibrtest

package
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 14, 2026 License: MIT Imports: 14 Imported by: 0

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewDB

func NewDB(t TB) *bun.DB

NewDB returns a fresh in-memory SQLite Bun DB. When t supports Cleanup (a real *testing.T), the DB is closed automatically at test end; otherwise the caller is responsible for closing it. A connection error calls Fatalf.

func Token

func Token(t TB, secret string, claims jwt.MapClaims) string

Token mints a signed JWT with the given claims, for testing authed routes without going through a login flow. A signing error calls Fatalf.

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 New

func New(t TB, app *fiber.App) *Client

New returns a Client for app.

func (*Client) Delete

func (c *Client) Delete(path string) *Response

Delete sends a DELETE request.

func (*Client) Get

func (c *Client) Get(path string) *Response

Get sends a GET request.

func (*Client) Patch

func (c *Client) Patch(path string, body any) *Response

Patch sends a PATCH request; a non-nil body is JSON-encoded.

func (*Client) Post

func (c *Client) Post(path string, body any) *Response

Post sends a POST request; a non-nil body is JSON-encoded.

func (*Client) Put

func (c *Client) Put(path string, body any) *Response

Put sends a PUT request; a non-nil body is JSON-encoded.

func (*Client) Request

func (c *Client) Request(method, path string) *Request

Request starts a builder for full control over a single request.

func (*Client) WithBearer

func (c *Client) WithBearer(token string) *Client

WithBearer returns a copy of the Client that sends "Authorization: Bearer <token>" on every request.

func (*Client) WithHeader

func (c *Client) WithHeader(key, value string) *Client

WithHeader returns a copy of the Client that sends the given header on every request.

func (*Client) WithTimeout

func (c *Client) WithTimeout(d time.Duration) *Client

WithTimeout returns a copy of the Client using the given per-request timeout.

type Request

type Request struct {
	// contains filtered or unexported fields
}

Request builds a single HTTP request against the Client's app.

func (*Request) Bearer

func (r *Request) Bearer(token string) *Request

Bearer sets the Authorization header for this request.

func (*Request) Body

func (r *Request) Body(raw []byte, contentType string) *Request

Body sets a raw body and content type.

func (*Request) Do

func (r *Request) Do() *Response

Do builds the request, runs it via app.Test, and returns the Response. A transport error calls Fatalf.

func (*Request) Header

func (r *Request) Header(key, value string) *Request

Header adds a per-request header (overrides the Client default for that key).

func (*Request) JSON

func (r *Request) JSON(v any) *Request

JSON sets the body to the JSON encoding of v with content type application/json. A marshal error calls Fatalf.

func (*Request) Query

func (r *Request) Query(key, value string) *Request

Query adds a query parameter.

type Response

type Response struct {
	Raw  *http.Response
	Body []byte
	// contains filtered or unexported fields
}

Response is an executed request's result. Body is read once so it can be inspected repeatedly.

func (*Response) ExpectStatus

func (r *Response) ExpectStatus(code int) *Response

ExpectStatus calls Fatalf unless the status equals code. It returns the Response for chaining.

func (*Response) JSON

func (r *Response) JSON(out any) *Response

JSON decodes the body into out, calling Fatalf on error. It returns the Response for chaining.

func (*Response) Status

func (r *Response) Status() int

Status returns the HTTP status code.

func (*Response) Text

func (r *Response) Text() string

Text returns the body as a string.

type TB

type TB interface {
	Helper()
	Fatalf(format string, args ...any)
}

TB is the subset of *testing.T the harness needs, so callers pass a real *testing.T and tests can supply a stub. (testing.TB cannot be implemented outside the testing package.)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL