From 25cc9a09fc61c97df4c05563c05a1d39ff31a65a Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Tue, 27 Feb 2024 08:09:58 +0100 Subject: [PATCH] Created Request Matching ProtoBuf (markdown) --- Request-Matching-ProtoBuf.md | 83 ++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 Request-Matching-ProtoBuf.md diff --git a/Request-Matching-ProtoBuf.md b/Request-Matching-ProtoBuf.md new file mode 100644 index 0000000..fcbab6e --- /dev/null +++ b/Request-Matching-ProtoBuf.md @@ -0,0 +1,83 @@ +## ProtoBufMatcher (ProtoBufMatcher) +Can be used to match a gRPC ProtoBuf message. + +See also [mstack.nl blog: gRPC / ProtoBuf Support](https://mstack.nl/blogs/wiremock-net-grpc/). + +### Proto Definition +Define a Proto Definition file (greet.proto) + +syntax = "proto3"; +``` proto +syntax = "proto3"; + +package greet; + +// The greeting service definition. +service Greeter { + // Sends a greeting + rpc SayHello (HelloRequest) returns (HelloReply); +} + +// The request message containing the user's name. +message HelloRequest { + string name = 1; +} + +// The response message containing the greetings +message HelloReply { + string message = 1; +} +``` + +#### C# option +Start the WireMock.Net server and define the mapping for the Grpc call: +```csharp +// Read the 'greet.proto' ProtoDefinition file as text and store it in a variable +var protoDefinitionText = File.ReadAllText(@"c:\grpc\greet.proto"); + +// Define an unique identifier for that ProtoDefinition to make it possible to refer +// to that ProtoDefinition in the different mappings +var protoDefinitionId = "GrpcGreet"; + +// Start the WireMockServer and enable HTTP/2 support +var server = WireMockServer.Start(useHttp2: true); + +server + // Now call the new AddProtoDefinition method to register this identifier + // and ProtoDefinition in WireMock.Net + .AddProtoDefinition(protoDefinitionId, protoDefinitionText) + + // Define the Request matching logic which means in this case: + // - Match on HTTP POST + // - Match when the client calls the SayHello method on the Greeter-service + // - Use a JsonMatcher so that this request is only mapped when the name + // equals "stef". + .Given(Request.Create() + .UsingPost() + .WithPath("/grpc/greet.Greeter/SayHello") + .WithBodyAsProtoBuf("greet.HelloRequest", new JsonMatcher(new { name = "stef" })) + ) + + // Define that this mapping should use the specified protoDefinitionId for both + // the Request and the Response + .WithProtoDefinition(protoDefinitionId) + + // Build a response which will: + // - Return the correct Content-Type header and Grpc Trailing header + // - Define the response as an anonymous object and use the Handlebars + // Transformer to return a personalized message + // - Return a ProtoBuf byte-array response using the HelloReply method + .RespondWith(Response.Create() + .WithHeader("Content-Type", "application/grpc") + .WithTrailingHeader("grpc-status", "0") + .WithBodyAsProtoBuf("greet.HelloReply", + new + { + message = "hello {{request.BodyAsJson.name}} {{request.method}}" + }) + .WithTransformer() + ); +``` + +#### JSON Mapping option +todo \ No newline at end of file