Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request matcher not working on grpc #193

Closed
elpadrinoIV opened this issue Sep 30, 2022 · 6 comments
Closed

Request matcher not working on grpc #193

elpadrinoIV opened this issue Sep 30, 2022 · 6 comments
Assignees
Labels
bug Something isn't working released

Comments

@elpadrinoIV
Copy link

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"
}
@elpadrinoIV elpadrinoIV added the bug Something isn't working label Sep 30, 2022
@shubhendumadhukar shubhendumadhukar moved this from To do to In Progress in Camouflage Issue Tracker Oct 1, 2022
@elpadrinoIV
Copy link
Author

Hello!
Any updates or ETA on this?

Thanks!

@twbecker
Copy link

So given this bug, is there any way to return different mock responses for the same API based on request parameters?

@elpadrinoIV
Copy link
Author

I think there's always the option to run arbitrary code (end of this section: https://testinggospels.github.io/camouflage/request-matching/).
I haven't tried it yet, though

@shubhendumadhukar
Copy link
Contributor

shubhendumadhukar commented 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.

@shubhendumadhukar
Copy link
Contributor

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

Camouflage Issue Tracker automation moved this from In Progress to Closed Jul 18, 2023
@github-actions
Copy link

github-actions bot commented Aug 4, 2023

🎉 This issue has been resolved in version 0.14.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released
Projects
Development

No branches or pull requests

3 participants