diff --git a/src/WireMock.Net.Abstractions/Models/IBodyData.cs b/src/WireMock.Net.Abstractions/Models/IBodyData.cs index bbcd4dd10..e7a1843a4 100644 --- a/src/WireMock.Net.Abstractions/Models/IBodyData.cs +++ b/src/WireMock.Net.Abstractions/Models/IBodyData.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Text; using WireMock.Types; @@ -75,7 +76,7 @@ public interface IBodyData /// /// Gets or sets the proto definition. /// - public string? ProtoDefinition { get; set; } + public Func? ProtoDefinition { get; set; } /// /// Gets or sets the full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". diff --git a/src/WireMock.Net/IMapping.cs b/src/WireMock.Net/IMapping.cs index 20b3e947b..2aa915228 100644 --- a/src/WireMock.Net/IMapping.cs +++ b/src/WireMock.Net/IMapping.cs @@ -69,12 +69,12 @@ public interface IMapping int? StateTimes { get; } /// - /// The Request matcher. + /// The RequestMatcher. /// IRequestMatcher RequestMatcher { get; } /// - /// The Provider. + /// The ResponseProvider. /// IResponseProvider Provider { get; } @@ -136,6 +136,11 @@ public interface IMapping /// double? Probability { get; } + /// + /// The Grpc ProtoDefinition which is used for this mapping (request and response). [Optional] + /// + string? ProtoDefinition { get; } + /// /// ProvideResponseAsync /// @@ -150,4 +155,44 @@ public interface IMapping /// The Next State. /// The . IRequestMatchResult GetRequestMatchResult(IRequestMessage requestMessage, string? nextState); -} \ No newline at end of file + + /// + /// Define a Grpc ProtoDefinition which is used for this mapping (request and response). + /// + /// The proto definition as a string. + /// The . + IMapping WithProtoDefinition(string protoDefinition); + + /// + /// Define the scenario. + /// + /// The scenario. + /// The . + IMapping WithScenario(string scenario); + + /// + /// Define the probability when this request should be matched. [Optional] + /// + /// The probability. + /// The . + IMapping WithProbability(double probability); +} + +/* + executionConditionState">State in which the current mapping can occur. [Optional] + nextState">The next state which will occur after the current mapping execution. [Optional] + stateTimes">Only when the current state is executed this number, the next state which will occur. [Optional] + webhooks">The Webhooks. [Optional] + useWebhooksFireAndForget">Use Fire and Forget for the defined webhook(s). [Optional] + timeSettings">The TimeSettings. [Optional] + data">The data object. [Optional] + + + string? executionConditionState, + string? nextState, + int? stateTimes, + IWebhook[]? webhooks, + bool? useWebhooksFireAndForget, + ITimeSettings? timeSettings, + object? data, +*/ \ No newline at end of file diff --git a/src/WireMock.Net/Mapping.cs b/src/WireMock.Net/Mapping.cs index 019c208b5..1b9440f2a 100644 --- a/src/WireMock.Net/Mapping.cs +++ b/src/WireMock.Net/Mapping.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using Stef.Validation; using WireMock.Matchers.Request; using WireMock.Models; using WireMock.ResponseProviders; @@ -31,7 +32,7 @@ public class Mapping : IMapping public int Priority { get; } /// - public string? Scenario { get; } + public string? Scenario { get; private set; } /// public string? ExecutionConditionState { get; } @@ -76,7 +77,10 @@ public class Mapping : IMapping public object? Data { get; } /// - public double? Probability { get; } + public double? Probability { get; private set; } + + /// + public string? ProtoDefinition { get; private set; } /// /// Initializes a new instance of the class. @@ -98,7 +102,6 @@ public class Mapping : IMapping /// Use Fire and Forget for the defined webhook(s). [Optional] /// The TimeSettings. [Optional] /// The data object. [Optional] - /// Define the probability when this request should be matched. [Optional] public Mapping( Guid guid, DateTime updatedAt, @@ -116,8 +119,8 @@ public Mapping( IWebhook[]? webhooks, bool? useWebhooksFireAndForget, ITimeSettings? timeSettings, - object? data, - double? probability) + object? data + ) { Guid = guid; UpdatedAt = updatedAt; @@ -136,9 +139,8 @@ public Mapping( UseWebhooksFireAndForget = useWebhooksFireAndForget; TimeSettings = timeSettings; Data = data; - Probability = probability; } - + /// public Task<(IResponseMessage Message, IMapping? Mapping)> ProvideResponseAsync(IRequestMessage requestMessage) { @@ -168,4 +170,25 @@ public IRequestMatchResult GetRequestMatchResult(IRequestMessage requestMessage, return result; } + + /// + public IMapping WithProtoDefinition(string protoDefinition) + { + ProtoDefinition = Guard.NotNullOrWhiteSpace(protoDefinition); + return this; + } + + /// + public IMapping WithScenario(string scenario) + { + Scenario = Guard.NotNullOrWhiteSpace(scenario); + return this; + } + + /// + public IMapping WithProbability(double probability) + { + Probability = Guard.NotNull(probability); + return this; + } } \ No newline at end of file diff --git a/src/WireMock.Net/MappingBuilder.cs b/src/WireMock.Net/MappingBuilder.cs index da6f479f7..8103ba2a0 100644 --- a/src/WireMock.Net/MappingBuilder.cs +++ b/src/WireMock.Net/MappingBuilder.cs @@ -6,6 +6,8 @@ using WireMock.Admin.Mappings; using WireMock.Matchers.Request; using WireMock.Owin; +using WireMock.RequestBuilders; +using WireMock.ResponseBuilders; using WireMock.Serialization; using WireMock.Server; using WireMock.Settings; @@ -146,6 +148,15 @@ private void RegisterMapping(IMapping mapping, bool saveToFile) { _mappingToFileSaver.SaveMappingToFile(mapping); } + + // Link this mapping to the Request + ((Request)mapping.RequestMatcher).Mapping = mapping; + + // Link this mapping to the Response + if (mapping.Provider is Response response) + { + response.Mapping = mapping; + } } private static string ToJson(object value) diff --git a/src/WireMock.Net/Matchers/ProtoBufMatcher.cs b/src/WireMock.Net/Matchers/ProtoBufMatcher.cs index 61541e366..d119929a7 100644 --- a/src/WireMock.Net/Matchers/ProtoBufMatcher.cs +++ b/src/WireMock.Net/Matchers/ProtoBufMatcher.cs @@ -20,9 +20,9 @@ public class ProtoBufMatcher : IBytesMatcher public MatchBehaviour MatchBehaviour { get; } /// - /// The proto definition. + /// The Func to define the proto definition as a string. /// - public string ProtoDefinition { get; } + public Func ProtoDefinition { get; } /// /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". @@ -44,13 +44,13 @@ public class ProtoBufMatcher : IBytesMatcher /// The match behaviour. (default = "AcceptOnMatch") /// The optional jsonMatcher to use to match the ProtoBuf as (json) object. public ProtoBufMatcher( - string protoDefinition, + Func protoDefinition, string messageType, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch, IObjectMatcher? matcher = null ) { - ProtoDefinition = Guard.NotNullOrWhiteSpace(protoDefinition); + ProtoDefinition = Guard.NotNull(protoDefinition); MessageType = Guard.NotNullOrWhiteSpace(messageType); Matcher = matcher; MatchBehaviour = matchBehaviour; @@ -63,7 +63,7 @@ public MatchResult IsMatch(byte[]? input) if (input != null) { - var request = new ConvertToObjectRequest(ProtoDefinition, MessageType, input); + var request = new ConvertToObjectRequest(ProtoDefinition(), MessageType, input); try { diff --git a/src/WireMock.Net/Matchers/Request/RequestMessageProtoBufMatcher.cs b/src/WireMock.Net/Matchers/Request/RequestMessageProtoBufMatcher.cs index c793eeaf6..e18d011f1 100644 --- a/src/WireMock.Net/Matchers/Request/RequestMessageProtoBufMatcher.cs +++ b/src/WireMock.Net/Matchers/Request/RequestMessageProtoBufMatcher.cs @@ -1,3 +1,5 @@ +using System; + namespace WireMock.Matchers.Request; /// @@ -14,10 +16,10 @@ public class RequestMessageProtoBufMatcher : IRequestMatcher /// Initializes a new instance of the class. /// /// The match behaviour. (default = "AcceptOnMatch") - /// The proto definition as a string. + /// The Func to define the proto definition as a string. /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". /// The optional matcher to use to match the ProtoBuf as (json) object. - public RequestMessageProtoBufMatcher(MatchBehaviour matchBehaviour, string protoDefinition, string messageType, IObjectMatcher? matcher = null) + public RequestMessageProtoBufMatcher(MatchBehaviour matchBehaviour, Func protoDefinition, string messageType, IObjectMatcher? matcher = null) { #if PROTOBUF Matcher = new ProtoBufMatcher(protoDefinition, messageType, matchBehaviour, matcher); diff --git a/src/WireMock.Net/Models/BodyData.cs b/src/WireMock.Net/Models/BodyData.cs index fc2ac1226..7cf0e915d 100644 --- a/src/WireMock.Net/Models/BodyData.cs +++ b/src/WireMock.Net/Models/BodyData.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Text; using WireMock.Types; @@ -48,7 +49,7 @@ public class BodyData : IBodyData #region ProtoBuf /// - public string? ProtoDefinition { get; set; } + public Func? ProtoDefinition { get; set; } /// public string? ProtoBufMessageType { get; set; } diff --git a/src/WireMock.Net/Owin/Mappers/IOwinResponseMapper.cs b/src/WireMock.Net/Owin/Mappers/IOwinResponseMapper.cs index 34c5954c1..511a08f0b 100644 --- a/src/WireMock.Net/Owin/Mappers/IOwinResponseMapper.cs +++ b/src/WireMock.Net/Owin/Mappers/IOwinResponseMapper.cs @@ -5,18 +5,17 @@ using IResponse = Microsoft.AspNetCore.Http.HttpResponse; #endif -namespace WireMock.Owin.Mappers +namespace WireMock.Owin.Mappers; + +/// +/// IOwinResponseMapper +/// +internal interface IOwinResponseMapper { /// - /// IOwinResponseMapper + /// Map ResponseMessage to IResponse. /// - internal interface IOwinResponseMapper - { - /// - /// Map ResponseMessage to IResponse. - /// - /// The ResponseMessage - /// The OwinResponse/HttpResponse - Task MapAsync(IResponseMessage? responseMessage, IResponse response); - } -} \ No newline at end of file + /// The ResponseMessage + /// The OwinResponse/HttpResponse + Task MapAsync(IResponseMessage? responseMessage, IResponse response); +} diff --git a/src/WireMock.Net/Owin/Mappers/OwinResponseMapper.cs b/src/WireMock.Net/Owin/Mappers/OwinResponseMapper.cs index 6a3760459..5c7e6cc52 100644 --- a/src/WireMock.Net/Owin/Mappers/OwinResponseMapper.cs +++ b/src/WireMock.Net/Owin/Mappers/OwinResponseMapper.cs @@ -144,7 +144,8 @@ private bool IsFault(IResponseMessage responseMessage) #if PROTOBUF case BodyType.ProtoBuf: - return ProtoBufUtils.GetProtoBufMessageWithHeader(responseMessage.BodyData.ProtoDefinition, responseMessage.BodyData.ProtoBufMessageType, responseMessage.BodyData.BodyAsJson); + var protoDefinition = responseMessage.BodyData.ProtoDefinition?.Invoke(); + return ProtoBufUtils.GetProtoBufMessageWithHeader(protoDefinition, responseMessage.BodyData.ProtoBufMessageType, responseMessage.BodyData.BodyAsJson); #endif case BodyType.Bytes: diff --git a/src/WireMock.Net/RequestBuilders/IProtoBufRequestBuilder.cs b/src/WireMock.Net/RequestBuilders/IProtoBufRequestBuilder.cs index 5e6dc4ad8..d36148d1f 100644 --- a/src/WireMock.Net/RequestBuilders/IProtoBufRequestBuilder.cs +++ b/src/WireMock.Net/RequestBuilders/IProtoBufRequestBuilder.cs @@ -25,4 +25,21 @@ public interface IProtoBufRequestBuilder : IGraphQLRequestBuilder /// The match behaviour. (default = "AcceptOnMatch") /// The . IRequestBuilder WithBodyAsProtoBuf(string protoDefinition, string messageType, IObjectMatcher matcher, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch); + + /// + /// WithGrpcProto + /// + /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". + /// The match behaviour. (default = "AcceptOnMatch") + /// The . + IRequestBuilder WithBodyAsProtoBuf(string messageType, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch); + + /// + /// WithGrpcProto + /// + /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". + /// The matcher to use to match the ProtoBuf as (json) object. + /// The match behaviour. (default = "AcceptOnMatch") + /// The . + IRequestBuilder WithBodyAsProtoBuf(string messageType, IObjectMatcher matcher, 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 19ac34a38..d50f87852 100644 --- a/src/WireMock.Net/RequestBuilders/Request.WithGrpcProto.cs +++ b/src/WireMock.Net/RequestBuilders/Request.WithGrpcProto.cs @@ -1,3 +1,4 @@ +using System.Linq; using WireMock.Matchers; using WireMock.Matchers.Request; @@ -8,14 +9,24 @@ public partial class Request /// public IRequestBuilder WithBodyAsProtoBuf(string protoDefinition, string messageType, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) { - _requestMatchers.Add(new RequestMessageProtoBufMatcher(matchBehaviour, protoDefinition, messageType)); - return this; + return Add(new RequestMessageProtoBufMatcher(matchBehaviour, () => protoDefinition, messageType)); } /// public IRequestBuilder WithBodyAsProtoBuf(string protoDefinition, string messageType, IObjectMatcher matcher, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) { - _requestMatchers.Add(new RequestMessageProtoBufMatcher(matchBehaviour, protoDefinition, messageType, matcher)); - return this; + return Add(new RequestMessageProtoBufMatcher(matchBehaviour, () => protoDefinition, messageType, matcher)); + } + + /// + public IRequestBuilder WithBodyAsProtoBuf(string messageType, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) + { + return Add(new RequestMessageProtoBufMatcher(matchBehaviour, () => Mapping.ProtoDefinition!, messageType)); + } + + /// + public IRequestBuilder WithBodyAsProtoBuf(string messageType, IObjectMatcher matcher, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) + { + return Add(new RequestMessageProtoBufMatcher(matchBehaviour, () => Mapping.ProtoDefinition!, messageType, matcher)); } } \ No newline at end of file diff --git a/src/WireMock.Net/RequestBuilders/Request.cs b/src/WireMock.Net/RequestBuilders/Request.cs index 10255d5bd..f375b6987 100644 --- a/src/WireMock.Net/RequestBuilders/Request.cs +++ b/src/WireMock.Net/RequestBuilders/Request.cs @@ -16,6 +16,11 @@ public partial class Request : RequestMessageCompositeMatcher, IRequestBuilder { private readonly IList _requestMatchers; + /// + /// The link back to the Mapping. + /// + public IMapping Mapping { get; set; } = null!; + /// /// Creates this instance. /// @@ -63,4 +68,15 @@ public IList GetRequestMessageMatchers() where T : IRequestMatcher { return _requestMatchers.OfType().FirstOrDefault(func); } + + private IRequestBuilder Add(T requestMatcher) where T : IRequestMatcher + { + foreach (var existing in _requestMatchers.OfType().ToArray()) + { + _requestMatchers.Remove(existing); + } + + _requestMatchers.Add(requestMatcher); + return this; + } } \ No newline at end of file diff --git a/src/WireMock.Net/ResponseBuilders/IBodyResponseBuilder.cs b/src/WireMock.Net/ResponseBuilders/IBodyResponseBuilder.cs index cccf26a33..83f591c9d 100644 --- a/src/WireMock.Net/ResponseBuilders/IBodyResponseBuilder.cs +++ b/src/WireMock.Net/ResponseBuilders/IBodyResponseBuilder.cs @@ -110,9 +110,9 @@ public interface IBodyResponseBuilder : IFaultResponseBuilder /// WithBody : Create a ProtoBuf byte[] response based on a proto definition, message type and the value. /// /// The proto definition as a string. - /// The method which is called on service. Format is "{package-name}.{service-name}-{method-name}". + /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". /// The object to convert to protobuf byte[]. - /// The [optional]. Default value is + /// The [optional]. Default value is NewtonsoftJsonConverter. /// The [optional]. /// A . IResponseBuilder WithBodyAsProtoBuf( @@ -122,4 +122,19 @@ IResponseBuilder WithBodyAsProtoBuf( IJsonConverter? jsonConverter = null, JsonConverterOptions? options = null ); + + /// + /// WithBody : Create a ProtoBuf byte[] response based on a proto definition, message type and the value. + /// + /// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}". + /// The object to convert to protobuf byte[]. + /// The [optional]. Default value is NewtonsoftJsonConverter. + /// The [optional]. + /// A . + IResponseBuilder WithBodyAsProtoBuf( + string messageType, + object value, + IJsonConverter? jsonConverter = null, + JsonConverterOptions? options = null + ); } \ No newline at end of file diff --git a/src/WireMock.Net/ResponseBuilders/Response.WithBody.cs b/src/WireMock.Net/ResponseBuilders/Response.WithBody.cs index 4e536ac3a..41b5d6bc8 100644 --- a/src/WireMock.Net/ResponseBuilders/Response.WithBody.cs +++ b/src/WireMock.Net/ResponseBuilders/Response.WithBody.cs @@ -228,7 +228,33 @@ public IResponseBuilder WithBodyAsProtoBuf( { DetectedBodyType = BodyType.ProtoBuf, BodyAsJson = value, - ProtoDefinition = protoDefinition, + ProtoDefinition = () => protoDefinition, + ProtoBufMessageType = messageType + }; +#endif + return this; + } + + /// + public IResponseBuilder WithBodyAsProtoBuf( + string messageType, + object value, + IJsonConverter? jsonConverter = null, + JsonConverterOptions? options = null + ) + { + Guard.NotNullOrWhiteSpace(messageType); + Guard.NotNull(value); + +#if !PROTOBUF + throw new System.NotSupportedException("The WithBodyAsProtoBuf method can not be used for .NETStandard1.3 or .NET Framework 4.6.1 or lower."); +#else + ResponseMessage.BodyDestination = null; + ResponseMessage.BodyData = new BodyData + { + DetectedBodyType = BodyType.ProtoBuf, + BodyAsJson = value, + ProtoDefinition = () => Mapping.ProtoDefinition, ProtoBufMessageType = messageType }; #endif diff --git a/src/WireMock.Net/ResponseBuilders/Response.cs b/src/WireMock.Net/ResponseBuilders/Response.cs index 3f17d6591..cf375dc90 100644 --- a/src/WireMock.Net/ResponseBuilders/Response.cs +++ b/src/WireMock.Net/ResponseBuilders/Response.cs @@ -1,8 +1,6 @@ // This source file is based on mock4net by Alexandre Victoor which is licensed under the Apache 2.0 License. // For more details see 'mock4net/LICENSE.txt' and 'mock4net/readme.md' in this project root. using System; -using System.Collections.Generic; -using System.Linq; using System.Net; using System.Threading; using System.Threading.Tasks; @@ -26,6 +24,9 @@ public partial class Response : IResponseBuilder private TimeSpan? _delay; + /// + public IMapping Mapping { get; set; } = null!; + /// /// The minimum random delay in milliseconds. /// diff --git a/src/WireMock.Net/ResponseProviders/DynamicAsyncResponseProvider.cs b/src/WireMock.Net/ResponseProviders/DynamicAsyncResponseProvider.cs index c70a06d5e..90201a34b 100644 --- a/src/WireMock.Net/ResponseProviders/DynamicAsyncResponseProvider.cs +++ b/src/WireMock.Net/ResponseProviders/DynamicAsyncResponseProvider.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using Stef.Validation; using WireMock.Settings; namespace WireMock.ResponseProviders; @@ -10,7 +11,7 @@ internal class DynamicAsyncResponseProvider : IResponseProvider public DynamicAsyncResponseProvider(Func> responseMessageFunc) { - _responseMessageFunc = responseMessageFunc; + _responseMessageFunc = Guard.NotNull(responseMessageFunc); } public async Task<(IResponseMessage Message, IMapping? Mapping)> ProvideResponseAsync(IMapping mapping, IRequestMessage requestMessage, WireMockServerSettings settings) diff --git a/src/WireMock.Net/ResponseProviders/DynamicResponseProvider.cs b/src/WireMock.Net/ResponseProviders/DynamicResponseProvider.cs index ecf0bda9d..2c151a1e9 100644 --- a/src/WireMock.Net/ResponseProviders/DynamicResponseProvider.cs +++ b/src/WireMock.Net/ResponseProviders/DynamicResponseProvider.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using Stef.Validation; using WireMock.Settings; namespace WireMock.ResponseProviders; @@ -10,7 +11,7 @@ internal class DynamicResponseProvider : IResponseProvider public DynamicResponseProvider(Func responseMessageFunc) { - _responseMessageFunc = responseMessageFunc; + _responseMessageFunc = Guard.NotNull(responseMessageFunc); } public Task<(IResponseMessage Message, IMapping? Mapping)> ProvideResponseAsync(IMapping mapping, IRequestMessage requestMessage, WireMockServerSettings settings) diff --git a/src/WireMock.Net/ResponseProviders/ProxyAsyncResponseProvider.cs b/src/WireMock.Net/ResponseProviders/ProxyAsyncResponseProvider.cs index 9aab96024..2cc6d853a 100644 --- a/src/WireMock.Net/ResponseProviders/ProxyAsyncResponseProvider.cs +++ b/src/WireMock.Net/ResponseProviders/ProxyAsyncResponseProvider.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using Stef.Validation; using WireMock.Settings; namespace WireMock.ResponseProviders; @@ -11,8 +12,8 @@ internal class ProxyAsyncResponseProvider : IResponseProvider public ProxyAsyncResponseProvider(Func> responseMessageFunc, WireMockServerSettings settings) { - _responseMessageFunc = responseMessageFunc; - _settings = settings; + _responseMessageFunc = Guard.NotNull(responseMessageFunc); + _settings = Guard.NotNull(settings); } public async Task<(IResponseMessage Message, IMapping? Mapping)> ProvideResponseAsync(IMapping mapping, IRequestMessage requestMessage, WireMockServerSettings settings) diff --git a/src/WireMock.Net/Serialization/MappingConverter.cs b/src/WireMock.Net/Serialization/MappingConverter.cs index 545dd54da..00701fbcb 100644 --- a/src/WireMock.Net/Serialization/MappingConverter.cs +++ b/src/WireMock.Net/Serialization/MappingConverter.cs @@ -471,7 +471,7 @@ private static void MapResponse(Response response, MappingModel mappingModel) break; case BodyType.ProtoBuf: - mappingModel.Response.ProtoDefinition = response.ResponseMessage.BodyData.ProtoDefinition; + mappingModel.Response.ProtoDefinition = response.ResponseMessage.BodyData.ProtoDefinition?.Invoke(); mappingModel.Response.ProtoBufMessageType = response.ResponseMessage.BodyData.ProtoBufMessageType; mappingModel.Response.BodyAsBytes = null; mappingModel.Response.BodyAsJson = response.ResponseMessage.BodyData.BodyAsJson; diff --git a/src/WireMock.Net/Serialization/MatcherMapper.cs b/src/WireMock.Net/Serialization/MatcherMapper.cs index b5aaabc8a..b504a0145 100644 --- a/src/WireMock.Net/Serialization/MatcherMapper.cs +++ b/src/WireMock.Net/Serialization/MatcherMapper.cs @@ -211,7 +211,7 @@ public MatcherMapper(WireMockServerSettings settings) #if PROTOBUF case ProtoBufMatcher protoBufMatcher: - model.Pattern = protoBufMatcher.ProtoDefinition; + model.Pattern = protoBufMatcher.ProtoDefinition(); model.ProtoBufMessageType = protoBufMatcher.MessageType; model.ContentMatcher = Map(protoBufMatcher.Matcher); break; @@ -264,7 +264,7 @@ private static ExactObjectMatcher CreateExactObjectMatcher(MatchBehaviour matchB } #if MIMEKIT - private MimePartMatcher CreateMimePartMatcher(MatchBehaviour matchBehaviour, MatcherModel? matcher) + private MimePartMatcher CreateMimePartMatcher(MatchBehaviour matchBehaviour, MatcherModel matcher) { var contentTypeMatcher = Map(matcher?.ContentTypeMatcher) as IStringMatcher; var contentDispositionMatcher = Map(matcher?.ContentDispositionMatcher) as IStringMatcher; @@ -276,13 +276,13 @@ private MimePartMatcher CreateMimePartMatcher(MatchBehaviour matchBehaviour, Mat #endif #if PROTOBUF - private ProtoBufMatcher CreateProtoBufMatcher(MatchBehaviour? matchBehaviour, string? protoDefinition, MatcherModel? matcher) + private ProtoBufMatcher CreateProtoBufMatcher(MatchBehaviour? matchBehaviour, string protoDefinition, MatcherModel matcher) { var objectMatcher = Map(matcher?.ContentMatcher) as IObjectMatcher; return new ProtoBufMatcher( - protoDefinition ?? string.Empty, - matcher?.ProtoBufMessageType ?? string.Empty, + () => protoDefinition, + matcher!.ProtoBufMessageType!, matchBehaviour ?? MatchBehaviour.AcceptOnMatch, objectMatcher ); diff --git a/src/WireMock.Net/Serialization/ProxyMappingConverter.cs b/src/WireMock.Net/Serialization/ProxyMappingConverter.cs index 3878c30f9..196f83efe 100644 --- a/src/WireMock.Net/Serialization/ProxyMappingConverter.cs +++ b/src/WireMock.Net/Serialization/ProxyMappingConverter.cs @@ -188,8 +188,7 @@ public ProxyMappingConverter(WireMockServerSettings settings, IGuidUtils guidUti webhooks: null, useWebhooksFireAndForget: null, timeSettings: null, - data: mapping?.Data, - probability: null + data: mapping?.Data ); } } \ No newline at end of file diff --git a/src/WireMock.Net/Server/IRespondWithAProvider.cs b/src/WireMock.Net/Server/IRespondWithAProvider.cs index ff6884d77..e00a43973 100644 --- a/src/WireMock.Net/Server/IRespondWithAProvider.cs +++ b/src/WireMock.Net/Server/IRespondWithAProvider.cs @@ -186,7 +186,7 @@ IRespondWithAProvider WithWebhook( IRespondWithAProvider WithProbability(double probability); /// - /// Add a Grpc ProtoDefinition which is used for the request and the response. + /// Define a Grpc ProtoDefinition which is used for the request and the response. /// /// The proto definition as a string. /// The . diff --git a/src/WireMock.Net/Server/RespondWithAProvider.cs b/src/WireMock.Net/Server/RespondWithAProvider.cs index 7dd71c338..6e30c38cb 100644 --- a/src/WireMock.Net/Server/RespondWithAProvider.cs +++ b/src/WireMock.Net/Server/RespondWithAProvider.cs @@ -2,7 +2,6 @@ // For more details see 'mock4net/LICENSE.txt' and 'mock4net/readme.md' in this project root. using System; using System.Collections.Generic; -using JetBrains.Annotations; using Stef.Validation; using WireMock.Matchers.Request; using WireMock.Models; @@ -34,6 +33,7 @@ internal class RespondWithAProvider : IRespondWithAProvider private int _timesInSameState = 1; private bool? _useWebhookFireAndForget; private double? _probability; + private string? _protoDefinition; public Guid Guid { get; private set; } @@ -43,8 +43,6 @@ internal class RespondWithAProvider : IRespondWithAProvider public object? Data { get; private set; } - public string? ProtoDefinition { get; private set; } - /// /// Initializes a new instance of the class. /// @@ -96,10 +94,19 @@ public void RespondWith(IResponseProvider provider) Webhooks, _useWebhookFireAndForget, TimeSettings, - Data, - _probability + Data ); + if (_probability != null) + { + mapping.WithProbability(_probability.Value); + } + + if (_protoDefinition != null) + { + mapping.WithProtoDefinition(_protoDefinition); + } + _registrationCallback(mapping, _saveToFile); } @@ -290,7 +297,7 @@ public IRespondWithAProvider WithProbability(double probability) /// public IRespondWithAProvider WithProtoDefinition(string protoDefinition) { - ProtoDefinition = Guard.NotNullOrWhiteSpace(protoDefinition); + _protoDefinition = Guard.NotNullOrWhiteSpace(protoDefinition); return this; } diff --git a/test/WireMock.Net.Tests/Grpc/WireMockServerTests.Grpc.cs b/test/WireMock.Net.Tests/Grpc/WireMockServerTests.Grpc.cs index 95dcd3dc1..61952d1c2 100644 --- a/test/WireMock.Net.Tests/Grpc/WireMockServerTests.Grpc.cs +++ b/test/WireMock.Net.Tests/Grpc/WireMockServerTests.Grpc.cs @@ -51,5 +51,45 @@ public async Task WireMockServer_WithBodyAsProtoBuf_InlineProtoDefinition_UsingG server.Stop(); } + + [Fact] + public async Task WireMockServer_WithBodyAsProtoBuf_MappingProtoDefinition_UsingGrpcGeneratedClient() + { + // Arrange + using var server = WireMockServer.Start(useHttp2: true); + + var jsonMatcher = new JsonMatcher(new { name = "stef" }); + + server + .Given(Request.Create() + .UsingPost() + .WithPath("/greet.Greeter/SayHello") + .WithBodyAsProtoBuf("greet.HelloRequest", jsonMatcher) + ) + .WithProtoDefinition(ProtoDefinition) + .RespondWith(Response.Create() + .WithHeader("Content-Type", "application/grpc") + .WithBodyAsProtoBuf("greet.HelloReply", + new + { + message = "hello stef {{request.method}}" + } + ) + .WithTrailingHeader("grpc-status", "0") + .WithTransformer() + ); + + // Act + var channel = GrpcChannel.ForAddress(server.Url!); + + var client = new Greeter.GreeterClient(channel); + + var reply = await client.SayHelloAsync(new HelloRequest { Name = "stef" }); + + // Assert + reply.Message.Should().Be("hello stef POST"); + + server.Stop(); + } } #endif \ No newline at end of file diff --git a/test/WireMock.Net.Tests/Matchers/ProtoBufMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/ProtoBufMatcherTests.cs index b9370acf6..954906029 100644 --- a/test/WireMock.Net.Tests/Matchers/ProtoBufMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/ProtoBufMatcherTests.cs @@ -35,7 +35,7 @@ public void ProtoBufMatcher_For_ValidProtoBuf_And_ValidMethod_NoJsonMatchers_IsM var bytes = Convert.FromBase64String("CgRzdGVm"); // Act - var matcher = new ProtoBufMatcher(ProtoDefinition, MessageType); + var matcher = new ProtoBufMatcher(() => ProtoDefinition, MessageType); var result = matcher.IsMatch(bytes); // Assert @@ -51,7 +51,7 @@ public void ProtoBufMatcher_For_ValidProtoBuf_And_ValidMethod_Using_JsonMatcher_ var bytes = Convert.FromBase64String("CgRzdGVm"); // Act - var matcher = new ProtoBufMatcher(ProtoDefinition, MessageType, matcher: jsonMatcher); + var matcher = new ProtoBufMatcher(() => ProtoDefinition, MessageType, matcher: jsonMatcher); var result = matcher.IsMatch(bytes); // Assert @@ -66,7 +66,7 @@ public void ProtoBufMatcher_For_InvalidProtoBuf_IsNoMatch() var bytes = new byte[] { 1, 2, 3 }; // Act - var matcher = new ProtoBufMatcher(ProtoDefinition, MessageType); + var matcher = new ProtoBufMatcher(() => ProtoDefinition, MessageType); var result = matcher.IsMatch(bytes); // Assert @@ -81,7 +81,7 @@ public void ProtoBufMatcher_For_InvalidMethod_IsNoMatch() var bytes = Convert.FromBase64String("CgRzdGVm"); // Act - var matcher = new ProtoBufMatcher(ProtoDefinition, "greet.Greeter.X"); + var matcher = new ProtoBufMatcher(() => ProtoDefinition, "greet.Greeter.X"); var result = matcher.IsMatch(bytes); // Assert diff --git a/test/WireMock.Net.Tests/Owin/GlobalExceptionMiddlewareTests.cs b/test/WireMock.Net.Tests/Owin/GlobalExceptionMiddlewareTests.cs index 5b1926c95..a85bafa61 100644 --- a/test/WireMock.Net.Tests/Owin/GlobalExceptionMiddlewareTests.cs +++ b/test/WireMock.Net.Tests/Owin/GlobalExceptionMiddlewareTests.cs @@ -28,7 +28,7 @@ public GlobalExceptionMiddlewareTests() _responseMapperMock = new Mock(); _responseMapperMock.SetupAllProperties(); - _responseMapperMock.Setup(m => m.MapAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); + _responseMapperMock.Setup(m => m.MapAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); _sut = new GlobalExceptionMiddleware(null, _optionsMock.Object, _responseMapperMock.Object); } diff --git a/test/WireMock.Net.Tests/Owin/Mappers/OwinResponseMapperTests.cs b/test/WireMock.Net.Tests/Owin/Mappers/OwinResponseMapperTests.cs index 65e4b9a70..cc60126ed 100644 --- a/test/WireMock.Net.Tests/Owin/Mappers/OwinResponseMapperTests.cs +++ b/test/WireMock.Net.Tests/Owin/Mappers/OwinResponseMapperTests.cs @@ -202,7 +202,7 @@ public async Task OwinResponseMapper_MapAsync_BodyAsBytes() public async Task OwinResponseMapper_MapAsync_BodyAsJson() { // Arrange - var json = new { t = "x", i = (string)null }; + var json = new { t = "x", i = (string?)null }; var responseMessage = new ResponseMessage { Headers = new Dictionary>(), diff --git a/test/WireMock.Net.Tests/Owin/WireMockMiddlewareTests.cs b/test/WireMock.Net.Tests/Owin/WireMockMiddlewareTests.cs index b7e85b890..cfd2b222e 100644 --- a/test/WireMock.Net.Tests/Owin/WireMockMiddlewareTests.cs +++ b/test/WireMock.Net.Tests/Owin/WireMockMiddlewareTests.cs @@ -70,7 +70,7 @@ public WireMockMiddlewareTests() _responseMapperMock = new Mock(); _responseMapperMock.SetupAllProperties(); - _responseMapperMock.Setup(m => m.MapAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); + _responseMapperMock.Setup(m => m.MapAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); _matcherMock = new Mock(); _matcherMock.SetupAllProperties(); @@ -216,7 +216,7 @@ public async Task WireMockMiddleware_Invoke_Mapping_Has_ProxyAndRecordSettings_A _mappingMock.SetupGet(m => m.Provider).Returns(responseBuilder); _mappingMock.SetupGet(m => m.Settings).Returns(settings); - var newMappingFromProxy = new Mapping(NewGuid, UpdatedAt, string.Empty, string.Empty, null, settings, Request.Create(), Response.Create(), 0, null, null, null, null, null, false, null, null, null); + var newMappingFromProxy = new Mapping(NewGuid, UpdatedAt, string.Empty, string.Empty, null, settings, Request.Create(), Response.Create(), 0, null, null, null, null, null, false, null, null); _mappingMock.Setup(m => m.ProvideResponseAsync(It.IsAny())).ReturnsAsync((new ResponseMessage(), newMappingFromProxy)); var requestBuilder = Request.Create().UsingAnyMethod(); @@ -270,7 +270,7 @@ public async Task WireMockMiddleware_Invoke_Mapping_Has_ProxyAndRecordSettings_A _mappingMock.SetupGet(m => m.Provider).Returns(responseBuilder); _mappingMock.SetupGet(m => m.Settings).Returns(settings); - var newMappingFromProxy = new Mapping(NewGuid, UpdatedAt, "my-title", "my-description", null, settings, Request.Create(), Response.Create(), 0, null, null, null, null, null, false, null, data: null, probability: null); + var newMappingFromProxy = new Mapping(NewGuid, UpdatedAt, "my-title", "my-description", null, settings, Request.Create(), Response.Create(), 0, null, null, null, null, null, false, null, data: null); _mappingMock.Setup(m => m.ProvideResponseAsync(It.IsAny())).ReturnsAsync((new ResponseMessage(), newMappingFromProxy)); var requestBuilder = Request.Create().UsingAnyMethod(); diff --git a/test/WireMock.Net.Tests/RequestBuilders/RequestBuilderWithProtoBufTests.cs b/test/WireMock.Net.Tests/RequestBuilders/RequestBuilderWithProtoBufTests.cs index 5eaa7425b..f87c058d9 100644 --- a/test/WireMock.Net.Tests/RequestBuilders/RequestBuilderWithProtoBufTests.cs +++ b/test/WireMock.Net.Tests/RequestBuilders/RequestBuilderWithProtoBufTests.cs @@ -40,7 +40,7 @@ public void RequestBuilder_WithGrpcProto_Without_JsonMatcher() matchers.Should().HaveCount(1); var protoBufMatcher = (ProtoBufMatcher)((RequestMessageProtoBufMatcher)matchers[0]).Matcher!; - protoBufMatcher.ProtoDefinition.Should().Be(TestProtoDefinition); + protoBufMatcher.ProtoDefinition().Should().Be(TestProtoDefinition); protoBufMatcher.MessageType.Should().Be(MessageType); protoBufMatcher.Matcher.Should().BeNull(); } @@ -57,7 +57,7 @@ public void RequestBuilder_WithGrpcProto_With_JsonMatcher() matchers.Should().HaveCount(1); var protoBufMatcher = (ProtoBufMatcher)((RequestMessageProtoBufMatcher)matchers[0]).Matcher!; - protoBufMatcher.ProtoDefinition.Should().Be(TestProtoDefinition); + protoBufMatcher.ProtoDefinition().Should().Be(TestProtoDefinition); protoBufMatcher.MessageType.Should().Be(MessageType); protoBufMatcher.Matcher.Should().BeOfType(); } diff --git a/test/WireMock.Net.Tests/Serialization/MappingConverterTests.ToCSharpCode.cs b/test/WireMock.Net.Tests/Serialization/MappingConverterTests.ToCSharpCode.cs index c34a32d91..324393e35 100644 --- a/test/WireMock.Net.Tests/Serialization/MappingConverterTests.ToCSharpCode.cs +++ b/test/WireMock.Net.Tests/Serialization/MappingConverterTests.ToCSharpCode.cs @@ -102,7 +102,7 @@ public Task ToCSharpCode_With_Server_And_AddStartIsFalse() return Verifier.Verify(code, VerifySettings); } - private Mapping CreateMapping() + private IMapping CreateMapping() { var guid = new Guid("8e7b9ab7-e18e-4502-8bc9-11e6679811cc"); var request = Request.Create() @@ -119,7 +119,8 @@ private Mapping CreateMapping() .WithDelay(12345) .WithTransformer(); - return new Mapping( + return new Mapping + ( guid, _updatedAt, string.Empty, @@ -136,9 +137,8 @@ private Mapping CreateMapping() null, false, null, - data: null, - probability: 0.3 - ); + data: null + ).WithProbability(0.3); } } #endif \ 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 ac1d372ea..1b14f2689 100644 --- a/test/WireMock.Net.Tests/Serialization/MappingConverterTests.cs +++ b/test/WireMock.Net.Tests/Serialization/MappingConverterTests.cs @@ -58,7 +58,7 @@ public Task ToMappingModel_With_SingleWebHook() } } }; - var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 0, null, null, null, null, webhooks, false, null, data: null, probability: null); + var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 0, null, null, null, null, webhooks, false, null, data: null); // Act var model = _sut.ToMappingModel(mapping); @@ -131,7 +131,7 @@ public Task ToMappingModel_With_MultipleWebHooks() } } }; - var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 0, null, null, null, null, webhooks, true, null, data: null, probability: null); + var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 0, null, null, null, null, webhooks, true, null, data: null); // Act var model = _sut.ToMappingModel(mapping); @@ -169,7 +169,7 @@ public Task ToMappingModel_WithTitle_And_Description_ReturnsCorrectModel() var description = "my-description"; var request = Request.Create(); var response = Response.Create(); - var mapping = new Mapping(_guid, _updatedAt, title, description, null, _settings, request, response, 0, null, null, null, null, null, false, null, data: null, probability: null); + var mapping = new Mapping(_guid, _updatedAt, title, description, null, _settings, request, response, 0, null, null, null, null, null, false, null, data: null); // Act var model = _sut.ToMappingModel(mapping); @@ -189,7 +189,7 @@ public Task ToMappingModel_WithPriority_ReturnsPriority() // Assign var request = Request.Create(); var response = Response.Create().WithBodyAsJson(new { x = "x" }).WithTransformer(); - var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null, probability: null); + var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null); // Act var model = _sut.ToMappingModel(mapping); @@ -218,7 +218,7 @@ public Task ToMappingModel_WithTimeSettings_ReturnsCorrectTimeSettings() End = end, TTL = ttl }; - var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, timeSettings, data: null, probability: null); + var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, timeSettings, data: null); // Act var model = _sut.ToMappingModel(mapping); @@ -249,7 +249,7 @@ public void ToMappingModel_WithDelayAsTimeSpan_ReturnsCorrectModel() { var request = Request.Create(); var response = Response.Create().WithDelay(test.Delay); - var mapping = new Mapping(Guid.NewGuid(), _updatedAt, string.Empty, string.Empty, string.Empty, _settings, request, response, 42, null, null, null, null, null, false, null, data: null, probability: null); + var mapping = new Mapping(Guid.NewGuid(), _updatedAt, string.Empty, string.Empty, string.Empty, _settings, request, response, 42, null, null, null, null, null, false, null, data: null); // Act var model = _sut.ToMappingModel(mapping); @@ -267,7 +267,7 @@ public Task ToMappingModel_WithDelay_ReturnsCorrectModel() var delay = 1000; var request = Request.Create(); var response = Response.Create().WithDelay(delay); - var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null, probability: null); + var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null); // Act var model = _sut.ToMappingModel(mapping); @@ -287,7 +287,7 @@ public Task ToMappingModel_WithRandomMinimumDelay_ReturnsCorrectModel() int minimumDelay = 1000; var request = Request.Create(); var response = Response.Create().WithRandomDelay(minimumDelay); - var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null, probability: null); + var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null); // Act var model = _sut.ToMappingModel(mapping); @@ -310,7 +310,7 @@ public Task ToMappingModel_WithRandomDelay_ReturnsCorrectModel() int maximumDelay = 2000; var request = Request.Create(); var response = Response.Create().WithRandomDelay(minimumDelay, maximumDelay); - var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null, probability: null); + var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null); // Act var model = _sut.ToMappingModel(mapping); @@ -332,7 +332,8 @@ public Task ToMappingModel_WithProbability_ReturnsCorrectModel() double probability = 0.4; var request = Request.Create(); var response = Response.Create(); - var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null, probability: probability); + var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 42, null, null, null, null, null, false, null, data: null) + .WithProbability(probability); // Act var model = _sut.ToMappingModel(mapping); @@ -351,7 +352,7 @@ public Task ToMappingModel_Request_WithClientIP_ReturnsCorrectModel() // Arrange var request = Request.Create().WithClientIP("1.2.3.4"); var response = Response.Create(); - 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, 42, null, null, null, null, null, false, null, null); // Act var model = _sut.ToMappingModel(mapping); @@ -383,7 +384,7 @@ public Task ToMappingModel_Request_WithHeader_And_Cookie_ReturnsCorrectModel() var response = Response.Create(); - var mapping = new Mapping(_guid, _updatedAt, null, null, null, _settings, request, response, 0, null, null, null, null, null, false, null, data: null, probability: null); + var mapping = new Mapping(_guid, _updatedAt, null, null, null, _settings, request, response, 0, null, null, null, null, null, false, null, data: null); // Act var model = _sut.ToMappingModel(mapping); @@ -409,7 +410,7 @@ public Task ToMappingModel_Response_WithHeader_ReturnsCorrectModel() .WithHeaders(new Dictionary> { { "w", new WireMockList("x") } }) .WithHeaders(new Dictionary> { { "w[]", new WireMockList("x", "y") } }); - var mapping = new Mapping(_guid, _updatedAt, null, null, null, _settings, request, response, 0, null, null, null, null, null, false, null, data: null, probability: null); + var mapping = new Mapping(_guid, _updatedAt, null, null, null, _settings, request, response, 0, null, null, null, null, null, false, null, data: null); // Act var model = _sut.ToMappingModel(mapping); @@ -430,7 +431,7 @@ public Task ToMappingModel_Response_WithHeaders_ReturnsCorrectModel() var response = Response.Create() .WithHeaders(new Dictionary> { { "w[]", new WireMockList("x", "y") } }); - var mapping = new Mapping(_guid, _updatedAt, null, null, null, _settings, request, response, 0, null, null, null, null, null, false, null, data: null, probability: null); + var mapping = new Mapping(_guid, _updatedAt, null, null, null, _settings, request, response, 0, null, null, null, null, null, false, null, data: null); // Act var model = _sut.ToMappingModel(mapping); @@ -453,7 +454,7 @@ public Task ToMappingModel_Response_WithTrailingHeader_ReturnsCorrectModel() .WithTrailingHeader("x1", "y") .WithTrailingHeader("x2", "y", "z"); - var mapping = new Mapping(_guid, _updatedAt, null, null, null, _settings, request, response, 0, null, null, null, null, null, false, null, data: null, probability: null); + var mapping = new Mapping(_guid, _updatedAt, null, null, null, _settings, request, response, 0, null, null, null, null, null, false, null, data: null); // Act var model = _sut.ToMappingModel(mapping); @@ -474,7 +475,7 @@ public Task ToMappingModel_Response_WithTrailingHeaders_ReturnsCorrectModel() var response = Response.Create() .WithTrailingHeaders(new Dictionary> { { "w[]", new WireMockList("x", "y") } }); - var mapping = new Mapping(_guid, _updatedAt, null, null, null, _settings, request, response, 0, null, null, null, null, null, false, null, data: null, probability: null); + var mapping = new Mapping(_guid, _updatedAt, null, null, null, _settings, request, response, 0, null, null, null, null, null, false, null, data: null); // Act var model = _sut.ToMappingModel(mapping); @@ -499,7 +500,7 @@ public Task ToMappingModel_WithParam_ReturnsCorrectModel() .WithParam("ExactMatcher", new ExactMatcher("exact")) ; var response = Response.Create(); - var mapping = new Mapping(_guid, _updatedAt, null, null, null, _settings, request, response, 0, null, null, null, null, null, false, null, data: null, probability: null); + var mapping = new Mapping(_guid, _updatedAt, null, null, null, _settings, request, response, 0, null, null, null, null, null, false, null, data: null); // Act var model = _sut.ToMappingModel(mapping); @@ -531,7 +532,7 @@ type Student { }"; var request = Request.Create().WithBodyAsGraphQLSchema(schema); var response = Response.Create(); - 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, 42, null, null, null, null, null, false, null, null); // Act var model = _sut.ToMappingModel(mapping); @@ -575,7 +576,7 @@ message HelloReply { 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); + var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 41, null, null, null, null, null, false, null, null); // Act var model = _sut.ToMappingModel(mapping); @@ -619,7 +620,7 @@ message HelloReply { .WithBodyAsProtoBuf(protoDefinition, "greet.HelloReply", protobufResponse) .WithTrailingHeader("grpc-status", "0"); - var mapping = new Mapping(_guid, _updatedAt, string.Empty, string.Empty, null, _settings, request, response, 43, 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); // 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 cf4ce0686..55ea177d0 100644 --- a/test/WireMock.Net.Tests/Serialization/MatcherMapperTests.cs +++ b/test/WireMock.Net.Tests/Serialization/MatcherMapperTests.cs @@ -226,7 +226,7 @@ message HelloReply { var jsonPattern = new { name = "stef" }; var jsonMatcher = new JsonMatcher(jsonPattern); - var matcher = new ProtoBufMatcher(protoDefinition, messageType, matcher: jsonMatcher); + var matcher = new ProtoBufMatcher(() => protoDefinition, messageType, matcher: jsonMatcher); // Act var model = _sut.Map(matcher)!; @@ -1085,7 +1085,7 @@ message HelloReply { var matcher = (ProtoBufMatcher)_sut.Map(model)!; // Assert - matcher.ProtoDefinition.Should().Be(protoDefinition); + matcher.ProtoDefinition().Should().Be(protoDefinition); matcher.Name.Should().Be(nameof(ProtoBufMatcher)); matcher.MessageType.Should().Be(messageType); matcher.Matcher?.Value.Should().Be(jsonMatcherPattern);