Serve mock HTTP/gRPC responses from .proto files and JSON stubs
1.7K
ProtoMock is a powerful Go-based server that dynamically mocks HTTP and gRPC endpoints using .proto files and JSON stub responses. It supports advanced matching and flexible configuration, making it ideal for integration testing, sandbox environments, and frontend/backend development workflows.
.proto + stub.json pairs from mocks/ foldersgrpcurl debuggingStart the server You can run using Docker or from source.
Create your mocks folder
Your mocks directory should follow this structure:
mocks/
โโโ http/
โ โโโ <mockName>/
โ โโโ service.proto
โ โโโ stubs/
โ โโโ stub1.json
โ โโโ stub2.json
โโโ grpc/
โโโ <mockName>/
โโโ service.proto
โโโ stubs/
โโโ stub1.json
โโโ stub2.json
Stub Format Each stub JSON must define:
{
"request": {
"method": "POST",
"url": "/test.FakeService/Hello",
"headers": {
"X-Test-Header": "mocked1"
},
"body": {
"user_id": "abc123"
}
},
"response": {
"status": 200,
"message": "test.TestResponse",
"body": {
"message": "Hello from fake service",
"code": 200
},
"proto": true
}
}
ProtoMock includes a web interface to view all configured mock endpoints. Simply navigate to http://localhost:8085/protomock-ui in your browser to see a list of all loaded HTTP and gRPC stubs.
For HTTP endpoints, you can now use plain JSON mocks without a .proto file. This is useful for mocking simple REST APIs. To use this feature, create a stubs directory inside mocks/http/<mockName>/ and place your JSON files there. The server will automatically pick them up.
In the stub file, you can omit the response.message and response.proto fields. For example:
{
"request": {
"method": "GET",
"url": "/api/v1/users"
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"body": [
{
"id": 1,
"name": "John Doe"
},
{
"id": 2,
"name": "Jane Doe"
}
]
}
}
ProtoMock supports regex matching for HTTP endpoint URLs. This allows you to match dynamic URLs with a single stub.
To use this feature, simply provide a valid regex in the url field of your stub file. For example:
{
"request": {
"method": "GET",
"url": "/hello/[0-9]+/world"
},
"response": {
"status": 200,
"body": {
"message": "Hello from regex world"
}
}
}
This stub will match any URL that starts with /hello/, followed by one or more digits, and ends with /world. For example, it will match /hello/123/world and /hello/456/world.
protomock/
โโโ cmd/
โ โโโ server/
โ โโโ main.go
โโโ internal/
โ โโโ grpcserver/
โ โ โโโ server.go
โ โ โโโ handler.go
โ โ โโโ utils.go
โ โโโ httpserver/
โ โ โโโ server.go
โ โ โโโ handler.go
โ โ โโโ matcher.go
โ โโโ loader/
โ โ โโโ loader.go
โ โ โโโ parser.go
โ โ โโโ message.go
โ โโโ models/
โ โโโ route.go
โ โโโ stub.go
โโโ mocks/
โ โโโ http/
โ โ โโโ <service>/...
โ โโโ grpc/
โ โโโ <service>/...
โโโ docker-compose.yml
docker-compose up --build
docker run -p 8085:8085 -p 8086:8086 \
-v $(pwd)/mymocks:/app/mocks \
avijeet7/protomock:latest
Replace
$(pwd)/mymockswith your actual mocks directory.
curl -X POST http://localhost:8085/hello/http \
-H "Content-Type: application/json" \
-H "X-Test-Header: mocked" \
-d '{"user_id": "abc123"}'
curl -X GET http://localhost:8085/hello/123/world
grpcurl -plaintext \
-proto mocks/grpc/hello/hello.proto \
-d '{}' \
localhost:8086 test.FakeService/Hello
MIT โ free to use and modify.
Content type
Image
Digest
sha256:7a2ace7dbโฆ
Size
11.3 MB
Last updated
20 days ago
docker pull avijeet7/protomock