fixture

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 31, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package fixture provides a declarative, in-process HTTP test server for per-test scenarios that exercise HTTP-level behaviour (status codes, redirects, slow responses, charset declarations, header echoes, etc.).

Lifecycle — "build then go":

srv := fixture.New().
    Route("GET", "/status/406", fixture.Status(406)).
    Route("GET", "/redirect",   fixture.Redirect("/final", 301)).
    Route("GET", "/final",      fixture.Body([]byte("ok"), "text/plain"))
defer srv.Close()
resp, err := http.Get(srv.URL() + "/redirect")

Call Route as many times as needed before the first call to URL(). Each Route mutates the underlying mux directly; once URL() is read, the server is considered live and additional Route calls — while not forbidden by the runtime — are racy and should be avoided.

The server binds to 127.0.0.1 via net/http/httptest and chooses a free port automatically. It only depends on the standard library and serves plain HTTP (no TLS). For TLS scenarios add a NewTLS constructor later.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Body

func Body(body []byte, contentType string) http.HandlerFunc

Body returns a handler that writes body with the given Content-Type and a 200 status. Passing an empty contentType skips the header (letting Go's default content sniffing apply).

func Charset

func Charset(label string, body []byte) http.HandlerFunc

Charset returns a handler that serves body verbatim with Content-Type: text/html; charset=<label>. The label is written exactly as given (e.g. "iso-8859-1", "UTF-8", "windows-1252") so engine charset detection has a real declared encoding to react to.

func Delay

func Delay(d time.Duration, inner http.Handler) http.HandlerFunc

Delay wraps inner with a time.Sleep before delegating. Composable with any other handler — Delay(2*time.Second, Status(200)) yields a slow 200.

func Headers

func Headers(h http.Header, inner http.Handler) http.HandlerFunc

Headers wraps inner with extra response headers. Headers from h are added (not replaced) before inner runs so inner can still override individual values via w.Header().Set.

func Redirect

func Redirect(location string, code int) http.HandlerFunc

Redirect returns a handler that writes a Location header and the given redirect status code (e.g. 301, 302, 307, 308). No body is written so downstream cache/dedupe logic doesn't see a redirect blob.

func Status

func Status(code int) http.HandlerFunc

Status returns a handler that writes the given HTTP status code with an empty body. Useful for /status/406, /status/429, etc.

Types

type Server

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

Server wraps an *httptest.Server and exposes a fluent route DSL.

func New

func New() *Server

New constructs a fixture Server with an empty mux and starts the underlying httptest.Server immediately so URL() is valid right away.

func (*Server) Close

func (s *Server) Close()

Close stops the underlying httptest.Server and releases its port.

func (*Server) Route

func (s *Server) Route(method, path string, h http.HandlerFunc) *Server

Route registers a handler for the given method and path. The method is matched exactly; mismatched methods receive a 405 response.

Routing uses Go 1.22+ method-prefixed ServeMux patterns (e.g. "GET /status/406"), which gives free 405 handling for the same path bound to a different method.

func (*Server) URL

func (s *Server) URL() string

URL returns the base URL of the running server, e.g. "http://127.0.0.1:54321".

Jump to

Keyboard shortcuts

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