mockserver

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2026 License: MIT Imports: 13 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package mockserver provides a gRPC server that stands in for a third party in tests. It is configured through an Expect() recorder and verified with AssertExpectations.

Expectations are declared against a service through the typed recorder protoc-gen-core's mock plugin generates for it, which wraps this package's untyped Recorder so that the endpoint is named by the compiler rather than by a string.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Recorder

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

Recorder declares expectations on a Server.

func (*Recorder) Unary

func (r *Recorder) Unary(fullMethodName string) *UnaryCall

Unary registers an expectation for a unary endpoint, named by its fully-qualified method name, with or without a leading slash: "/package.Service/Method". Expectations are matched in declaration order, and one declared with Times stops matching once it is exhausted, so several expectations on the same endpoint reply in the order they were declared.

Prefer the typed recorder generated for the service by protoc-gen-core's mock plugin, which names the endpoint and types the request and response for you. This is the escape hatch for a service whose generated mock is not on hand.

type Request

type Request struct {
	FullMethodName string
	Message        proto.Message
}

Request is a single request the server received. Its method name is spelled the way gRPC and the generated stubs' <Service>_<Method>_FullMethodName constants spell it, so an assertion on it can name the endpoint through one of those constants.

type Server

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

Server serves canned responses for any proto service. Requests are routed through an unknown-service handler rather than generated server stubs, so an expectation names its endpoint by fully-qualified method name and the request and response types are resolved from the proto registry.

Calls that match no expectation fail with codes.Unimplemented and are reported by AssertExpectations.

func NewServer

func NewServer(host string, port int) (*Server, error)

NewServer starts a mock gRPC server on the given port. A port of 0 binds an ephemeral one, which Address reports. The listener is bound before returning, so a port clash surfaces here rather than asynchronously.

func (*Server) Address

func (s *Server) Address() string

Address returns the host:port the mock is listening on.

func (*Server) AssertExpectations

func (s *Server) AssertExpectations(t mock.TestingT) bool

AssertExpectations fails the test if any expectation went unmet, or any call matched none.

func (*Server) Close

func (s *Server) Close()

Close stops the server and releases the port.

func (*Server) Expect

func (s *Server) Expect() *Recorder

Expect returns a recorder for declaring expectations.

func (*Server) Host

func (s *Server) Host() string

Host returns the host clients should be pointed at.

func (*Server) Port

func (s *Server) Port() int

Port returns the port clients should be pointed at.

func (*Server) Requests

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

Requests returns a copy of every request received so far, including unmatched ones.

func (*Server) Reset

func (s *Server) Reset()

Reset drops every expectation and recorded request, so each test starts from a clean slate.

type UnaryCall

type UnaryCall struct {
	mock.Cardinality
	// contains filtered or unexported fields
}

UnaryCall is an expectation on a unary endpoint.

func Handle

func Handle[Request, Response proto.Message](
	unaryCall *UnaryCall, handler func(context.Context, Request) (Response, error),
) *UnaryCall

Handle computes the reply from the request, for an endpoint whose response depends on what it was called with, with a function typed to that endpoint:

mockserver.Handle(server.Expect().Unary(pb.MockService_Echo_FullMethodName),
	func(ctx context.Context, request *pb.EchoRequest) (*pb.EchoResponse, error) { ... },
)

It takes precedence over Return and ReturnError. It is a function rather than a method on UnaryCall because Go has no generic methods. The generated typed recorders wrap it, so a test declaring its expectations through those never names the request and response types itself; a handler that means to work in terms of proto.Message instantiates it at that type instead.

func (*UnaryCall) AnyTimes

func (c *UnaryCall) AnyTimes() *UnaryCall

AnyTimes lets the expectation match any number of times, including none. Use it for endpoints a dependency calls on its own schedule, which no test asserts on.

func (*UnaryCall) Return

func (c *UnaryCall) Return(response proto.Message) *UnaryCall

Return sets the message to reply with. Without it, the endpoint's zero-value response message is returned.

func (*UnaryCall) ReturnError

func (c *UnaryCall) ReturnError(err error) *UnaryCall

ReturnError sets the error to fail with. Pass a status error to control the code the caller sees; anything else surfaces as codes.Unknown, as it would from a real server.

func (*UnaryCall) Times

func (c *UnaryCall) Times(count int) *UnaryCall

Times requires the expectation to be matched exactly count times.

func (*UnaryCall) WithPayload

func (c *UnaryCall) WithPayload(payload proto.Message) *UnaryCall

WithPayload restricts the expectation to requests equal to the given message. Without it, any request to the endpoint matches.

Jump to

Keyboard shortcuts

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