Documentation
¶
Overview ¶
Package testkit provides small helpers for generated runtime integration tests.
Index ¶
- func AssertEmitted[E any](t testing.TB, events []contracts.EventEnvelope, ...)
- func AssertHeader(t testing.TB, handler http.Handler, method, requestPath, name, want string)
- func AssertNoEvents(t testing.TB, events []contracts.EventEnvelope)
- func AssertStatus(t testing.TB, handler http.Handler, method, requestPath, body string, want int)
- func CaptureCommandEvents[C, R any](t testing.TB, registry *contracts.Registry, command C) (R, []contracts.EventEnvelope)
- func ContractRegistry(register ...func(*contracts.Registry)) *contracts.Registry
- func Response(handler http.Handler, scenario Scenario) *httptest.ResponseRecorder
- func Run(t testing.TB, handler http.Handler, scenarios []Scenario)
- type Client
- func (client *Client) BaseURL() string
- func (client *Client) Close()
- func (client *Client) Do(t testing.TB, request Request) Result
- func (client *Client) Get(t testing.TB, path string) Result
- func (client *Client) PostForm(t testing.TB, path string, form url.Values) Result
- func (client *Client) PostJSON(t testing.TB, path string, value any) Result
- type Request
- type Result
- func (result Result) AssertBodyContains(t testing.TB, want string)
- func (result Result) AssertBodyEquals(t testing.TB, want string)
- func (result Result) AssertContentType(t testing.TB, want string)
- func (result Result) AssertCookie(t testing.TB, name string) *http.Cookie
- func (result Result) AssertHeader(t testing.TB, name, want string)
- func (result Result) AssertHeaderContains(t testing.TB, name, want string)
- func (result Result) AssertJSONEqual(t testing.TB, want any)
- func (result Result) AssertStatus(t testing.TB, want int)
- func (result Result) AssertStatusRange(t testing.TB, min, max int)
- type Scenario
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 ¶
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 ¶
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 ¶
ContractRegistry creates an in-memory registry for contract tests.
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
NewClient creates a cookie-preserving client that executes requests directly against handler without opening a listener.
func NewServerClient ¶ added in v0.10.0
NewServerClient creates a cookie-preserving client backed by an httptest server. Use this when behavior depends on the HTTP transport.
func (*Client) Close ¶ added in v0.10.0
func (client *Client) Close()
Close shuts down the backing test server, if one exists.
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 (Request) WithCookie ¶ added in v0.10.0
WithCookie returns a copy of request with a cookie attached.
func (Request) WithHeader ¶ added in v0.10.0
WithHeader returns a copy of request with a header set.
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
AssertBodyContains checks that the response body contains text.
func (Result) AssertBodyEquals ¶ added in v0.10.0
AssertBodyEquals checks the complete response body.
func (Result) AssertContentType ¶ added in v0.10.0
AssertContentType checks the response Content-Type prefix.
func (Result) AssertCookie ¶ added in v0.10.0
AssertCookie checks that a response Set-Cookie with name exists.
func (Result) AssertHeader ¶ added in v0.10.0
AssertHeader checks one exact response header value.
func (Result) AssertHeaderContains ¶ added in v0.10.0
AssertHeaderContains checks that one response header contains text.
func (Result) AssertJSONEqual ¶ added in v0.10.0
AssertJSONEqual compares the response body with want after JSON normalization.
func (Result) AssertStatus ¶ added in v0.10.0
AssertStatus checks the response status code.