grpcmock

package module
v0.0.0-...-649a588 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 14 Imported by: 0

README

grpcmock

Go Reference test codecov

grpcmock provides a mock gRPC server from a generated gRPC client code.

This package currently supports unary RPCs only.

func TestClient(t *testing.T) {
  ts := grpcmock.NewServer(t)
  conn := ts.ClientConn()
  client := hello.NewGrpcTestServiceClient(conn)

  // Register a mock response.
  helloRPC := grpcmock.Register(ts, "/hello.GrpcTestService/Hello", hello.GrpcTestServiceClient.Hello).
    Response(&hello.HelloResponse{
      Message: "Hello, world!",
    })
  ts.Start()

  ctx := context.Background()
  res, _ := client.Hello(ctx, &hello.HelloRequest{Name: "qawatake"})

  if res.Message != "Hello, world!" {
    t.Errorf("unexpected response: %s", res.Message)
  }
  {
    // Retrieve the request(s)
    got := helloRPC.Requests()[0].Body
    if got.Name != "qawatake" {
      t.Errorf("unexpected request: %v", got)
    }
  }
}

References

  • k1LoW/grpcstub: provides a stub gRPC server from protobuf files (not from generated gRPC client code)

Documentation

Overview

Example
package main

import (
	"context"
	"fmt"
	"testing"

	"github.com/qawatake/grpcmock"
	"github.com/qawatake/grpcmock/testdata/gen/hello"
)

func main() {
	t := &testing.T{} // Provided by test

	ts := grpcmock.NewServer(t)
	client := hello.NewGrpcTestServiceClient(ts.ClientConn())

	// Register a mock response.
	helloRPC := grpcmock.Register(ts, "/hello.GrpcTestService/Hello", hello.GrpcTestServiceClient.Hello).
		Response(&hello.HelloResponse{
			Message: "Hello, world!",
		})

	// Start the server after registering the method.
	ts.Start()

	ctx := context.Background()
	res, _ := client.Hello(ctx, &hello.HelloRequest{Name: "qawatake"})

	fmt.Println("Response:", res.Message)
	fmt.Println("Request:", helloRPC.Requests()[0].Body.Name)
}
Output:

Response: Hello, world!
Request: qawatake

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Matcher

type Matcher[I, O protoreflect.ProtoMessage] struct {
	// contains filtered or unexported fields
}

func Register

func Register[R any, I, O protoreflect.ProtoMessage](s *Server, fullMethodName string, method RPC[R, I, O]) *Matcher[I, O]

Register registers a gRPC method to the internal gRPC server. Register must be called before Server.Start.

Example: Register(ts, "/hello.GrpcTestService/Hello", hello.GrpcTestServiceClient.Hello)

func (*Matcher[I, O]) Handler

func (m *Matcher[I, O]) Handler(f func(req I, h metadata.MD) (O, error)) *Matcher[I, O]

Handler sets the handler for the matcher.

func (*Matcher[I, O]) Match

func (m *Matcher[I, O]) Match(f func(I) bool) *Matcher[I, O]

Match sets the request matcher.

func (*Matcher[I, O]) MatchHeader

func (m *Matcher[I, O]) MatchHeader(key, value string) *Matcher[I, O]

MatchHeader sets the request header matcher.

func (*Matcher[I, O]) Requests

func (m *Matcher[I, O]) Requests() []*Request[I]

Requests returns the requests received by the matcher.

func (*Matcher[I, O]) Response

func (m *Matcher[I, O]) Response(message O) *Matcher[I, O]

Response sets the response body to be returned by the matcher.

func (*Matcher[I, O]) Status

func (m *Matcher[I, O]) Status(s *status.Status) *Matcher[I, O]

Status sets the status to be returned by the matcher.

type RPC

type RPC[R any, I, O protoreflect.ProtoMessage] func(R, context.Context, I, ...grpc.CallOption) (O, error)

RPC is a generic function type for gRPC methods.

Example: hello.GrpcTestServiceClient.Hello

type Request

type Request[I protoreflect.ProtoMessage] struct {
	Body    I
	Headers metadata.MD
}

type Server

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

func NewServer

func NewServer(t TB) *Server

func (*Server) Addr

func (s *Server) Addr() string

func (*Server) ClientConn

func (s *Server) ClientConn() *grpc.ClientConn

func (*Server) Start

func (s *Server) Start()

type TB

type TB interface {
	Error(args ...any)
	Errorf(format string, args ...any)
	Fatal(args ...any)
	Fatalf(format string, args ...any)
	Helper()
	Cleanup(func())
}

Jump to

Keyboard shortcuts

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