Documentation
¶
Overview ¶
Package http provides test-owned HTTP servers and bounded clients. Requests traverse the supplied production handler, including its authentication and middleware. It does not inject identities or replace response contracts.
Index ¶
- Constants
- func AssertCookie(t testing.TB, response httpclient.Response, name string) *stdhttp.Cookie
- func AssertHeader(t testing.TB, response httpclient.Response, name, want string)
- func AssertJSONPath[T any](t testing.TB, response httpclient.Response, pointer string, want T)
- func AssertNoContent(t testing.TB, response httpclient.Response)
- func AssertRedirect(t testing.TB, response httpclient.Response, location string)
- func AssertStatus(t testing.TB, response httpclient.Response, want int)
- func AssertValidationErrors(t testing.TB, response httpclient.Response, fields ...string)
- func DecodeJSON[T any](ctx context.Context, response httpclient.Response, descriptor contract.JSON[T]) (T, error)
- func JSON[T any](ctx context.Context, request httpclient.Request, descriptor contract.JSON[T], ...) (httpclient.Request, error)
- type Client
- func ActingAsSession[M model.Identifiable, K any](t testing.TB, client *Client, sessions *session.Sessions[M, K], ...) *Client
- func ActingAsToken[M model.Identifiable, K any](t testing.TB, client *Client, tokens *token.Tokens[M, K], ...) *Client
- func Connect(t testing.TB, url string, configure ...Option) *Client
- func New(t testing.TB, handler stdhttp.Handler, configure ...Option) *Client
- type Option
Constants ¶
const DefaultTimeout = 5 * time.Second
DefaultTimeout bounds each test request, including its response body.
Variables ¶
This section is empty.
Functions ¶
func AssertCookie ¶
AssertCookie requires exactly one Set-Cookie for name and returns it for attribute checks. Secret values are never printed.
func AssertHeader ¶
func AssertHeader(t testing.TB, response httpclient.Response, name, want string)
AssertHeader compares a response header's first value. An absent header matches only an empty want.
func AssertJSONPath ¶
AssertJSONPath compares the JSON value at an RFC 6901 pointer (for example "/data/items/0/name"; "" selects the document) with want encoded by Go's JSON encoder. Object key order and insignificant whitespace are ignored. Prefer DecodeJSON with the generated DTO contract for complete responses.
func AssertNoContent ¶
func AssertNoContent(t testing.TB, response httpclient.Response)
AssertNoContent requires status 204 and an empty body.
func AssertRedirect ¶
func AssertRedirect(t testing.TB, response httpclient.Response, location string)
AssertRedirect requires a 3xx status and an exact Location. The test client never follows redirects, so the original response is inspected.
func AssertStatus ¶
func AssertStatus(t testing.TB, response httpclient.Response, want int)
AssertStatus reports a test error when the response status differs from want.
func AssertValidationErrors ¶
func AssertValidationErrors(t testing.TB, response httpclient.Response, fields ...string)
AssertValidationErrors requires the framework's 422 validation envelope with an issue at every listed field. Fields are wire JSON pointers such as "/email"; a missing leading slash is added. Issue messages are not compared.
func DecodeJSON ¶
func DecodeJSON[T any](ctx context.Context, response httpclient.Response, descriptor contract.JSON[T]) (T, error)
DecodeJSON validates the complete response against its generated DTO contract. HTTP success/status assertions remain explicit.
Types ¶
type Client ¶
type Client struct {
*httpclient.Client
URL string
// contains filtered or unexported fields
}
Client embeds the production bounded client. Prefer relative request paths; URL also supports a separate realtime client against this owned server.
func ActingAsSession ¶
func ActingAsSession[M model.Identifiable, K any](t testing.TB, client *Client, sessions *session.Sessions[M, K], proof auth.Proof[M, K], options session.IssueOptions, cookie foundryhttp.Cookie[secret.String]) *Client
ActingAsSession issues a real session through the configured session binding and returns an independent client sending it in the application's declared credential cookie. Cookie-authenticated unsafe requests still require the application's origin/CSRF policy; the helper does not bypass it.
func ActingAsToken ¶
func ActingAsToken[M model.Identifiable, K any](t testing.TB, client *Client, tokens *token.Tokens[M, K], proof auth.Proof[M, K], options token.IssueOptions[M]) *Client
ActingAsToken issues a real access token for proof through the application's configured token binding, then returns an independent client that sends it as a bearer credential. Every request still runs token verification, provider eligibility, scopes and policies; the helper grants nothing by itself. Build proof with auth.NewProof for the actor and assurance the test represents.
func Connect ¶
Connect owns a bounded client for an already-running test HTTP application. The caller owns its server; register application cleanup before connecting.
type Option ¶
type Option func(*options) error
Option configures a test HTTP client.
func WithHeaders ¶
WithHeaders sends these headers on every request. Each named header replaces the values a derived client inherits (see With) instead of adding a second value, so re-authenticating never sends two credentials. Values are copied; the production client still bounds and validates them.
func WithTimeout ¶
WithTimeout replaces DefaultTimeout for slow handlers, such as streaming or large fixtures. It must be positive and at most one hour.