From 83922fd543cca61524f5fef7c795fde081ab2c69 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Sun, 11 Jan 2026 18:05:35 +0100 Subject: [PATCH] ... --- .../Admin/Requests/LogRequestMatchModel.cs | 3 ++- .../Matchers/Request/IRequestMatchResult.cs | 7 +++++++ .../Matchers/Request/MatchDetail.cs | 10 ++++++++++ .../Logging/WireMockConsoleLogger.cs | 2 +- .../Matchers/Request/RequestMatchResult.cs | 9 +++++++++ .../Request/RequestMessageClientIPMatcher.cs | 4 ++-- .../Request/RequestMessageMultiPartMatcher.cs | 11 ++++++----- .../Request/RequestMessagePathMatcher.cs | 4 ++-- .../Serialization/LogEntryMapper.cs | 11 ++++++----- .../Server/WireMockServer.ConvertMapping.cs | 2 +- .../Matchers/MatchResult.cs | 18 +++++++++++++++++- 11 files changed, 63 insertions(+), 18 deletions(-) diff --git a/src/WireMock.Net.Abstractions/Admin/Requests/LogRequestMatchModel.cs b/src/WireMock.Net.Abstractions/Admin/Requests/LogRequestMatchModel.cs index 05212618..4a2451f3 100644 --- a/src/WireMock.Net.Abstractions/Admin/Requests/LogRequestMatchModel.cs +++ b/src/WireMock.Net.Abstractions/Admin/Requests/LogRequestMatchModel.cs @@ -1,6 +1,7 @@ // Copyright © WireMock.Net using System.Collections.Generic; +using WireMock.Matchers.Request; namespace WireMock.Admin.Requests; @@ -47,5 +48,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/IRequestMatchResult.cs b/src/WireMock.Net.Abstractions/Matchers/Request/IRequestMatchResult.cs index c5640113..9dee2645 100644 --- a/src/WireMock.Net.Abstractions/Matchers/Request/IRequestMatchResult.cs +++ b/src/WireMock.Net.Abstractions/Matchers/Request/IRequestMatchResult.cs @@ -55,4 +55,11 @@ public interface IRequestMatchResult : IComparable /// The exception [Optional]. /// The score. double AddScore(Type matcherType, double score, Exception? exception); + + /// + /// Adds the score. + /// + /// The matchDetail. + /// The score. + double AddMatchDetail(MatchDetail matchDetail); } \ 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 7aa6e436..adac58e2 100644 --- a/src/WireMock.Net.Abstractions/Matchers/Request/MatchDetail.cs +++ b/src/WireMock.Net.Abstractions/Matchers/Request/MatchDetail.cs @@ -14,6 +14,11 @@ public class MatchDetail /// public required Type MatcherType { get; set; } + /// + /// Gets or sets the type of the matcher. + /// + public required string Name { get; set; } + /// /// Gets or sets the score between 0.0 and 1.0 /// @@ -24,4 +29,9 @@ public class MatchDetail /// [Optional] /// public Exception? Exception { get; set; } + + /// + /// The child MatchResults in case of multiple matchers. + /// + public MatchDetail[]? MatchDetails { get; set; } } \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/Logging/WireMockConsoleLogger.cs b/src/WireMock.Net.Minimal/Logging/WireMockConsoleLogger.cs index 04ba6939..72f35fbb 100644 --- a/src/WireMock.Net.Minimal/Logging/WireMockConsoleLogger.cs +++ b/src/WireMock.Net.Minimal/Logging/WireMockConsoleLogger.cs @@ -69,7 +69,7 @@ public class WireMockConsoleLogger : IWireMockLogger /// public void DebugRequestResponse(LogEntryModel logEntryModel, bool isAdminRequest) { - string message = JsonConvert.SerializeObject(logEntryModel, Formatting.Indented); + string message = JsonConvert.SerializeObject(logEntryModel, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); Console.WriteLine(Format("DebugRequestResponse", "Admin[{0}] {1}", isAdminRequest, message)); } diff --git a/src/WireMock.Net.Minimal/Matchers/Request/RequestMatchResult.cs b/src/WireMock.Net.Minimal/Matchers/Request/RequestMatchResult.cs index 8bccb94f..0d057375 100644 --- a/src/WireMock.Net.Minimal/Matchers/Request/RequestMatchResult.cs +++ b/src/WireMock.Net.Minimal/Matchers/Request/RequestMatchResult.cs @@ -31,6 +31,7 @@ public class RequestMatchResult : IRequestMatchResult { MatchDetails.Add(new MatchDetail { + Name = matcherType.Name.Replace("RequestMessage", string.Empty), MatcherType = matcherType, Score = score, Exception = exception @@ -39,6 +40,14 @@ public class RequestMatchResult : IRequestMatchResult return score; } + /// + public double AddMatchDetail(MatchDetail matchDetail) + { + MatchDetails.Add(matchDetail); + + return matchDetail.Score; + } + /// /// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object. /// diff --git a/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageClientIPMatcher.cs b/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageClientIPMatcher.cs index aaa0c3d1..3c08fb11 100644 --- a/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageClientIPMatcher.cs +++ b/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageClientIPMatcher.cs @@ -79,8 +79,8 @@ public class RequestMessageClientIPMatcher : IRequestMatcher /// public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult) { - var (score, exception) = GetMatchResult(requestMessage).Expand(); - return requestMatchResult.AddScore(GetType(), score, exception); + var matchDetail = GetMatchResult(requestMessage).ToMatchDetail(); + return requestMatchResult.AddMatchDetail(matchDetail); } private MatchResult GetMatchResult(IRequestMessage requestMessage) diff --git a/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageMultiPartMatcher.cs b/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageMultiPartMatcher.cs index 5d32ebf0..31e8a0e7 100644 --- a/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageMultiPartMatcher.cs +++ b/src/WireMock.Net.Minimal/Matchers/Request/RequestMessageMultiPartMatcher.cs @@ -15,7 +15,7 @@ public class RequestMessageMultiPartMatcher : IRequestMatcher /// /// The name of this matcher. /// - public const string MatcherName = "MultiPartMatcher"; + public const string Name = "MultiPartMatcher"; private readonly IMimeKitUtils _mimeKitUtils = LoadMimeKitUtils(); @@ -59,19 +59,20 @@ public class RequestMessageMultiPartMatcher : IRequestMatcher /// public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult) { - var score = MatchScores.Mismatch; + var matchDetail = MatchResult.From(Name).ToMatchDetail(); Exception? exception = null; if (Matchers == null) { - return requestMatchResult.AddScore(GetType(), score, null); + return requestMatchResult.AddMatchDetail(matchDetail); } if (!_mimeKitUtils.TryGetMimeMessage(requestMessage, out var message)) { - return requestMatchResult.AddScore(GetType(), score, null); + return requestMatchResult.AddMatchDetail(matchDetail); } + double score = MatchScores.Mismatch; try { foreach (var mimePartMatcher in Matchers.OfType().ToArray()) @@ -99,7 +100,7 @@ public class RequestMessageMultiPartMatcher : IRequestMatcher exception = ex; } - return requestMatchResult.AddScore(GetType(), score, exception); + return requestMatchResult.AddMatchDetail(MatchResult.From(Name, score, exception).ToMatchDetail()); } private static IMimeKitUtils LoadMimeKitUtils() diff --git a/src/WireMock.Net.Minimal/Matchers/Request/RequestMessagePathMatcher.cs b/src/WireMock.Net.Minimal/Matchers/Request/RequestMessagePathMatcher.cs index 2d247b85..09f8e8df 100644 --- a/src/WireMock.Net.Minimal/Matchers/Request/RequestMessagePathMatcher.cs +++ b/src/WireMock.Net.Minimal/Matchers/Request/RequestMessagePathMatcher.cs @@ -77,8 +77,8 @@ public class RequestMessagePathMatcher : IRequestMatcher /// public double GetMatchingScore(IRequestMessage requestMessage, IRequestMatchResult requestMatchResult) { - var (score, exception) = GetMatchResult(requestMessage).Expand(); - return requestMatchResult.AddScore(GetType(), score, exception); + var matchDetail = GetMatchResult(requestMessage).ToMatchDetail(); + return requestMatchResult.AddMatchDetail(matchDetail); } private MatchResult GetMatchResult(IRequestMessage requestMessage) diff --git a/src/WireMock.Net.Minimal/Serialization/LogEntryMapper.cs b/src/WireMock.Net.Minimal/Serialization/LogEntryMapper.cs index 53a5ba7e..c0f439de 100644 --- a/src/WireMock.Net.Minimal/Serialization/LogEntryMapper.cs +++ b/src/WireMock.Net.Minimal/Serialization/LogEntryMapper.cs @@ -166,11 +166,12 @@ internal class LogEntryMapper TotalScore = matchResult.TotalScore, TotalNumber = matchResult.TotalNumber, AverageTotalScore = matchResult.AverageTotalScore, - MatchDetails = matchResult.MatchDetails.Select(md => new - { - Name = md.MatcherType.Name.Replace("RequestMessage", string.Empty), - md.Score - } as object).ToList() + MatchDetails = matchResult.MatchDetails + //MatchDetails = matchResult.MatchDetails.Select(md => new + //{ + // Name = md.MatcherType.Name.Replace("RequestMessage", string.Empty), + // md.Score + //} as object).ToList() }; } } \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/Server/WireMockServer.ConvertMapping.cs b/src/WireMock.Net.Minimal/Server/WireMockServer.ConvertMapping.cs index 5258efd7..4c8d171b 100644 --- a/src/WireMock.Net.Minimal/Server/WireMockServer.ConvertMapping.cs +++ b/src/WireMock.Net.Minimal/Server/WireMockServer.ConvertMapping.cs @@ -255,7 +255,7 @@ public partial class WireMockServer { var matchOperator = StringUtils.ParseMatchOperator(requestModel.Body.MatchOperator); - if (requestModel.Body.MatcherName == RequestMessageMultiPartMatcher.MatcherName) + if (requestModel.Body.MatcherName == RequestMessageMultiPartMatcher.Name) { requestBuilder = requestBuilder.WithMultiPart(_matcherMapper.Map(requestModel.Body.Matchers), matchOperator: matchOperator); } diff --git a/src/WireMock.Net.Shared/Matchers/MatchResult.cs b/src/WireMock.Net.Shared/Matchers/MatchResult.cs index ce79ec5a..f9bff87e 100644 --- a/src/WireMock.Net.Shared/Matchers/MatchResult.cs +++ b/src/WireMock.Net.Shared/Matchers/MatchResult.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Linq; using Stef.Validation; using WireMock.Extensions; +using WireMock.Matchers.Request; namespace WireMock.Matchers; @@ -30,7 +31,7 @@ public class MatchResult public required string Name { get; set; } /// - /// The sub MatchResults in case of multiple matchers. + /// The child MatchResults in case of multiple matchers. /// public MatchResult[]? MatchResults { get; set; } @@ -99,4 +100,19 @@ public class MatchResult { return (Score, Exception); } + + /// + /// Convert to . + /// + public MatchDetail ToMatchDetail() + { + return new MatchDetail + { + Name = Name, + MatcherType = typeof(MatchResult), + Score = Score, + Exception = Exception, + MatchDetails = MatchResults?.Select(mr => mr.ToMatchDetail()).ToArray() + }; + } } \ No newline at end of file