fauxrpc

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2024 License: MIT Imports: 14 Imported by: 2

README

FauxRPC

Quickly and easily set up a mock gRPC/gRPC-Web/Connect/Protobuf-powered REST API that returns random test data. No complicated code generation step, just pass the protobuf descriptors and go!

flowchart LR

fauxrpc(FauxRPC)

bsr("BSR\n(Buf Schema Registry)") -->|buf build| descriptors
descriptors("Descriptors\nbinpb,json,txtpb,yaml") --> fauxrpc
protobuf(Protobuf Files) --> fauxrpc
fauxrpc -->|gRPC| microservices(Microservices)
fauxrpc -->|gRPC-Web| frontend(Web Frontend)
fauxrpc -->|Connect| other-frontend(Other Frontend)
fauxrpc -->|REST| api(REST API Client)
Mock out services from descriptors

Now you can use protobuf descriptors to automatically make a fake API:

$ buf build buf.build/bufbuild/registry -o descriptors.binpb
$ go run github.com/sudorandom/fauxrpc/cmd/fauxrpc@latest run --schema=descriptors.binpb
Mock out services using reflection

Use the reflection API to fake another gRPC server(s).

$ go run github.com/sudorandom/fauxrpc/cmd/fauxrpc@latest run --schema=https://demo.connectrpc.com
2024/08/15 19:10:01 INFO add file name=connectrpc.eliza.v1 path=connectrpc/eliza/v1/eliza.proto
2024/08/15 19:10:01 INFO Listening on http://127.0.0.1:6660
2024/08/15 19:10:01 INFO See available methods: buf curl --http2-prior-knowledge http://127.0.0.1:6660 --list-methods

# (in another shell) List out methods
$ buf curl --http2-prior-knowledge http://127.0.0.1:6660 --list-methods
connectrpc.eliza.v1.ElizaService/Converse
connectrpc.eliza.v1.ElizaService/Introduce
connectrpc.eliza.v1.ElizaService/Say

# Make a request! (connect)
$ buf curl --http2-prior-knowledge http://127.0.0.1:6660/connectrpc.eliza.v1.ElizaService/Say
{
  "sentence": "Mollitia ratione ea modi libero corrupti minus qui autem et."
}

# Make a request with gRPC-Web
$ buf curl --http2-prior-knowledge --protocol=grpcweb http://127.0.0.1:6660/connectrpc.eliza.v1.ElizaService/Say
{
  "sentence": "Eos illum consequatur adipisci eum et voluptatum quas id consequatur."
}

# Make a request with gRPC-Web
$ buf curl --http2-prior-knowledge --protocol=grpc http://127.0.0.1:6660/connectrpc.eliza.v1.ElizaService/Say
{
  "sentence": "Autem voluptatem quam aut ipsam voluptatem velit architecto ducimus quibusdam."
}
Support for many different descriptors from different sources

This shows using descriptors from a file and from server reflection:

$ go run github.com/sudorandom/fauxrpc/cmd/fauxrpc@latest run --schema=descriptors.binpb --schema=https://demo.connectrpc.com

Status: Alpha

This project is just starting out. I plan to add a lot of things that make this tool actually usable in more situations.

  • Use known protovalidate rules to determine how to generate output.
  • Service for adding/updating/removing stub responses.
  • Configuration file
  • BSR Support (maybe, using buf build to emit descriptors works well enough IMO)
  • Templating for stub responses, maybe give the option to use values from the input in the output
  • Heuristics "firstName" should probably generate first names, etc.
  • Testing for REST translations. I have no idea if this actually works
  • Streaming support

Documentation

Index

Constants

View Source
const MaxNestedDepth = 20

Variables

This section is empty.

Functions

func GenerateBytes

func GenerateBytes(faker *gofakeit.Faker, hints BytesHints) []byte

func GenerateFixed32

func GenerateFixed32(faker *gofakeit.Faker, hints Fixed32Hints) uint32

func GenerateFixed64

func GenerateFixed64(faker *gofakeit.Faker, hints Fixed64Hints) uint64

func GenerateFloat32

func GenerateFloat32(faker *gofakeit.Faker, hints Float32Hints) float32

func GenerateFloat64

func GenerateFloat64(faker *gofakeit.Faker, hints Float64Hints) float64

func GenerateInt32

func GenerateInt32(faker *gofakeit.Faker, hints Int32Hints) int32

func GenerateInt64

func GenerateInt64(faker *gofakeit.Faker, hints Int64Hints) int64

func GenerateSFixed64

func GenerateSFixed64(faker *gofakeit.Faker, hints SFixed64Hints) int64

func GenerateSFixedInt32

func GenerateSFixedInt32(faker *gofakeit.Faker, hints SFixedInt32Hints) int32

func GenerateSInt32

func GenerateSInt32(faker *gofakeit.Faker, hints SInt32Hints) int32

func GenerateSInt64

func GenerateSInt64(faker *gofakeit.Faker, hints SInt64Hints) int64

func GenerateString

func GenerateString(faker *gofakeit.Faker, hints StringHints) string

func GenerateUInt32

func GenerateUInt32(faker *gofakeit.Faker, hints UInt32Hints) uint32

func GenerateUInt64

func GenerateUInt64(faker *gofakeit.Faker, hints UInt64Hints) uint64

func NewDataGenerator

func NewDataGenerator() *dataGenerator

Types

type BytesHints

type BytesHints struct {
	Rules     *validate.BytesRules
	FirstName bool
	LastName  bool
	Name      bool
	UUID      bool
	URL       bool
	Version   bool
}

type DataGenerator

type DataGenerator interface {
	SetData(msg *dynamicpb.Message)
}

type Fixed32Hints

type Fixed32Hints struct {
	Rules *validate.Fixed32Rules
}

type Fixed64Hints

type Fixed64Hints struct {
	Rules *validate.Fixed64Rules
}

type Float32Hints

type Float32Hints struct {
	Rules *validate.FloatRules
}

type Float64Hints

type Float64Hints struct {
	Rules *validate.DoubleRules
}

type Int32Hints

type Int32Hints struct {
	Rules *validate.Int32Rules
}

type Int64Hints

type Int64Hints struct {
	Rules *validate.Int64Rules
}

type SFixed64Hints

type SFixed64Hints struct {
	Rules *validate.SFixed64Rules
}

type SFixedInt32Hints

type SFixedInt32Hints struct {
	Rules *validate.SFixed32Rules
}

type SInt32Hints

type SInt32Hints struct {
	Rules *validate.SInt32Rules
}

type SInt64Hints

type SInt64Hints struct {
	Rules *validate.SInt64Rules
}

type StringHints

type StringHints struct {
	Rules     *validate.StringRules
	FirstName bool
	LastName  bool
	Name      bool
	UUID      bool
	URL       bool
	Version   bool
}

type UInt32Hints

type UInt32Hints struct {
	Rules *validate.UInt32Rules
}

type UInt64Hints

type UInt64Hints struct {
	Rules *validate.UInt64Rules
}

Directories

Path Synopsis
cmd
fauxrpc command
example
private

Jump to

Keyboard shortcuts

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