Documentation ¶
Overview ¶
Package request provides all the expectations for an RPC method.
Index ¶
- func CountCall(r Request)
- func Handle(ctx context.Context, r Request, in interface{}, out interface{}) error
- func HeaderMatcher(r Request) matcher.HeaderMatcher
- func NumCalls(r Request) int
- func PayloadMatcher(r Request) *matcher.PayloadMatcher
- func ServiceMethod(r Request) service.Method
- func SetRepeatability(r Request, i RepeatedTime)
- type BidirectionalStreamRequest
- func (r *BidirectionalStreamRequest) After(d time.Duration) *BidirectionalStreamRequest
- func (r *BidirectionalStreamRequest) Once() *BidirectionalStreamRequest
- func (r *BidirectionalStreamRequest) ReturnCode(code codes.Code)
- func (r *BidirectionalStreamRequest) ReturnError(code codes.Code, msg string)
- func (r *BidirectionalStreamRequest) ReturnErrorMessage(msg string)
- func (r *BidirectionalStreamRequest) ReturnErrorf(code codes.Code, format string, args ...interface{})
- func (r *BidirectionalStreamRequest) Run(handler func(ctx context.Context, s grpc.ServerStream) error)
- func (r *BidirectionalStreamRequest) Times(i RepeatedTime) *BidirectionalStreamRequest
- func (r *BidirectionalStreamRequest) Twice() *BidirectionalStreamRequest
- func (r *BidirectionalStreamRequest) UnlimitedTimes() *BidirectionalStreamRequest
- func (r *BidirectionalStreamRequest) WaitUntil(w <-chan time.Time) *BidirectionalStreamRequest
- func (r *BidirectionalStreamRequest) WithHeader(header string, value interface{}) *BidirectionalStreamRequest
- func (r *BidirectionalStreamRequest) WithHeaders(headers map[string]interface{}) *BidirectionalStreamRequest
- type ClientStreamRequest
- func (r *ClientStreamRequest) After(d time.Duration) *ClientStreamRequest
- func (r *ClientStreamRequest) Once() *ClientStreamRequest
- func (r *ClientStreamRequest) Return(v interface{})
- func (r *ClientStreamRequest) ReturnCode(code codes.Code)
- func (r *ClientStreamRequest) ReturnError(code codes.Code, msg string)
- func (r *ClientStreamRequest) ReturnErrorMessage(msg string)
- func (r *ClientStreamRequest) ReturnErrorf(code codes.Code, format string, args ...interface{})
- func (r *ClientStreamRequest) ReturnFile(filePath string)
- func (r *ClientStreamRequest) ReturnJSON(v interface{})
- func (r *ClientStreamRequest) Returnf(format string, args ...interface{})
- func (r *ClientStreamRequest) Run(handler func(ctx context.Context, s grpc.ServerStream) (interface{}, error))
- func (r *ClientStreamRequest) Times(i RepeatedTime) *ClientStreamRequest
- func (r *ClientStreamRequest) Twice() *ClientStreamRequest
- func (r *ClientStreamRequest) UnlimitedTimes() *ClientStreamRequest
- func (r *ClientStreamRequest) WaitUntil(w <-chan time.Time) *ClientStreamRequest
- func (r *ClientStreamRequest) WithHeader(header string, value interface{}) *ClientStreamRequest
- func (r *ClientStreamRequest) WithHeaders(headers map[string]interface{}) *ClientStreamRequest
- func (r *ClientStreamRequest) WithPayload(in interface{}) *ClientStreamRequest
- func (r *ClientStreamRequest) WithPayloadf(format string, args ...interface{}) *ClientStreamRequest
- type Handler
- type RepeatedTime
- type Request
- type ServerStreamRequest
- func (r *ServerStreamRequest) After(d time.Duration) *ServerStreamRequest
- func (r *ServerStreamRequest) Once() *ServerStreamRequest
- func (r *ServerStreamRequest) Return(v interface{})
- func (r *ServerStreamRequest) ReturnCode(code codes.Code)
- func (r *ServerStreamRequest) ReturnError(code codes.Code, msg string)
- func (r *ServerStreamRequest) ReturnErrorMessage(msg string)
- func (r *ServerStreamRequest) ReturnErrorf(code codes.Code, format string, args ...interface{})
- func (r *ServerStreamRequest) ReturnFile(filePath string)
- func (r *ServerStreamRequest) ReturnJSON(v interface{})
- func (r *ServerStreamRequest) ReturnStream() *serverStreamHandler
- func (r *ServerStreamRequest) Returnf(format string, args ...interface{})
- func (r *ServerStreamRequest) Run(handler func(ctx context.Context, in interface{}, s grpc.ServerStream) error)
- func (r *ServerStreamRequest) Times(i RepeatedTime) *ServerStreamRequest
- func (r *ServerStreamRequest) Twice() *ServerStreamRequest
- func (r *ServerStreamRequest) UnlimitedTimes() *ServerStreamRequest
- func (r *ServerStreamRequest) WaitUntil(w <-chan time.Time) *ServerStreamRequest
- func (r *ServerStreamRequest) WithHeader(header string, value interface{}) *ServerStreamRequest
- func (r *ServerStreamRequest) WithHeaders(headers map[string]interface{}) *ServerStreamRequest
- func (r *ServerStreamRequest) WithPayload(in interface{}) *ServerStreamRequest
- func (r *ServerStreamRequest) WithPayloadf(format string, args ...interface{}) *ServerStreamRequest
- type UnaryRequest
- func (r *UnaryRequest) After(d time.Duration) *UnaryRequest
- func (r *UnaryRequest) Once() *UnaryRequest
- func (r *UnaryRequest) Return(v interface{})
- func (r *UnaryRequest) ReturnCode(code codes.Code)
- func (r *UnaryRequest) ReturnError(code codes.Code, msg string)
- func (r *UnaryRequest) ReturnErrorMessage(msg string)
- func (r *UnaryRequest) ReturnErrorf(code codes.Code, format string, args ...interface{})
- func (r *UnaryRequest) ReturnFile(filePath string)
- func (r *UnaryRequest) ReturnJSON(v interface{})
- func (r *UnaryRequest) Returnf(format string, args ...interface{})
- func (r *UnaryRequest) Run(handler func(ctx context.Context, in interface{}) (interface{}, error))
- func (r *UnaryRequest) Times(i RepeatedTime) *UnaryRequest
- func (r *UnaryRequest) Twice() *UnaryRequest
- func (r *UnaryRequest) UnlimitedTimes() *UnaryRequest
- func (r *UnaryRequest) WaitUntil(w <-chan time.Time) *UnaryRequest
- func (r *UnaryRequest) WithHeader(header string, value interface{}) *UnaryRequest
- func (r *UnaryRequest) WithHeaders(headers map[string]interface{}) *UnaryRequest
- func (r *UnaryRequest) WithPayload(in interface{}) *UnaryRequest
- func (r *UnaryRequest) WithPayloadf(format string, args ...interface{}) *UnaryRequest
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HeaderMatcher ¶
func HeaderMatcher(r Request) matcher.HeaderMatcher
HeaderMatcher returns the headerMatcher matcher of the given request.
func PayloadMatcher ¶
func PayloadMatcher(r Request) *matcher.PayloadMatcher
PayloadMatcher returns the payload matcher of the given request.
func ServiceMethod ¶
ServiceMethod returns the service method of the given request.
func SetRepeatability ¶
func SetRepeatability(r Request, i RepeatedTime)
SetRepeatability sets the repeatability for the given request.
Types ¶
type BidirectionalStreamRequest ¶ added in v0.10.0
type BidirectionalStreamRequest struct {
// contains filtered or unexported fields
}
BidirectionalStreamRequest represents the expectation for a client-stream request.
func NewBidirectionalStreamRequest ¶ added in v0.10.0
func NewBidirectionalStreamRequest(locker sync.Locker, svc *service.Method) *BidirectionalStreamRequest
NewBidirectionalStreamRequest creates a new client-stream expectation.
func (*BidirectionalStreamRequest) After ¶ added in v0.10.0
func (r *BidirectionalStreamRequest) After(d time.Duration) *BidirectionalStreamRequest
After sets how long to block until the call returns.
Server.ExpectBidirectionalStream("grpctest.Service/TransformItems"). After(time.Second). Run(func(context.Context, grpc.ServerStream) error { return nil })
func (*BidirectionalStreamRequest) Once ¶ added in v0.10.0
func (r *BidirectionalStreamRequest) Once() *BidirectionalStreamRequest
Once indicates that the mock should only return the value once.
Server.ExpectBidirectionalStream("grpctest.Service/TransformItems"). Once(). Run(func(context.Context, grpc.ServerStream) error { return nil })
See: BidirectionalStreamRequest.Twice(), BidirectionalStreamRequest.UnlimitedTimes(), BidirectionalStreamRequest.Times().
func (*BidirectionalStreamRequest) ReturnCode ¶ added in v0.10.0
func (r *BidirectionalStreamRequest) ReturnCode(code codes.Code)
ReturnCode sets the response code.
Server.ExpectBidirectionalStream("grpc.Service/TransformItems"). ReturnCode(codes.OK)
See: BidirectionalStreamRequest.ReturnErrorMessage(), BidirectionalStreamRequest.ReturnError(), BidirectionalStreamRequest.ReturnErrorf().
func (*BidirectionalStreamRequest) ReturnError ¶ added in v0.10.0
func (r *BidirectionalStreamRequest) ReturnError(code codes.Code, msg string)
ReturnError sets the response error.
Server.ExpectBidirectionalStream("grpc.Service/TransformItems"). ReturnError(codes.Internal, "Internal Server Error")
See: BidirectionalStreamRequest.ReturnCode(), BidirectionalStreamRequest.ReturnErrorMessage(), BidirectionalStreamRequest.ReturnErrorf().
func (*BidirectionalStreamRequest) ReturnErrorMessage ¶ added in v0.10.0
func (r *BidirectionalStreamRequest) ReturnErrorMessage(msg string)
ReturnErrorMessage sets the response error message.
Server.ExpectBidirectionalStream("grpc.Service/TransformItems"). ReturnErrorMessage("Internal Server Error")
See: BidirectionalStreamRequest.ReturnCode(), BidirectionalStreamRequest.ReturnError(), BidirectionalStreamRequest.ReturnErrorf().
func (*BidirectionalStreamRequest) ReturnErrorf ¶ added in v0.10.0
func (r *BidirectionalStreamRequest) ReturnErrorf(code codes.Code, format string, args ...interface{})
ReturnErrorf sets the response error.
Server.ExpectBidirectionalStream("grpc.Service/TransformItems"). ReturnErrorf(codes.NotFound, "Item %d not found", 42)
See: BidirectionalStreamRequest.ReturnCode(), BidirectionalStreamRequest.ReturnErrorMessage(), BidirectionalStreamRequest.ReturnError().
func (*BidirectionalStreamRequest) Run ¶ added in v0.10.0
func (r *BidirectionalStreamRequest) Run(handler func(ctx context.Context, s grpc.ServerStream) error)
Run sets a custom handler to handle the given request.
Server.ExpectBidirectionalStream("grpc.Service/TransformItems"). Run(func(context.Context, grpc.ServerStream) error { return nil })
func (*BidirectionalStreamRequest) Times ¶ added in v0.10.0
func (r *BidirectionalStreamRequest) Times(i RepeatedTime) *BidirectionalStreamRequest
Times indicates that the mock should only return the indicated number of times.
Server.ExpectBidirectionalStream("grpctest.Service/TransformItems"). Times(5). Run(func(context.Context, grpc.ServerStream) error { return nil })
See: BidirectionalStreamRequest.Once(), BidirectionalStreamRequest.Twice(), BidirectionalStreamRequest.UnlimitedTimes().
func (*BidirectionalStreamRequest) Twice ¶ added in v0.10.0
func (r *BidirectionalStreamRequest) Twice() *BidirectionalStreamRequest
Twice indicates that the mock should only return the value twice.
Server.ExpectBidirectionalStream("grpctest.Service/TransformItems"). Twice(). Run(func(context.Context, grpc.ServerStream) error { return nil })
See: BidirectionalStreamRequest.Once(), BidirectionalStreamRequest.UnlimitedTimes(), BidirectionalStreamRequest.Times().
func (*BidirectionalStreamRequest) UnlimitedTimes ¶ added in v0.10.0
func (r *BidirectionalStreamRequest) UnlimitedTimes() *BidirectionalStreamRequest
UnlimitedTimes indicates that the mock should return the value at least once and there is no max limit in the number of return.
Server.ExpectBidirectionalStream("grpctest.Service/TransformItems"). UnlimitedTimes(). Run(func(context.Context, grpc.ServerStream) error { return nil })
See: BidirectionalStreamRequest.Once(), BidirectionalStreamRequest.Twice(), BidirectionalStreamRequest.Times().
func (*BidirectionalStreamRequest) WaitUntil ¶ added in v0.10.0
func (r *BidirectionalStreamRequest) WaitUntil(w <-chan time.Time) *BidirectionalStreamRequest
WaitUntil sets the channel that will block the mocked return until its closed or a message is received.
Server.ExpectBidirectionalStream("grpctest.Service/TransformItems"). WaitUntil(time.After(time.Second)). Run(func(context.Context, grpc.ServerStream) error { return nil })
func (*BidirectionalStreamRequest) WithHeader ¶ added in v0.10.0
func (r *BidirectionalStreamRequest) WithHeader(header string, value interface{}) *BidirectionalStreamRequest
WithHeader sets an expected header of the given request.
Server.ExpectBidirectionalStream("grpctest.Service/TransformItems"). WithHeader("Locale", "en-US")
func (*BidirectionalStreamRequest) WithHeaders ¶ added in v0.10.0
func (r *BidirectionalStreamRequest) WithHeaders(headers map[string]interface{}) *BidirectionalStreamRequest
WithHeaders sets a list of expected headers of the given request.
Server.ExpectBidirectionalStream("grpctest.Service/TransformItems"). WithHeaders(map[string]interface{}{"Locale": "en-US"})
type ClientStreamRequest ¶
type ClientStreamRequest struct {
// contains filtered or unexported fields
}
ClientStreamRequest represents the expectation for a client-stream request.
func NewClientStreamRequest ¶
func NewClientStreamRequest(locker sync.Locker, svc *service.Method) *ClientStreamRequest
NewClientStreamRequest creates a new client-stream expectation.
func (*ClientStreamRequest) After ¶
func (r *ClientStreamRequest) After(d time.Duration) *ClientStreamRequest
After sets how long to block until the call returns.
Server.ExpectClientStream("grpctest.Service/CreateItems"). After(time.Second). Return(`{"num_items": 1}`)
func (*ClientStreamRequest) Once ¶
func (r *ClientStreamRequest) Once() *ClientStreamRequest
Once indicates that the mock should only return the value once.
Server.ExpectClientStream("grpctest.Service/CreateItems"). Return(`{"num_items": 1}`) Once()
See: ClientStreamRequest.Twice(), ClientStreamRequest.UnlimitedTimes(), ClientStreamRequest.Times().
func (*ClientStreamRequest) Return ¶
func (r *ClientStreamRequest) Return(v interface{})
Return sets the result to return to client.
Server.ExpectClientStream("grpc.Service/CreateItems"). Return(`{"num_items": 1}`)
See: ClientStreamRequest.Returnf(), ClientStreamRequest.ReturnJSON(), ClientStreamRequest.ReturnFile().
func (*ClientStreamRequest) ReturnCode ¶
func (r *ClientStreamRequest) ReturnCode(code codes.Code)
ReturnCode sets the response code.
Server.ExpectClientStream("grpc.Service/CreateItems"). ReturnCode(codes.OK)
See: ClientStreamRequest.ReturnErrorMessage(), ClientStreamRequest.ReturnError(), ClientStreamRequest.ReturnErrorf().
func (*ClientStreamRequest) ReturnError ¶
func (r *ClientStreamRequest) ReturnError(code codes.Code, msg string)
ReturnError sets the response error.
Server.ExpectClientStream("grpc.Service/CreateItems"). ReturnError(codes.Internal, "Internal Server Error")
See: ClientStreamRequest.ReturnCode(), ClientStreamRequest.ReturnErrorMessage(), ClientStreamRequest.ReturnErrorf().
func (*ClientStreamRequest) ReturnErrorMessage ¶
func (r *ClientStreamRequest) ReturnErrorMessage(msg string)
ReturnErrorMessage sets the response error message.
Server.ExpectClientStream("grpc.Service/CreateItems"). ReturnErrorMessage("Internal Server Error")
See: ClientStreamRequest.ReturnCode(), ClientStreamRequest.ReturnError(), ClientStreamRequest.ReturnErrorf().
func (*ClientStreamRequest) ReturnErrorf ¶
func (r *ClientStreamRequest) ReturnErrorf(code codes.Code, format string, args ...interface{})
ReturnErrorf sets the response error.
Server.ExpectClientStream("grpc.Service/CreateItems"). ReturnErrorf(codes.NotFound, "Item %d not found", 42)
See: ClientStreamRequest.ReturnCode(), ClientStreamRequest.ReturnErrorMessage(), ClientStreamRequest.ReturnError().
func (*ClientStreamRequest) ReturnFile ¶
func (r *ClientStreamRequest) ReturnFile(filePath string)
ReturnFile reads the file and uses its content as the result to return to client.
Server.ExpectUnary("grpctest.Service/CreateItems"). ReturnFile("resources/fixtures/response.json")
See: ClientStreamRequest.Return(), ClientStreamRequest.Returnf(), ClientStreamRequest.ReturnJSON().
func (*ClientStreamRequest) ReturnJSON ¶
func (r *ClientStreamRequest) ReturnJSON(v interface{})
ReturnJSON marshals the object using json.Marshal and uses it as the result to return to client.
Server.ExpectClientStream("grpc.Service/CreateItems"). ReturnJSON(map[string]interface{}{"num_items": 1})
See: ClientStreamRequest.Return(), ClientStreamRequest.Returnf(), ClientStreamRequest.ReturnFile().
func (*ClientStreamRequest) Returnf ¶
func (r *ClientStreamRequest) Returnf(format string, args ...interface{})
Returnf formats according to a format specifier and use it as the result to return to client.
Server.ExpectClientStream("grpc.Service/CreateItems"). Returnf(`{"num_items": %d}`, 1)
See: ClientStreamRequest.Return(), ClientStreamRequest.ReturnJSON(), ClientStreamRequest.ReturnFile().
func (*ClientStreamRequest) Run ¶
func (r *ClientStreamRequest) Run(handler func(ctx context.Context, s grpc.ServerStream) (interface{}, error))
Run sets a custom handler to handle the given request.
Server.ExpectClientStream("grpc.Service/CreateItems"). Run(func(context.Context, grpc.ServerStreamer) (interface{}, error) { return &grpctest.CreateItemsResponse{NumItems: 1}, nil })
func (*ClientStreamRequest) Times ¶
func (r *ClientStreamRequest) Times(i RepeatedTime) *ClientStreamRequest
Times indicates that the mock should only return the indicated number of times.
Server.ExpectClientStream("grpctest.Service/CreateItems"). Return(`{"num_items": 1}`) Times(5)
See: ClientStreamRequest.Once(), ClientStreamRequest.Twice(), ClientStreamRequest.UnlimitedTimes().
func (*ClientStreamRequest) Twice ¶
func (r *ClientStreamRequest) Twice() *ClientStreamRequest
Twice indicates that the mock should only return the value twice.
Server.ExpectClientStream("grpctest.Service/CreateItems"). Return(`{"num_items": 1}`) Twice()
See: ClientStreamRequest.Once(), ClientStreamRequest.UnlimitedTimes(), ClientStreamRequest.Times().
func (*ClientStreamRequest) UnlimitedTimes ¶
func (r *ClientStreamRequest) UnlimitedTimes() *ClientStreamRequest
UnlimitedTimes indicates that the mock should return the value at least once and there is no max limit in the number of return.
Server.ExpectClientStream("grpctest.Service/CreateItems"). Return(`{"num_items": 1}`) UnlimitedTimes()
See: ClientStreamRequest.Once(), ClientStreamRequest.Twice(), ClientStreamRequest.Times().
func (*ClientStreamRequest) WaitUntil ¶
func (r *ClientStreamRequest) WaitUntil(w <-chan time.Time) *ClientStreamRequest
WaitUntil sets the channel that will block the mocked return until its closed or a message is received.
Server.ExpectClientStream("grpctest.Service/CreateItems"). WaitUntil(time.After(time.Second)). Return(`{"num_items": 1}`)
func (*ClientStreamRequest) WithHeader ¶
func (r *ClientStreamRequest) WithHeader(header string, value interface{}) *ClientStreamRequest
WithHeader sets an expected header of the given request.
Server.ExpectClientStream("grpctest.Service/CreateItems"). WithHeader("Locale", "en-US")
func (*ClientStreamRequest) WithHeaders ¶
func (r *ClientStreamRequest) WithHeaders(headers map[string]interface{}) *ClientStreamRequest
WithHeaders sets a list of expected headers of the given request.
Server.ExpectClientStream("grpctest.Service/CreateItems"). WithHeaders(map[string]interface{}{"Locale": "en-US"})
func (*ClientStreamRequest) WithPayload ¶
func (r *ClientStreamRequest) WithPayload(in interface{}) *ClientStreamRequest
WithPayload sets the expected payload of the given request. It could be a JSON []byte, JSON string, an object (that will be marshaled), or a custom matcher.
Server.ExpectClientStream("grpctest.Service/CreateItems"). WithPayload(`[{"name": "Foobar"}]`)
See: ClientStreamRequest.WithPayloadf().
func (*ClientStreamRequest) WithPayloadf ¶
func (r *ClientStreamRequest) WithPayloadf(format string, args ...interface{}) *ClientStreamRequest
WithPayloadf formats according to a format specifier and use it as the expected payload of the given request.
Server.ExpectClientStream("grpctest.Service/CreateItems"). WithPayloadf(`[{"name": %q}]`, "Foobar")
See: ClientStreamRequest.WithPayload().
type RepeatedTime ¶ added in v0.15.0
type RepeatedTime uint32
RepeatedTime represents a number of times that a request could be repeated.
const UnlimitedTimes RepeatedTime = 0
UnlimitedTimes indicates that a request could be repeated without limits.
func Repeatability ¶
func Repeatability(r Request) RepeatedTime
Repeatability gets the repeatability of the given request.
type Request ¶
type Request interface {
// contains filtered or unexported methods
}
Request represents the grpc request expectation.
type ServerStreamRequest ¶
type ServerStreamRequest struct {
// contains filtered or unexported fields
}
ServerStreamRequest represents the expectation for a server-stream request.
func NewServerStreamRequest ¶
func NewServerStreamRequest(locker sync.Locker, svc *service.Method) *ServerStreamRequest
NewServerStreamRequest creates a new server-stream expectation.
func (*ServerStreamRequest) After ¶
func (r *ServerStreamRequest) After(d time.Duration) *ServerStreamRequest
After sets how long to block until the call returns.
Server.ExpectServerStream("grpctest.Service/ListItems"). After(time.Second). Return(`[{"message": "hello world!"}]`)
func (*ServerStreamRequest) Once ¶
func (r *ServerStreamRequest) Once() *ServerStreamRequest
Once indicates that the mock should only return the value once.
Server.ExpectServerStream("grpctest.Service/ListItems"). Return(`[{"id": 42}]`) Once()
See: ServerStreamRequest.Twice(), ServerStreamRequest.UnlimitedTimes(), ServerStreamRequest.Times().
func (*ServerStreamRequest) Return ¶
func (r *ServerStreamRequest) Return(v interface{})
Return sets the result to return to client.
Server.ExpectServerStream("grpc.Service/ListItems"). Return(`[{"id": 42}]`)
See: ServerStreamRequest.Returnf(), ServerStreamRequest.ReturnJSON(), ServerStreamRequest.ReturnFile(), ServerStreamRequest.ReturnStream().
func (*ServerStreamRequest) ReturnCode ¶
func (r *ServerStreamRequest) ReturnCode(code codes.Code)
ReturnCode sets the response code.
Server.ExpectServerStream("grpc.Service/ListItems"). ReturnCode(codes.OK)
See: ServerStreamRequest.ReturnErrorMessage(), ServerStreamRequest.ReturnError(), ServerStreamRequest.ReturnErrorf().
func (*ServerStreamRequest) ReturnError ¶
func (r *ServerStreamRequest) ReturnError(code codes.Code, msg string)
ReturnError sets the response error.
Server.ExpectServerStream("grpc.Service/ListItems"). ReturnError(codes.Internal, "Internal Server Error")
See: ServerStreamRequest.ReturnCode(), ServerStreamRequest.ReturnErrorMessage(), ServerStreamRequest.ReturnErrorf().
func (*ServerStreamRequest) ReturnErrorMessage ¶
func (r *ServerStreamRequest) ReturnErrorMessage(msg string)
ReturnErrorMessage sets the response error message.
Server.ExpectServerStream("grpc.Service/ListItems"). ReturnErrorMessage("Internal Server Error")
See: ServerStreamRequest.ReturnCode(), ServerStreamRequest.ReturnError(), ServerStreamRequest.ReturnErrorf().
func (*ServerStreamRequest) ReturnErrorf ¶
func (r *ServerStreamRequest) ReturnErrorf(code codes.Code, format string, args ...interface{})
ReturnErrorf sets the response error.
Server.ExpectServerStream("grpc.Service/ListItems"). ReturnErrorf(codes.NotFound, "Item %d not found", 42)
See: ServerStreamRequest.ReturnCode(), ServerStreamRequest.ReturnErrorMessage(), ServerStreamRequest.ReturnError().
func (*ServerStreamRequest) ReturnFile ¶
func (r *ServerStreamRequest) ReturnFile(filePath string)
ReturnFile reads the file and uses its content as the result to return to client.
Server.ExpectServerStream("grpc.Service/ListItems"). ReturnFile("resources/fixtures/response.json")
See: ServerStreamRequest.Return(), ServerStreamRequest.Returnf(), ServerStreamRequest.ReturnJSON(), ServerStreamRequest.ReturnStream().
func (*ServerStreamRequest) ReturnJSON ¶
func (r *ServerStreamRequest) ReturnJSON(v interface{})
ReturnJSON marshals the object using json.Marshal and uses it as the result to return to client.
Server.ExpectServerStream("grpc.Service/ListItems"). ReturnJSON([]map[string]string{{"foo": "bar"}})
See: ServerStreamRequest.Return(), ServerStreamRequest.Returnf(), ServerStreamRequest.ReturnFile(), ServerStreamRequest.ReturnStream().
func (*ServerStreamRequest) ReturnStream ¶
func (r *ServerStreamRequest) ReturnStream() *serverStreamHandler
ReturnStream returns the stream with custom behaviors.
Server.ExpectServerStream("grpc.Service/ListItems"). ReturnStream(). Send(grpctest.Item{ Id: 42, Locale: "en-US", Name: "Foobar", }). ReturnError(codes.Internal, "stream error")
See: ServerStreamRequest.Return(), ServerStreamRequest.Returnf(), ServerStreamRequest.ReturnJSON(), ServerStreamRequest.ReturnFile().
func (*ServerStreamRequest) Returnf ¶
func (r *ServerStreamRequest) Returnf(format string, args ...interface{})
Returnf formats according to a format specifier and use it as the result to return to client.
Server.ExpectServerStream("grpc.Service/ListItems"). Returnf(`[{"id": %d}]`, 42)
See: ServerStreamRequest.Return(), ServerStreamRequest.ReturnJSON(), ServerStreamRequest.ReturnFile(), ServerStreamRequest.ReturnStream().
func (*ServerStreamRequest) Run ¶
func (r *ServerStreamRequest) Run(handler func(ctx context.Context, in interface{}, s grpc.ServerStream) error)
Run sets a custom handler to handle the given request.
Server.ExpectServerStream("grpc.Service/ListItems"). Run(func(ctx context.Context, in interface{}, srv interface{}) error { srv := out.(grpc.ServerStreamer) return srv.SendMsg(grpctest.Item{Id: 42}) })
func (*ServerStreamRequest) Times ¶
func (r *ServerStreamRequest) Times(i RepeatedTime) *ServerStreamRequest
Times indicates that the mock should only return the indicated number of times.
Server.ExpectServerStream("grpctest.Service/ListItems"). Return(`[{"id": 42}]`) Times(5)
See: ServerStreamRequest.Once(), ServerStreamRequest.Twice(), ServerStreamRequest.UnlimitedTimes().
func (*ServerStreamRequest) Twice ¶
func (r *ServerStreamRequest) Twice() *ServerStreamRequest
Twice indicates that the mock should only return the value twice.
Server.ExpectServerStream("grpctest.Service/ListItems"). Return(`[{"id": 42}]`) Twice()
See: ServerStreamRequest.Once(), ServerStreamRequest.UnlimitedTimes(), ServerStreamRequest.Times().
func (*ServerStreamRequest) UnlimitedTimes ¶
func (r *ServerStreamRequest) UnlimitedTimes() *ServerStreamRequest
UnlimitedTimes indicates that the mock should return the value at least once and there is no max limit in the number of return.
Server.ExpectServerStream("grpctest.Service/ListItems"). Return(`[{"id": 42}]`) UnlimitedTimes()
See: ServerStreamRequest.Once(), ServerStreamRequest.Twice(), ServerStreamRequest.Times().
func (*ServerStreamRequest) WaitUntil ¶
func (r *ServerStreamRequest) WaitUntil(w <-chan time.Time) *ServerStreamRequest
WaitUntil sets the channel that will block the mocked return until its closed or a message is received.
Server.ExpectServerStream("grpctest.Service/ListItems"). WaitUntil(time.After(time.Second)). Return(`[{"message": "hello world!"}]`)
func (*ServerStreamRequest) WithHeader ¶
func (r *ServerStreamRequest) WithHeader(header string, value interface{}) *ServerStreamRequest
WithHeader sets an expected header of the given request.
Server.ExpectServerStream("grpctest.Service/ListItems"). WithHeader("Locale", "en-US")
func (*ServerStreamRequest) WithHeaders ¶
func (r *ServerStreamRequest) WithHeaders(headers map[string]interface{}) *ServerStreamRequest
WithHeaders sets a list of expected headers of the given request.
Server.ExpectServerStream("grpctest.Service/ListItems"). WithHeaders(map[string]interface{}{"Locale": "en-US"})
func (*ServerStreamRequest) WithPayload ¶
func (r *ServerStreamRequest) WithPayload(in interface{}) *ServerStreamRequest
WithPayload sets the expected payload of the given request. It could be a JSON []byte, JSON string, or a slice of objects (that will be marshaled).
Server.ExpectServerStream("grpctest.Service/ListItems"). WithPayload(`{"message": "hello world!"}`)
See: ServerStreamRequest.WithPayloadf().
func (*ServerStreamRequest) WithPayloadf ¶
func (r *ServerStreamRequest) WithPayloadf(format string, args ...interface{}) *ServerStreamRequest
WithPayloadf formats according to a format specifier and use it as the expected payload of the given request.
Server.ExpectServerStream("grpctest.Service/ListItems"). WithPayloadf(`{"message": "hello %s"}`, "john")
See: ServerStreamRequest.WithPayload().
type UnaryRequest ¶
type UnaryRequest struct {
// contains filtered or unexported fields
}
UnaryRequest represents the expectation for a unary request.
func NewUnaryRequest ¶
func NewUnaryRequest(locker sync.Locker, svc *service.Method) *UnaryRequest
NewUnaryRequest creates a new unary request.
func (*UnaryRequest) After ¶
func (r *UnaryRequest) After(d time.Duration) *UnaryRequest
After sets how long to block until the call returns.
Server.ExpectUnary("grpctest.Service/GetItem"). After(time.Second). Return("hello world!")
func (*UnaryRequest) Once ¶
func (r *UnaryRequest) Once() *UnaryRequest
Once indicates that the mock should only return the value once.
Server.ExpectUnary("grpctest.Service/GetItem"). Return("hello world!"). Once()
func (*UnaryRequest) Return ¶
func (r *UnaryRequest) Return(v interface{})
Return sets the result to return to client.
Server.ExpectUnary("grpctest.Service/GetItem"). Return(`{"message": "hello world!"}`)
func (*UnaryRequest) ReturnCode ¶
func (r *UnaryRequest) ReturnCode(code codes.Code)
ReturnCode sets the response code.
Server.ExpectUnary("grpctest.Service/GetItem"). ReturnCode(codes.OK)
func (*UnaryRequest) ReturnError ¶
func (r *UnaryRequest) ReturnError(code codes.Code, msg string)
ReturnError sets the response error.
Server.ExpectUnary("grpctest.Service/GetItem"). ReturnError(codes.Internal, "Internal Server Error")
func (*UnaryRequest) ReturnErrorMessage ¶
func (r *UnaryRequest) ReturnErrorMessage(msg string)
ReturnErrorMessage sets the response error message.
Server.ExpectUnary("grpctest.Service/GetItem"). ReturnErrorMessage("Internal Server Error")
func (*UnaryRequest) ReturnErrorf ¶
func (r *UnaryRequest) ReturnErrorf(code codes.Code, format string, args ...interface{})
ReturnErrorf sets the response error.
Server.ExpectUnary("grpctest.Service/GetItem"). ReturnErrorf(codes.NotFound, "Item %d not found", 42)
func (*UnaryRequest) ReturnFile ¶
func (r *UnaryRequest) ReturnFile(filePath string)
ReturnFile reads the file and uses its content as the result to return to client.
Server.ExpectUnary("grpctest.Service/GetItem"). ReturnFile("resources/fixtures/response.json")
func (*UnaryRequest) ReturnJSON ¶
func (r *UnaryRequest) ReturnJSON(v interface{})
ReturnJSON marshals the object using json.Marshal and uses it as the result to return to client.
Server.ExpectUnary("grpctest.Service/GetItem"). ReturnJSON(map[string]string{"foo": "bar"})
func (*UnaryRequest) Returnf ¶
func (r *UnaryRequest) Returnf(format string, args ...interface{})
Returnf formats according to a format specifier and use it as the result to return to client.
Server.ExpectUnary("grpctest.Service/GetItem"). Returnf(`{"message": %q}`, "hello")
func (*UnaryRequest) Run ¶
func (r *UnaryRequest) Run(handler func(ctx context.Context, in interface{}) (interface{}, error))
Run sets a custom handler to handle the given request.
Server.ExpectUnary("grpctest.Service/GetItem"). Run(func(ctx context.Context, in interface{}) (interface{}, error) { return &Item{}, nil })
func (*UnaryRequest) Times ¶
func (r *UnaryRequest) Times(i RepeatedTime) *UnaryRequest
Times indicates that the mock should only return the indicated number of times.
Server.ExpectUnary("grpctest.Service/GetItem"). Return("hello world!"). Times(5)
func (*UnaryRequest) Twice ¶
func (r *UnaryRequest) Twice() *UnaryRequest
Twice indicates that the mock should only return the value twice.
Server.ExpectUnary("grpctest.Service/GetItem"). Return("hello world!"). Twice()
func (*UnaryRequest) UnlimitedTimes ¶
func (r *UnaryRequest) UnlimitedTimes() *UnaryRequest
UnlimitedTimes indicates that the mock should return the value at least once and there is no max limit in the number of return.
Server.ExpectUnary("grpctest.Service/GetItem"). Return("hello world!"). UnlimitedTimes()
func (*UnaryRequest) WaitUntil ¶
func (r *UnaryRequest) WaitUntil(w <-chan time.Time) *UnaryRequest
WaitUntil sets the channel that will block the mocked return until its closed or a message is received.
Server.ExpectUnary("grpctest.Service/GetItem"). WaitUntil(time.After(time.Second)). Return("hello world!")
func (*UnaryRequest) WithHeader ¶
func (r *UnaryRequest) WithHeader(header string, value interface{}) *UnaryRequest
WithHeader sets an expected header of the given request.
Server.ExpectUnary("grpctest.Service/GetItem"). WithHeader("Locale", "en-US")
nolint: unparam
func (*UnaryRequest) WithHeaders ¶
func (r *UnaryRequest) WithHeaders(headers map[string]interface{}) *UnaryRequest
WithHeaders sets a list of expected headers of the given request.
Server.ExpectUnary("grpctest.Service/GetItem"). WithHeaders(map[string]interface{}{"Locale": "en-US"})
func (*UnaryRequest) WithPayload ¶
func (r *UnaryRequest) WithPayload(in interface{}) *UnaryRequest
WithPayload sets the expected payload of the given request. It could be []byte, string, or a matcher.Matcher.
Server.ExpectUnary("grpctest.Service/GetItem"). WithPayload(`{"id": 41}`) Server.ExpectUnary("grpctest.Service/GetItem"). WithPayload(&Item{Id: 41}) Server.ExpectUnary("grpctest.Service/GetItem"). WithPayload(func(actual interface{}) (bool, error) { in, ok := actual.(*Item) if !ok { return false, nil } return in.Id == 42, nil }) Server.ExpectUnary("grpctest.Service/GetItem"). WithPayload(&Item{Id: 41})
func (*UnaryRequest) WithPayloadf ¶
func (r *UnaryRequest) WithPayloadf(format string, args ...interface{}) *UnaryRequest
WithPayloadf formats according to a format specifier and use it as the expected payload of the given request.
Server.ExpectUnary("grpctest.Service/GetItem"). WithPayloadf(`{"message": "hello %s"}`, "john")