Giter VIP home page Giter VIP logo

grpc-mock's Introduction

grpc-mock

npm version

A simple mock gRPC server on Node.js.

const {createMockServer} = require("grpc-mock");
const mockServer = createMockServer({
  protoPath: "/path/to/greeter.proto",
  packageName: "greeter",
  serviceName: "Greeter",
  rules: [
    { method: "hello", input: { message: "test" }, output: { message: "Hello" } },
    { method: "goodbye", input: ".*", output: { message: "Goodbye" } },
    
    {
      method: "howAreYou",
      streamType: "client",
      stream: [
        { input: { message: "Hi" } },
        { input: { message: "How are you?" } },
      ],
      output: { message: "I'm fine, thank you" }
    },
    
    {
      method: "niceToMeetYou",
      streamType: "server",
      stream: [
        { output: { message: "Hi, I'm Sana" } },
        { output: { message: "Nice to meet you too" } },
      ],
      input: { message: "Hi. I'm John. Nice to meet you" }
    },
    
    {
      method: "chat",
      streamType: "mutual",
      stream: [
        { input: { message: "Hi" }, output: { message: "Hi there" } },
        { input: { message: "How are you?" }, output: { message: "I'm fine, thank you." } },
      ]
    },
    
    { method: "returnsError", input: { }, error: { code: 3, message: "Message text is required"} },
    
    {
      method: "returnsErrorWithMetadata",
      streamType: "server",
      input: { },
      error: { code: 3, message: "Message text is required", metadata: { key: "value"}}
    }
  ]
});
mockServer.listen("0.0.0.0:50051");
syntax="proto3";

package greeter;

service Greeter {
  rpc Hello (RequestGreet) returns (ResponseGreet) {}
  rpc Goodbye (RequestGreet) returns (ResponseGreet) {}
  rpc HowAreYou (stream RequestGreet) returns (ResponseGreet) {}
  rpc NiceToMeetYou (RequestGreet) returns (stream ResponseGreet) {}
  rpc Chat (stream RequestGreet) returns (stream ResponseGreet) {}
}

message RequestGreet {
  string message = 1;
}

message ResponseGreet {
  string message = 1;
}

api

createMockServer({protoPath,packageName,serviceName,options,rules}): grpc-kit.GrpcServer

arg name type required/optional description
protoPath String Required path to .proto file
packageName String Required name of package
serviceName String Required name of service
options @grpc/proto-loader.Options Optional options for @grpc/proto-loader to load .proto file. In detail, please check here out. Default is null
rules Array<Rule> Required Array of Rules

Rule

prop name type required/optional description
method String Required path to .proto file
streamType Enum<"client"|"server"|"mutual"> Optional Type of stream. Set client if only using client side stream, set server if only using server side stream, and set mutual if using both of client and server side stream. Set null/undefined if not using stream. Default is null
input Object|String Required when streamType is null or server Specifying an expected input. Raw object or pattern string(RegExp) is available
output String Required when streamType is null or client Specifying an output to an expected input
stream Array<Chunk> Required when streamType is client, server and mutual Array of Chunks
error Object Optional If provided, server will respond with this error object

Chunk

prop name type required/optional description
input Object|String Required when streamType is client. Optional when streamType is mutual Specifying an expected input. Raw object or pattern string(RegExp) is available.
output Object Required when streamType is server. Optional when streamType is mutual Specifying an output to an expected input

grpc-mock's People

Contributors

aborovsky avatar dbainbri-ciena avatar dependabot[bot] avatar selvakn avatar yoshiyukikato avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

grpc-mock's Issues

Input metadata Match

First of all, thank you for this awesome package.

I have been using this for some time and was able to work around every problem found, but there is something i can't seem to discover which is metadata matching.

image

In the above sample, i wan't to match 2 requests:
1- Metadata with Culture: 'pt-PT'
2- Empty Metadata

The problem with this is that grpc-mock will always return the first stream instead of throwing the error provided in the second array element. I saw that metadata is implemented for error output responses but not for input.

This is something important for Sad Path tests, i could try to submit a PR, but first i would like to know if this is already possible somehow.

Thanks in advance.

Message with two enums, this is returning INVALID_ARGUMENT

I'm having this problem, when I add the second enum in the message, it returns me the error: INVALID_ARGUMENT

Messases exemple:

`
message RequestGreet {
string message = 1;
repeated Pair metaData = 2;
bytes file = 3;
DocumentSide documentSide = 4;
DocumentType documentType = 5;

}

message ResponseGreet {
string message = 1;
repeated Pair metaData = 2;
bytes file = 3;
DocumentSide documentSide = 4;
DocumentType documentType = 5;
}

message Pair {
string key = 1;
string value = 2;
}

enum DocumentType {
RG = 0;
CNH = 1;
Self = 2;
}

enum DocumentSide {
front = 0;
back = 1;
}

`

Multiple proto files

