servertest

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package servertest provides helpers for testing tyche routers and handlers with the standard library's httptest, removing the boilerplate of building requests and unwrapping the standard DataResponse envelope.

client := servertest.New(t, router)
resp := client.POST("/v1/users", User{Name: "Ada"})
resp.AssertStatus(http.StatusCreated)
got := servertest.DecodeData[User](t, resp)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode[T any](tb testing.TB, r *Response) T

Decode decodes the raw response body into dst (no envelope unwrapping). It fails the test on a decoding error.

func DecodeData

func DecodeData[T any](tb testing.TB, r *Response) T

DecodeData decodes the response body as the standard {"data": T} envelope and returns the unwrapped value. It fails the test on a decoding error.

Types

type Client

type Client struct {

	// Header holds default headers applied to every request. Per-request
	// headers set via WithHeader take precedence.
	Header http.Header
	// contains filtered or unexported fields
}

Client issues requests against an http.Handler (typically a *server.API) using httptest, recording responses for assertions.

func New

func New(tb testing.TB, handler http.Handler) *Client

New returns a Client that drives handler. tb is used to fail tests on request-construction or decoding errors.

func (*Client) DELETE

func (c *Client) DELETE(target string, opts ...RequestOption) *Response

DELETE issues a DELETE request.

func (*Client) Do

func (c *Client) Do(method, target string, body any, opts ...RequestOption) *Response

Do builds a request for the given method and target, marshaling body per the rules of [encodeBody], serves it, and returns the recorded Response.

func (*Client) GET

func (c *Client) GET(target string, opts ...RequestOption) *Response

GET issues a GET request.

func (*Client) PATCH

func (c *Client) PATCH(target string, body any, opts ...RequestOption) *Response

PATCH issues a PATCH request with the given body.

func (*Client) POST

func (c *Client) POST(target string, body any, opts ...RequestOption) *Response

POST issues a POST request with the given body.

func (*Client) PUT

func (c *Client) PUT(target string, body any, opts ...RequestOption) *Response

PUT issues a PUT request with the given body.

type Problem

type Problem struct {
	Type   string              `json:"type"`
	Title  string              `json:"title"`
	Detail string              `json:"detail"`
	Errors []ValidationProblem `json:"errors"`
	Status int                 `json:"status"`
}

Problem is the RFC 9457 problem+json shape that tyche emits for errors.

func DecodeProblem

func DecodeProblem(tb testing.TB, r *Response) Problem

DecodeProblem decodes the response body as a problem+json document. It fails the test on a decoding error.

type RequestOption

type RequestOption func(*http.Request)

RequestOption mutates a request before it is served, e.g. to set headers.

func WithBearerToken

func WithBearerToken(token string) RequestOption

WithBearerToken sets an Authorization: Bearer header.

func WithHeader

func WithHeader(key, value string) RequestOption

WithHeader sets a request header.

type Response

type Response struct {
	*httptest.ResponseRecorder
	// contains filtered or unexported fields
}

Response wraps a recorded response with assertion and decoding helpers.

func (*Response) AssertStatus

func (r *Response) AssertStatus(want int) *Response

AssertStatus fails the test if the response status does not match want. It returns the Response for chaining.

type ValidationProblem

type ValidationProblem struct {
	Pointer string `json:"pointer"`
	Code    string `json:"code"`
	Message string `json:"message"`
}

ValidationProblem is a single field-level validation error.

Jump to

Keyboard shortcuts

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