diff --git a/src/WireMock.Net.Abstractions/Admin/Requests/LogRequestMatchModel.cs b/src/WireMock.Net.Abstractions/Admin/Requests/LogRequestMatchModel.cs index 6a379627..05212618 100644 --- a/src/WireMock.Net.Abstractions/Admin/Requests/LogRequestMatchModel.cs +++ b/src/WireMock.Net.Abstractions/Admin/Requests/LogRequestMatchModel.cs @@ -47,5 +47,5 @@ public class LogRequestMatchModel /// /// The match details. /// - public IList MatchDetails { get; set; } + public IList MatchDetails { get; set; } = []; } \ No newline at end of file diff --git a/src/WireMock.Net.Abstractions/Matchers/Request/MatchDetail.cs b/src/WireMock.Net.Abstractions/Matchers/Request/MatchDetail.cs index 6f38a330..7aa6e436 100644 --- a/src/WireMock.Net.Abstractions/Matchers/Request/MatchDetail.cs +++ b/src/WireMock.Net.Abstractions/Matchers/Request/MatchDetail.cs @@ -12,7 +12,7 @@ public class MatchDetail /// /// Gets or sets the type of the matcher. /// - public Type MatcherType { get; set; } = null!; + public required Type MatcherType { get; set; } /// /// Gets or sets the score between 0.0 and 1.0 diff --git a/src/WireMock.Net.GraphQL/Matchers/GraphQLMatcher.cs b/src/WireMock.Net.GraphQL/Matchers/GraphQLMatcher.cs index aaf830d1..6c4f2d5d 100644 --- a/src/WireMock.Net.GraphQL/Matchers/GraphQLMatcher.cs +++ b/src/WireMock.Net.GraphQL/Matchers/GraphQLMatcher.cs @@ -140,7 +140,7 @@ public class GraphQLMatcher : IGraphQLMatcher } } - return new MatchResult(MatchBehaviourHelper.Convert(MatchBehaviour, score), exception); + return MatchResult.From(Name, MatchBehaviourHelper.Convert(MatchBehaviour, score), exception); } /// diff --git a/src/WireMock.Net.Matchers.CSharpCode/Matchers/CSharpCodeMatcher.cs b/src/WireMock.Net.Matchers.CSharpCode/Matchers/CSharpCodeMatcher.cs index 7e557b24..eb7cd034 100644 --- a/src/WireMock.Net.Matchers.CSharpCode/Matchers/CSharpCodeMatcher.cs +++ b/src/WireMock.Net.Matchers.CSharpCode/Matchers/CSharpCodeMatcher.cs @@ -97,7 +97,7 @@ public class CSharpCodeMatcher : ICSharpCodeMatcher } } - return new MatchResult(MatchBehaviourHelper.Convert(MatchBehaviour, score), exception); + return MatchResult.From(Name, MatchBehaviourHelper.Convert(MatchBehaviour, score), exception); } private bool IsMatch(dynamic input, string pattern) diff --git a/src/WireMock.Net.MimePart/Matchers/MimePartMatcher.cs b/src/WireMock.Net.MimePart/Matchers/MimePartMatcher.cs index fe17cdd4..53f76b7c 100644 --- a/src/WireMock.Net.MimePart/Matchers/MimePartMatcher.cs +++ b/src/WireMock.Net.MimePart/Matchers/MimePartMatcher.cs @@ -1,6 +1,7 @@ // Copyright © WireMock.Net using System; +using System.Collections.Generic; using WireMock.Matchers.Helpers; using WireMock.Models.Mime; using WireMock.Util; @@ -12,7 +13,7 @@ namespace WireMock.Matchers; /// public class MimePartMatcher : IMimePartMatcher { - private readonly Func[] _funcs; + private readonly IList<(string Name, Func func)> _matcherFunctions; /// public string Name => nameof(MimePartMatcher); @@ -49,34 +50,47 @@ public class MimePartMatcher : IMimePartMatcher ContentTransferEncodingMatcher = contentTransferEncodingMatcher; ContentMatcher = contentMatcher; - _funcs = - [ - mp => ContentTypeMatcher?.IsMatch(GetContentTypeAsString(mp.ContentType)) ?? MatchScores.Perfect, - mp => ContentDispositionMatcher?.IsMatch(mp.ContentDisposition?.ToString()?.Replace("Content-Disposition: ", string.Empty)) ?? MatchScores.Perfect, - mp => ContentTransferEncodingMatcher?.IsMatch(mp.ContentTransferEncoding.ToLowerInvariant()) ?? MatchScores.Perfect, - MatchOnContent - ]; + _matcherFunctions = new List<(string Name, Func func)>(); + if (ContentTypeMatcher != null) + { + _matcherFunctions.Add((nameof(ContentTypeMatcher), mp => ContentTypeMatcher.IsMatch(GetContentTypeAsString(mp.ContentType)))); + } + + if (ContentDispositionMatcher != null) + { + _matcherFunctions.Add((nameof(ContentDispositionMatcher), mp => ContentDispositionMatcher.IsMatch(mp.ContentDisposition?.ToString()?.Replace("Content-Disposition: ", string.Empty)))); + } + + if (ContentTransferEncodingMatcher != null) + { + _matcherFunctions.Add((nameof(ContentTransferEncodingMatcher), mp => ContentTransferEncodingMatcher.IsMatch(mp.ContentTransferEncoding.ToLowerInvariant()))); + } + + if (ContentMatcher != null) + { + _matcherFunctions.Add((ContentMatcher.Name, MatchOnContent)); + } } /// public MatchResult IsMatch(IMimePartData value) { - var score = MatchScores.Mismatch; - Exception? exception = null; + var results = new List(); - try + foreach (var matcherFunction in _matcherFunctions) { - if (Array.TrueForAll(_funcs, func => func(value).IsPerfect())) + try { - score = MatchScores.Perfect; + var matchResult = matcherFunction.func(value); + results.Add(MatchResult.From(matcherFunction.Name, matchResult.Score)); + } + catch (Exception ex) + { + results.Add(MatchResult.From(matcherFunction.Name, MatchScores.Mismatch, ex)); } } - catch (Exception ex) - { - exception = ex; - } - return new MatchResult(MatchBehaviourHelper.Convert(MatchBehaviour, score), exception); + return MatchResult.From(nameof(MimePartMatcher), results, MatchOperator.And); } /// @@ -87,11 +101,6 @@ public class MimePartMatcher : IMimePartMatcher private MatchResult MatchOnContent(IMimePartData mimePart) { - if (ContentMatcher == null) - { - return MatchScores.Perfect; - } - var bodyParserSettings = new BodyParserSettings { Stream = mimePart.Open(), @@ -102,7 +111,7 @@ public class MimePartMatcher : IMimePartMatcher }; var bodyData = BodyParser.ParseAsync(bodyParserSettings).ConfigureAwait(false).GetAwaiter().GetResult(); - return BodyDataMatchScoreCalculator.CalculateMatchScore(bodyData, ContentMatcher); + return BodyDataMatchScoreCalculator.CalculateMatchScore(bodyData, ContentMatcher!); } private static string? GetContentTypeAsString(IContentTypeData? contentType) diff --git a/src/WireMock.Net.Minimal/Authentication/AzureADAuthenticationMatcher.cs b/src/WireMock.Net.Minimal/Authentication/AzureADAuthenticationMatcher.cs index 93d0fedd..65763ff8 100644 --- a/src/WireMock.Net.Minimal/Authentication/AzureADAuthenticationMatcher.cs +++ b/src/WireMock.Net.Minimal/Authentication/AzureADAuthenticationMatcher.cs @@ -54,7 +54,7 @@ internal class AzureADAuthenticationMatcher : IStringMatcher { if (string.IsNullOrEmpty(input)) { - return MatchScores.Mismatch; + return MatchResult.From(Name); } var token = Regex.Replace(input, BearerPrefix, string.Empty, RegexOptions.IgnoreCase, RegexConstants.DefaultTimeout); @@ -83,11 +83,11 @@ internal class AzureADAuthenticationMatcher : IStringMatcher // Throws an Exception as the token is invalid (expired, invalid-formatted, tenant mismatch, etc.) _jwtSecurityTokenHandler.ValidateToken(token, validationParameters, out _); - return MatchScores.Perfect; + return MatchResult.From(Name, MatchScores.Perfect); } catch (Exception ex) { - return new MatchResult(MatchScores.Mismatch, ex); + return MatchResult.From(Name, ex); } } diff --git a/src/WireMock.Net.Minimal/Matchers/ContentTypeMatcher.cs b/src/WireMock.Net.Minimal/Matchers/ContentTypeMatcher.cs index 88a56d0f..fdbc06e1 100644 --- a/src/WireMock.Net.Minimal/Matchers/ContentTypeMatcher.cs +++ b/src/WireMock.Net.Minimal/Matchers/ContentTypeMatcher.cs @@ -62,7 +62,7 @@ public class ContentTypeMatcher : WildcardMatcher { if (string.IsNullOrEmpty(input) || !MediaTypeHeaderValue.TryParse(input, out var contentType)) { - return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.Mismatch); + return MatchResult.From(Name, MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.Mismatch)); } return base.IsMatch(contentType.MediaType); diff --git a/src/WireMock.Net.Minimal/Matchers/ExactMatcher.cs b/src/WireMock.Net.Minimal/Matchers/ExactMatcher.cs index 67ce552b..5a745054 100644 --- a/src/WireMock.Net.Minimal/Matchers/ExactMatcher.cs +++ b/src/WireMock.Net.Minimal/Matchers/ExactMatcher.cs @@ -75,7 +75,7 @@ public class ExactMatcher : IStringMatcher, IIgnoreCaseMatcher : pattern => pattern == input; var score = MatchScores.ToScore(_values.Select(v => equals(v)).ToArray(), MatchOperator); - return new MatchResult(MatchBehaviourHelper.Convert(MatchBehaviour, score)); + return MatchResult.From(Name, MatchBehaviourHelper.Convert(MatchBehaviour, score)); } /// diff --git a/src/WireMock.Net.Minimal/Matchers/FormUrlEncodedMatcher.cs b/src/WireMock.Net.Minimal/Matchers/FormUrlEncodedMatcher.cs index 5dd81f5b..65f7ba20 100644 --- a/src/WireMock.Net.Minimal/Matchers/FormUrlEncodedMatcher.cs +++ b/src/WireMock.Net.Minimal/Matchers/FormUrlEncodedMatcher.cs @@ -106,18 +106,18 @@ public class FormUrlEncodedMatcher : IStringMatcher, IIgnoreCaseMatcher // Input is null or empty and if no patterns defined, return Perfect match. if (string.IsNullOrEmpty(input) && _patterns.Length == 0) { - return new MatchResult(MatchScores.Perfect); + return MatchResult.From(Name, MatchScores.Perfect); } if (!QueryStringParser.TryParse(input, IgnoreCase, out var inputNameValueCollection)) { - return new MatchResult(MatchScores.Mismatch); + return MatchResult.From(Name, MatchScores.Mismatch); } var matches = GetMatches(inputNameValueCollection); var score = MatchScores.ToScore(matches, MatchOperator); - return new MatchResult(MatchBehaviourHelper.Convert(MatchBehaviour, score)); + return MatchResult.From(Name, MatchBehaviourHelper.Convert(MatchBehaviour, score)); } private bool[] GetMatches(IDictionary inputNameValueCollection) diff --git a/src/WireMock.Net.Minimal/Matchers/JSONPathMatcher.cs b/src/WireMock.Net.Minimal/Matchers/JSONPathMatcher.cs index 517a4d7d..043e83c4 100644 --- a/src/WireMock.Net.Minimal/Matchers/JSONPathMatcher.cs +++ b/src/WireMock.Net.Minimal/Matchers/JSONPathMatcher.cs @@ -80,7 +80,7 @@ public class JsonPathMatcher : IStringMatcher, IObjectMatcher } } - return new MatchResult(MatchBehaviourHelper.Convert(MatchBehaviour, score), exception); + return MatchResult.From(Name, MatchBehaviourHelper.Convert(MatchBehaviour, score), exception); } /// @@ -104,7 +104,7 @@ public class JsonPathMatcher : IStringMatcher, IObjectMatcher } } - return new MatchResult(MatchBehaviourHelper.Convert(MatchBehaviour, score), exception); + return MatchResult.From(Name, MatchBehaviourHelper.Convert(MatchBehaviour, score), exception); } /// diff --git a/src/WireMock.Net.Minimal/Matchers/JmesPathMatcher.cs b/src/WireMock.Net.Minimal/Matchers/JmesPathMatcher.cs index 3e468448..1b6cfb7e 100644 --- a/src/WireMock.Net.Minimal/Matchers/JmesPathMatcher.cs +++ b/src/WireMock.Net.Minimal/Matchers/JmesPathMatcher.cs @@ -87,7 +87,7 @@ public class JmesPathMatcher : IStringMatcher, IObjectMatcher } } - return new MatchResult(MatchBehaviourHelper.Convert(MatchBehaviour, score), exception); + return MatchResult.From(Name, MatchBehaviourHelper.Convert(MatchBehaviour, score), exception); } /// @@ -102,7 +102,7 @@ public class JmesPathMatcher : IStringMatcher, IObjectMatcher return IsMatch(inputAsString); } - return MatchBehaviourHelper.Convert(MatchBehaviour, score); + return MatchResult.From(Name, MatchBehaviourHelper.Convert(MatchBehaviour, score)); } /// diff --git a/src/WireMock.Net.Minimal/Matchers/JsonMatcher.cs b/src/WireMock.Net.Minimal/Matchers/JsonMatcher.cs index 52ee6960..8d10f28b 100644 --- a/src/WireMock.Net.Minimal/Matchers/JsonMatcher.cs +++ b/src/WireMock.Net.Minimal/Matchers/JsonMatcher.cs @@ -96,7 +96,7 @@ public class JsonMatcher : IJsonMatcher } } - return new MatchResult(MatchBehaviourHelper.Convert(MatchBehaviour, score), error); + return MatchResult.From(Name, MatchBehaviourHelper.Convert(MatchBehaviour, score), error); } /// diff --git a/src/WireMock.Net.Minimal/Matchers/Request/RequestMatchResult.cs b/src/WireMock.Net.Minimal/Matchers/Request/RequestMatchResult.cs index 8342853f..8bccb94f 100644 --- a/src/WireMock.Net.Minimal/Matchers/Request/RequestMatchResult.cs +++ b/src/WireMock.Net.Minimal/Matchers/Request/RequestMatchResult.cs @@ -24,12 +24,17 @@ public class RequestMatchResult : IRequestMatchResult public double AverageTotalScore => TotalNumber == 0 ? MatchScores.Mismatch : TotalScore / TotalNumber; /// - public IList MatchDetails { get; } = new List(); + public IList MatchDetails { get; } = []; /// public double AddScore(Type matcherType, double score, Exception? exception) { - MatchDetails.Add(new MatchDetail { MatcherType = matcherType, Score = score, Exception = exception }); + MatchDetails.Add(new MatchDetail + { + MatcherType = matcherType, + Score = score, + Exception = exception + }); return score; } diff --git a/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageBodyMatcher.cs b/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageBodyMatcher.cs index e05ddf94..5f38189b 100644 --- a/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageBodyMatcher.cs +++ b/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageBodyMatcher.cs @@ -17,27 +17,27 @@ public class RequestMessageBodyMatcher : IRequestMatcher /// /// The body function /// - public Func? Func { get; } + public Func? MatchOnBodyAsStringFunc { get; } /// /// The body data function for byte[] /// - public Func? DataFunc { get; } + public Func? MatchOnBodyAsBytesFunc { get; } /// /// The body data function for json /// - public Func? JsonFunc { get; } + public Func? MatchOnBodyAsJsonFunc { get; } /// /// The body data function for BodyData /// - public Func? BodyDataFunc { get; } + public Func? MatchOnBodyAsBodyDataFunc { get; } /// /// The body data function for FormUrlEncoded /// - public Func?, bool>? FormUrlEncodedFunc { get; } + public Func?, bool>? MatchOnBodyAsFormUrlEncodedFunc { get; } /// /// The matchers. @@ -85,7 +85,7 @@ public class RequestMessageBodyMatcher : IRequestMatcher /// The function. public RequestMessageBodyMatcher(Func func) { - Func = Guard.NotNull(func); + MatchOnBodyAsStringFunc = Guard.NotNull(func); } /// @@ -94,7 +94,7 @@ public class RequestMessageBodyMatcher : IRequestMatcher /// The function. public RequestMessageBodyMatcher(Func func) { - DataFunc = Guard.NotNull(func); + MatchOnBodyAsBytesFunc = Guard.NotNull(func); } /// @@ -103,7 +103,7 @@ public class RequestMessageBodyMatcher : IRequestMatcher /// The function. public RequestMessageBodyMatcher(Func func) { - JsonFunc = Guard.NotNull(func); + MatchOnBodyAsJsonFunc = Guard.NotNull(func); } /// @@ -112,7 +112,7 @@ public class RequestMessageBodyMatcher : IRequestMatcher /// The function. public RequestMessageBodyMatcher(Func func) { - BodyDataFunc = Guard.NotNull(func); + MatchOnBodyAsBodyDataFunc = Guard.NotNull(func); } /// @@ -121,7 +121,7 @@ public class RequestMessageBodyMatcher : IRequestMatcher /// The function. public RequestMessageBodyMatcher(Func?, bool> func) { - FormUrlEncodedFunc = Guard.NotNull(func); + MatchOnBodyAsFormUrlEncodedFunc = Guard.NotNull(func); } /// @@ -147,43 +147,43 @@ public class RequestMessageBodyMatcher : IRequestMatcher /// public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult) { - var (score, exception) = CalculateMatchScore(requestMessage).Expand(); + var (score, exception) = CalculateMatchResult(requestMessage).Expand(); return requestMatchResult.AddScore(GetType(), score, exception); } - private MatchResult CalculateMatchScore(IRequestMessage requestMessage) + private MatchResult CalculateMatchResult(IRequestMessage requestMessage) { if (Matchers != null && Matchers.Any()) { var results = Matchers.Select(matcher => BodyDataMatchScoreCalculator.CalculateMatchScore(requestMessage.BodyData, matcher)).ToArray(); - return MatchResult.From(results, MatchOperator); + return MatchResult.From(nameof(RequestMessageBodyMatcher), results, MatchOperator); } - if (Func != null) + if (MatchOnBodyAsStringFunc != null) { - return MatchScores.ToScore(Func(requestMessage.BodyData?.BodyAsString)); + return MatchResult.From($"{nameof(RequestMessageBodyMatcher)}:{nameof(MatchOnBodyAsStringFunc)}", MatchScores.ToScore(MatchOnBodyAsStringFunc(requestMessage.BodyData?.BodyAsString))); } - if (FormUrlEncodedFunc != null) + if (MatchOnBodyAsFormUrlEncodedFunc != null) { - return MatchScores.ToScore(FormUrlEncodedFunc(requestMessage.BodyData?.BodyAsFormUrlEncoded)); + return MatchResult.From($"{nameof(RequestMessageBodyMatcher)}:{nameof(MatchOnBodyAsFormUrlEncodedFunc)}", MatchScores.ToScore(MatchOnBodyAsFormUrlEncodedFunc(requestMessage.BodyData?.BodyAsFormUrlEncoded))); } - if (JsonFunc != null) + if (MatchOnBodyAsJsonFunc != null) { - return MatchScores.ToScore(JsonFunc(requestMessage.BodyData?.BodyAsJson)); + return MatchResult.From($"{nameof(RequestMessageBodyMatcher)}:{nameof(MatchOnBodyAsJsonFunc)}", MatchScores.ToScore(MatchOnBodyAsJsonFunc(requestMessage.BodyData?.BodyAsJson))); } - if (DataFunc != null) + if (MatchOnBodyAsBytesFunc != null) { - return MatchScores.ToScore(DataFunc(requestMessage.BodyData?.BodyAsBytes)); + return MatchResult.From($"{nameof(RequestMessageBodyMatcher)}:{nameof(MatchOnBodyAsBytesFunc)}", MatchScores.ToScore(MatchOnBodyAsBytesFunc(requestMessage.BodyData?.BodyAsBytes))); } - if (BodyDataFunc != null) + if (MatchOnBodyAsBodyDataFunc != null) { - return MatchScores.ToScore(BodyDataFunc(requestMessage.BodyData)); + return MatchResult.From($"{nameof(RequestMessageBodyMatcher)}:{nameof(MatchOnBodyAsBodyDataFunc)}", MatchScores.ToScore(MatchOnBodyAsBodyDataFunc(requestMessage.BodyData))); } - return default; + return MatchResult.From(nameof(RequestMessageBodyMatcher)); } } \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageBodyMatcher`1.cs b/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageBodyMatcher`1.cs index 2ee8a4ab..0a85821f 100644 --- a/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageBodyMatcher`1.cs +++ b/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageBodyMatcher`1.cs @@ -11,6 +11,8 @@ namespace WireMock.Matchers.Request; /// public class RequestMessageBodyMatcher : IRequestMatcher { + private const string _name = nameof(RequestMessageBodyMatcher); + /// /// The body data function for type T /// @@ -46,15 +48,15 @@ public class RequestMessageBodyMatcher : IRequestMatcher try { var bodyAsT = jsonObject.ToObject(); - return MatchScores.ToScore(Func(bodyAsT)); + return MatchResult.From(_name, MatchScores.ToScore(Func(bodyAsT))); } catch (Exception ex) { - return new MatchResult(ex); + return MatchResult.From(_name, ex); } } } - return default; + return MatchResult.From(_name); } } \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageClientIPMatcher.cs b/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageClientIPMatcher.cs index 78d531fa..aaa0c3d1 100644 --- a/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageClientIPMatcher.cs +++ b/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageClientIPMatcher.cs @@ -14,6 +14,8 @@ namespace WireMock.Matchers.Request; /// public class RequestMessageClientIPMatcher : IRequestMatcher { + private const string _name = nameof(RequestMessageClientIPMatcher); + /// /// The matchers /// @@ -86,15 +88,15 @@ public class RequestMessageClientIPMatcher : IRequestMatcher if (Matchers != null) { var results = Matchers.Select(m => m.IsMatch(requestMessage.ClientIP)).ToArray(); - return MatchResult.From(results, MatchOperator); + return MatchResult.From(_name, results, MatchOperator); } if (Funcs != null) { var results = Funcs.Select(func => func(requestMessage.ClientIP)).ToArray(); - return MatchScores.ToScore(results, MatchOperator); + return MatchResult.From(_name, MatchScores.ToScore(results, MatchOperator)); } - return default; + return MatchResult.From(_name); } } \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageCookieMatcher.cs b/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageCookieMatcher.cs index 9613d48d..af985d83 100644 --- a/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageCookieMatcher.cs +++ b/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageCookieMatcher.cs @@ -13,6 +13,8 @@ namespace WireMock.Matchers.Request; /// public class RequestMessageCookieMatcher : IRequestMatcher { + private const string _name = nameof(RequestMessageCookieMatcher); + /// /// MatchBehaviour /// @@ -104,7 +106,7 @@ public class RequestMessageCookieMatcher : IRequestMatcher { if (requestMessage.Cookies == null) { - return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.Mismatch); + return MatchResult.From(_name, MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.Mismatch)); } // Check if we want to use IgnoreCase to compare the Cookie-Name and Cookie-Value @@ -112,19 +114,19 @@ public class RequestMessageCookieMatcher : IRequestMatcher if (Funcs != null) { - return MatchScores.ToScore(Funcs.Any(f => f(cookies))); + return MatchResult.From(_name, MatchScores.ToScore(Funcs.Any(f => f(cookies)))); } if (Matchers == null) { - return default; + return MatchResult.From(_name); } if (!cookies.ContainsKey(Name)) { - return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.Mismatch); + return MatchResult.From(_name, MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.Mismatch)); } - return Matchers.Max(m => m.IsMatch(cookies[Name])); + return MatchResult.From(_name, Matchers.Max(m => m.IsMatch(cookies[Name]))?.Score ?? MatchScores.Mismatch); } } \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageHeaderMatcher.cs b/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageHeaderMatcher.cs index 77a5f04c..53d54dc5 100644 --- a/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageHeaderMatcher.cs +++ b/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageHeaderMatcher.cs @@ -14,6 +14,8 @@ namespace WireMock.Matchers.Request; /// public class RequestMessageHeaderMatcher : IRequestMatcher { + private const string _name = nameof(RequestMessageCookieMatcher); + /// /// MatchBehaviour /// @@ -117,7 +119,7 @@ public class RequestMessageHeaderMatcher : IRequestMatcher { if (requestMessage.Headers == null) { - return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.Mismatch); + return MatchResult.From(_name, MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.Mismatch)); } // Check if we want to use IgnoreCase to compare the Header-Name and Header-Value(s) @@ -126,14 +128,14 @@ public class RequestMessageHeaderMatcher : IRequestMatcher if (Funcs != null) { var funcResults = Funcs.Select(f => f(headers.ToDictionary(entry => entry.Key, entry => entry.Value.ToArray()))).ToArray(); - return MatchScores.ToScore(funcResults, MatchOperator); + return MatchResult.From(_name, MatchScores.ToScore(funcResults, MatchOperator)); } if (Matchers != null) { if (!headers.ContainsKey(Name)) { - return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.Mismatch); + return MatchResult.From(_name, MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.Mismatch)); } var results = new List(); @@ -141,12 +143,12 @@ public class RequestMessageHeaderMatcher : IRequestMatcher { var resultsPerMatcher = headers[Name].Select(matcher.IsMatch).ToArray(); - results.Add(MatchResult.From(resultsPerMatcher, MatchOperator.And)); + results.Add(MatchResult.From(_name, resultsPerMatcher, MatchOperator.And)); } - return MatchResult.From(results, MatchOperator); + return MatchResult.From(_name, results, MatchOperator); } - return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.Mismatch); + return MatchResult.From(_name, MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.Mismatch)); } } \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageHttpVersionMatcher.cs b/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageHttpVersionMatcher.cs index 2de8c924..6125806d 100644 --- a/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageHttpVersionMatcher.cs +++ b/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageHttpVersionMatcher.cs @@ -11,6 +11,8 @@ namespace WireMock.Matchers.Request; /// public class RequestMessageHttpVersionMatcher : IRequestMatcher { + private const string _name = nameof(RequestMessageHttpVersionMatcher); + /// /// The matcher. /// @@ -19,7 +21,7 @@ public class RequestMessageHttpVersionMatcher : IRequestMatcher /// /// The func. /// - public Func? Func { get; } + public Func? MatcherOnStringFunc { get; } /// /// The @@ -61,7 +63,7 @@ public class RequestMessageHttpVersionMatcher : IRequestMatcher /// The function. public RequestMessageHttpVersionMatcher(Func func) { - Func = Guard.NotNull(func); + MatcherOnStringFunc = Guard.NotNull(func); } /// @@ -78,11 +80,11 @@ public class RequestMessageHttpVersionMatcher : IRequestMatcher return Matcher.IsMatch(requestMessage.HttpVersion); } - if (Func != null) + if (MatcherOnStringFunc != null) { - return MatchScores.ToScore(Func(requestMessage.HttpVersion)); + return MatchResult.From($"{_name}:{nameof(MatcherOnStringFunc)}", MatchScores.ToScore(MatcherOnStringFunc(requestMessage.HttpVersion))); } - return default; + return MatchResult.From(_name); } } \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageMultiPartMatcher.cs b/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageMultiPartMatcher.cs index e5e9de7e..5d32ebf0 100644 --- a/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageMultiPartMatcher.cs +++ b/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageMultiPartMatcher.cs @@ -62,7 +62,7 @@ public class RequestMessageMultiPartMatcher : IRequestMatcher var score = MatchScores.Mismatch; Exception? exception = null; - if (Matchers?.Any() != true) + if (Matchers == null) { return requestMatchResult.AddScore(GetType(), score, null); } diff --git a/src/WireMock.Net.Minimal/Matchers/Request/RequestMessagePathMatcher.cs b/src/WireMock.Net.Minimal/Matchers/Request/RequestMessagePathMatcher.cs index 3cf4c037..2d247b85 100644 --- a/src/WireMock.Net.Minimal/Matchers/Request/RequestMessagePathMatcher.cs +++ b/src/WireMock.Net.Minimal/Matchers/Request/RequestMessagePathMatcher.cs @@ -86,15 +86,16 @@ public class RequestMessagePathMatcher : IRequestMatcher if (Matchers != null) { var results = Matchers.Select(m => m.IsMatch(requestMessage.Path)).ToArray(); - return MatchResult.From(results, MatchOperator); + return MatchResult.From(nameof(RequestMessagePathMatcher), results, MatchOperator); } if (Funcs != null) { var results = Funcs.Select(func => func(requestMessage.Path)).ToArray(); - return MatchScores.ToScore(results, MatchOperator); + var score = MatchScores.ToScore(results, MatchOperator); + return MatchResult.From(nameof(RequestMessagePathMatcher), score); } - return default; + return MatchResult.From(nameof(RequestMessagePathMatcher)); } } \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageUrlMatcher.cs b/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageUrlMatcher.cs index 810afb59..9e3986c9 100644 --- a/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageUrlMatcher.cs +++ b/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageUrlMatcher.cs @@ -86,15 +86,16 @@ public class RequestMessageUrlMatcher : IRequestMatcher if (Matchers != null) { var results = Matchers.Select(m => m.IsMatch(requestMessage.Url)).ToArray(); - return MatchResult.From(results, MatchOperator); + return MatchResult.From(nameof(RequestMessageUrlMatcher), results, MatchOperator); } if (Funcs != null) { var results = Funcs.Select(func => func(requestMessage.Url)).ToArray(); - return MatchScores.ToScore(results, MatchOperator); + var score = MatchScores.ToScore(results, MatchOperator); + return MatchResult.From(nameof(RequestMessageUrlMatcher), score); } - return default; + return MatchResult.From(nameof(RequestMessageUrlMatcher)); } } \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/Matchers/SimMetricsMatcher.cs b/src/WireMock.Net.Minimal/Matchers/SimMetricsMatcher.cs index eb730b43..1c2ab1d9 100644 --- a/src/WireMock.Net.Minimal/Matchers/SimMetricsMatcher.cs +++ b/src/WireMock.Net.Minimal/Matchers/SimMetricsMatcher.cs @@ -86,7 +86,7 @@ public class SimMetricsMatcher : IStringMatcher IStringMetric stringMetricType = GetStringMetricType(); var score = MatchScores.ToScore(_patterns.Select(p => stringMetricType.GetSimilarity(p.GetPattern(), input)).ToArray(), MatchOperator); - return MatchBehaviourHelper.Convert(MatchBehaviour, score); + return MatchResult.From(Name, MatchBehaviourHelper.Convert(MatchBehaviour, score)); } /// diff --git a/src/WireMock.Net.Minimal/Matchers/XPathMatcher.cs b/src/WireMock.Net.Minimal/Matchers/XPathMatcher.cs index 7504ce31..0065fe24 100644 --- a/src/WireMock.Net.Minimal/Matchers/XPathMatcher.cs +++ b/src/WireMock.Net.Minimal/Matchers/XPathMatcher.cs @@ -116,7 +116,7 @@ public class XPathMatcher : IStringMatcher private MatchResult CreateMatchResult(double score, Exception? exception = null) { - return new MatchResult(MatchBehaviourHelper.Convert(MatchBehaviour, score), exception); + return MatchResult.From(Name, MatchBehaviourHelper.Convert(MatchBehaviour, score), exception); } private sealed class XPathEvaluator diff --git a/src/WireMock.Net.ProtoBuf/Matchers/ProtoBufMatcher.cs b/src/WireMock.Net.ProtoBuf/Matchers/ProtoBufMatcher.cs index 7f79d896..14e4c35a 100644 --- a/src/WireMock.Net.ProtoBuf/Matchers/ProtoBufMatcher.cs +++ b/src/WireMock.Net.ProtoBuf/Matchers/ProtoBufMatcher.cs @@ -57,7 +57,7 @@ public class ProtoBufMatcher : IProtoBufMatcher /// public async Task IsMatchAsync(byte[]? input, CancellationToken cancellationToken = default) { - var result = new MatchResult(); + var result = MatchResult.From(Name); if (input != null) { @@ -65,11 +65,11 @@ public class ProtoBufMatcher : IProtoBufMatcher { var instance = await DecodeAsync(input, true, cancellationToken).ConfigureAwait(false); - result = Matcher?.IsMatch(instance) ?? new MatchResult(MatchScores.Perfect); + result = Matcher?.IsMatch(instance) ?? MatchResult.From(Name, MatchScores.Perfect); } - catch (Exception e) + catch (Exception ex) { - result = new MatchResult(MatchScores.Mismatch, e); + result = MatchResult.From(Name, ex); } } diff --git a/src/WireMock.Net.Shared/Matchers/ExactObjectMatcher.cs b/src/WireMock.Net.Shared/Matchers/ExactObjectMatcher.cs index f37590b1..51641bdb 100644 --- a/src/WireMock.Net.Shared/Matchers/ExactObjectMatcher.cs +++ b/src/WireMock.Net.Shared/Matchers/ExactObjectMatcher.cs @@ -68,7 +68,7 @@ public class ExactObjectMatcher : IObjectMatcher equals = Equals(Value, input); } - return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(equals)); + return MatchResult.From(Name, MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(equals))); } /// diff --git a/src/WireMock.Net.Shared/Matchers/Helpers/BodyDataMatchScoreCalculator.cs b/src/WireMock.Net.Shared/Matchers/Helpers/BodyDataMatchScoreCalculator.cs index 1dcbd265..41b2876e 100644 --- a/src/WireMock.Net.Shared/Matchers/Helpers/BodyDataMatchScoreCalculator.cs +++ b/src/WireMock.Net.Shared/Matchers/Helpers/BodyDataMatchScoreCalculator.cs @@ -8,13 +8,15 @@ namespace WireMock.Matchers.Helpers; internal static class BodyDataMatchScoreCalculator { + private static string _name = nameof(BodyDataMatchScoreCalculator); + internal static MatchResult CalculateMatchScore(IBodyData? bodyData, IMatcher matcher) { Guard.NotNull(matcher); if (bodyData == null) { - return default; + return MatchResult.From(_name); } if (matcher is NotNullOrEmptyMatcher notNullOrEmptyMatcher) @@ -30,7 +32,7 @@ internal static class BodyDataMatchScoreCalculator return notNullOrEmptyMatcher.IsMatch(bodyData.BodyAsBytes); default: - return default; + return MatchResult.From(_name); } } @@ -68,6 +70,6 @@ internal static class BodyDataMatchScoreCalculator return protoBufMatcher.IsMatchAsync(bodyData.BodyAsBytes).GetAwaiter().GetResult(); } - return default; + return MatchResult.From(_name); } } \ No newline at end of file diff --git a/src/WireMock.Net.Shared/Matchers/MatchBehaviourHelper.cs b/src/WireMock.Net.Shared/Matchers/MatchBehaviourHelper.cs index a9450282..53d9b500 100644 --- a/src/WireMock.Net.Shared/Matchers/MatchBehaviourHelper.cs +++ b/src/WireMock.Net.Shared/Matchers/MatchBehaviourHelper.cs @@ -36,6 +36,6 @@ internal static class MatchBehaviourHelper /// match result internal static MatchResult Convert(MatchBehaviour matchBehaviour, MatchResult result) { - return matchBehaviour == MatchBehaviour.AcceptOnMatch ? result : new MatchResult(Convert(matchBehaviour, result.Score), result.Exception); + return matchBehaviour == MatchBehaviour.AcceptOnMatch ? result : MatchResult.From(result.Name, Convert(matchBehaviour, result.Score), result.Exception); } } \ No newline at end of file diff --git a/src/WireMock.Net.Shared/Matchers/MatchResult.cs b/src/WireMock.Net.Shared/Matchers/MatchResult.cs index 5419c1ab..ce79ec5a 100644 --- a/src/WireMock.Net.Shared/Matchers/MatchResult.cs +++ b/src/WireMock.Net.Shared/Matchers/MatchResult.cs @@ -11,7 +11,7 @@ namespace WireMock.Matchers; /// /// The MatchResult which contains the score (value between 0.0 - 1.0 of the similarity) and an optional error message. /// -public struct MatchResult +public class MatchResult { /// /// A value between 0.0 - 1.0 of the similarity. @@ -25,46 +25,55 @@ public struct MatchResult public Exception? Exception { get; set; } /// - /// Create a MatchResult + /// The name or description of the matcher. /// - /// A value between 0.0 - 1.0 of the similarity. - /// The exception in case the matching fails. [Optional] - public MatchResult(double score, Exception? exception = null) - { - Score = score; - Exception = exception; - } + public required string Name { get; set; } /// - /// Create a MatchResult + /// The sub MatchResults in case of multiple matchers. /// - /// The exception in case the matching fails. - public MatchResult(Exception exception) - { - Exception = Guard.NotNull(exception); - } - - /// - /// Implicitly converts a double to a MatchResult. - /// - /// The score - public static implicit operator MatchResult(double score) - { - return new MatchResult(score); - } + public MatchResult[]? MatchResults { get; set; } /// /// Is the value a perfect match? /// public bool IsPerfect() => MatchScores.IsPerfect(Score); + /// + /// Create a MatchResult. + /// + /// The name or description of the matcher. + /// A value between 0.0 - 1.0 of the similarity. + /// The exception in case the matching fails. [Optional] + public static MatchResult From(string name, double score = 0, Exception? exception = null) + { + return new MatchResult + { + Name = name, + Score = score, + Exception = exception + }; + } + + /// + /// Create a MatchResult from exception. + /// + /// The name or description of the matcher. + /// The exception in case the matching fails. + /// MatchResult + public static MatchResult From(string name, Exception exception) + { + return From(name, MatchScores.Mismatch, exception); + } + /// /// Create a MatchResult from multiple MatchResults. /// + /// The name or description of the matcher. /// A list of MatchResults. /// The MatchOperator /// MatchResult - public static MatchResult From(IReadOnlyList matchResults, MatchOperator matchOperator) + public static MatchResult From(string name, IReadOnlyList matchResults, MatchOperator matchOperator) { Guard.NotNullOrEmpty(matchResults); @@ -75,6 +84,8 @@ public struct MatchResult return new MatchResult { + Name = name, + MatchResults = matchResults.ToArray(), Score = MatchScores.ToScore(matchResults.Select(r => r.Score).ToArray(), matchOperator), Exception = matchResults.Select(m => m.Exception).OfType().ToArray().ToException() }; diff --git a/src/WireMock.Net.Shared/Matchers/NotNullOrEmptyMatcher.cs b/src/WireMock.Net.Shared/Matchers/NotNullOrEmptyMatcher.cs index 51f89adf..666ab3e2 100644 --- a/src/WireMock.Net.Shared/Matchers/NotNullOrEmptyMatcher.cs +++ b/src/WireMock.Net.Shared/Matchers/NotNullOrEmptyMatcher.cs @@ -1,6 +1,5 @@ // Copyright © WireMock.Net -using System; using System.Linq; using AnyOfTypes; using WireMock.Extensions; @@ -53,7 +52,7 @@ public class NotNullOrEmptyMatcher : IObjectMatcher, IStringMatcher break; } - return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(match)); + return MatchResult.From(Name, MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(match))); } /// @@ -61,7 +60,7 @@ public class NotNullOrEmptyMatcher : IObjectMatcher, IStringMatcher { var match = !string.IsNullOrEmpty(input); - return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(match)); + return MatchResult.From(Name, MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(match))); } /// diff --git a/src/WireMock.Net.Shared/Matchers/RegexMatcher.cs b/src/WireMock.Net.Shared/Matchers/RegexMatcher.cs index fab9edf6..bf00622d 100644 --- a/src/WireMock.Net.Shared/Matchers/RegexMatcher.cs +++ b/src/WireMock.Net.Shared/Matchers/RegexMatcher.cs @@ -111,7 +111,7 @@ public class RegexMatcher : IStringMatcher, IIgnoreCaseMatcher } } - return new MatchResult(MatchBehaviourHelper.Convert(MatchBehaviour, score), exception); + return MatchResult.From(Name, MatchBehaviourHelper.Convert(MatchBehaviour, score), exception); } /// diff --git a/src/WireMock.Net.Shared/Matchers/Request/RequestMessageGraphQLMatcher.cs b/src/WireMock.Net.Shared/Matchers/Request/RequestMessageGraphQLMatcher.cs index 313be8e5..960ad662 100644 --- a/src/WireMock.Net.Shared/Matchers/Request/RequestMessageGraphQLMatcher.cs +++ b/src/WireMock.Net.Shared/Matchers/Request/RequestMessageGraphQLMatcher.cs @@ -73,7 +73,7 @@ public class RequestMessageGraphQLMatcher : IRequestMatcher public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult) { var results = CalculateMatchResults(requestMessage); - var (score, exception) = MatchResult.From(results, MatchOperator).Expand(); + var (score, exception) = MatchResult.From(nameof(RequestMessageGraphQLMatcher), results, MatchOperator).Expand(); return requestMatchResult.AddScore(GetType(), score, exception); } @@ -86,12 +86,12 @@ public class RequestMessageGraphQLMatcher : IRequestMatcher return stringMatcher.IsMatch(requestMessage.BodyData.BodyAsString); } - return default; + return MatchResult.From(nameof(RequestMessageGraphQLMatcher)); } private IReadOnlyList CalculateMatchResults(IRequestMessage requestMessage) { - return Matchers == null ? [new MatchResult()] : Matchers.Select(matcher => CalculateMatchResult(requestMessage, matcher)).ToArray(); + return Matchers == null ? [MatchResult.From(nameof(RequestMessageGraphQLMatcher))] : Matchers.Select(matcher => CalculateMatchResult(requestMessage, matcher)).ToArray(); } private static IMatcher[] CreateMatcherArray( diff --git a/test/WireMock.Net.Tests.UsingNuGet/WireMock.Net.Tests.UsingNuGet.csproj b/test/WireMock.Net.Tests.UsingNuGet/WireMock.Net.Tests.UsingNuGet.csproj index 4facbda3..52ac5745 100644 --- a/test/WireMock.Net.Tests.UsingNuGet/WireMock.Net.Tests.UsingNuGet.csproj +++ b/test/WireMock.Net.Tests.UsingNuGet/WireMock.Net.Tests.UsingNuGet.csproj @@ -20,7 +20,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive - + all diff --git a/test/WireMock.Net.Tests.UsingNuGet/WireMockServerTests.WithMultiPart.cs b/test/WireMock.Net.Tests.UsingNuGet/WireMockServerTests.WithMultiPart.cs index 4237df8e..57c9b0f8 100644 --- a/test/WireMock.Net.Tests.UsingNuGet/WireMockServerTests.WithMultiPart.cs +++ b/test/WireMock.Net.Tests.UsingNuGet/WireMockServerTests.WithMultiPart.cs @@ -27,7 +27,7 @@ public partial class WireMockServerTests var textPlainMatcher = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, textPlainContentTypeMatcher, null, null, textPlainContentMatcher); var textJson = "{ \"Key\" : \"Value\" }"; - var textJsonContentType = "text/json"; + var textJsonContentType = "applicatiom/json"; var textJsonContentTypeMatcher = new ContentTypeMatcher(textJsonContentType); var textJsonContentMatcher = new JsonMatcher(new { Key = "Value" }, true); var jsonMatcher = new MimePartMatcher(MatchBehaviour.AcceptOnMatch, textJsonContentTypeMatcher, null, null, textJsonContentMatcher); diff --git a/test/WireMock.Net.Tests/Matchers/RequestMatchResultTests.cs b/test/WireMock.Net.Tests/Matchers/RequestMatchResultTests.cs index ad2af984..b033bdc5 100644 --- a/test/WireMock.Net.Tests/Matchers/RequestMatchResultTests.cs +++ b/test/WireMock.Net.Tests/Matchers/RequestMatchResultTests.cs @@ -15,12 +15,12 @@ public class RequestMatchResultTests { // Arrange var result1 = new RequestMatchResult(); - result1.AddScore(typeof(WildcardMatcher), 1, null); - result1.AddScore(typeof(WildcardMatcher), 0.9, null); + result1.AddMatchResult(typeof(WildcardMatcher), 1, null); + result1.AddMatchResult(typeof(WildcardMatcher), 0.9, null); var result2 = new RequestMatchResult(); - result2.AddScore(typeof(JsonMatcher), 1, null); - result2.AddScore(typeof(JsonMatcher), 1, null); + result2.AddMatchResult(typeof(JsonMatcher), 1, null); + result2.AddMatchResult(typeof(JsonMatcher), 1, null); var results = new[] { result1, result2 }; @@ -36,13 +36,13 @@ public class RequestMatchResultTests { // Arrange var result1 = new RequestMatchResult(); - result1.AddScore(typeof(WildcardMatcher), 1, null); - result1.AddScore(typeof(WildcardMatcher), 1, null); + result1.AddMatchResult(typeof(WildcardMatcher), 1, null); + result1.AddMatchResult(typeof(WildcardMatcher), 1, null); var result2 = new RequestMatchResult(); - result2.AddScore(typeof(JsonMatcher), 1, null); - result2.AddScore(typeof(JsonMatcher), 1, null); - result2.AddScore(typeof(JsonMatcher), 1, null); + result2.AddMatchResult(typeof(JsonMatcher), 1, null); + result2.AddMatchResult(typeof(JsonMatcher), 1, null); + result2.AddMatchResult(typeof(JsonMatcher), 1, null); var results = new[] { result1, result2 }; diff --git a/test/WireMock.Net.Tests/Owin/MappingMatcherTests.cs b/test/WireMock.Net.Tests/Owin/MappingMatcherTests.cs index e81bce1e..5c0111a6 100644 --- a/test/WireMock.Net.Tests/Owin/MappingMatcherTests.cs +++ b/test/WireMock.Net.Tests/Owin/MappingMatcherTests.cs @@ -243,7 +243,7 @@ public class MappingMatcherTests var requestMatchResult = new RequestMatchResult(); foreach (var score in match.scores) { - requestMatchResult.AddScore(typeof(object), score, null); + requestMatchResult.AddMatchResult(typeof(object), score, null); } mappingMock.SetupGet(m => m.Probability).Returns(match.probability); diff --git a/test/WireMock.Net.Tests/RequestMatchers/RequestMessageBodyMatcherTests.cs b/test/WireMock.Net.Tests/RequestMatchers/RequestMessageBodyMatcherTests.cs index ab53577a..27934771 100644 --- a/test/WireMock.Net.Tests/RequestMatchers/RequestMessageBodyMatcherTests.cs +++ b/test/WireMock.Net.Tests/RequestMatchers/RequestMessageBodyMatcherTests.cs @@ -29,7 +29,7 @@ public class RequestMessageBodyMatcherTests DetectedBodyType = BodyType.String }; var stringMatcherMock = new Mock(); - stringMatcherMock.Setup(m => m.IsMatch(It.IsAny())).Returns(1d); + stringMatcherMock.Setup(m => m.IsMatch(It.IsAny())).Returns(MatchResult.From(nameof(IStringMatcher), 1d)); var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body); @@ -61,10 +61,10 @@ public class RequestMessageBodyMatcherTests DetectedBodyType = BodyType.String }; var stringMatcherMock1 = new Mock(); - stringMatcherMock1.Setup(m => m.IsMatch(It.IsAny())).Returns(one); + stringMatcherMock1.Setup(m => m.IsMatch(It.IsAny())).Returns(MatchResult.From(nameof(IStringMatcher), one)); var stringMatcherMock2 = new Mock(); - stringMatcherMock2.Setup(m => m.IsMatch(It.IsAny())).Returns(two); + stringMatcherMock2.Setup(m => m.IsMatch(It.IsAny())).Returns(MatchResult.From(nameof(IStringMatcher), two)); var matchers = new[] { stringMatcherMock1.Object, stringMatcherMock2.Object }; @@ -102,10 +102,10 @@ public class RequestMessageBodyMatcherTests DetectedBodyType = BodyType.String }; var stringMatcherMock1 = new Mock(); - stringMatcherMock1.Setup(m => m.IsMatch(It.IsAny())).Returns(one); + stringMatcherMock1.Setup(m => m.IsMatch(It.IsAny())).Returns(MatchResult.From(nameof(IStringMatcher), one)); var stringMatcherMock2 = new Mock(); - stringMatcherMock2.Setup(m => m.IsMatch(It.IsAny())).Returns(two); + stringMatcherMock2.Setup(m => m.IsMatch(It.IsAny())).Returns(MatchResult.From(nameof(IStringMatcher), two)); var matchers = new[] { stringMatcherMock1.Object, stringMatcherMock2.Object }; @@ -143,10 +143,10 @@ public class RequestMessageBodyMatcherTests DetectedBodyType = BodyType.String }; var stringMatcherMock1 = new Mock(); - stringMatcherMock1.Setup(m => m.IsMatch(It.IsAny())).Returns(one); + stringMatcherMock1.Setup(m => m.IsMatch(It.IsAny())).Returns(MatchResult.From(nameof(IStringMatcher), one)); var stringMatcherMock2 = new Mock(); - stringMatcherMock2.Setup(m => m.IsMatch(It.IsAny())).Returns(two); + stringMatcherMock2.Setup(m => m.IsMatch(It.IsAny())).Returns(MatchResult.From(nameof(IStringMatcher), two)); var matchers = new[] { stringMatcherMock1.Object, stringMatcherMock2.Object }; @@ -175,11 +175,11 @@ public class RequestMessageBodyMatcherTests // Assign var body = new BodyData { - BodyAsBytes = new byte[] { 1 }, + BodyAsBytes = [1], DetectedBodyType = BodyType.Bytes }; var stringMatcherMock = new Mock(); - stringMatcherMock.Setup(m => m.IsMatch(It.IsAny())).Returns(0.5d); + stringMatcherMock.Setup(m => m.IsMatch(It.IsAny())).Returns(MatchResult.From(nameof(IStringMatcher), 0.5d)); var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body); @@ -207,7 +207,7 @@ public class RequestMessageBodyMatcherTests DetectedBodyType = BodyType.Json }; var stringMatcherMock = new Mock(); - stringMatcherMock.Setup(m => m.IsMatch(It.IsAny())).Returns(1.0d); + stringMatcherMock.Setup(m => m.IsMatch(It.IsAny())).Returns(MatchResult.From(nameof(IStringMatcher), 1.0d)); var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body); @@ -235,7 +235,7 @@ public class RequestMessageBodyMatcherTests DetectedBodyType = BodyType.Json }; var stringMatcherMock = new Mock(); - stringMatcherMock.Setup(m => m.IsMatch(It.IsAny())).Returns(1d); + stringMatcherMock.Setup(m => m.IsMatch(It.IsAny())).Returns(MatchResult.From(nameof(IStringMatcher), 1d)); stringMatcherMock.SetupGet(m => m.MatchOperator).Returns(MatchOperator.Or); var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body); @@ -263,7 +263,7 @@ public class RequestMessageBodyMatcherTests DetectedBodyType = BodyType.Json }; var objectMatcherMock = new Mock(); - objectMatcherMock.Setup(m => m.IsMatch(It.IsAny())).Returns(1d); + objectMatcherMock.Setup(m => m.IsMatch(It.IsAny())).Returns(MatchResult.From(nameof(IStringMatcher), 1d)); var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body); @@ -387,7 +387,7 @@ public class RequestMessageBodyMatcherTests DetectedBodyType = BodyType.Bytes }; var objectMatcherMock = new Mock(); - objectMatcherMock.Setup(m => m.IsMatch(It.IsAny())).Returns(1.0d); + objectMatcherMock.Setup(m => m.IsMatch(It.IsAny())).Returns(MatchResult.From(nameof(IStringMatcher), 1.0d)); var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body); diff --git a/test/WireMock.Net.Tests/RequestMatchers/RequestMessageGraphQLMatcherTests.cs b/test/WireMock.Net.Tests/RequestMatchers/RequestMessageGraphQLMatcherTests.cs index e882ef23..88a8137c 100644 --- a/test/WireMock.Net.Tests/RequestMatchers/RequestMessageGraphQLMatcherTests.cs +++ b/test/WireMock.Net.Tests/RequestMatchers/RequestMessageGraphQLMatcherTests.cs @@ -25,7 +25,7 @@ public class RequestMessageGraphQLMatcherTests DetectedBodyType = BodyType.String }; var stringMatcherMock = new Mock(); - stringMatcherMock.Setup(m => m.IsMatch(It.IsAny())).Returns(1d); + stringMatcherMock.Setup(m => m.IsMatch(It.IsAny())).Returns(MatchResult.From(nameof(IStringMatcher), 1d)); var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body); @@ -57,10 +57,10 @@ public class RequestMessageGraphQLMatcherTests DetectedBodyType = BodyType.String }; var stringMatcherMock1 = new Mock(); - stringMatcherMock1.Setup(m => m.IsMatch(It.IsAny())).Returns(one); + stringMatcherMock1.Setup(m => m.IsMatch(It.IsAny())).Returns(MatchResult.From(nameof(IStringMatcher), one)); var stringMatcherMock2 = new Mock(); - stringMatcherMock2.Setup(m => m.IsMatch(It.IsAny())).Returns(two); + stringMatcherMock2.Setup(m => m.IsMatch(It.IsAny())).Returns(MatchResult.From(nameof(IStringMatcher), two)); var matchers = new[] { stringMatcherMock1.Object, stringMatcherMock2.Object }; @@ -98,10 +98,10 @@ public class RequestMessageGraphQLMatcherTests DetectedBodyType = BodyType.String }; var stringMatcherMock1 = new Mock(); - stringMatcherMock1.Setup(m => m.IsMatch(It.IsAny())).Returns(one); + stringMatcherMock1.Setup(m => m.IsMatch(It.IsAny())).Returns(MatchResult.From(nameof(IStringMatcher), one)); var stringMatcherMock2 = new Mock(); - stringMatcherMock2.Setup(m => m.IsMatch(It.IsAny())).Returns(two); + stringMatcherMock2.Setup(m => m.IsMatch(It.IsAny())).Returns(MatchResult.From(nameof(IStringMatcher), two)); var matchers = new[] { stringMatcherMock1.Object, stringMatcherMock2.Object }; @@ -139,10 +139,10 @@ public class RequestMessageGraphQLMatcherTests DetectedBodyType = BodyType.String }; var stringMatcherMock1 = new Mock(); - stringMatcherMock1.Setup(m => m.IsMatch(It.IsAny())).Returns(one); + stringMatcherMock1.Setup(m => m.IsMatch(It.IsAny())).Returns(MatchResult.From(nameof(IStringMatcher), one)); var stringMatcherMock2 = new Mock(); - stringMatcherMock2.Setup(m => m.IsMatch(It.IsAny())).Returns(two); + stringMatcherMock2.Setup(m => m.IsMatch(It.IsAny())).Returns(MatchResult.From(nameof(IStringMatcher), two)); var matchers = new[] { stringMatcherMock1.Object, stringMatcherMock2.Object }; @@ -175,7 +175,7 @@ public class RequestMessageGraphQLMatcherTests DetectedBodyType = BodyType.Bytes }; var stringMatcherMock = new Mock(); - stringMatcherMock.Setup(m => m.IsMatch(It.IsAny())).Returns(0.5d); + stringMatcherMock.Setup(m => m.IsMatch(It.IsAny())).Returns(MatchResult.From(nameof(IStringMatcher), 0.5d)); var requestMessage = new RequestMessage(new UrlDetails("http://localhost"), "GET", "127.0.0.1", body); diff --git a/test/WireMock.Net.Tests/Serialization/CustomPathParamMatcher.cs b/test/WireMock.Net.Tests/Serialization/CustomPathParamMatcher.cs index 9cf3c3ad..5885c974 100644 --- a/test/WireMock.Net.Tests/Serialization/CustomPathParamMatcher.cs +++ b/test/WireMock.Net.Tests/Serialization/CustomPathParamMatcher.cs @@ -46,7 +46,7 @@ public class CustomPathParamMatcher : IStringMatcher var inputParts = GetPathParts(input); if (inputParts.Length != _pathParts.Length) { - return MatchScores.Mismatch; + return MatchResult.From(Name); } try @@ -60,29 +60,29 @@ public class CustomPathParamMatcher : IStringMatcher var pathParamName = pathPart.Trim('{').Trim('}'); if (!_pathParams.ContainsKey(pathParamName)) { - return MatchScores.Mismatch; + return MatchResult.From(Name); } if (!Regex.IsMatch(inputPart, _pathParams[pathParamName], RegexOptions.IgnoreCase)) { - return MatchScores.Mismatch; + return MatchResult.From(Name); } } else { if (!inputPart.Equals(pathPart, StringComparison.InvariantCultureIgnoreCase)) { - return MatchScores.Mismatch; + return MatchResult.From(Name); } } } } catch { - return MatchScores.Mismatch; + return MatchResult.From(Name); } - return MatchScores.Perfect; + return MatchResult.From(Name, MatchScores.Perfect); } public AnyOf[] GetPatterns()