Documentation
¶
Overview ¶
Package httpmock provides JSONC-driven outbound HTTP mocks for itestkit cases.
Index ¶
- Constants
- func NewRegistry[C serverProvider](customHandlers map[string]itestkit.Handler[C]) itestkit.MapRegistry[C]
- type AwaitHTTPCallsHandler
- func (AwaitHTTPCallsHandler[C]) DecodeExpectedResponse(raw json.RawMessage) (any, error)
- func (AwaitHTTPCallsHandler[C]) DecodeRequest(raw json.RawMessage) (any, error)
- func (AwaitHTTPCallsHandler[C]) Invoke(ctx context.Context, client C, request any) (any, error)
- func (AwaitHTTPCallsHandler[C]) NormalizeResponse(response any) (any, error)
- type CallExpectation
- type CheckResult
- type CountMode
- type HeadersMode
- type ObservedRequest
- type Option
- type Ordering
- type Plan
- type PlanHTTPCallsHandler
- func (PlanHTTPCallsHandler[C]) DecodeExpectedResponse(raw json.RawMessage) (any, error)
- func (PlanHTTPCallsHandler[C]) DecodeRequest(raw json.RawMessage) (any, error)
- func (PlanHTTPCallsHandler[C]) Invoke(ctx context.Context, client C, request any) (any, error)
- func (PlanHTTPCallsHandler[C]) NormalizeResponse(response any) (any, error)
- type PlanHTTPCallsResponse
- type QueryMode
- type Response
- type Server
- func (server *Server) Await(_ context.Context) (CheckResult, error)
- func (server *Server) Plan(ctx context.Context, plan Plan) error
- func (server *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request)
- func (server *Server) URL() string
- func (server *Server) Verify(ctx context.Context) (CheckResult, error)
- type VerifyHTTPCallsHandler
- func (VerifyHTTPCallsHandler[C]) DecodeExpectedResponse(raw json.RawMessage) (any, error)
- func (VerifyHTTPCallsHandler[C]) DecodeRequest(raw json.RawMessage) (any, error)
- func (VerifyHTTPCallsHandler[C]) Invoke(ctx context.Context, client C, request any) (any, error)
- func (VerifyHTTPCallsHandler[C]) NormalizeResponse(response any) (any, error)
Constants ¶
const ( // PlanHTTPCallsHandlerName configures planned outbound HTTP calls. PlanHTTPCallsHandlerName = "PlanHTTPCalls" // AwaitHTTPCallsHandlerName checks planned HTTP calls during an await step. AwaitHTTPCallsHandlerName = "AwaitHTTPCalls" // VerifyHTTPCallsHandlerName checks planned HTTP calls during a verify step. VerifyHTTPCallsHandlerName = "VerifyHTTPCalls" )
const ( // QueryModeExact requires exact parsed query equality. QueryModeExact QueryMode = "exact" // QueryModeSubset allows extra actual query parameters. QueryModeSubset QueryMode = "subset" // HeadersModeExact requires exact header equality. HeadersModeExact HeadersMode = "exact" // HeadersModeSubset allows extra actual headers. HeadersModeSubset HeadersMode = "subset" // OrderingStrict requires the observed request order to match the plan order. OrderingStrict Ordering = "strict" // OrderingAny ignores observed request order during verification. OrderingAny Ordering = "any" // CountModeExact requires exactly expected_count matching requests. CountModeExact CountMode = "exact" // CountModeAtLeast requires at least expected_count matching requests. CountModeAtLeast CountMode = "at_least" )
Variables ¶
This section is empty.
Functions ¶
func NewRegistry ¶
func NewRegistry[C serverProvider](customHandlers map[string]itestkit.Handler[C]) itestkit.MapRegistry[C]
NewRegistry returns a registry with preset HTTP mock handlers and custom handlers.
The case harness type C must expose HTTPMock() *Server.
Types ¶
type AwaitHTTPCallsHandler ¶
type AwaitHTTPCallsHandler[C serverProvider] struct{}
AwaitHTTPCallsHandler checks the current HTTP call state.
func (AwaitHTTPCallsHandler[C]) DecodeExpectedResponse ¶
func (AwaitHTTPCallsHandler[C]) DecodeExpectedResponse(raw json.RawMessage) (any, error)
DecodeExpectedResponse decodes the expected await response.
func (AwaitHTTPCallsHandler[C]) DecodeRequest ¶
func (AwaitHTTPCallsHandler[C]) DecodeRequest(raw json.RawMessage) (any, error)
DecodeRequest decodes an empty await request.
func (AwaitHTTPCallsHandler[C]) NormalizeResponse ¶
func (AwaitHTTPCallsHandler[C]) NormalizeResponse(response any) (any, error)
NormalizeResponse returns the await response unchanged.
type CallExpectation ¶
type CallExpectation struct {
Name string `json:"name,omitempty"`
ExpectedCount int `json:"expected_count"`
CountMode CountMode `json:"count_mode,omitempty"`
Method string `json:"method"`
Path string `json:"path"`
Query map[string][]string `json:"query,omitempty"`
QueryMode QueryMode `json:"query_mode,omitempty"`
Headers map[string][]string `json:"headers,omitempty"`
HeadersMode HeadersMode `json:"headers_mode,omitempty"`
HeadersPresent []string `json:"headers_present,omitempty"`
Body json.RawMessage `json:"body,omitempty"`
BodySubset json.RawMessage `json:"body_subset,omitempty"`
RawBody *string `json:"raw_body,omitempty"`
FormBody map[string][]string `json:"form_body,omitempty"`
FormBodySubset map[string][]string `json:"form_body_subset,omitempty"`
Response Response `json:"response"`
}
CallExpectation describes one expected HTTP request and its stub response.
type CheckResult ¶
type CheckResult struct {
MatchedCount int `json:"matched_count"`
ObservedRequests []ObservedRequest `json:"observed_requests"`
LastMismatchReason string `json:"last_mismatch_reason,omitempty"`
}
CheckResult describes observed request matching state.
type ObservedRequest ¶
type ObservedRequest struct {
Method string `json:"method"`
Path string `json:"path"`
Query map[string][]string `json:"query,omitempty"`
Headers map[string][]string `json:"headers,omitempty"`
Body string `json:"body,omitempty"`
}
ObservedRequest describes a request recorded by the mock server.
type Option ¶
type Option func(*config)
Option changes server construction settings.
func WithMaxRequestBodyBytes ¶
WithMaxRequestBodyBytes sets the maximum recorded request body size.
type Plan ¶
type Plan struct {
Calls []CallExpectation `json:"calls"`
Ordering Ordering `json:"ordering,omitempty"`
}
Plan describes planned HTTP calls for one test case.
type PlanHTTPCallsHandler ¶
type PlanHTTPCallsHandler[C serverProvider] struct{}
PlanHTTPCallsHandler stores the HTTP call plan in the case server.
func (PlanHTTPCallsHandler[C]) DecodeExpectedResponse ¶
func (PlanHTTPCallsHandler[C]) DecodeExpectedResponse(raw json.RawMessage) (any, error)
DecodeExpectedResponse decodes the expected plan response.
func (PlanHTTPCallsHandler[C]) DecodeRequest ¶
func (PlanHTTPCallsHandler[C]) DecodeRequest(raw json.RawMessage) (any, error)
DecodeRequest decodes a planned HTTP call set.
func (PlanHTTPCallsHandler[C]) NormalizeResponse ¶
func (PlanHTTPCallsHandler[C]) NormalizeResponse(response any) (any, error)
NormalizeResponse returns the plan response unchanged.
type PlanHTTPCallsResponse ¶
type PlanHTTPCallsResponse struct {
Planned bool `json:"planned"`
ExpectedCalls int `json:"expected_calls"`
}
PlanHTTPCallsResponse describes a successful plan step result.
type Response ¶
type Response struct {
Status int `json:"status"`
Headers map[string][]string `json:"headers,omitempty"`
Body json.RawMessage `json:"body,omitempty"`
RawBody *string `json:"raw_body,omitempty"`
}
Response describes the HTTP response returned for a planned request.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server records HTTP requests and returns planned responses.
func NewPassThroughServer ¶
NewPassThroughServer starts an HTTP mock server that verifies requests and delegates matched calls to handler.
func (*Server) Await ¶
func (server *Server) Await(_ context.Context) (CheckResult, error)
Await checks whether planned HTTP calls have happened yet.
type VerifyHTTPCallsHandler ¶
type VerifyHTTPCallsHandler[C serverProvider] struct{}
VerifyHTTPCallsHandler checks the final HTTP call state.
func (VerifyHTTPCallsHandler[C]) DecodeExpectedResponse ¶
func (VerifyHTTPCallsHandler[C]) DecodeExpectedResponse(raw json.RawMessage) (any, error)
DecodeExpectedResponse decodes the expected verify response.
func (VerifyHTTPCallsHandler[C]) DecodeRequest ¶
func (VerifyHTTPCallsHandler[C]) DecodeRequest(raw json.RawMessage) (any, error)
DecodeRequest decodes an empty verify request.
func (VerifyHTTPCallsHandler[C]) NormalizeResponse ¶
func (VerifyHTTPCallsHandler[C]) NormalizeResponse(response any) (any, error)
NormalizeResponse returns the verify response unchanged.