testkit

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2026 License: MPL-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package testkit provides small helpers for generated runtime integration tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertEmitted

func AssertEmitted[E any](t testing.TB, events []contracts.EventEnvelope, category contracts.EventCategory, check func(E))

AssertEmitted finds one captured event with the requested category and type.

func AssertHeader

func AssertHeader(t testing.TB, handler http.Handler, method, requestPath, name, want string)

AssertHeader checks one response header value.

func AssertNoEvents

func AssertNoEvents(t testing.TB, events []contracts.EventEnvelope)

AssertNoEvents fails when a command unexpectedly emitted events.

func AssertStatus

func AssertStatus(t testing.TB, handler http.Handler, method, requestPath, body string, want int)

AssertStatus checks one request's response status.

func CaptureCommandEvents

func CaptureCommandEvents[C, R any](t testing.TB, registry *contracts.Registry, command C) (R, []contracts.EventEnvelope)

CaptureCommandEvents runs a command and fails the test on command errors.

func ContractRegistry

func ContractRegistry(register ...func(*contracts.Registry)) *contracts.Registry

ContractRegistry creates an in-memory registry for contract tests.

func Response

func Response(handler http.Handler, scenario Scenario) *httptest.ResponseRecorder

Response executes one scenario and returns the recorder for custom checks.

func Run

func Run(t testing.TB, handler http.Handler, scenarios []Scenario)

Run executes scenarios against handler.

Types

type Client added in v0.10.0

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

Client drives generated app handlers with a cookie jar. It can execute requests directly against an http.Handler or through an httptest.Server.

func NewClient added in v0.10.0

func NewClient(t testing.TB, handler http.Handler) *Client

NewClient creates a cookie-preserving client that executes requests directly against handler without opening a listener.

func NewServerClient added in v0.10.0

func NewServerClient(t testing.TB, handler http.Handler) *Client

NewServerClient creates a cookie-preserving client backed by an httptest server. Use this when behavior depends on the HTTP transport.

func (*Client) BaseURL added in v0.10.0

func (client *Client) BaseURL() string

BaseURL returns the client's absolute base URL.

func (*Client) Close added in v0.10.0

func (client *Client) Close()

Close shuts down the backing test server, if one exists.

func (*Client) Do added in v0.10.0

func (client *Client) Do(t testing.TB, request Request) Result

Do executes one request and returns the captured result.

func (*Client) Get added in v0.10.0

func (client *Client) Get(t testing.TB, path string) Result

Get executes a GET request.

func (*Client) PostForm added in v0.10.0

func (client *Client) PostForm(t testing.TB, path string, form url.Values) Result

PostForm executes a form POST request.

func (*Client) PostJSON added in v0.10.0

func (client *Client) PostJSON(t testing.TB, path string, value any) Result

PostJSON executes a JSON POST request.

type Request added in v0.10.0

type Request struct {
	Method     string
	Path       string
	Body       string
	Headers    map[string]string
	Query      url.Values
	Form       url.Values
	JSON       any
	Cookies    []*http.Cookie
	Host       string
	RemoteAddr string
	Context    context.Context
}

Request describes one generated-handler test request.

func Get added in v0.10.0

func Get(path string) Request

Get builds a GET request.

func PostForm added in v0.10.0

func PostForm(path string, form url.Values) Request

PostForm builds an application/x-www-form-urlencoded POST request.

func PostJSON added in v0.10.0

func PostJSON(path string, value any) Request

PostJSON builds an application/json POST request.

func (Request) WithCookie added in v0.10.0

func (request Request) WithCookie(cookie *http.Cookie) Request

WithCookie returns a copy of request with a cookie attached.

func (Request) WithHeader added in v0.10.0

func (request Request) WithHeader(name, value string) Request

WithHeader returns a copy of request with a header set.

func (Request) WithQuery added in v0.10.0

func (request Request) WithQuery(name, value string) Request

WithQuery returns a copy of request with a query value appended.

type Result added in v0.10.0

type Result struct {
	StatusCode int
	Header     http.Header
	Body       string
	Cookies    []*http.Cookie
	Request    Request
}

Result captures one test response.

func (Result) AssertBodyContains added in v0.10.0

func (result Result) AssertBodyContains(t testing.TB, want string)

AssertBodyContains checks that the response body contains text.

func (Result) AssertBodyEquals added in v0.10.0

func (result Result) AssertBodyEquals(t testing.TB, want string)

AssertBodyEquals checks the complete response body.

func (Result) AssertContentType added in v0.10.0

func (result Result) AssertContentType(t testing.TB, want string)

AssertContentType checks the response Content-Type prefix.

func (Result) AssertCookie added in v0.10.0

func (result Result) AssertCookie(t testing.TB, name string) *http.Cookie

AssertCookie checks that a response Set-Cookie with name exists.

func (Result) AssertHeader added in v0.10.0

func (result Result) AssertHeader(t testing.TB, name, want string)

AssertHeader checks one exact response header value.

func (Result) AssertHeaderContains added in v0.10.0

func (result Result) AssertHeaderContains(t testing.TB, name, want string)

AssertHeaderContains checks that one response header contains text.

func (Result) AssertJSONEqual added in v0.10.0

func (result Result) AssertJSONEqual(t testing.TB, want any)

AssertJSONEqual compares the response body with want after JSON normalization.

func (Result) AssertStatus added in v0.10.0

func (result Result) AssertStatus(t testing.TB, want int)

AssertStatus checks the response status code.

func (Result) AssertStatusRange added in v0.10.0

func (result Result) AssertStatusRange(t testing.TB, min, max int)

AssertStatusRange checks that the response status code is within [min, max].

type Scenario

type Scenario struct {
	Name             string
	Method           string
	Path             string
	Body             string
	Headers          map[string]string
	WantStatus       int
	WantHeader       map[string]string
	WantBodyContains string
}

Scenario describes one HTTP expectation against a generated app handler.

Jump to

Keyboard shortcuts

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