Documentation
¶
Overview ¶
Package apitest provides testing helpers for HTTP handlers built with apikit.
Index ¶
- type RequestBuilder
- func (b *RequestBuilder) Build() *http.Request
- func (b *RequestBuilder) WithBearerToken(token string) *RequestBuilder
- func (b *RequestBuilder) WithBody(v any) *RequestBuilder
- func (b *RequestBuilder) WithContext(ctx context.Context) *RequestBuilder
- func (b *RequestBuilder) WithHeader(key, value string) *RequestBuilder
- func (b *RequestBuilder) WithPathValue(key, value string) *RequestBuilder
- func (b *RequestBuilder) WithQuery(key, value string) *RequestBuilder
- type Response
- func (r *Response) AssertBodyContains(t *testing.T, substr string)
- func (r *Response) AssertError(t *testing.T, code string)
- func (r *Response) AssertHeader(t *testing.T, key, expected string)
- func (r *Response) AssertStatus(t *testing.T, expected int)
- func (r *Response) AssertSuccess(t *testing.T)
- func (r *Response) AssertValidationError(t *testing.T, field string)
- func (r *Response) Decode(v any) error
- func (r *Response) Envelope() (*response.Envelope, error)
- func (r *Response) Header(key string) string
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 ¶
Response wraps a recorded HTTP response for easy inspection and assertions.
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 ¶
AssertBodyContains asserts that the response body contains the given substring.
func (*Response) AssertError ¶
AssertError asserts that the response has success=false and the error code matches.
func (*Response) AssertHeader ¶
AssertHeader asserts that the response header matches the expected value.
func (*Response) AssertStatus ¶
AssertStatus asserts that the response status code matches the expected value.
func (*Response) AssertSuccess ¶
AssertSuccess asserts that the response has a 2xx status code and success=true in the envelope.
func (*Response) AssertValidationError ¶
AssertValidationError asserts that the response contains a validation error with the given field name present in the error fields.