fauxrpc

package module
v0.15.25 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2025 License: MIT Imports: 17 Imported by: 2

README

FauxRPC

Go Go Report Card Go Reference

FauxRPC is a powerful tool that empowers you to accelerate development and testing by effortlessly generating fake implementations of gRPC, gRPC-Web, Connect, and REST services. If you have a protobuf-based workflow, this tool could help.

Why FauxRPC?

  • Faster Development & Testing: Work independently without relying on fully functional backend services.
  • Isolation & Control: Test frontend components in isolation with controlled fake data.
  • Multi-Protocol Support: Supports multiple protocols (gRPC, gRPC-Web, Connect, and REST).
  • Prototyping & Demos: Create prototypes and demos quickly without building the full backend. Fake it till you make it.
  • API Stubs: Define static or dynamic API responses with powerful stubs featuring CEL expressions for precise behavior control. Stubs can be defined using config files or dynamically at runtime.
  • Improved Collaboration: Bridge the gap between frontend and backend teams.
  • Plays well with others: Test data from FauxRPC will try to automatically follow any protovalidate constraints that are defined.
  • Request Validation: Ensure data integrity with automatic request validation using protovalidate. Catch errors early and prevent invalid data from reaching your application logic.

See the the documentation website for more!

Get Started

Install via source
go install github.com/sudorandom/fauxrpc/cmd/fauxrpc@latest
Pre-built binaries

Binaries are built for several platforms for each release. See the latest ones on the releases page.

Quick Start

Pass protobuf descriptors to FauxRPC and a test server will be created, returning random fake data!

$ fauxrpc run --schema=service.binpb

That's... it. Now you can call it with your gRPC/gRPC-Web/Connect clients:

$ buf curl --http2-prior-knowledge http://127.0.0.1:6660/my.own.v1.service/HelloWorld
{
  "text": "Thundercats."
}

Go to the documentation website for more!

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFaked = errors.New("no data was faked either because there was no relevant data or the faker was just out of faker juice")
)

Functions

func Bool added in v0.0.9

Bool returns a fake boolean value given a field descriptor.

func Bytes added in v0.0.9

func Bytes(fd protoreflect.FieldDescriptor, opts GenOptions) []byte

Bytes returns a fake []byte value given a field descriptor.

func Enum added in v0.0.9

Enum returns a fake enum value given a field descriptor.

func FieldValue added in v0.3.0

func Fixed32 added in v0.0.9

Fixed32 returns a fake fixed32 value given a field descriptor.

func Fixed64 added in v0.0.9

Fixed64 returns a fake fixed64 value given a field descriptor.

func Float32 added in v0.0.9

Float32 returns a fake float32 value given a field descriptor.

func Float64 added in v0.0.9

Float64 returns a fake float64 value given a field descriptor.

func GoogleDuration added in v0.0.9

func GoogleDuration(fd protoreflect.FieldDescriptor, opts GenOptions) *durationpb.Duration

GoogleDuration generates a random google.protobuf.Duration value.

func GoogleTimestamp added in v0.0.9

func GoogleTimestamp(fd protoreflect.FieldDescriptor, opts GenOptions) *timestamppb.Timestamp

GoogleTimestamp generates a random google.protobuf.Timestamp value.

func GoogleValue added in v0.0.9

func GoogleValue(fd protoreflect.FieldDescriptor, opts GenOptions) *structpb.Value

func Int32 added in v0.0.9

Int32 returns a fake int32 value given a field descriptor.

func Int64 added in v0.0.9

Int64 returns a fake int64 value given a field descriptor.

func Map added in v0.0.16

Map returns a fake repeated value given a field descriptor.

func NewFauxFaker added in v0.3.0

func NewFauxFaker() *fauxFaker

func NewMessage added in v0.0.10

NewMessage creates a new message populated with fake data given a protoreflect.MessageDescriptor

Example
package main

import (
	"fmt"

	elizav1 "buf.build/gen/go/connectrpc/eliza/protocolbuffers/go/connectrpc/eliza/v1"
	"github.com/sudorandom/fauxrpc"
	"google.golang.org/protobuf/encoding/protojson"
)

func main() {
	msg, _ := fauxrpc.NewMessage(elizav1.File_connectrpc_eliza_v1_eliza_proto.Messages().ByName("SayResponse"), fauxrpc.GenOptions{})
	b, _ := protojson.MarshalOptions{Indent: "  "}.Marshal(msg)
	fmt.Println(string(b))
}
Output:

func NewMultiFaker added in v0.3.0

func NewMultiFaker(fakers []ProtoFaker) multiFaker

func Repeated added in v0.0.16

Repeated returns a fake repeated value given a field descriptor.

func SFixed32 added in v0.0.9

func SFixed32(fd protoreflect.FieldDescriptor, opts GenOptions) int32

SFixed32 returns a fake sfixedint32 value given a field descriptor.

func SFixed64 added in v0.0.9

func SFixed64(fd protoreflect.FieldDescriptor, opts GenOptions) int64

SFixed64 returns a fake sfixed64 value given a field descriptor.

func SInt32 added in v0.0.9

SInt32 returns a fake sint32 value given a field descriptor.

func SInt64 added in v0.0.9

SInt64 returns a fake sint64 value given a field descriptor.

func SetDataOnMessage added in v0.0.4

func SetDataOnMessage(msg protoreflect.ProtoMessage, opts GenOptions) error

SetDataOnMessage generates fake data given a protoreflect.ProtoMessage and sets the field values.

Example
package main

import (
	"fmt"
	"log"

	elizav1 "buf.build/gen/go/connectrpc/eliza/protocolbuffers/go/connectrpc/eliza/v1"
	"github.com/sudorandom/fauxrpc"
	"google.golang.org/protobuf/encoding/protojson"
)

func main() {
	msg := &elizav1.SayResponse{}
	if err := fauxrpc.SetDataOnMessage(msg, fauxrpc.GenOptions{}); err != nil {
		log.Fatalf("error: %s", err) // handle error
	}
	b, _ := protojson.MarshalOptions{Indent: "  "}.Marshal(msg)
	fmt.Println(string(b))
}
Output:

func String added in v0.0.9

String returns a fake string value given a field descriptor.

func UInt32 added in v0.0.9

UInt32 returns a fake uint32 value given a field descriptor.

func UInt64 added in v0.0.9

UInt64 returns a fake uint64 value given a field descriptor.

Types

type GenOptions added in v0.0.14

type GenOptions struct {
	MaxDepth int
	Faker    *gofakeit.Faker
	Context  context.Context
	// contains filtered or unexported fields
}

func (GenOptions) GetContext added in v0.3.0

func (st GenOptions) GetContext() context.Context

type ProtoFaker added in v0.3.0

type ProtoFaker interface {
	SetDataOnMessage(msg protoreflect.ProtoMessage, opts GenOptions) error
}

Jump to

Keyboard shortcuts

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