servermock is a go package that can be used to mock out http or grpc servers simply without any external server implementations.
- Start a server
err := servermock.Serve(ctx, Printf, ":8000")
- Set the response
err = servermock.SetResponse("http://localhost:8000", servermock.Request{
Path: "/foo.service.bar.SomethingAPI/GetWhatever",
Body: []byte(`{"Hello": "true"}`),
StatusCode: 200,
})
- Send a request
resp, err := http.Get("http://localhost:8000/foo.service.bar.SomethingAPI/GetWhatever")
// {"Hello": "true"}
- Run the docker container
docker run -p 8000:8000 joshcarp/servermock
- 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
- Send a request
curl localhost:8000/foo.service.bar.SomethingAPI/GetWhatever
> {"Hello": "true"}
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.
- @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.