request

package
v0.25.0 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2023 License: MIT Imports: 23 Imported by: 1

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func CountCall deprecated

func CountCall(r Request)

CountCall sets the repeatability for the given request.

Deprecated: See go.nhat.io/grpcmock/planner.Expectation.

func Handle deprecated

func Handle(ctx context.Context, r Request, in any, out any) error

Handle handles the request.

Deprecated: The function will be removed in the future.

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 NumCalls deprecated

func NumCalls(r Request) int

NumCalls returns the number of times the request was called.

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 ServiceMethod(r Request) service.Method

ServiceMethod returns the service method of the given request.

Deprecated: See go.nhat.io/grpcmock/planner.Expectation.

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

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

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

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

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

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

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

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

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 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 Handler

type Handler func(ctx context.Context, in any, out any) error

Handler handles a grpc request.

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

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

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

func (r *UnaryRequest) Run(handler func(ctx context.Context, in any) (any, error))

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

Jump to

Keyboard shortcuts

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