request

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2022 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package request provides all the expectations for an RPC method.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CountCall

func CountCall(r Request)

CountCall sets the repeatability for the given request.

func Handle

func Handle(ctx context.Context, r Request, in interface{}, out interface{}) error

Handle handles the request.

func HeaderMatcher

func HeaderMatcher(r Request) matcher.HeaderMatcher

HeaderMatcher returns the headerMatcher matcher of the given request.

func NumCalls

func NumCalls(r Request) int

NumCalls returns the number of times the request was called.

func PayloadMatcher

func PayloadMatcher(r Request) *matcher.PayloadMatcher

PayloadMatcher returns the payload matcher of the given request.

func ServiceMethod

func ServiceMethod(r Request) service.Method

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

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

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

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

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

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

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

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

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

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

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 Handler

type Handler func(ctx context.Context, in interface{}, out interface{}) error

Handler handles a grpc request.

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

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

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

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

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")

Jump to

Keyboard shortcuts

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