Help us shape the future of Go! Take the 2026 Go Developer Survey

http

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 21 Imported by: 0

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

View Source
const DefaultTimeout = 5 * time.Second

DefaultTimeout bounds each test request, including its response body.

Variables

This section is empty.

Functions

func AssertCookie

func AssertCookie(t testing.TB, response httpclient.Response, name string) *stdhttp.Cookie

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

func AssertJSONPath[T any](t testing.TB, response httpclient.Response, pointer string, want T)

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.

func JSON

func JSON[T any](ctx context.Context, request httpclient.Request, descriptor contract.JSON[T], payload T) (httpclient.Request, error)

JSON uses the same generated request contract as production outbound calls.

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

func Connect(t testing.TB, url string, configure ...Option) *Client

Connect owns a bounded client for an already-running test HTTP application. The caller owns its server; register application cleanup before connecting.

func New

func New(t testing.TB, handler stdhttp.Handler, configure ...Option) *Client

New registers cleanup before starting the server. The handler must propagate request cancellation and return from its work before server cleanup completes. Register application cleanup before this helper so clients close first.

func (*Client) With

func (c *Client) With(t testing.TB, configure ...Option) *Client

With returns an independent client for the same server with additional options, such as credential headers. This client remains unchanged.

type Option

type Option func(*options) error

Option configures a test HTTP client.

func WithHeaders

func WithHeaders(headers stdhttp.Header) Option

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

func WithTimeout(timeout time.Duration) Option

WithTimeout replaces DefaultTimeout for slow handlers, such as streaming or large fixtures. It must be positive and at most one hour.

Jump to

Keyboard shortcuts

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