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
- func DecodeJSON(w http.ResponseWriter, r *http.Request, v any) error
- func WriteError(w http.ResponseWriter, status int, msg string)
- func WriteJSON(w http.ResponseWriter, status int, v any)
- type Fault
- type Request
- type Server
- func (s *Server) Close(ctx context.Context) error
- func (s *Server) HandleControl(pattern string, h http.HandlerFunc)
- func (s *Server) Name() string
- func (s *Server) Requests() []Request
- func (s *Server) ResetFaults()
- func (s *Server) ResetRequests()
- func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (s *Server) SetFault(f Fault) error
- func (s *Server) Start(ctx context.Context, addr string) error
- func (s *Server) URL() string
Constants ¶
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 )
const ( EncodingUTF8 = "utf8" EncodingBase64 = "base64" )
Variables ¶
This section is empty.
Functions ¶
func DecodeJSON ¶
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}.
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.
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 ¶
New returns a server named after the system it fakes, such as "Mattermost", that answers with h.
func (*Server) Close ¶
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) ResetFaults ¶
func (s *Server) ResetFaults()
func (*Server) ResetRequests ¶
func (s *Server) ResetRequests()
func (*Server) SetFault ¶
SetFault adds a fault, replacing an earlier one for the same path in its place.
func (*Server) Start ¶
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 ¶
URL is the base URL, such as http://127.0.0.1:18065, or "" before Start.