Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bavix/gripmock

Repository files navigation

GripMock

Coverage Status Go Report Card License: MIT

GripMock πŸš€

The fastest and most reliable gRPC mock server for testing and development.

GripMock creates a mock server from your .proto files or compiled .pb descriptors, making gRPC testing simple and efficient. Perfect for end-to-end testing, development environments, and CI/CD pipelines.

✨ Features

  • πŸš€ Instant Setup - Create a working gRPC server in seconds
  • πŸ“ YAML & JSON Support - Define stubs in your preferred format
  • πŸ”„ All Streaming Types - Unary, server, client, and bidirectional streaming
  • ⚑ 20-35% Faster - Enhanced performance for quicker tests
  • πŸ”’ 100% Backward Compatible - All existing tests continue to work
  • 🐳 Docker Ready - Lightweight container for CI/CD
  • πŸ–₯️ Web Interface - Manage stubs through a friendly dashboard
  • πŸ“‹ JSON Schema - Full IDE support with validation
  • ❀️ Health Checks - Production-ready monitoring endpoints

πŸ“š Documentation

πŸ“– Full Documentation - Complete guide with examples

πŸ†š Why Choose Our Fork?

This service is a fork of tokopedia/gripmock, but you should choose our fork. Here's why:

πŸ†• New Features

  • βœ… YAML support as JSON alternative for static stubs
  • βœ… Health check endpoints (/api/health/liveness, /api/health/readiness)
  • βœ… Header matching support for authentication testing
  • βœ… gRPC error codes for realistic error simulation
  • βœ… Priority system for controlling stub matching order
  • βœ… Binary descriptor support (.pb files) for faster startup
  • βœ… Array streaming for server streaming methods
  • βœ… JSON Schema validation with IDE support
  • βœ… Enhanced performance with 20-35% speed improvements

πŸ”§ Improvements

  • βœ… Updated dependencies - All deprecated packages fixed
  • βœ… Reduced image size - Optimized Docker containers
  • βœ… Better error handling - 404 errors for missing stubs
  • βœ… Active maintenance - Regular updates and bug fixes
  • βœ… Comprehensive documentation - Complete guides and examples

πŸ–₯️ Web Interface

gripmock-ui

Access the web dashboard at http://localhost:4771/ to manage your stubs visually.

πŸš€ Quick Start

Installation

Choose your preferred installation method:

🍺 Homebrew (Recommended)

brew tap gripmock/tap
brew install gripmock

πŸ“¦ Shell Script

curl -s https://raw.githubusercontent.com/bavix/gripmock/refs/heads/master/setup.sh | sh -s

🐳 Docker

docker pull bavix/gripmock

πŸ”§ Go Install

go install github.com/bavix/gripmock/v3@latest

Basic Usage

Start with a .proto file:

gripmock service.proto

Add static stubs:

gripmock --stub stubs/ service.proto

Using Docker:

docker run -p 4770:4770 -p 4771:4771 \
  -v $(pwd)/stubs:/stubs \
  -v $(pwd)/proto:/proto \
  bavix/gripmock --stub=/stubs /proto/service.proto
  • Port 4770: gRPC server
  • Port 4771: Web UI and REST API

πŸ“– Examples

Check out our comprehensive examples in the examples folder:

  • πŸ”„ Streaming - Server, client, and bidirectional streaming
  • πŸ“ File Uploads - Test chunked file uploads
  • πŸ’¬ Real-time Chat - Bidirectional communication
  • πŸ“Š Data Feeds - Continuous data streaming
  • πŸ” Authentication - Header-based auth testing
  • ⚑ Performance - High-throughput scenarios

Greeter: dynamic stub demo

Stub (universal):

# yaml-language-server: $schema=https://bavix.github.io/gripmock/schema/stub.json
# examples/projects/greeter/stub_say_hello.yaml
- service: helloworld.Greeter
  method: SayHello
  input:
    matches:
      name: ".+"
  output:
    data:
      message: "Hello, {{.Request.name}}!"  # dynamic template lives in output

Notes:

  • Put dynamic templates only in output (e.g., data, headers, stream).
  • Keep input matching static (no {{ ... }} in equals/contains/matches).
