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 ¶
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 ¶
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 ¶
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) AssertExpectations ¶
AssertExpectations fails the test if any expectation went unmet, or any call matched none.
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 ¶
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 ¶
Return sets the message to reply with. Without it, the endpoint's zero-value response message is returned.
func (*UnaryCall) ReturnError ¶
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.