Is it possible to specify two protobuf files? I have a GRPC server which has "grpc health-check" and other services.

Wrong behaviour: callback not being called if no rule match the input

The case: currently implementation for each request looks though rules at https://github.com/YoshiyukiKato/grpc-mock/blob/master/index.js#L67 and trying to find corresponding rule and output.

If no rule was found, then callback not being called at all. And no exception was raised.
As result current test running process just get stuck (mocha, jest test runner, for example). And you have no idea what's going wrong. Cause your grpc client code expects a ccallback from GRPC at any case. But grpc-mock will not call it in that case.

As a developer, i have only one way out: set a breakpoint at grpc-mock's for loop and find wrong request, that was not able to find proper rule. It'a a headache.

Propose to change the behaviour: if no rules was matched current request, rais an exception.
Similar logic implemented at nock library: https://github.com/nock/nock#expectations

support grpc errors

Thanks for the great package! Is there a plan to support returning gRPC errors from the mock server? Something like:

  rules: [
    {
      method: "howAreYou",
      input: { message: "Hi" },
      error: {
         code: 5,
         message: "Resource not found",
         data: ... (optional custom data),
    },

That would be quite useful for me..

Getting "weird server reply"

Hello!

I tried this service last weekend, when I got the mock server up and running, I tested using cURL and got a "Weird server reply"

curl 'http://localhost:8080/s12.iot.v1.DevicesService/ListDevices' -X POST -I
curl: (8) Weird server reply

Do you know if there something obvious I'm skipping?

Cheers!

parameters with underbars `_` not supported

I am attempting to return an ENUM value as part of an output. I have tried both specifying the value as its string representation as well as its integer representation, but neither seems to be working. Should this work?

Steps to start the server

Hi,
The README.md could contains details on how to start this mock-grpc locally. I am new to node.js and all I want is this mock server to be up and running. How do I do that?
TIA

Include google-gax or other imported .proto (using includeDirs options or other) not work ?

hi, has anyone got this package working using some import like "empty.proto" from google ?

i can find theses .proto files in my ./node_modules/google-gax/build/protos/google/protobuf/ folder

But i can't make it work even with includeDirs options like that :
options: {includeDirs: ['./node_modules/google-gax/build/protos']}

syntax = "proto3";

import "google/protobuf/empty.proto";
import "google/protobuf/wrappers.proto";

package myPackage;

Has anyone got an idea ?

Thank you very much

RegExp doesn't work

I tried:
{
input: {
param: "^[a-zA-Z]+"
}
}
and I set param to "uuid" in the request , but it doesn't work and throws "io.grpc.StatusRuntimeException: INVALID_ARGUMENT: unexpected input pattern"

[Feature Request] Partial input matching

Hi, thanks again for your work :)

Sometimes you just care about a part of the input of the request. For example,

Input object: user: String, friends: List<User>, random: Number

pseudo-rules:

  • if user: "john" then return blah
  • if user: "laura"then return an error

In these cases I don't care about the friends and I can't predict the random number. I know that with strings you can use .* but it doesn't work with other types.

Would it be possible for the server to match like this?

Input & Output configuration

  • configurable input & output
{
  input: {
    stream: Boolean; //stream or not
    body: Object | string; //raw object or pattern string
  },
  
  output: {
    stream: Boolean; //stream or not
    body: Object
  }
}

[Feature Request] Change server rules on the fly

Hi, first of all thanks for your work!

Sometimes in the same tests suite you have different tests that need different set of rules, and today the only way to change the rules is starting a new server with those new rules. An example would be expecting the same request to return successfully on one test, and fail on another (the input can't be changed).

Would it be possible to update those rules on the fly so we can make the tests use only 1 server? also making them faster?

Refactoring

Because this lib's code has been complex and dirty.

multiple responses for unary request

What is the proper response to specify multiple unary responses based on input pattern?

{
    method: "myMethod",
    input: { param: "one" },
    output: { resp: "two" },
},
{
    method: "myMethod",
    input: { param: "three" },
    output: { resp: "four" },
}

seems to make the server crash as well as return unexpected input pattern

/usr/local/lib/node_modules/grpc-mock/node_modules/grpc/src/server.js:69
  call.startBatch(error_batch, function(){});
       ^

Error: startBatch failed
    at handleError (/usr/local/lib/node_modules/grpc-mock/node_modules/grpc/src/server.js:69:8)
    at sendUnaryData (/usr/local/lib/node_modules/grpc-mock/node_modules/grpc/src/server.js:595:9)
    at HandlerFactory.<anonymous> (/usr/local/lib/node_modules/grpc-mock/index.js:143:15)
    at Object.DeleteDevice (/usr/local/lib/node_modules/grpc-mock/node_modules/grpc-kit/index.js:53:28)
    at /usr/local/lib/node_modules/grpc-mock/node_modules/grpc/src/server.js:590:13

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.