Categorygithub.com/joshcarp/servermock
modulepackage
0.0.0-20211028114158-47648612a19f
Repository: https://github.com/joshcarp/servermock.git
Documentation: pkg.go.dev

# 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.

# Packages

No description provided by the author
No description provided by the author

# Functions

GetResponse Gets all of the requests that are currently stored for a key */.
No description provided by the author
No description provided by the author
No description provided by the author
Serve servers a servermock server and blocks until the server is running.
No description provided by the author
No description provided by the author
SetGRPCResponse sets the return to pth to a bytes marshaled from a proto message */.
SetGRPCResponse sets the return to pth to bytes marshaled from an interface */.
SetResponse sets the return to pth to that of b bytes */.

# Constants

No description provided by the author

# Structs

No description provided by the author
No description provided by the author

# Type aliases

No description provided by the author