improbable-eng / grpc-web

gRPC Web implementation for Golang and TypeScript
Apache License 2.0
4.36k stars 434 forks source link

Mocking server side #119

Open vahdet opened 6 years ago

vahdet commented 6 years ago

Thanks for this smart step in a time almost every grpc use-case is about communicating between server 👏

Now, the thing I am curious about is if there is a way for mocking grpc server for focusing on frontend development (like moxios does for when using axios for REST requests)? Do you use one or are there samples of this usage somewhere in the repo.

Wish y'all a great year btw 🥂

easyCZ commented 6 years ago

Hi @vahdet,

We don't currently have any examples of mocking responses on the client side. We use grpc-web in production but resort to isolating a grpc-web request to a single function which we mock in unit tests. For more thorough testing, fixtures are used on the backend and real requests are made.

We could implement an interceptor style mechanism to allow outgoing requests to be mocked. There's an added complexity compared to moxios, the mocked content must be grpc compatible and therefore we'd likely have to construct proto objects as the backing mock response.

At the moment, this is unlikely to be a priority for us - we want to get websocket support done first - but you're welcome to take a stab at it and we can help out where needed.

easyCZ commented 6 years ago

@vahdet It is possible to add a mock transport layer when specifying your request. This would involve writing a mock transport which conforms to the Transport interface. It can be set for both unary and invoke methods. This is somewhat of a work around for now but we are looking to provide slightly better support for this in the future.

https://github.com/improbable-eng/grpc-web/blob/8c67aa7c70c05dc6b0fd5e34712800e0d9926c2e/ts/src/grpc.ts#L79-L88

easyCZ commented 6 years ago

@vahdet did you manage to solve this problem? Would be interesting to see how, if you did. Thanks!