question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Request matcher not working on grpc

See original GitHub issue

Describe the bug Trying to match with is + capture doesn’t work

To Reproduce Steps to reproduce the behavior:

  1. Tree structure:
./mocks
    |- /test
        |- Greeter
             |- SayHello.mock
./protos
    |- simple.proto
./config.yml
  1. File content:
// simple.proto
syntax = "proto3";

package test;

service Greeter {
  rpc SayHello (HelloRequest) returns (HelloReply);
}

message HelloRequest {
  string name = 1;
}

message HelloReply {
  string message = 1;
}
// SayHello.mock
{{#is (capture using='jsonpath' selector='$.name') 'Bob' }}
{
  "message": "{{capture using='jsonpath' selector='$.name'}}"
}
{{else}}
{
  "message": "Not Bob, you are: {{capture using='jsonpath' selector='$.name'}}"
}
{{/is}}
// config.yml
...
  grpc:
    enable: true
    #host: localhost
    host: 0.0.0.0
    port: 4312
    mocks_dir: "./mocks"
    protos_dir: "./protos"
    grpc_tls: false
  1. Run camouflage: docker run -d -p 4312:4312 -v $(pwd):/app --name camouflage shubhendumadhukar/camouflage
  2. Hit endpoint with Bob:
grpcurl \
  -proto protos/simple.proto \
  -d '{"name": "Bob"}' \
  -plaintext \
  localhost:4312 \
  test.Greeter/SayHello
{
  "message": "Not Bob, you are: Bob"
}
  1. Hit endpoint with Alice:
grpcurl \
  -proto protos/simple.proto \
  -d '{"name": "Alice"}' \
  -plaintext \
  localhost:4312 \
  test.Greeter/SayHello
{
  "message": "Not Bob, you are: Alice"
}

Expected behavior Step 4 should print a different message: { "message": "Bob" }.

The jsonpath is correct, because when returning the message, it correctly says “you are: Bob”. However, the comparison doesn’t seem to be working.

Additional context

2022-09-30 15:07:53 debug: Unary Request: {"name":"Bob"}
2022-09-30 15:07:53 debug: Mock file path: mocks/test/Greeter/SayHello.mock
2022-09-30 15:07:53 debug: Response: {
  "message": "Not Bob, you are: Bob"
}

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
shubhendumadhukarcommented, Nov 19, 2022

Apologies for the delay in response. I have been occupied elsewhere and had only been looking at pull requests.

I have however looked into the issue. TLDR; Use regex instead of jsonpath as an interim solution.

Explanation:

  1. Capture helper: It seems like if we use jsonpath to capture a value, the type of value is object.
  2. Is helper: The comparison takes into account the types of the values being compared.
  3. Why the results are unexpected? a. The left hand side value i.e. (capture using='jsonpath' selector='$.name') has a type of object b. The right hand side value i.e. ‘Bob’ has a type of string c. Since comparison consider types, the condition returns false.
  4. Using regex both left hand and right hand side values are strings hence the comparison returns true. (capture using='regex' selector='\"name\": \"(.*?)\"')

Next Steps:

  1. Add notes in documentation.
  2. Provide fix for the jsonpath path issue.
0reactions
shubhendumadhukarcommented, Dec 12, 2022

Added new operator ==, this should disable strict type checking. Modifying mock as shown below would produce appropriate result.

Before {{#is (capture using='jsonpath' selector='$.name') 'Bob' }}

After {{#is (capture using='jsonpath' selector='$.name') '==' 'Bob' }}

Updated mock content

{{#is (capture using='jsonpath' selector='$.name') '==' 'Bob' }}
{
  "message": "{{capture using='jsonpath' selector='$.name'}}"
}
{{else}}
{
  "message": "Not Bob, you are: {{capture using='jsonpath' selector='$.name'}}"
}
{{/is}}

Change would be available in next release

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom incoming header matcher isn't working as expected
We have a custom HeaderMatcher that forwards all HTTP headers, but it stopped working in grpc-gateway 2.1. To Reproduce. We created a custom ......
Read more >
Cloud EndPoint GRPC dont match proto definition
I'm using Google Cloud EndPoints to access my GRPC server written in GoLang that run in Kuberkules. I was ...
Read more >
Trouble-Shooting | grpc-spring-boot-starter - GitHub Pages
The Problem. The certificate does not match the target's address/name. The Solution. Configure an override for the name comparison by adding the following...
Read more >
Troubleshoot gRPC on .NET Core - Microsoft Learn
This document discusses commonly encountered problems when developing gRPC apps on .NET. Mismatch between client and service SSL/TLS ...
Read more >
Troubleshooting Response Errors | Cloud Endpoints with gRPC
This page describes how to troubleshoot errors that you receive in a response from a request to your API. Upstream backend unavailable.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found