Documentation
¶
Overview ¶
Package httpmock provides canonical httptest.Server factories for common fleet upstream patterns. It removes the boilerplate of wiring up httptest.NewServer, setting response codes, and asserting call counts.
Usage:
// a server that always returns 200 JSON:
srv := httpmock.JSON(t, 200, map[string]any{"ok": true})
defer srv.Close()
// a server with per-request routing:
srv := httpmock.New(t).
Handle("/verify", httpmock.RespondJSON(200, verifyResp)).
Handle("/health", httpmock.RespondStatus(200)).
Build()
defer srv.Close()
// assert the number of calls to a path:
srv.AssertCalls(t, "/verify", 1)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder constructs a Server with per-path handlers.
func (*Builder) Handle ¶
Handle registers a handler for the given pattern (same syntax as http.ServeMux).
func (*Builder) HandleFunc ¶
func (b *Builder) HandleFunc(pattern string, fn http.HandlerFunc) *Builder
HandleFunc registers a handler function for the given pattern.
type Response ¶
Response describes a canned HTTP response.
func RespondJSON ¶
RespondJSON creates a Response with a JSON body.
func RespondStatus ¶
RespondStatus creates a Response with no body.
type Server ¶
Server wraps httptest.Server and records per-path call counts.
func JSON ¶
JSON creates a test server that responds to all requests with the given status code and JSON body. t.Cleanup registers server Close automatically.
func Sequence ¶
Sequence creates a test server that responds to successive requests with the provided responses in order. When the sequence is exhausted, the last response is repeated.
func Status ¶
Status creates a test server that responds to all requests with the given HTTP status code and no body.
func (*Server) AssertAtLeast ¶
AssertAtLeast fails t if the number of calls to path is less than min.
func (*Server) AssertCalls ¶
AssertCalls fails t if the number of calls to path does not equal want.