GithubHelp home page GithubHelp logo

k1low / grpcstub Goto Github PK

View Code? Open in Web Editor NEW
11.0 3.0 1.0 314 KB

grpcstub provides gRPC server and client conn ( *grpc.ClientConn ) for stubbing, for testing in Go.

License: MIT License

Makefile 3.23% Go 96.77%
buf go grpc protobuf stub stub-server testing buf-schema-registry

grpcstub's Introduction

grpcstub Go Reference Coverage Code to Test Ratio Test Execution Time

grpcstub provides gRPC server and client conn ( *grpc.ClientConn ) for stubbing, for testing in Go.

There is an HTTP version stubbing tool with the same design concept, httpstub.

Usage

package myapp

import (
	"io"
	"net/http"
	"testing"

	"github.com/k1LoW/grpcstub"
	"github.com/k1LoW/myapp/protobuf/gen/go/routeguide"
)

func TestClient(t *testing.T) {
	ctx := context.Background()
	ts := grpcstub.NewServer(t, "path/to/protobuf")
	t.Cleanup(func() {
		ts.Close()
	})
	ts.Method("GetFeature").Response(&routeguite.Feature{
		Name: "hello",
		Location: &routeguide.Point{
			Latitude:  10,
			Longitude: 13,
		},
	})
	// OR
	// ts.Method("GetFeature").Response(map[string]any{"name": "hello", "location": map[string]any{"latitude": 10, "longitude": 13}})

	client := routeguide.NewRouteGuideClient(ts.Conn())
	if _, err := client.GetFeature(ctx, &routeguide.Point{
		Latitude:  10,
		Longitude: 13,
	}); err != nil {
		t.Fatal(err)
	}
	{
		got := len(ts.Requests())
		if want := 1; got != want {
			t.Errorf("got %v\nwant %v", got, want)
		}
	}
	req := ts.Requests()[0]
	{
		got := int32(req.Message["longitude"].(float64))
		if want := int32(13); got != want {
			t.Errorf("got %v\nwant %v", got, want)
		}
	}
}

Dynamic Response

grpcstub can return responses dynamically using the protocol buffer schema.

Dynamic response to all requests

ts := grpcstub.NewServer(t, "path/to/protobuf")
t.Cleanup(func() {
	ts.Close()
})
ts.ResponseDynamic()

Dynamic response to a request to a specific method (rpc)

ts := grpcstub.NewServer(t, "path/to/protobuf")
t.Cleanup(func() {
	ts.Close()
})
ts.Service("routeguide.RouteGuide").Method("GetFeature").ResponseDynamic()

Dynamic response with your own generators

ts := grpcstub.NewServer(t, "path/to/protobuf")
t.Cleanup(func() {
	ts.Close()
})
fk := faker.New()
want := time.Now()
opts := []GeneratorOption{
	Generator("*_id", func(req *grpcstub.Request) any {
		return fk.UUID().V4()
	}),
	Generator("*_time", func(req *grpcstub.Request) any {
		return want
	}),
}
ts.ResponseDynamic(opts...)

Test data

References

grpcstub's People

Contributors

dependabot[bot] avatar github-actions[bot] avatar k1low avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

qawatake

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.