servermock

package module
v0.0.0-...-4764861 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2021 License: Apache-2.0 Imports: 21 Imported by: 0

README

servermock

Status GitHub Issues GitHub Pull Requests License


📝 Table of Contents

🧐 About

servermock is a go package that can be used to mock out http or grpc servers simply without any external server implementations.

🚀 Usage

Inline in golang

  1. Start a server
err := servermock.Serve(ctx, Printf, ":8000")
  1. Set the response
err = servermock.SetResponse("http://localhost:8000", servermock.Request{
		Path:       "/foo.service.bar.SomethingAPI/GetWhatever",
		Body:       []byte(`{"Hello": "true"}`),
		StatusCode: 200,
	})
  1. Send a request
resp, err := http.Get("http://localhost:8000/foo.service.bar.SomethingAPI/GetWhatever")
// {"Hello": "true"}

In a docker container

  1. Run the docker container
docker run -p 8000:8000 joshcarp/servermock
  1. Set the response conforming to the servermock.Request type
curl --header "Content-Type: application/json" --header "SERVERMOCK-MODE: SET" --request POST --data '{"path":"/foo.service.bar.SomethingAPI/GetWhatever","body":"eyJIZWxsbyI6ICJ0cnVlIn0=","status_code":200' http://localhost:8000/foo.service.bar.SomethingAPI/GetWhatever
  1. Send a request
curl localhost:8000/foo.service.bar.SomethingAPI/GetWhatever
> {"Hello": "true"}                                                                                                  

gRPC vs REST servers

Setting data always occurs over http 1.0 using the json payload, gRPC servers are, after all, just servers that return some bytes.

see example/example_test.go for full examples.

✍️ Authors

🎉 Acknowledgements

  • @emmaCullen had the original idea for this package.
  • github.com/dnaeon/go-vcr is similar but different; whilst any network traffic can be recorded and replayed, servermock tries tosimplify mocking of servers in unit tests/contexts where writing a specific server implementation is a little too much.

Documentation

Index

Constants

View Source
const HEADERMODE = "SERVERMOCK-MODE"

Variables

This section is empty.

Functions

func InterfaceToWriter

func InterfaceToWriter(v interface{}, wr http.ResponseWriter)

func New

func New(log Logger) *server

func Reset

func Reset(url string, pth string) error

func Serve

func Serve(ctx context.Context, log Logger, addr string) error

Serve servers a servermock server and blocks until the server is running. Use context.WithCancel to stop the server

func ServeLis

func ServeLis(ctx context.Context, log Logger, ln net.Listener) error

func ServeRand

func ServeRand(ctx context.Context, log Logger) (int, error)

func SetGRPCResponse

func SetGRPCResponse(url string, m proto.Message, request Request) error

SetGRPCResponse sets the return to pth to a bytes marshaled from a proto message

func SetJsonResponse

func SetJsonResponse(url string, pth string, m interface{}, headers metadata.MD, isError bool, statusCode int) error

SetGRPCResponse sets the return to pth to bytes marshaled from an interface

func SetResponse

func SetResponse(url string, request Request) error

SetResponse sets the return to pth to that of b bytes

Types

type HeaderPair

type HeaderPair struct {
	Key string
	Val []string
}

type Logger

type Logger func(format string, args ...interface{})

type Request

type Request struct {
	Path       string              `json:"path"`
	Headers    map[string][]string `json:"headers"`
	Body       []byte              `json:"body"`
	IsError    bool                `json:"is_error"`
	StatusCode int                 `json:"status_code"`
	IsQueue    bool                `json:"is_queue"`
	HeaderKeys HeaderPair          `json:"header_keys"`
}

func GetResponses

func GetResponses(url string, pth string) (reqs []Request, err error)

GetResponse Gets all of the requests that are currently stored for a key

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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