Hello everyone, has anyone used wiremock for grps ...
# help
а
Hello everyone, has anyone used wiremock for grps requests?
Copy code
"request": {
    "method": "POST",
    "urlPath": "myurl",
    "bodyPatterns": [
      {
        "matchesJsonPath": "$.customer_id"
      }
    ]
  },
l
Hi, do you mean
gRPC
requests or is
grps
something specific?
а
sorry, I meant grpc
here is my request
Copy code
grpcurl -plaintext -d '{
  "customer_id": "123"
 }' \
 -proto customer_service.proto \
 localhost:8080 myurl/GetContacts
l
I can't see any reason why that wouldn't work. Could you try changing the matcher to
equalToJson
just to see if that works ? You will need to provide the whole request:
Copy code
"bodyPatterns" : [{
      "equalToJson" : "{ \"customer_id\":  \"123\" }"
    }]
а
also doesn't work. here is the full request
Copy code
{
  "request": {
    "method": "POST",
    "urlPath": "/myurl/GetContacts",
    "bodyPatterns": [
      {
        "equalToJson": "{ \"customer_id\": \"123\" }"
      }
    ]
  },
  "response": {
    "status": 200,
    "body": "{\"phone\": {\"contact_id\": 1, \"value\": \"<tel:123-456-7890|123-456-7890>\"}, \"phone_reserve\": {\"contact_id\": 2, \"value\": \"<tel:098-765-4321|098-765-4321>\"}, \"email\": {\"contact_id\": 3, \"value\": \"<mailto:example@example.com|example@example.com>\"}, \"email_reserve\": {\"contact_id\": 4, \"value\": \"<mailto:reserve@example.com|reserve@example.com>\"}}",
    "transformers": [
      "response-template"
    ]
  }
}
l
And without the bodyPatterns, you get the response you have specified in the mapping file?
а
yes, everything works fine without it
Copy code
grpcurl -plaintext -d '{
  "customer_id": "123"
 }' \
 -proto customer_service.proto \
 localhost:8080 myurl/GetContacts
{
 "phone": {
  "contactId": "1",
  "value": "<tel:123-456-7890|123-456-7890>"
 },
 "phoneReserve": {
  "contactId": "2",
  "value": "<tel:098-765-4321|098-765-4321>"
 },
 "email": {
  "contactId": "3",
  "value": "<mailto:example@example.com|example@example.com>"
 },
 "emailReserve": {
  "contactId": "4",
  "value": "<mailto:reserve@example.com|reserve@example.com>"
 }
}
l
I honestly can't see why that wouldn't work. What do you see when the request doesn't match? Can you copy all the output in the console ?
Is it possible to share your proto file or is that private?
а
grpcurl -plaintext -d '{ "customer_id": "123" }' \ -proto myurl/customer_service.proto \ localhost:8080 myurl.CustomerService/GetContacts ERROR: Code: NotFound Message: No matching stub mapping found for gRPC request
rpc GetContacts(GetContactsRequest) returns (GetContactsResponse) { option (google.api.http) = { post: "/customer/v1/GetContacts", body: "*", }; } //+ message GetContactsRequest{ int64 customer_id = 1 [(validate.rules).int64 = {gt:0}]; } my proto method
l
Let me see if I can replicate
Where is your
GetContactsResponse
defined?
а
The response is in the messages file, and the method is in the service
l
Can I just check something. In your proto file it looks like you are defining your
customer_id
as an
int64
but in your
grpcurl
request you are sending it as a string. Can you try making these consistent and try again
а
same error
same error
Is there a way to get a list of valid methods for wiremock using http2?
l
I think with
grpcurl
you can use -
grpcurl -plaintext localhost:8080 list
I think this might be because WireMock is changing the json it is parsing into camel case. If you look at the json you have provided in your stub definition and the json that is returned, you can see the json that is returned is not the same. For example -
phone_reserve
is changed to
phoneReserve
. I think the same is happening when you add a body pattern matcher. It is changing the
customer_id
to
customerId
before doing the match. To get this to work you will need to update your matcher:
Copy code
"bodyPatterns": [
      {
        "equalToJson" : "{ \"customerId\": 123 }"
      }
    ]
I don't know if this is wiremock making the change or the .dsc file generated by protoc
OK, I think it is the
protoc
compiler that is performing this switch so when WireMock reads the descriptor file it generates json in camel case which makes matching problematic if you specify the non camel case json. In my simple example I have running locally I have added these
json_name
items to each field and that now works as expected.
Copy code
syntax = "proto3";

package myUrl;

message GetContactsRequest {
  int32 customer_id = 1 [json_name = "customer_id"];
}

message GetContactsResponse {
  ContactDetails phone = 1 [json_name = "phone"];
  ContactDetails phone_reserve = 2 [json_name = "phone_reserve"];
  ContactDetails email = 3 [json_name = "email"];
  ContactDetails email_reserve = 4 [json_name = "email_reserve"];
}

message ContactDetails {
  string contact_id = 1 [json_name = "contact_id"];
  string value = 2 [json_name = "value"];
}

service ContactsService {
  rpc getContacts(GetContactsRequest) returns (GetContactsResponse);
}
1
I have just tested this with an
equalsToJson
matcher and it seems to be working as expected.
а
Thank you so much, this really works. I would never have found the problem myself.
l
No problem at all. It was a bit of a tricky one to be honest. The way I found the issue was by looking at the Wiremock request log via the Wiremock admin api.
The admin api reference can be found here - https://wiremock.org/docs/standalone/admin-api-reference/