Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/Tigh-Gherr/grpc-mock

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/Tigh-Gherr/grpc-mock

  • v0.0.0-20210121115102-01ad96606dfd
  • Source
  • Go
  • Socket score

Version published
Created
Source

grpc-mock

grpc-mock is yet another grpc mock server inspired by GripMock.

grpc-mock implements the grpc mock server by reflections of .proto files instead of code generation that GripMock does.

Installation

From Source

Use go tool to install:

go get github.com/monlabs/grpc-mock/cmd/gmock

Usage

Quickstart

Step 1: Write your proto file which should contains at least one service.

helloworld.proto

syntax = "proto3";

package helloworld;

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

Step 2: Write the stub files in JSON format for the services defined in the proto file.

stubs/helloworld.json

[
  {
    "service": "helloworld.Greeter",
    "method": "SayHello",
    "in": {
      "equals": {
        "name": "hi"
      }
    },
    "out": {
      "data": {
        "message": "lemon"
      }
    }
  },
  {
    "service": "helloworld.Greeter",
    "method": "SayHello",
    "in": {
      "matches": {
        "name": "^[0-9]+$"
      }
    },
    "out": {
      "data": {
        "message": "hi numbers"
      }
    }
  }
]

Step 3: Start the grpc mock server.

./stubs is the directory where all stub files reside.

grpc-mock -mock-addr :22222 -import-path . -proto helloworld.proto -stub-dir ./stubs

The flag mock-addr defines the address mock server listens on.

Step 4: Ready to test it. We use grpcurl to invoke rpc methods.

grpcurl -plaintext -d '{"name": "01"}' localhost:22222 helloworld.Greeter/SayHello

The output is:

{
  "message": "hi numbers"
}

More examples

Stubs

The input and expected output comprise of a Stub. When a request is received, the mock server tries to match the stub with the request, and then reponds with the expected output in the matched stub. There're two ways to manage your stubs: static and dynamic.

Static: predefine stubs in files.

Like in the example above, you save the stubs in files. Let the mock server load them on starting.

Dynamic: call API to CRUD stubs.

You also can start the mock server without specified stubs and call the admin API to dynamically add stubs. The API server runs on :22220.

  • GET /v1/stubs: get all stubs.
  • POST /v1/stubs: create stubs.
  • DELETE /v1/stubs: delete stubs.

Matching Rules

grpc-mock has the same matching rules as gripmock.

FAQs

Package last updated on 21 Jan 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc