apitest

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: AGPL-3.0 Imports: 15 Imported by: 0

Documentation

Overview

Package apitest boots GoRun for API tests and drives /api/v1 with a bearer API key. It is importable by any module's tests whose module comes after "api" in kernel.BootOrder (the import-order test also covers _test.go files).

Set kernel.LogOutput = io.Discard in TestMain to keep test output clean.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Boot

func Boot(t testing.TB, mods ...kernel.Module) *kernel.App

Boot boots GoRun on a temp data dir with the settings and auth modules followed by mods. Unless mods already contains a module named "api", the api module is inserted before the first module that comes after "api" in kernel.BootOrder (or appended). The app is closed when t ends.

func Data

func Data(body map[string]any) map[string]any

Data returns body["data"] as an object (nil if it is not one).

func ErrorCode

func ErrorCode(body map[string]any) string

ErrorCode returns body["error"]["code"] ("" if absent).

func URL

func URL(path string) string

URL maps a client path to the request path: "/api/…" is kept, "/v1/…" gets "/api" prepended, anything else gets "/api/v1" ("/content" → "/api/v1/content").

Types

type Client

type Client struct {
	Token string       // plaintext bearer key
	User  *auth.User   // key owner
	Key   *auth.APIKey // the key
	// contains filtered or unexported fields
}

Client calls the API of a booted app with a bearer key.

func New

func New(t testing.TB, app *kernel.App, o Options) *Client

New returns a client for a fresh user and key configured by o.

func NewClient

func NewClient(t testing.TB, app *kernel.App, scopes ...string) *Client

NewClient returns a client whose key belongs to a fresh admin user and carries scopes (default "*").

func (*Client) Anonymous

func (c *Client) Anonymous() *Client

Anonymous returns a copy of c that sends no credentials.

func (*Client) Delete

func (c *Client) Delete(path string, body any) (status int, resp map[string]any)

Delete performs DELETE path (body may be nil).

func (*Client) Do

func (c *Client) Do(method, path string, body any, header http.Header) *httptest.ResponseRecorder

Do sends a request with the bearer key (body: nil, []byte/string sent raw, anything else JSON-encoded) and returns the recorded response.

func (*Client) Get

func (c *Client) Get(path string) (status int, body map[string]any)

Get performs GET path and decodes the envelope.

func (*Client) Patch

func (c *Client) Patch(path string, body any) (status int, resp map[string]any)

Patch performs PATCH path with a JSON body.

func (*Client) Post

func (c *Client) Post(path string, body any) (status int, resp map[string]any)

Post performs POST path with a JSON body.

type Options

type Options struct {
	Role       string     // owner role, default "admin"
	Scopes     []string   // key scopes, default ["*"]
	RatePerMin int        // key rate limit, default 100000 (effectively unlimited)
	Kind       string     // auth.KeyKindAPI (default) or auth.KeyKindAgent
	User       *auth.User // existing key owner; nil creates a fresh user with Role
}

Options configure New.

Jump to

Keyboard shortcuts

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