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 ¶
- func Decode[T any](tb testing.TB, r *Response) T
- func DecodeData[T any](tb testing.TB, r *Response) T
- type Client
- func (c *Client) DELETE(target string, opts ...RequestOption) *Response
- func (c *Client) Do(method, target string, body any, opts ...RequestOption) *Response
- func (c *Client) GET(target string, opts ...RequestOption) *Response
- func (c *Client) PATCH(target string, body any, opts ...RequestOption) *Response
- func (c *Client) POST(target string, body any, opts ...RequestOption) *Response
- func (c *Client) PUT(target string, body any, opts ...RequestOption) *Response
- type Problem
- type RequestOption
- type Response
- type ValidationProblem
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 ¶
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.
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.
type RequestOption ¶
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 ¶
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.