mockserver

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package mockserver provides a configurable HTTP mock server for functional testing. It is designed to run as a test service, started by the test runner before test execution and stopped afterward. Routes are defined declaratively in solution YAML via the TestConfig.Services field.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EchoResponse

type EchoResponse struct {
	Method  string            `json:"method"`
	Path    string            `json:"path"`
	Headers map[string]string `json:"headers"`
	Body    string            `json:"body"`
	Query   map[string]string `json:"query"`
}

EchoResponse is the JSON body returned when Echo is true.

type Request

type Request struct {
	Method  string            `json:"method"`
	Path    string            `json:"path"`
	Headers map[string]string `json:"headers"`
	Body    string            `json:"body"`
	Time    time.Time         `json:"time"`
}

Request records a received HTTP request for later inspection.

type Route

type Route struct {
	// Path is the URL path to match (exact match).
	Path string `json:"path" yaml:"path" doc:"URL path to match (exact)" maxLength:"500"`

	// Method is the HTTP method to match. Empty matches all methods.
	Method string `json:"method,omitempty" yaml:"method,omitempty" doc:"HTTP method to match (empty = all)" maxLength:"10"`

	// BodyContains is an optional substring that must appear in the request body
	// for the route to match. Useful for routing POST endpoints like GraphQL where
	// all requests hit the same path but carry different query payloads.
	BodyContains string `json:"bodyContains,omitempty" yaml:"bodyContains,omitempty" doc:"Request body must contain this substring" maxLength:"1000"`

	// Status is the HTTP status code to return (default: 200).
	Status int `json:"status,omitempty" yaml:"status,omitempty" doc:"HTTP status code to return" maximum:"599"`

	// Body is the response body string.
	Body string `json:"body,omitempty" yaml:"body,omitempty" doc:"Response body" maxLength:"100000"`

	// Headers are response headers to set.
	Headers map[string]string `json:"headers,omitempty" yaml:"headers,omitempty" doc:"Response headers"`

	// Delay is the simulated response delay as a Go duration string (e.g., "100ms").
	Delay string `json:"delay,omitempty" yaml:"delay,omitempty" doc:"Simulated response delay" maxLength:"20"`

	// Echo when true returns the request details as JSON instead of Body.
	Echo bool `json:"echo,omitempty" yaml:"echo,omitempty" doc:"Return request details as JSON"`
}

Route defines a single mock HTTP endpoint.

type Server

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

Server is a configurable HTTP mock server for testing.

func New

func New(routes []Route) *Server

New creates a new mock server with the given routes.

func (*Server) BaseURL

func (s *Server) BaseURL() string

BaseURL returns the base URL for the server (e.g., "http://127.0.0.1:12345").

func (*Server) Port

func (s *Server) Port() int

Port returns the port the server is listening on.

func (*Server) Requests

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

Requests returns a copy of all recorded requests.

func (*Server) Start

func (s *Server) Start() error

Start begins listening on a random available port. The assigned port can be retrieved via Port().

func (*Server) Stop

func (s *Server) Stop() error

Stop shuts down the server gracefully.

Jump to

Keyboard shortcuts

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