diff --git a/src/WireMock.Net.Abstractions/Admin/Mappings/ResponseModel.cs b/src/WireMock.Net.Abstractions/Admin/Mappings/ResponseModel.cs index 0dacb64eb..0f1683889 100644 --- a/src/WireMock.Net.Abstractions/Admin/Mappings/ResponseModel.cs +++ b/src/WireMock.Net.Abstractions/Admin/Mappings/ResponseModel.cs @@ -35,7 +35,7 @@ public class ResponseModel public bool? BodyAsJsonIndented { get; set; } /// - /// Gets or sets the body (as bytearray). + /// Gets or sets the body (as byte array). /// public byte[]? BodyAsBytes { get; set; } @@ -123,4 +123,21 @@ public class ResponseModel /// Gets or sets the WebProxy settings. /// public WebProxyModel? WebProxy { get; set; } + + #region ProtoBuf + /// + /// Gets or sets the proto definition. + /// + public string? ProtoDefinition { get; set; } + + /// + /// Gets or sets the full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". + /// + public string? ProtoBufMessageType { get; set; } + + /// + /// Gets or sets the body as JSON object (which defines a ProtoBuf message byte array). + /// + public object? BodyAsProtoBufJson { get; set; } + #endregion } \ No newline at end of file diff --git a/src/WireMock.Net.Abstractions/Models/IBodyData.cs b/src/WireMock.Net.Abstractions/Models/IBodyData.cs index 557be21c0..c827fdb17 100644 --- a/src/WireMock.Net.Abstractions/Models/IBodyData.cs +++ b/src/WireMock.Net.Abstractions/Models/IBodyData.cs @@ -10,7 +10,7 @@ namespace WireMock.Util; public interface IBodyData { /// - /// The body (as bytearray). + /// The body (as byte array). /// byte[]? BodyAsBytes { get; set; } @@ -68,4 +68,21 @@ public interface IBodyData /// Defines if this BodyData is the result of a dynamically created response-string. ( /// public string? IsFuncUsed { get; set; } + + #region ProtoBuf + /// + /// Gets or sets the proto definition. + /// + public string? ProtoDefinition { get; set; } + + /// + /// Gets or sets the full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". + /// + public string? ProtoBufMessageType { get; set; } + + /// + /// Gets or sets the body as JSON object (which defines a ProtoBuf message byte array). + /// + public object? BodyAsProtoBufJson { get; set; } + #endregion } \ No newline at end of file diff --git a/src/WireMock.Net.Abstractions/Types/BodyType.cs b/src/WireMock.Net.Abstractions/Types/BodyType.cs index a03534d1a..47a62ae22 100644 --- a/src/WireMock.Net.Abstractions/Types/BodyType.cs +++ b/src/WireMock.Net.Abstractions/Types/BodyType.cs @@ -38,5 +38,10 @@ public enum BodyType /// /// Body is a String which is x-www-form-urlencoded. /// - FormUrlEncoded + FormUrlEncoded, + + /// + /// Body is a ProtoBuf Byte array + /// + ProtoBuf } \ No newline at end of file diff --git a/src/WireMock.Net/Matchers/ProtoBufMatcher.cs b/src/WireMock.Net/Matchers/ProtoBufMatcher.cs index 9c6024d6d..f527827f4 100644 --- a/src/WireMock.Net/Matchers/ProtoBufMatcher.cs +++ b/src/WireMock.Net/Matchers/ProtoBufMatcher.cs @@ -10,7 +10,7 @@ namespace WireMock.Matchers; /// /// Grpc ProtoBuf Matcher /// -/// +/// public class ProtoBufMatcher : IBytesMatcher { /// @@ -20,7 +20,7 @@ public class ProtoBufMatcher : IBytesMatcher public MatchBehaviour MatchBehaviour { get; } /// - /// The proto definition as a string. + /// The proto definition. /// public string ProtoDefinition { get; } @@ -37,9 +37,9 @@ public class ProtoBufMatcher : IBytesMatcher private static readonly IConverter ProtoBufToJsonConverter = SingletonFactory.GetInstance(); /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// - /// The proto definition as a string. + /// The proto definition. /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". /// The match behaviour. (default = "AcceptOnMatch") /// The optional jsonMatcher to use to match the ProtoBuf as (json) object. diff --git a/src/WireMock.Net/Models/BodyData.cs b/src/WireMock.Net/Models/BodyData.cs index a8036e50f..652e522a2 100644 --- a/src/WireMock.Net/Models/BodyData.cs +++ b/src/WireMock.Net/Models/BodyData.cs @@ -9,7 +9,7 @@ namespace WireMock.Util; /// public class BodyData : IBodyData { - /// + /// public Encoding? Encoding { get; set; } /// @@ -18,30 +18,42 @@ public class BodyData : IBodyData /// public IDictionary? BodyAsFormUrlEncoded { get; set; } - /// + /// public object? BodyAsJson { get; set; } - /// + /// public byte[]? BodyAsBytes { get; set; } - - /// + + /// public bool? BodyAsJsonIndented { get; set; } - /// + /// public string? BodyAsFile { get; set; } - /// + /// public bool? BodyAsFileIsCached { get; set; } - /// + /// public BodyType? DetectedBodyType { get; set; } - /// + /// public BodyType? DetectedBodyTypeFromContentType { get; set; } - /// + /// public string? DetectedCompression { get; set; } /// public string? IsFuncUsed { get; set; } + + #region ProtoBuf + /// + public string? ProtoDefinition { get; set; } + + /// + public string? ProtoBufMessageType { get; set; } + + /// + public object? BodyAsProtoBufJson { get; set; } + + #endregion } \ No newline at end of file diff --git a/src/WireMock.Net/Owin/Mappers/OwinResponseMapper.cs b/src/WireMock.Net/Owin/Mappers/OwinResponseMapper.cs index 5062117fd..8742b8d9c 100644 --- a/src/WireMock.Net/Owin/Mappers/OwinResponseMapper.cs +++ b/src/WireMock.Net/Owin/Mappers/OwinResponseMapper.cs @@ -138,6 +138,9 @@ private bool IsFault(IResponseMessage responseMessage) var jsonBody = JsonConvert.SerializeObject(responseMessage.BodyData.BodyAsJson, new JsonSerializerSettings { Formatting = formatting, NullValueHandling = NullValueHandling.Ignore }); return (responseMessage.BodyData.Encoding ?? _utf8NoBom).GetBytes(jsonBody); + case BodyType.ProtoBuf: + return responseMessage.BodyData.BodyAsBytes; + case BodyType.Bytes: return responseMessage.BodyData.BodyAsBytes; diff --git a/src/WireMock.Net/RequestBuilders/IProtoBufRequestBuilder.cs b/src/WireMock.Net/RequestBuilders/IProtoBufRequestBuilder.cs index af5febd10..2a42bdf87 100644 --- a/src/WireMock.Net/RequestBuilders/IProtoBufRequestBuilder.cs +++ b/src/WireMock.Net/RequestBuilders/IProtoBufRequestBuilder.cs @@ -14,7 +14,7 @@ public interface IProtoBufRequestBuilder : IGraphQLRequestBuilder /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". /// The match behaviour. (default = "AcceptOnMatch") /// The . - IRequestBuilder WithGrpcProto(string protoDefinition, string messageType, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch); + IRequestBuilder WithBodyAsProtoBuf(string protoDefinition, string messageType, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch); /// /// WithGrpcProto @@ -24,5 +24,5 @@ public interface IProtoBufRequestBuilder : IGraphQLRequestBuilder /// The jsonMatcher to use to match the ProtoBuf as (json) object. /// The match behaviour. (default = "AcceptOnMatch") /// The . - IRequestBuilder WithGrpcProto(string protoDefinition, string messageType, IJsonMatcher jsonMatcher, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch); + IRequestBuilder WithBodyAsProtoBuf(string protoDefinition, string messageType, IJsonMatcher jsonMatcher, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch); } \ No newline at end of file diff --git a/src/WireMock.Net/RequestBuilders/Request.WithGrpcProto.cs b/src/WireMock.Net/RequestBuilders/Request.WithGrpcProto.cs index ece44966c..95aadfafd 100644 --- a/src/WireMock.Net/RequestBuilders/Request.WithGrpcProto.cs +++ b/src/WireMock.Net/RequestBuilders/Request.WithGrpcProto.cs @@ -6,14 +6,14 @@ namespace WireMock.RequestBuilders; public partial class Request { /// - public IRequestBuilder WithGrpcProto(string protoDefinition, string messageType, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) + public IRequestBuilder WithBodyAsProtoBuf(string protoDefinition, string messageType, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) { _requestMatchers.Add(new RequestMessageProtoBufMatcher(matchBehaviour, protoDefinition, messageType)); return this; } /// - public IRequestBuilder WithGrpcProto(string protoDefinition, string messageType, IJsonMatcher jsonMatcher, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) + public IRequestBuilder WithBodyAsProtoBuf(string protoDefinition, string messageType, IJsonMatcher jsonMatcher, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) { _requestMatchers.Add(new RequestMessageProtoBufMatcher(matchBehaviour, protoDefinition, messageType, jsonMatcher)); return this; diff --git a/src/WireMock.Net/ResponseBuilders/Response.WithBody.cs b/src/WireMock.Net/ResponseBuilders/Response.WithBody.cs index ed50540f7..9a7e69697 100644 --- a/src/WireMock.Net/ResponseBuilders/Response.WithBody.cs +++ b/src/WireMock.Net/ResponseBuilders/Response.WithBody.cs @@ -238,8 +238,11 @@ public IResponseBuilder WithBodyAsProtoBuf( ResponseMessage.BodyDestination = null; ResponseMessage.BodyData = new BodyData { - DetectedBodyType = BodyType.Bytes, - BodyAsBytes = bytes + DetectedBodyType = BodyType.ProtoBuf, + BodyAsBytes = bytes, + BodyAsProtoBufJson = value, + ProtoDefinition = protoDefinition, + ProtoBufMessageType = messageType }; #endif return this; diff --git a/src/WireMock.Net/Serialization/MappingConverter.cs b/src/WireMock.Net/Serialization/MappingConverter.cs index 7752a6d04..cc6a8001d 100644 --- a/src/WireMock.Net/Serialization/MappingConverter.cs +++ b/src/WireMock.Net/Serialization/MappingConverter.cs @@ -419,43 +419,7 @@ public MappingModel ToMappingModel(IMapping mapping) mappingModel.Response.UseTransformerForBodyAsFile = response.UseTransformerForBodyAsFile; } - if (response.ResponseMessage.BodyData != null) - { - switch (response.ResponseMessage.BodyData?.DetectedBodyType) - { - case BodyType.String: - case BodyType.FormUrlEncoded: - mappingModel.Response.Body = response.ResponseMessage.BodyData.BodyAsString; - break; - - case BodyType.Json: - mappingModel.Response.BodyAsJson = response.ResponseMessage.BodyData.BodyAsJson; - if (response.ResponseMessage.BodyData.BodyAsJsonIndented == true) - { - mappingModel.Response.BodyAsJsonIndented = response.ResponseMessage.BodyData.BodyAsJsonIndented; - } - break; - - case BodyType.Bytes: - mappingModel.Response.BodyAsBytes = response.ResponseMessage.BodyData.BodyAsBytes; - break; - - case BodyType.File: - mappingModel.Response.BodyAsFile = response.ResponseMessage.BodyData.BodyAsFile; - mappingModel.Response.BodyAsFileIsCached = response.ResponseMessage.BodyData.BodyAsFileIsCached; - break; - } - - if (response.ResponseMessage.BodyData?.Encoding != null && response.ResponseMessage.BodyData.Encoding.WebName != "utf-8") - { - mappingModel.Response.BodyEncoding = new EncodingModel - { - EncodingName = response.ResponseMessage.BodyData.Encoding.EncodingName, - CodePage = response.ResponseMessage.BodyData.Encoding.CodePage, - WebName = response.ResponseMessage.BodyData.Encoding.WebName - }; - } - } + MapResponse(response, mappingModel); if (response.ResponseMessage.FaultType != FaultType.NONE) { @@ -470,6 +434,56 @@ public MappingModel ToMappingModel(IMapping mapping) return mappingModel; } + private static void MapResponse(Response response, MappingModel mappingModel) + { + if (response.ResponseMessage.BodyData == null) + { + return; + } + + switch (response.ResponseMessage.BodyData?.DetectedBodyType) + { + case BodyType.String: + case BodyType.FormUrlEncoded: + mappingModel.Response.Body = response.ResponseMessage.BodyData.BodyAsString; + break; + + case BodyType.Json: + mappingModel.Response.BodyAsJson = response.ResponseMessage.BodyData.BodyAsJson; + if (response.ResponseMessage.BodyData.BodyAsJsonIndented == true) + { + mappingModel.Response.BodyAsJsonIndented = response.ResponseMessage.BodyData.BodyAsJsonIndented; + } + break; + + case BodyType.ProtoBuf: + mappingModel.Response.ProtoDefinition = response.ResponseMessage.BodyData.ProtoDefinition; + mappingModel.Response.ProtoBufMessageType = response.ResponseMessage.BodyData.ProtoBufMessageType; + mappingModel.Response.BodyAsBytes = null; + mappingModel.Response.BodyAsProtoBufJson = response.ResponseMessage.BodyData.BodyAsProtoBufJson; + break; + + case BodyType.Bytes: + mappingModel.Response.BodyAsBytes = response.ResponseMessage.BodyData.BodyAsBytes; + break; + + case BodyType.File: + mappingModel.Response.BodyAsFile = response.ResponseMessage.BodyData.BodyAsFile; + mappingModel.Response.BodyAsFileIsCached = response.ResponseMessage.BodyData.BodyAsFileIsCached; + break; + } + + if (response.ResponseMessage.BodyData?.Encoding != null && response.ResponseMessage.BodyData.Encoding.WebName != "utf-8") + { + mappingModel.Response.BodyEncoding = new EncodingModel + { + EncodingName = response.ResponseMessage.BodyData.Encoding.EncodingName, + CodePage = response.ResponseMessage.BodyData.Encoding.CodePage, + WebName = response.ResponseMessage.BodyData.Encoding.WebName + }; + } + } + private static string GetString(IStringMatcher stringMatcher) { return stringMatcher.GetPatterns().Select(p => ToCSharpStringLiteral(p.GetPattern())).First(); diff --git a/test/WireMock.Net.Tests/RequestBuilders/RequestBuilderWithProtoBufTests.cs b/test/WireMock.Net.Tests/RequestBuilders/RequestBuilderWithProtoBufTests.cs index 369d9ea47..0d2dc178b 100644 --- a/test/WireMock.Net.Tests/RequestBuilders/RequestBuilderWithProtoBufTests.cs +++ b/test/WireMock.Net.Tests/RequestBuilders/RequestBuilderWithProtoBufTests.cs @@ -33,7 +33,7 @@ message HelloReply { public void RequestBuilder_WithGrpcProto_Without_JsonMatcher() { // Act - var requestBuilder = (Request)Request.Create().WithGrpcProto(TestProtoDefinition, MessageType); + var requestBuilder = (Request)Request.Create().WithBodyAsProtoBuf(TestProtoDefinition, MessageType); // Assert var matchers = requestBuilder.GetPrivateFieldValue>("_requestMatchers"); @@ -50,7 +50,7 @@ public void RequestBuilder_WithGrpcProto_With_JsonMatcher() { // Act var jsonMatcher = new JsonMatcher(new { name = "stef" }); - var requestBuilder = (Request)Request.Create().WithGrpcProto(TestProtoDefinition, MessageType, jsonMatcher); + var requestBuilder = (Request)Request.Create().WithBodyAsProtoBuf(TestProtoDefinition, MessageType, jsonMatcher); // Assert var matchers = requestBuilder.GetPrivateFieldValue>("_requestMatchers"); diff --git a/test/WireMock.Net.Tests/Serialization/MappingConverterTests.ToMappingModel_Request_WithGrpcProto_ReturnsCorrectModel.verified.txt b/test/WireMock.Net.Tests/Serialization/MappingConverterTests.ToMappingModel_Request_WithBodyAsProtoBuf_ReturnsCorrectModel.verified.txt similarity index 92% rename from test/WireMock.Net.Tests/Serialization/MappingConverterTests.ToMappingModel_Request_WithGrpcProto_ReturnsCorrectModel.verified.txt rename to test/WireMock.Net.Tests/Serialization/MappingConverterTests.ToMappingModel_Request_WithBodyAsProtoBuf_ReturnsCorrectModel.verified.txt index ff679eec2..f41b88294 100644 --- a/test/WireMock.Net.Tests/Serialization/MappingConverterTests.ToMappingModel_Request_WithGrpcProto_ReturnsCorrectModel.verified.txt +++ b/test/WireMock.Net.Tests/Serialization/MappingConverterTests.ToMappingModel_Request_WithBodyAsProtoBuf_ReturnsCorrectModel.verified.txt @@ -3,7 +3,7 @@ UpdatedAt: DateTime_1, Title: , Description: , - Priority: 42, + Priority: 41, Request: { Path: { Matchers: [ @@ -48,8 +48,6 @@ message HelloReply { } } }, - Response: { - BodyAsBytes: CgVoZWxsbw== - }, + Response: {}, UseWebhooksFireAndForget: false } \ No newline at end of file diff --git a/test/WireMock.Net.Tests/Serialization/MappingConverterTests.ToMappingModel_Response_WithBodyAsProtoBuf_ReturnsCorrectModel.verified.txt b/test/WireMock.Net.Tests/Serialization/MappingConverterTests.ToMappingModel_Response_WithBodyAsProtoBuf_ReturnsCorrectModel.verified.txt new file mode 100644 index 000000000..355b8e021 --- /dev/null +++ b/test/WireMock.Net.Tests/Serialization/MappingConverterTests.ToMappingModel_Response_WithBodyAsProtoBuf_ReturnsCorrectModel.verified.txt @@ -0,0 +1,32 @@ +{ + Guid: Guid_1, + UpdatedAt: DateTime_1, + Title: , + Description: , + Priority: 43, + Request: {}, + Response: { + ProtoDefinition: +syntax = "proto3"; + +package greet; + +service Greeter { + rpc SayHello (HelloRequest) returns (HelloReply); +} + +message HelloRequest { + string name = 1; +} + +message HelloReply { + string message = 1; +} +, + ProtoBufMessageType: greet.HelloReply, + BodyAsProtoBufJson: { + message: hello + } + }, + UseWebhooksFireAndForget: false +} \ No newline at end of file diff --git a/test/WireMock.Net.Tests/Serialization/MappingConverterTests.WireMockServer_WithGrpcProto.verified.txt b/test/WireMock.Net.Tests/Serialization/MappingConverterTests.WireMockServer_WithGrpcProto.verified.txt deleted file mode 100644 index 7f27dc875..000000000 --- a/test/WireMock.Net.Tests/Serialization/MappingConverterTests.WireMockServer_WithGrpcProto.verified.txt +++ /dev/null @@ -1,24 +0,0 @@ -{ - Guid: Guid_1, - UpdatedAt: DateTime_1, - Title: , - Description: , - Priority: 42, - Request: { - Path: { - Matchers: [ - { - Name: WildcardMatcher, - Pattern: /grpc/greet-Greeter-SayHello, - IgnoreCase: false - } - ] - }, - Methods: [ - POST - ] - }, - Response: { - }, - UseWebhooksFireAndForget: false -} \ No newline at end of file diff --git a/test/WireMock.Net.Tests/Serialization/MappingConverterTests.cs b/test/WireMock.Net.Tests/Serialization/MappingConverterTests.cs index 097964fa7..b20257f2d 100644 --- a/test/WireMock.Net.Tests/Serialization/MappingConverterTests.cs +++ b/test/WireMock.Net.Tests/Serialization/MappingConverterTests.cs @@ -452,7 +452,7 @@ type Student { #if PROTOBUF [Fact] - public Task ToMappingModel_Request_WithGrpcProto_ReturnsCorrectModel() + public Task ToMappingModel_Request_WithBodyAsProtoBuf_ReturnsCorrectModel() { // Arrange const string protoDefinition = @" @@ -482,12 +482,54 @@ message HelloReply { var request = Request.Create() .UsingPost() .WithPath("/grpc/greet-Greeter-SayHello") - .WithGrpcProto(protoDefinition, "greet.HelloRequest", jsonMatcher); + .WithBodyAsProtoBuf(protoDefinition, "greet.HelloRequest", jsonMatcher); + + var response = Response.Create(); + + var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 41, null, null, null, null, null, false, null, null, null); + + // Act + var model = _sut.ToMappingModel(mapping); + + // Assert + model.Should().NotBeNull(); + + // Verify + return Verifier.Verify(model); + } + + [Fact] + public Task ToMappingModel_Response_WithBodyAsProtoBuf_ReturnsCorrectModel() + { + // Arrange + const string protoDefinition = @" +syntax = ""proto3""; + +package greet; + +service Greeter { + rpc SayHello (HelloRequest) returns (HelloReply); +} + +message HelloRequest { + string name = 1; +} + +message HelloReply { + string message = 1; +} +"; + var protobufResponse = new + { + message = "hello" + }; + + var request = Request.Create(); var response = Response.Create() .WithBodyAsProtoBuf(protoDefinition, "greet.HelloReply", protobufResponse); - var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, null, null); + var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 43, null, null, null, null, null, false, null, null, null); // Act var model = _sut.ToMappingModel(mapping); diff --git a/test/WireMock.Net.Tests/Serialization/MatcherMapperTests.cs b/test/WireMock.Net.Tests/Serialization/MatcherMapperTests.cs index 58b1bd9a3..0059e140e 100644 --- a/test/WireMock.Net.Tests/Serialization/MatcherMapperTests.cs +++ b/test/WireMock.Net.Tests/Serialization/MatcherMapperTests.cs @@ -27,7 +27,7 @@ public MatcherMapperTests() } [Fact] - public void MatcherMapper_Map_IMatcher_Null() + public void MatcherMapper_Map_Matcher_IMatcher_Null() { // Act var model = _sut.Map((IMatcher?)null); @@ -37,7 +37,7 @@ public void MatcherMapper_Map_IMatcher_Null() } [Fact] - public void MatcherMapper_Map_IMatchers_Null() + public void MatcherMapper_Map_Matcher_IMatchers_Null() { // Act var model = _sut.Map((IMatcher[]?)null); @@ -47,7 +47,7 @@ public void MatcherMapper_Map_IMatchers_Null() } [Fact] - public void MatcherMapper_Map_IMatchers() + public void MatcherMapper_Map_Matcher_IMatchers() { // Assign var matcherMock1 = new Mock(); @@ -62,7 +62,7 @@ public void MatcherMapper_Map_IMatchers() #if MIMEKIT [Fact] - public void MatcherMapper_Map_MimePartMatcher() + public void MatcherMapper_Map_Matcher_MimePartMatcher() { // Arrange var bytes = Convert.FromBase64String("c3RlZg=="); @@ -95,7 +95,7 @@ public void MatcherMapper_Map_MimePartMatcher() #endif [Fact] - public void MatcherMapper_Map_IStringMatcher() + public void MatcherMapper_Map_Matcher_IStringMatcher() { // Assign var matcherMock = new Mock(); @@ -115,7 +115,7 @@ public void MatcherMapper_Map_IStringMatcher() } [Fact] - public void MatcherMapper_Map_IStringMatcher_With_PatternAsFile() + public void MatcherMapper_Map_Matcher_IStringMatcher_With_PatternAsFile() { // Arrange var pattern = new StringPattern { Pattern = "p", PatternAsFile = "pf" }; @@ -136,7 +136,7 @@ public void MatcherMapper_Map_IStringMatcher_With_PatternAsFile() } [Fact] - public void MatcherMapper_Map_IIgnoreCaseMatcher() + public void MatcherMapper_Map_Matcher_IIgnoreCaseMatcher() { // Assign var matcherMock = new Mock(); @@ -150,7 +150,7 @@ public void MatcherMapper_Map_IIgnoreCaseMatcher() } [Fact] - public void MatcherMapper_Map_XPathMatcher() + public void MatcherMapper_Map_Matcher_XPathMatcher() { // Assign var xmlNamespaceMap = new[] @@ -171,7 +171,7 @@ public void MatcherMapper_Map_XPathMatcher() #if GRAPHQL [Fact] - public void MatcherMapper_Map_GraphQLMatcher() + public void MatcherMapper_Map_Matcher_GraphQLMatcher() { // Assign const string testSchema = @" @@ -199,6 +199,47 @@ type Mutation { } #endif +#if PROTOBUF + [Fact] + public void MatcherMapper_Map_Matcher_ProtoBufMatcher() + { + // Arrange + const string protoDefinition = @" +syntax = ""proto3""; + +package greet; + +service Greeter { + rpc SayHello (HelloRequest) returns (HelloReply); +} + +message HelloRequest { + string name = 1; +} + +message HelloReply { + string message = 1; +} +"; + const string messageType = "greet.HelloRequest"; + + var jsonPattern = new { name = "stef" }; + var jsonMatcher = new JsonMatcher(jsonPattern); + + var matcher = new ProtoBufMatcher(protoDefinition, messageType, jsonMatcher: jsonMatcher); + + // Act + var model = _sut.Map(matcher)!; + + // Assert + model.Name.Should().Be(nameof(ProtoBufMatcher)); + model.Pattern.Should().Be(protoDefinition); + model.ProtoBufMessageType.Should().Be(messageType); + model.ContentMatcher?.Name.Should().Be("JsonMatcher"); + model.ContentMatcher?.Pattern.Should().Be(jsonPattern); + } +#endif + [Fact] public void MatcherMapper_Map_MatcherModel_Null() { diff --git a/test/WireMock.Net.Tests/WireMockServerTests.WithGrpcProto.cs b/test/WireMock.Net.Tests/WireMockServerTests.WithGrpcProto.cs index a7e964a6a..8a810c6af 100644 --- a/test/WireMock.Net.Tests/WireMockServerTests.WithGrpcProto.cs +++ b/test/WireMock.Net.Tests/WireMockServerTests.WithGrpcProto.cs @@ -33,7 +33,7 @@ message HelloReply { } "; [Fact] - public async Task WireMockServer_WithGrpcProto() + public async Task WireMockServer_WithBodyAsProtoBuf() { // Arrange var bytes = Convert.FromBase64String("CgRzdGVm"); @@ -45,7 +45,7 @@ public async Task WireMockServer_WithGrpcProto() .Given(Request.Create() .UsingPost() .WithPath("/grpc/greet-Greeter-SayHello") - .WithGrpcProto(ProtoDefinition, "greet.HelloRequest", jsonMatcher) + .WithBodyAsProtoBuf(ProtoDefinition, "greet.HelloRequest", jsonMatcher) ) .RespondWith(Response.Create() .WithBodyAsProtoBuf(ProtoDefinition, "greet.HelloReply",