Skip to content

joshcarp/servermock

Repository files navigation

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.

About

mock grpc and http servers with ease

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published