Request matcher not working on grpc
See original GitHub issueDescribe the bug
Trying to match with is
+ capture
doesn’t work
To Reproduce Steps to reproduce the behavior:
- Tree structure:
./mocks
|- /test
|- Greeter
|- SayHello.mock
./protos
|- simple.proto
./config.yml
- 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
- Run camouflage:
docker run -d -p 4312:4312 -v $(pwd):/app --name camouflage shubhendumadhukar/camouflage
- Hit endpoint with
Bob
:
grpcurl \
-proto protos/simple.proto \
-d '{"name": "Bob"}' \
-plaintext \
localhost:4312 \
test.Greeter/SayHello
{
"message": "Not Bob, you are: Bob"
}
- 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:
- Created a year ago
- Comments:5 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:
jsonpath
to capture a value, the type of value isobject
.(capture using='jsonpath' selector='$.name')
has a type ofobject
b. The right hand side value i.e. ‘Bob’ has a type ofstring
c. Since comparison consider types, the condition returnsfalse
.regex
both left hand and right hand side values are strings hence the comparison returnstrue
.(capture using='regex' selector='\"name\": \"(.*?)\"')
Next Steps:
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
Change would be available in next release