https://github.com/joshcarp/servermock
mock grpc and http servers with ease
https://github.com/joshcarp/servermock
golang grpc http mock
Last synced: 16 days ago
JSON representation
mock grpc and http servers with ease
- Host: GitHub
- URL: https://github.com/joshcarp/servermock
- Owner: joshcarp
- License: apache-2.0
- Created: 2021-04-09T03:47:11.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-03-07T02:41:22.000Z (almost 3 years ago)
- Last Synced: 2026-01-12T05:24:48.619Z (about 1 month ago)
- Topics: golang, grpc, http, mock
- Language: Go
- Homepage:
- Size: 99.6 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
servermock
[]()
[](https://github.com/joshcarp/servermock/issues)
[](https://github.com/joshcarp/servermock/pulls)
[](/LICENSE)
---
## 📝 Table of Contents
- [About](#about)
- [Getting Started](#getting_started)
- [Usage](#usage)
- [Authors](#authors)
- [Acknowledgments](#acknowledgement)
servermock is a go package that can be used to mock out http or grpc servers simply without any external server implementations.
### Inline in golang
1. Start a server
```golang
err := servermock.Serve(ctx, Printf, ":8000")
```
2. Set the response
```golang
err = servermock.SetResponse("http://localhost:8000", servermock.Request{
Path: "/foo.service.bar.SomethingAPI/GetWhatever",
Body: []byte(`{"Hello": "true"}`),
StatusCode: 200,
})
```
3. Send a request
```golang
resp, err := http.Get("http://localhost:8000/foo.service.bar.SomethingAPI/GetWhatever")
// {"Hello": "true"}
```
### In a docker container
1. Run the docker container
```bash
docker run -p 8000:8000 joshcarp/servermock
```
2. Set the response conforming to the `servermock.Request` type
```bash
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
```
3. Send a request
```bash
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](example/example_test.go) for full examples.
## ✍️ Authors
- [@joshcarp](https://github.com/joshcarp)
## 🎉 Acknowledgements
- [@emmaCullen](https://github.com/emmaCullen) had the original idea for this package.
- [github.com/dnaeon/go-vcr](https://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.