Documentation ¶
Overview ¶
Package request provides all the expectations for an RPC method.
Deprecated: The package will be removed in the future. There is no replacement.
Index ¶
- func CountCall(r Request)deprecated
- func Handle(ctx context.Context, r Request, in any, out any) errordeprecated
- func HeaderMatcher(r Request) matcher.HeaderMatcherdeprecated
- func NumCalls(r Request) intdeprecated
- func PayloadMatcher(r Request) *matcher.PayloadMatcherdeprecated
- func ServiceMethod(r Request) service.Methoddeprecated
- func SetRepeatability(r Request, i RepeatedTime)deprecated
- type BidirectionalStreamRequestdeprecated
- 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 ...any)
- 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 any) *BidirectionalStreamRequest
- func (r *BidirectionalStreamRequest) WithHeaders(headers map[string]any) *BidirectionalStreamRequest
- type ClientStreamRequestdeprecated
- func (r *ClientStreamRequest) After(d time.Duration) *ClientStreamRequest
- func (r *ClientStreamRequest) Once() *ClientStreamRequest
- func (r *ClientStreamRequest) Return(v any)
- 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 ...any)
- func (r *ClientStreamRequest) ReturnFile(filePath string)
- func (r *ClientStreamRequest) ReturnJSON(v any)
- func (r *ClientStreamRequest) Returnf(format string, args ...any)
- func (r *ClientStreamRequest) Run(handler func(ctx context.Context, s grpc.ServerStream) (any, 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 any) *ClientStreamRequest
- func (r *ClientStreamRequest) WithHeaders(headers map[string]any) *ClientStreamRequest
- func (r *ClientStreamRequest) WithPayload(in any) *ClientStreamRequest
- func (r *ClientStreamRequest) WithPayloadf(format string, args ...any) *ClientStreamRequest
- type Handler
- type RepeatedTimedeprecated
- type Requestdeprecated
- type ServerStreamRequestdeprecated
- func (r *ServerStreamRequest) After(d time.Duration) *ServerStreamRequest
- func (r *ServerStreamRequest) Once() *ServerStreamRequest
- func (r *ServerStreamRequest) Return(v any)
- 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 ...any)
- func (r *ServerStreamRequest) ReturnFile(filePath string)
- func (r *ServerStreamRequest) ReturnJSON(v any)
- func (r *ServerStreamRequest) ReturnStream() *serverStreamHandler
- func (r *ServerStreamRequest) Returnf(format string, args ...any)
- func (r *ServerStreamRequest) Run(handler func(ctx context.Context, in any, 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 any) *ServerStreamRequest
- func (r *ServerStreamRequest) WithHeaders(headers map[string]any) *ServerStreamRequest
- func (r *ServerStreamRequest) WithPayload(in any) *ServerStreamRequest
- func (r *ServerStreamRequest) WithPayloadf(format string, args ...any) *ServerStreamRequest
- type UnaryRequestdeprecated
- func (r *UnaryRequest) After(d time.Duration) *UnaryRequest
- func (r *UnaryRequest) Once() *UnaryRequest
- func (r *UnaryRequest) Return(v any)
- 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 ...any)
- func (r *UnaryRequest) ReturnFile(filePath string)
- func (r *UnaryRequest) ReturnJSON(v any)
- func (r *UnaryRequest) Returnf(format string, args ...any)
- func (r *UnaryRequest) Run(handler func(ctx context.Context, in any) (any, 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 any) *UnaryRequest
- func (r *UnaryRequest) WithHeaders(headers map[string]any) *UnaryRequest
- func (r *UnaryRequest) WithPayload(in any) *UnaryRequest
- func (r *UnaryRequest) WithPayloadf(format string, args ...any) *UnaryRequest
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HeaderMatcher
deprecated
func HeaderMatcher(r Request) matcher.HeaderMatcher
HeaderMatcher returns the headerMatcher matcher of the given request.
Deprecated: See go.nhat.io/grpcmock/planner.Expectation.
func PayloadMatcher
deprecated
func PayloadMatcher(r Request) *matcher.PayloadMatcher
PayloadMatcher returns the payload matcher of the given request.
Deprecated: See go.nhat.io/grpcmock/planner.Expectation.
func ServiceMethod
deprecated
func SetRepeatability
deprecated
func SetRepeatability(r Request, i RepeatedTime)
SetRepeatability sets the repeatability for the given request.
Deprecated: The function will be removed in the future.
Types ¶
type BidirectionalStreamRequest
deprecated
type BidirectionalStreamRequest struct {
// contains filtered or unexported fields
}
BidirectionalStreamRequest represents the expectation for a client-stream request.
Deprecated: Use go.nhat.io/grpcmock.BidirectionalStreamExpectation instead.
func NewBidirectionalStreamRequest
deprecated
func NewBidirectionalStreamRequest(locker sync.Locker, svc *service.Method) *BidirectionalStreamRequest
NewBidirectionalStreamRequest creates a new client-stream expectation.
Deprecated: The function will be removed in the future.
func (*BidirectionalStreamRequest) After ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
func (r *BidirectionalStreamRequest) ReturnErrorf(code codes.Code, format string, args ...any)
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
func (r *BidirectionalStreamRequest) WithHeader(header string, value any) *BidirectionalStreamRequest
WithHeader sets an expected header of the given request.
Server.ExpectBidirectionalStream("grpctest.Service/TransformItems"). WithHeader("Locale", "en-US")
func (*BidirectionalStreamRequest) WithHeaders ¶
func (r *BidirectionalStreamRequest) WithHeaders(headers map[string]any) *BidirectionalStreamRequest
WithHeaders sets a list of expected headers of the given request.
Server.ExpectBidirectionalStream("grpctest.Service/TransformItems"). WithHeaders(map[string]any{"Locale": "en-US"})
type ClientStreamRequest
deprecated
type ClientStreamRequest struct {
// contains filtered or unexported fields
}
ClientStreamRequest represents the expectation for a client-stream request.
Deprecated:go.nhat.io/grpcmock.ClientStreamExpectation instead.
func NewClientStreamRequest
deprecated
func NewClientStreamRequest(locker sync.Locker, svc *service.Method) *ClientStreamRequest
NewClientStreamRequest creates a new client-stream expectation.
Deprecated: The function will be removed in the future.
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 any)
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 ...any)
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 any)
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]any{"num_items": 1})
See: ClientStreamRequest.Return(), ClientStreamRequest.Returnf(), ClientStreamRequest.ReturnFile().
func (*ClientStreamRequest) Returnf ¶
func (r *ClientStreamRequest) Returnf(format string, args ...any)
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) (any, error))
Run sets a custom handler to handle the given request.
Server.ExpectClientStream("grpc.Service/CreateItems"). Run(func(context.Context, grpc.ServerStreamer) (any, 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 any) *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]any) *ClientStreamRequest
WithHeaders sets a list of expected headers of the given request.
Server.ExpectClientStream("grpctest.Service/CreateItems"). WithHeaders(map[string]any{"Locale": "en-US"})
func (*ClientStreamRequest) WithPayload ¶
func (r *ClientStreamRequest) WithPayload(in any) *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 ...any) *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
deprecated
type RepeatedTime uint32
RepeatedTime represents a number of times that a request could be repeated.
Deprecated: Use uint instead.
const UnlimitedTimes RepeatedTime = 0
UnlimitedTimes indicates that a request could be repeated without limits.
Deprecated: Use go.nhat.io/grpcmock/planner.UnlimitedTimes instead.
func Repeatability
deprecated
func Repeatability(r Request) RepeatedTime
Repeatability gets the repeatability of the given request.
Deprecated: See go.nhat.io/grpcmock/planner.Expectation.
type Request
deprecated
type Request interface {
// contains filtered or unexported methods
}
Request represents the grpc request expectation.
Deprecated: Use go.nhat.io/grpcmock.UnaryExpectation, go.nhat.io/grpcmock.ServerStreamExpectation, go.nhat.io/grpcmock.ClientStreamExpectation, go.nhat.io/grpcmock.BidirectionalStreamExpectation instead.
type ServerStreamRequest
deprecated
type ServerStreamRequest struct {
// contains filtered or unexported fields
}
ServerStreamRequest represents the expectation for a server-stream request.
Deprecated: Use go.nhat.io/grpcmock.ServerStreamExpectation instead.
func NewServerStreamRequest
deprecated
func NewServerStreamRequest(locker sync.Locker, svc *service.Method) *ServerStreamRequest
NewServerStreamRequest creates a new server-stream expectation.
Deprecated: The function will be removed in the future.
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 any)
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 ...any)
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 any)
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 ...any)
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 any, 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 any, srv any) 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 any) *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]any) *ServerStreamRequest
WithHeaders sets a list of expected headers of the given request.
Server.ExpectServerStream("grpctest.Service/ListItems"). WithHeaders(map[string]any{"Locale": "en-US"})
func (*ServerStreamRequest) WithPayload ¶
func (r *ServerStreamRequest) WithPayload(in any) *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 ...any) *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
deprecated
type UnaryRequest struct {
// contains filtered or unexported fields
}
UnaryRequest represents the expectation for a unary request.
Deprecated: Use go.nhat.io/grpcmock.UnaryExpectation instead.
func NewUnaryRequest
deprecated
func NewUnaryRequest(locker sync.Locker, svc *service.Method) *UnaryRequest
NewUnaryRequest creates a new unary request.
Deprecated: The function will be removed in the future.
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 any)
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 ...any)
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 any)
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 ...any)
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 ¶
Run sets a custom handler to handle the given request.
Server.ExpectUnary("grpctest.Service/GetItem"). Run(func(ctx context.Context, in any) (any, 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 any) *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]any) *UnaryRequest
WithHeaders sets a list of expected headers of the given request.
Server.ExpectUnary("grpctest.Service/GetItem"). WithHeaders(map[string]any{"Locale": "en-US"})
func (*UnaryRequest) WithPayload ¶
func (r *UnaryRequest) WithPayload(in any) *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 any) (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 ...any) *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")