Sign inSign up

avijeet7/protomock

By avijeet7

โ€ขUpdated 20 days ago

Serve mock HTTP/gRPC responses from .proto files and JSON stubs

Image
API management
0

1.7K

avijeet7/protomock repository overview

โ ๐Ÿงช ProtoMock: Dynamic HTTP & gRPC Mock Server

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.


โ ๐Ÿ“ฆ Features

  • โœ… Serve both HTTP and gRPC endpoints from a single mock server
  • ๐Ÿ” Automatically load .proto + stub.json pairs from mocks/ folders
  • โšก Supports dynamic path + message type mapping from stubs
  • ๐Ÿง  JSON stub bodies auto-marshaled to Protobuf
  • ๐Ÿ” Header & body request matching for HTTP and gRPC
  • ๐Ÿค– Supports Protobuf or JSON response encoding
  • ๐ŸŽฏ Regex URL matching for HTTP endpoints
  • ๐Ÿ›  gRPC reflection for grpcurl debugging
  • ๐Ÿ” Optional header and partial body matching (WireMock style)
  • ๐Ÿ’ป Web UI to view configured mocks
  • ๐Ÿ“ Supports plain JSON mocks for http endpoints
  • ๐Ÿงฐ Clean, extensible codebase with modular separation

โ ๐Ÿš€ How to Use

  1. Start the server You can run using Docker or from source.

  2. 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
    
  3. 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
      }
    }
    

โ โœจ Web UI

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.

โ ๐Ÿ“ Plain JSON Mocks

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"
      }
    ]
  }
}

โ ๐ŸŽฏ Regex URL Matching

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.


โ ๐Ÿ“ Project Structure

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

โ ๐Ÿš€ Running the Project

โ โ–ถ๏ธ For Local Development
docker-compose up --build
โ โ–ถ๏ธ From DockerHub (User Usage)
docker run -p 8085:8085 -p 8086:8086 \
  -v $(pwd)/mymocks:/app/mocks \
  avijeet7/protomock:latest

Replace $(pwd)/mymocks with your actual mocks directory.


โ ๐Ÿงช Testing

โ ๐Ÿ”— HTTP Example
curl -X POST http://localhost:8085/hello/http \
  -H "Content-Type: application/json" \
  -H "X-Test-Header: mocked" \
  -d '{"user_id": "abc123"}'
โ ๐Ÿ”— Regex HTTP Example
curl -X GET http://localhost:8085/hello/123/world
โ ๐Ÿ”— gRPC Example
grpcurl -plaintext \
  -proto mocks/grpc/hello/hello.proto \
  -d '{}' \
  localhost:8086 test.FakeService/Hello

โ ๐Ÿ“Œ License

MIT โ€” free to use and modify.

Tag summary

Content type

Image

Digest

sha256:7a2ace7dbโ€ฆ

Size

11.3 MB

Last updated

20 days ago

docker pull avijeet7/protomock