grpcmock

package
v1.3.3 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GrpcDefinition

type GrpcDefinition struct {
	Service         string
	Method          string
	ExpectedRequest []byte            // raw proto bytes of expected request (nil = skip verification)
	Response        []byte            // raw proto bytes of response
	ResponseStatus  codes.Code        // gRPC status code
	Metadata        map[string]string // trailing metadata
}

GrpcDefinition describes mock response for a single gRPC method.

type GrpcLoader added in v1.3.3

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

GrpcLoader reads per-test grpcMocks YAML definitions and calls SetDefinition on the appropriate registered GrpcMock, converting JSON response bodies to proto wire bytes.

func NewGrpcLoader added in v1.3.3

func NewGrpcLoader(registry *GrpcMocks) *GrpcLoader

NewGrpcLoader creates a loader that uses registry to resolve mock instances and the registry's DescriptorSource for JSON→proto conversion.

func (*GrpcLoader) Load added in v1.3.3

func (l *GrpcLoader) Load(defs map[string]interface{}) error

Load accepts the map parsed from the grpcMocks YAML field. Each key is a mock name; each value is a map with: service, method, responseBody (JSON string), responseStatus (int).

type GrpcMock

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

GrpcMock is a mock gRPC server analogous to mocks.ServiceMock for HTTP.

func New

func New() *GrpcMock

New creates a new gRPC mock server with gRPC reflection enabled. Reflection is backed by protoregistry.GlobalFiles, so any proto-generated Go package imported in the test binary is automatically discoverable.

func (*GrpcMock) Addr

func (m *GrpcMock) Addr() string

Addr returns the listener address or empty string if not started.

func (*GrpcMock) EndRunningContext

func (m *GrpcMock) EndRunningContext() []error

EndRunningContext returns verification errors accumulated during the run.

func (*GrpcMock) GetRecordedRequests

func (m *GrpcMock) GetRecordedRequests() []*RecordedRequest

GetRecordedRequests returns a copy of all recorded incoming requests.

func (*GrpcMock) ResetDefinitions

func (m *GrpcMock) ResetDefinitions()

ResetDefinitions clears all definitions, recorded requests and errors.

func (*GrpcMock) SetDefinition

func (m *GrpcMock) SetDefinition(def *GrpcDefinition)

SetDefinition registers a mock response definition for a gRPC method.

func (*GrpcMock) StartServer

func (m *GrpcMock) StartServer(addr string) error

StartServer starts the gRPC server on the given address.

func (*GrpcMock) Stop

func (m *GrpcMock) Stop()

Stop gracefully stops the gRPC server.

type GrpcMocks added in v1.3.3

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

GrpcMocks is a registry of named GrpcMock instances, shared with a descriptor source used for JSON→proto conversion at load time.

func NewGrpcMocks added in v1.3.3

func NewGrpcMocks(ds grpcurl.DescriptorSource) *GrpcMocks

NewGrpcMocks creates a registry backed by ds. Pass nil to auto-discover descriptors from protoregistry.GlobalFiles — this works out of the box when the test binary imports proto-generated Go packages.

func (*GrpcMocks) Add added in v1.3.3

func (g *GrpcMocks) Add(name string, mock *GrpcMock)

Add registers a mock under name.

func (*GrpcMocks) Get added in v1.3.3

func (g *GrpcMocks) Get(name string) *GrpcMock

Get returns the mock registered under name, or nil.

func (*GrpcMocks) GetNames added in v1.3.3

func (g *GrpcMocks) GetNames() []string

GetNames returns all registered mock names.

func (*GrpcMocks) ResetAll added in v1.3.3

func (g *GrpcMocks) ResetAll()

ResetAll calls ResetDefinitions on every registered mock.

type RecordedRequest

type RecordedRequest struct {
	Method  string
	Payload []byte // raw proto bytes
}

RecordedRequest is an incoming request captured by the mock server.

Jump to

Keyboard shortcuts

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