# Start server
go run main.go examples/projects/greeter/service.proto --stub examples/projects/greeter

# Call via grpcurl
grpcurl -plaintext -d '{"name":"Alex"}' localhost:4770 helloworld.Greeter/SayHello

Expected response:

{
  "message": "Hello, Alex!"
}

πŸ”§ Stubbing

Basic Stub Example

service: Greeter
method: SayHello
input:
  equals:
    name: "gripmock"
output:
  data:
    message: "Hello GripMock!"

Advanced Features

Priority System:

- service: UserService
  method: GetUser
  priority: 100  # Higher priority
  input:
    equals:
      id: "admin"
  output:
    data:
      role: "administrator"

- service: UserService
  method: GetUser
  priority: 1    # Lower priority (fallback)
  input:
    contains:
      id: "user"
  output:
    data:
      role: "user"

Streaming Support:

service: TrackService
method: StreamData
input:
  equals:
    sensor_id: "GPS001"
output:
  stream:
    - position: {"lat": 40.7128, "lng": -74.0060}
      timestamp: "2024-01-01T12:00:00Z"
    - position: {"lat": 40.7130, "lng": -74.0062}
      timestamp: "2024-01-01T12:00:05Z"

Dynamic Templates

GripMock supports dynamic templates in the output section using Go's text/template syntax.

  • Access request fields: {{.Request.field}}
  • Access headers: {{.Headers.header_name}}
  • Client streaming context: {{.Requests}} (slice of received messages), {{len .Requests}}, {{(index .Requests 0).field}}
  • Bidirectional streaming: {{.MessageIndex}} gives the current message index (0-based)
  • Math helpers: sum, avg, mul, min, max, add, sub, div
  • Utility: json, split, join, upper, lower, title, sprintf, int, int64, float, round, floor, ceil

Important rules:

  • Do not use dynamic templates inside input.equals, input.contains, or input.matches (matching must be static)
  • For server streaming, if both output.stream and output.error/output.code are set, messages are sent first and then the error is returned. If output.stream is empty, the error is returned immediately

Header Matching:

service: AuthService
method: ValidateToken
headers:
  equals:
    authorization: "Bearer valid-token"
input:
  equals:
    token: "abc123"
output:
  data:
    valid: true
    user_id: "user123"

πŸ” Input Matching

GripMock supports three powerful matching strategies:

1. Exact Match (equals)

input:
  equals:
    name: "gripmock"
    age: 25
    active: true

2. Partial Match (contains)

input:
  contains:
    name: "grip"  # Matches "gripmock", "gripster", etc.

3. Regex Match (matches)

input:
  matches:
    email: "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"
    phone: "^\\+?[1-9]\\d{1,14}$"

πŸ› οΈ API

REST API Endpoints

  • GET /api/stubs - List all stubs
  • POST /api/stubs - Add new stub
  • POST /api/stubs/search - Find matching stub
  • DELETE /api/stubs - Clear all stubs
  • GET /api/health/liveness - Health check
  • GET /api/health/readiness - Readiness check

Example API Usage

# Add a stub
curl -X POST http://localhost:4771/api/stubs \
  -H "Content-Type: application/json" \
  -d '{
    "service": "Greeter",
    "method": "SayHello",
    "input": {"equals": {"name": "world"}},
    "output": {"data": {"message": "Hello World!"}}
  }'

# Search for matching stub
curl -X POST http://localhost:4771/api/stubs/search \
  -H "Content-Type: application/json" \
  -d '{
    "service": "Greeter",
    "method": "SayHello",
    "data": {"name": "world"}
  }'

πŸ“‹ JSON Schema Support

Add schema validation to your stub files for IDE support:

JSON files:

{
  "$schema": "https://bavix.github.io/gripmock/schema/stub.json",
  "service": "MyService",
  "method": "MyMethod"
}

YAML files:

# yaml-language-server: $schema=https://bavix.github.io/gripmock/schema/stub.json
service: MyService
method: MyMethod

πŸ”— Useful Resources

🀝 Contributing

We welcome contributions! Please see our Contributing Guide for details.

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


Made with ❀️ by the GripMock community