apitest

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package apitest provides helpers for end-to-end HTTP tests that drive a real application handler through an httptest.Server. It removes the boilerplate of building requests, sending JSON, asserting status codes and decoding bodies.

srv := apitest.New(t, handler)
created := srv.PostJSON("/widgets", `{"name":"gadget"}`).
	ExpectStatus(http.StatusCreated).JSON()
id := created["id"].(string)
srv.Get("/widgets/"+id, apitest.WithBearer(token)).ExpectStatus(http.StatusOK)

It depends only on the standard library, so it is safe to use from any module's tests without pulling heavy dependencies.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*http.Request)

Option customizes an outbound request, e.g. to set headers.

func WithBearer

func WithBearer(token string) Option

WithBearer sets an Authorization: Bearer <token> header.

func WithHeader

func WithHeader(key, value string) Option

WithHeader sets a request header.

type Response

type Response struct {
	StatusCode int
	Header     http.Header
	Body       []byte
	// contains filtered or unexported fields
}

Response holds a completed HTTP response with its body buffered for repeated assertions and decoding.

func (*Response) Decode

func (r *Response) Decode(v any)

Decode unmarshals the JSON body into v, failing the test on error.

func (*Response) ExpectStatus

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

ExpectStatus fails the test unless the response status equals want. It returns the receiver for chaining.

func (*Response) JSON

func (r *Response) JSON() map[string]any

JSON decodes the body into a generic object map and returns it.

func (*Response) JSONArray

func (r *Response) JSONArray() []map[string]any

JSONArray decodes the body into a slice of generic object maps.

type Server

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

Server wraps an httptest.Server with JSON request and assertion helpers.

func New

func New(t *testing.T, handler http.Handler) *Server

New starts an httptest.Server for handler and registers its shutdown with t.Cleanup.

func (*Server) Delete

func (s *Server) Delete(path string, opts ...Option) *Response

Delete issues a DELETE request.

func (*Server) Do

func (s *Server) Do(method, path, body string, opts ...Option) *Response

Do sends method to path with an optional raw string body (JSON when non-empty) and returns the buffered response. It fails the test on transport errors.

func (*Server) Get

func (s *Server) Get(path string, opts ...Option) *Response

Get issues a GET request.

func (*Server) PostJSON

func (s *Server) PostJSON(path, body string, opts ...Option) *Response

PostJSON issues a POST with a JSON body.

func (*Server) PutJSON

func (s *Server) PutJSON(path, body string, opts ...Option) *Response

PutJSON issues a PUT with a JSON body.

Jump to

Keyboard shortcuts

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