grpcmock

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2021 License: MIT Imports: 8 Imported by: 0

README

gRPC Test Utilities for Golang

GitHub Releases Build Status codecov Go Report Card GoDevDoc Donate

Test gRPC service and client like a pro.

Prerequisites

  • Go >= 1.16

Install

go get github.com/nhatthm/grpcmock

Usage

Invoke a gRPC method
Unary Method
package main

import (
	"context"
	"time"

	"github.com/nhatthm/grpcmock"
	"google.golang.org/grpc/test/bufconn"
)

func getItem(l *bufconn.Listener, id int32) (interface{}, error) {
	ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*50)
	defer cancel()

	out := &Item{}
	err := grpcmock.InvokeUnary(ctx, "myservice/GetItem",
		&GetItemRequest{Id: id}, out,
		grpcmock.WithHeader("Locale", "en-US"),
		grpcmock.WithBufConnDialer(l),
		grpcmock.WithInsecure(),
	)

	return out, err
}

Donation

If this project help you reduce time to develop, you can give me a cup of coffee :)

Paypal donation

paypal

       or scan this

Documentation

Overview

Package grpcmock provides functionalities for testing grpc client and server.

Index

Constants

View Source
const (
	// ErrMissingMethod indicates that there is no method in the url.
	ErrMissingMethod err = "missing method"
)

Variables

This section is empty.

Functions

func InvokeUnary

func InvokeUnary(
	ctx context.Context,
	method string,
	in interface{},
	out interface{},
	opts ...InvokeOption,
) error

InvokeUnary invokes a unary method.

Types

type ContextDialer

type ContextDialer = func(context.Context, string) (net.Conn, error)

ContextDialer is to set up the dialer.

type InvokeOption

type InvokeOption func(c *invokeConfig)

InvokeOption sets invoker config.

func WithBufConnDialer

func WithBufConnDialer(l *bufconn.Listener) InvokeOption

WithBufConnDialer sets a *bufconn.Listener as the context dialer.

See:

  • grpcmock.WithContextDialer()

func WithCallOption

func WithCallOption(opts ...grpc.CallOption) InvokeOption

WithCallOption sets call options.

func WithContextDialer

func WithContextDialer(d ContextDialer) InvokeOption

WithContextDialer sets a context dialer to create connections.

See:

  • grpcmock.WithBufConnDialer()

func WithDialOptions

func WithDialOptions(opts ...grpc.DialOption) InvokeOption

WithDialOptions sets dial options.

func WithHeader

func WithHeader(key, value string) InvokeOption

WithHeader sets request header.

func WithHeaders

func WithHeaders(header map[string]string) InvokeOption

WithHeaders sets request header.

func WithInsecure

func WithInsecure() InvokeOption

WithInsecure disables transport security for the connections.

Jump to

Keyboard shortcuts

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