apitest

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package apitest provides testing helpers for HTTP handlers built with apikit.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RequestBuilder

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

RequestBuilder provides a fluent API to construct *http.Request for tests.

func NewRequest

func NewRequest(method, target string) *RequestBuilder

NewRequest creates a new RequestBuilder with the given method and target path.

func (*RequestBuilder) Build

func (b *RequestBuilder) Build() *http.Request

Build constructs the *http.Request. It panics if JSON marshaling fails.

func (*RequestBuilder) WithBearerToken

func (b *RequestBuilder) WithBearerToken(token string) *RequestBuilder

WithBearerToken sets the Authorization header with a Bearer token.

func (*RequestBuilder) WithBody

func (b *RequestBuilder) WithBody(v any) *RequestBuilder

WithBody sets the request body. The value will be JSON-marshaled.

func (*RequestBuilder) WithContext

func (b *RequestBuilder) WithContext(ctx context.Context) *RequestBuilder

WithContext sets the request context.

func (*RequestBuilder) WithHeader

func (b *RequestBuilder) WithHeader(key, value string) *RequestBuilder

WithHeader adds a header to the request.

func (*RequestBuilder) WithPathValue

func (b *RequestBuilder) WithPathValue(key, value string) *RequestBuilder

WithPathValue sets a path parameter value (Go 1.22+ routing).

func (*RequestBuilder) WithQuery

func (b *RequestBuilder) WithQuery(key, value string) *RequestBuilder

WithQuery adds a query parameter to the request.

type Response

type Response struct {
	StatusCode int
	Headers    http.Header
	Body       []byte
}

Response wraps a recorded HTTP response for easy inspection and assertions.

func Record

func Record(handler http.Handler, req *http.Request) *Response

Record executes an http.Handler with the given request and returns a *Response.

func RecordHandler

func RecordHandler(fn response.HandlerFunc, req *http.Request) *Response

RecordHandler wraps a response.HandlerFunc with response.Handle and records the result.

func (*Response) AssertBodyContains

func (r *Response) AssertBodyContains(t *testing.T, substr string)

AssertBodyContains asserts that the response body contains the given substring.

func (*Response) AssertError

func (r *Response) AssertError(t *testing.T, code string)

AssertError asserts that the response has success=false and the error code matches.

func (*Response) AssertHeader

func (r *Response) AssertHeader(t *testing.T, key, expected string)

AssertHeader asserts that the response header matches the expected value.

func (*Response) AssertStatus

func (r *Response) AssertStatus(t *testing.T, expected int)

AssertStatus asserts that the response status code matches the expected value.

func (*Response) AssertSuccess

func (r *Response) AssertSuccess(t *testing.T)

AssertSuccess asserts that the response has a 2xx status code and success=true in the envelope.

func (*Response) AssertValidationError

func (r *Response) AssertValidationError(t *testing.T, field string)

AssertValidationError asserts that the response contains a validation error with the given field name present in the error fields.

func (*Response) Decode

func (r *Response) Decode(v any) error

Decode unmarshals the response body into v.

func (*Response) Envelope

func (r *Response) Envelope() (*response.Envelope, error)

Envelope decodes the response body as a response.Envelope.

func (*Response) Header

func (r *Response) Header(key string) string

Header returns the first value for the given header key.

Jump to

Keyboard shortcuts

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