mockhttp

package
v0.17.3 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2026 License: LGPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package mockhttp provides the ability to define mocked routes, specifying request validations to be performed on incoming requests to these routes, and mock responses to be returned for these requests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateMockServerWithResponders added in v0.17.2

func CreateMockServerWithResponders(mux *http.ServeMux, c *qt.C, ip string, port int, responders []*RouteResponder) *httptest.Server

CreateMockServerWithResponders creates a mock HTTP server using the provided http.ServeMux. The server listens on the provided IP address and port, on the routes already present on the provided http.ServeMux and those provided as RouteResponder to this function.

Types

type Route

type Route struct {
	// Method refers to the HTTP Method (http.MethodPost, http.MethodGet, etc).
	Method string
	// Endpoint refers to the route path specified either as an exact path or
	// as a regex prefixed with '=~'.
	// Example:
	//		`/stores`,
	//		`=~/stores/(w+)\z`   (matches '/stores/<store-id>')
	Endpoint string
}

Route represents a callable API endpoint.

type RouteResponder

type RouteResponder struct {
	// Route refers to a callable API endpoint.
	Route Route

	// ExpectedReqBody allows to specify the expected request body for requests
	// that call this Route.
	ExpectedReqBody any
	// ExpectedReqQueryParams allows to specify the expected request query
	// params for requests that call this Route.
	ExpectedReqQueryParams url.Values
	// ExpectedPathParams allows to specify the expected path parameters for
	// requests that call this Route, in the format name=value.
	ExpectedPathParams map[string]string
	// MockResponse allows to configure a mock response body to be returned.
	MockResponse any
	// MockResponseStatus allows to configure the response status to be used.
	// If not specified, defaults to http.StatusOK.
	MockResponseStatus int
	// contains filtered or unexported fields
}

RouteResponder provides a way to define a mock http responder, wherein a request to a specific route can be validated as per some predefined expectation and mock responses can be configured to be returned when called.

func (*RouteResponder) Register added in v0.17.2

func (r *RouteResponder) Register(mux *http.ServeMux)

Register registers the Route with the provided http.ServeMux.

func (*RouteResponder) Validate added in v0.17.2

func (r *RouteResponder) Validate(c *qt.C)

Validate runs validations for the route, ensuring that the received request matches the predefined expectations.

Jump to

Keyboard shortcuts

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