fakeserver

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: AGPL-3.0 Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package fakeserver is the HTTP harness the fake servers are built on: it records every request with the status it was answered with, answers scripted faults and serves the control endpoints under /_fake/. The same servers run in Go tests and in `muster dev`.

Index

Constants

View Source
const (
	// ControlPrefix starts the paths of the control endpoints; requests below it are neither recorded nor faulted.
	ControlPrefix = "/_fake/"
	// MaxRequests is how many requests a server keeps; the oldest are dropped first.
	MaxRequests = 10_000
	// MaxBodyBytes is how much of a request body is recorded; the handler still receives the whole body.
	MaxBodyBytes = 1 << 20
)
View Source
const (
	EncodingUTF8   = "utf8"
	EncodingBase64 = "base64"
)

Variables

This section is empty.

Functions

func DecodeJSON

func DecodeJSON(w http.ResponseWriter, r *http.Request, v any) error

DecodeJSON reads a control request's body as exactly one JSON value into v, rejecting unknown fields.

func WriteError

func WriteError(w http.ResponseWriter, status int, msg string)

WriteError answers a control request with {"error": msg}.

func WriteJSON

func WriteJSON(w http.ResponseWriter, status int, v any)

Types

type Fault

type Fault struct {
	Path              string `json:"path"`
	Status            int    `json:"status"`
	RetryAfterSeconds int    `json:"retry_after_seconds"`
	DelayMs           int    `json:"delay_ms"`
	Body              string `json:"body"`
	ContentType       string `json:"content_type"`
	Location          string `json:"location"`
	Times             int    `json:"times"`
}

Fault is a scripted answer for the requests to a path. With a Status, a fault waits DelayMs and answers that status with Body, as ContentType when it is set and otherwise as JSON when Body is valid JSON and as text when it is not, and with the Location header when Location is set. Without a Status, the fake answers as usual, but the answer is held for DelayMs after the fake made it: what the request changed is done even when the client gives up waiting.

Path is an exact path or a path.Match pattern, in which `*` matches any run of characters within one segment (`?` and `[...]` work as there too). An exact path wins over patterns, and among patterns the one added first wins. Times is how many requests the fault hits before it is removed; 0 is every request until the faults are reset.

func (Fault) Validate

func (f Fault) Validate() error

type Request

type Request struct {
	Method  string              `json:"method"`
	Path    string              `json:"path"`
	Query   string              `json:"query"`
	Headers map[string][]string `json:"headers"`
	// Body is the body as text, or in base64 when BodyEncoding says so because it is not valid UTF-8.
	Body         string `json:"body"`
	BodyEncoding string `json:"body_encoding"`
	AtMs         int64  `json:"at_ms"`
	// Status is the status the request was answered with, filled in once the answer is written; it is 0 while the
	// request is served and when no answer was written: the client gave up during a fault's delay, or the handler
	// aborted.
	Status int `json:"status"`
}

Request is a recorded request as GET /_fake/requests lists it.

type Server

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

Server wraps a fake's own handler with recording, faults and the control endpoints.

func New

func New(name string, h http.Handler) *Server

New returns a server named after the system it fakes, such as "Mattermost", that answers with h.

func (*Server) Close

func (s *Server) Close(ctx context.Context) error

Close cancels the context of every request, so running handlers stop and held delays abort their responses, closes the connections that have not sent a request, and shuts the server down gracefully; when ctx ends first, it closes every connection.

func (*Server) HandleControl

func (s *Server) HandleControl(pattern string, h http.HandlerFunc)

HandleControl adds a control endpoint for a ServeMux pattern whose path is under /_fake/, such as "POST /_fake/send". A pattern outside it could never be reached, so it panics like ServeMux does on a bad pattern.

func (*Server) Name

func (s *Server) Name() string

func (*Server) Requests

func (s *Server) Requests() []Request

Requests returns the recorded requests in arrival order.

func (*Server) ResetFaults

func (s *Server) ResetFaults()

func (*Server) ResetRequests

func (s *Server) ResetRequests()

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Server) SetFault

func (s *Server) SetFault(f Fault) error

SetFault adds a fault, replacing an earlier one for the same path in its place.

func (*Server) Start

func (s *Server) Start(ctx context.Context, addr string) error

Start listens on addr, such as 127.0.0.1:0 in tests, and serves in the background until Close. The context bounds the start; requests get a context that Close cancels.

func (*Server) URL

func (s *Server) URL() string

URL is the base URL, such as http://127.0.0.1:18065, or "" before Start.

Jump to

Keyboard shortcuts

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