mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 13:00:33 +01:00
Page:
Request Matching ProtoBuf
Pages
Admin API Reference
Compatibility WireMock.org
Conflict on Microsoft.CodeAnalysis.CSharp
Cors
Could not load file or assembly RestEase
Development Information
Faults
FluentAssertions
Home
KestrelServerOptions
Mapping
MimeKit and MimeKitLite
MyGet preview versions
Pact
Proxying
References
RegexExtended
Request Matcher FormUrlEncodedMatcher
Request Matchers
Request Matching CSharpCode
Request Matching GraphQLMatcher
Request Matching JsonMatcher
Request Matching JsonPartialMatcher
Request Matching JsonPartialWildcardMatcher
Request Matching JsonPathMatcher
Request Matching MimePartMatcher
Request Matching ProtoBuf
Request Matching Tips
Request Matching
Response Templating
Scenarios and States
Settings
Stubbing
Using HTTPS (SSL)
Using WireMock in UnitTests
Using WireMock.Net.Aspire
Using WireMock.Net.Testcontainers
Webhook
What Is WireMock.Net
WireMock as a (Azure) Web App
WireMock as a Windows Service
WireMock as a standalone process
WireMock as dotnet tool
WireMock commandline parameters
WireMock.Org
Xamarin Could not load file or assembly
Clone
2
Request Matching ProtoBuf
Stef Heyenrath edited this page 2025-02-01 09:58:24 +01:00
ProtoBufMatcher (ProtoBufMatcher)
Can be used to match a gRPC ProtoBuf message.
See also mstack.nl blog: gRPC / ProtoBuf Support.
Proto Definition
Define a Proto Definition file (greet.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:
// 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()
);
Multiple Proto Definition files
If you have multiple proto files, you have to follow these 2 rules:
- The first file provided in the array should be the main proto file.
- A comment is needed for each referenced (imported) proto file, so that WireMock.Net knows how to resolve.
Main proto
syntax = "proto3";
import "request.proto";
package greet;
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply);
}
message HelloReply {
string message = 1;
}
other proto file
// request.proto
syntax = "proto3";
package greet;
message HelloRequest {
string name = 1;
}
C# code
var greet = File.ReadAllText(@"c:\grpc\greet.proto");
var request = File.ReadAllText(@"c:\grpc\request.proto");
. . .
server
// Now call the new AddProtoDefinition method to register this identifier and the 2 ProtoDefinitions in WireMock.Net
.AddProtoDefinition(protoDefinitionId, greet, request)
JSON Mapping option
todo
Pages
- Home
- What is WireMock.Net
- WireMock.Org
- References
- Settings
- Admin REST API
- Proxying
- Stubbing
- Webhook
- Request Matching
- Response Templating
- Unit Testing
- Using WireMock
- Advanced
- Errors