mockutil

package
v2.44.0 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handler

func Handler(t *testing.T, requests []Request) http.HandlerFunc

Handler is using a Server to mock http requests provided by the user.

Experimental: `exp` package is experimental, breaking changes may occur within minor releases.

Types

type Request

type Request struct {
	Method string
	Path   string
	Want   func(t *testing.T, r *http.Request)

	Status  int
	JSON    any
	JSONRaw string
	TextRaw string
}

Request describes a http request that a httptest.Server should receive, and the corresponding response to return.

Additional checks on the request (e.g. request body) may be added using the Request.Want function.

The response body is populated from either a JSON struct, or a JSON string.

type Server added in v2.15.0

type Server struct {
	*httptest.Server
	// contains filtered or unexported fields
}

Server embeds a httptest.Server that answers HTTP calls with a list of expected Request.

Request matching is based on the request count, and the user provided request will be iterated over.

A Server must be created using the NewServer function.

Experimental: `exp` package is experimental, breaking changes may occur within minor releases.

func NewServer added in v2.17.0

func NewServer(t *testing.T, requests []Request) *Server

NewServer returns a new mock server that closes itself at the end of the test.

Experimental: `exp` package is experimental, breaking changes may occur within minor releases.

func (*Server) Expect added in v2.17.0

func (m *Server) Expect(requests []Request)

Expect adds requests to the list of requests expected by the Server.

Experimental: `exp` package is experimental, breaking changes may occur within minor releases.

Jump to

Keyboard shortcuts

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