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 ¶
- type Matcher
- func (m *Matcher[I, O]) Handler(f func(req I, h metadata.MD) (O, error)) *Matcher[I, O]
- func (m *Matcher[I, O]) Match(f func(I) bool) *Matcher[I, O]
- func (m *Matcher[I, O]) MatchHeader(key, value string) *Matcher[I, O]
- func (m *Matcher[I, O]) Requests() []*Request[I]
- func (m *Matcher[I, O]) Response(message O) *Matcher[I, O]
- func (m *Matcher[I, O]) Status(s *status.Status) *Matcher[I, O]
- type RPC
- type Request
- type Server
- type TB
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]) MatchHeader ¶
MatchHeader sets the request header 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 (*Server) ClientConn ¶
func (s *Server) ClientConn() *grpc.ClientConn
Click to show internal directories.
Click to hide internal directories.