From 571f434b9ac0900f9f18abfc818858c4f263567d Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Tue, 14 Feb 2017 21:38:44 +0100 Subject: [PATCH] Score --- .../WireMock.Net.ConsoleApplication/Program.cs | 2 +- .../Admin/Requests/LogRequestMatchModel.cs | 12 ++++++------ src/WireMock.Net/Matchers/MatchScores.cs | 3 +-- .../Matchers/Request/RequestMatchResult.cs | 14 +++++++------- .../Request/RequestMessageBodyMatcher.cs | 4 ++-- .../Request/RequestMessageCookieMatcher.cs | 4 ++-- .../Request/RequestMessageHeaderMatcher.cs | 4 ++-- .../Request/RequestMessageMethodMatcher.cs | 4 ++-- .../Request/RequestMessageParamMatcher.cs | 4 ++-- .../Request/RequestMessagePathMatcher.cs | 4 ++-- .../Request/RequestMessageUrlMatcher.cs | 4 ++-- .../Server/FluentMockServer.Admin.cs | 6 ++++-- src/WireMock.Net/Server/FluentMockServer.cs | 17 +++++++++++++++-- .../Server/IRespondWithAProvider.cs | 7 +++++++ src/WireMock.Net/Server/RespondWithAProvider.cs | 10 ++++++++++ 15 files changed, 65 insertions(+), 34 deletions(-) diff --git a/examples/WireMock.Net.ConsoleApplication/Program.cs b/examples/WireMock.Net.ConsoleApplication/Program.cs index a6dd3fb3..28e2ca06 100644 --- a/examples/WireMock.Net.ConsoleApplication/Program.cs +++ b/examples/WireMock.Net.ConsoleApplication/Program.cs @@ -79,7 +79,7 @@ namespace WireMock.Net.ConsoleApplication // http://localhost:8080/any/any?start=1000&stop=1&stop=2 server .Given(Request.Create().WithPath("/*").UsingGet()) - .WithGuid(Guid.Parse("90356dba-b36c-469a-a17e-669cd84f1f05")) + .WithGuid("90356dba-b36c-469a-a17e-669cd84f1f05") .AtPriority(server.Mappings.Count() + 1) .RespondWith(Response.Create() .WithStatusCode(200) diff --git a/src/WireMock.Net/Admin/Requests/LogRequestMatchModel.cs b/src/WireMock.Net/Admin/Requests/LogRequestMatchModel.cs index 56adfe2d..d2d916e7 100644 --- a/src/WireMock.Net/Admin/Requests/LogRequestMatchModel.cs +++ b/src/WireMock.Net/Admin/Requests/LogRequestMatchModel.cs @@ -6,12 +6,12 @@ public class LogRequestMatchModel { /// - /// Gets or sets the number of matches. + /// Gets or sets the match-score. /// /// - /// The number of matches. + /// The match-score. /// - public double MatchScore { get; set; } + public double TotalScore { get; set; } /// /// Gets or sets the total number of matches. @@ -19,7 +19,7 @@ /// /// The total number of matches. /// - public int Total { get; set; } + public int TotalNumber { get; set; } /// /// Gets or sets a value indicating whether this instance is perfect match. @@ -27,7 +27,7 @@ /// /// true if this instance is perfect match; otherwise, false. /// - public bool IsPerfectMatch => MatchScore == Total; + public bool IsPerfectMatch { get; set; } /// /// Gets the match percentage. @@ -35,6 +35,6 @@ /// /// The match percentage. /// - public double MatchPercentage => Total == 0 ? 100 : 100.0 * MatchScore / Total; + public double AverageTotalScore { get; set; } } } \ No newline at end of file diff --git a/src/WireMock.Net/Matchers/MatchScores.cs b/src/WireMock.Net/Matchers/MatchScores.cs index 8aabdfc5..5304ba88 100644 --- a/src/WireMock.Net/Matchers/MatchScores.cs +++ b/src/WireMock.Net/Matchers/MatchScores.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; namespace WireMock.Matchers diff --git a/src/WireMock.Net/Matchers/Request/RequestMatchResult.cs b/src/WireMock.Net/Matchers/Request/RequestMatchResult.cs index bdc14ed8..8068846f 100644 --- a/src/WireMock.Net/Matchers/Request/RequestMatchResult.cs +++ b/src/WireMock.Net/Matchers/Request/RequestMatchResult.cs @@ -8,12 +8,12 @@ namespace WireMock.Matchers.Request public class RequestMatchResult : IComparable { /// - /// Gets or sets the matches score. + /// Gets or sets the match-score. /// /// - /// The number of matches. + /// The match-score. /// - public double MatchScore { get; set; } + public double TotalScore { get; set; } /// /// Gets or sets the total number of matches. @@ -21,7 +21,7 @@ namespace WireMock.Matchers.Request /// /// The total number of matches. /// - public int Total { get; set; } + public int TotalNumber { get; set; } /// /// Gets or sets a value indicating whether this instance is perfect match. @@ -29,7 +29,7 @@ namespace WireMock.Matchers.Request /// /// true if this instance is perfect match; otherwise, false. /// - public bool IsPerfectMatch => Math.Abs(MatchScore - Total) < MatchScores.Tolerance; + public bool IsPerfectMatch => Math.Abs(TotalScore - TotalNumber) < MatchScores.Tolerance; /// /// Gets the match percentage. @@ -37,7 +37,7 @@ namespace WireMock.Matchers.Request /// /// The match percentage. /// - public double MatchPercentage => Total == 0 ? 1.0 : MatchScore / Total; + public double AverageTotalScore => TotalNumber == 0 ? 0.0 : TotalScore / TotalNumber; /// /// 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. @@ -51,7 +51,7 @@ namespace WireMock.Matchers.Request { var compareObj = (RequestMatchResult)obj; - return compareObj.MatchPercentage.CompareTo(MatchPercentage); + return compareObj.AverageTotalScore.CompareTo(AverageTotalScore); } } } \ No newline at end of file diff --git a/src/WireMock.Net/Matchers/Request/RequestMessageBodyMatcher.cs b/src/WireMock.Net/Matchers/Request/RequestMessageBodyMatcher.cs index d85f7b54..52796f70 100644 --- a/src/WireMock.Net/Matchers/Request/RequestMessageBodyMatcher.cs +++ b/src/WireMock.Net/Matchers/Request/RequestMessageBodyMatcher.cs @@ -98,9 +98,9 @@ namespace WireMock.Matchers.Request public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult) { double score = IsMatch(requestMessage); - requestMatchResult.MatchScore += score; + requestMatchResult.TotalScore += score; - requestMatchResult.Total++; + requestMatchResult.TotalNumber++; return score; } diff --git a/src/WireMock.Net/Matchers/Request/RequestMessageCookieMatcher.cs b/src/WireMock.Net/Matchers/Request/RequestMessageCookieMatcher.cs index 56664493..5eb4f6cb 100644 --- a/src/WireMock.Net/Matchers/Request/RequestMessageCookieMatcher.cs +++ b/src/WireMock.Net/Matchers/Request/RequestMessageCookieMatcher.cs @@ -77,9 +77,9 @@ namespace WireMock.Matchers.Request public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult) { double score = IsMatch(requestMessage); - requestMatchResult.MatchScore += score; + requestMatchResult.TotalScore += score; - requestMatchResult.Total++; + requestMatchResult.TotalNumber++; return score; } diff --git a/src/WireMock.Net/Matchers/Request/RequestMessageHeaderMatcher.cs b/src/WireMock.Net/Matchers/Request/RequestMessageHeaderMatcher.cs index 7127f324..0431d70b 100644 --- a/src/WireMock.Net/Matchers/Request/RequestMessageHeaderMatcher.cs +++ b/src/WireMock.Net/Matchers/Request/RequestMessageHeaderMatcher.cs @@ -77,9 +77,9 @@ namespace WireMock.Matchers.Request public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult) { double score = IsMatch(requestMessage); - requestMatchResult.MatchScore += score; + requestMatchResult.TotalScore += score; - requestMatchResult.Total++; + requestMatchResult.TotalNumber++; return score; } diff --git a/src/WireMock.Net/Matchers/Request/RequestMessageMethodMatcher.cs b/src/WireMock.Net/Matchers/Request/RequestMessageMethodMatcher.cs index 02e19ba0..9d3c7854 100644 --- a/src/WireMock.Net/Matchers/Request/RequestMessageMethodMatcher.cs +++ b/src/WireMock.Net/Matchers/Request/RequestMessageMethodMatcher.cs @@ -37,9 +37,9 @@ namespace WireMock.Matchers.Request public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult) { double score = IsMatch(requestMessage); - requestMatchResult.MatchScore += score; + requestMatchResult.TotalScore += score; - requestMatchResult.Total++; + requestMatchResult.TotalNumber++; return score; } diff --git a/src/WireMock.Net/Matchers/Request/RequestMessageParamMatcher.cs b/src/WireMock.Net/Matchers/Request/RequestMessageParamMatcher.cs index 942b123b..68d7b109 100644 --- a/src/WireMock.Net/Matchers/Request/RequestMessageParamMatcher.cs +++ b/src/WireMock.Net/Matchers/Request/RequestMessageParamMatcher.cs @@ -65,9 +65,9 @@ namespace WireMock.Matchers.Request public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult) { double score = IsMatch(requestMessage); - requestMatchResult.MatchScore += score; + requestMatchResult.TotalScore += score; - requestMatchResult.Total++; + requestMatchResult.TotalNumber++; return score; } diff --git a/src/WireMock.Net/Matchers/Request/RequestMessagePathMatcher.cs b/src/WireMock.Net/Matchers/Request/RequestMessagePathMatcher.cs index 925bf319..9ffee882 100644 --- a/src/WireMock.Net/Matchers/Request/RequestMessagePathMatcher.cs +++ b/src/WireMock.Net/Matchers/Request/RequestMessagePathMatcher.cs @@ -60,9 +60,9 @@ namespace WireMock.Matchers.Request public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult) { double score = IsMatch(requestMessage); - requestMatchResult.MatchScore += score; + requestMatchResult.TotalScore += score; - requestMatchResult.Total++; + requestMatchResult.TotalNumber++; return score; } diff --git a/src/WireMock.Net/Matchers/Request/RequestMessageUrlMatcher.cs b/src/WireMock.Net/Matchers/Request/RequestMessageUrlMatcher.cs index 87020dc9..e0af64d5 100644 --- a/src/WireMock.Net/Matchers/Request/RequestMessageUrlMatcher.cs +++ b/src/WireMock.Net/Matchers/Request/RequestMessageUrlMatcher.cs @@ -60,9 +60,9 @@ namespace WireMock.Matchers.Request public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult) { double score = IsMatch(requestMessage); - requestMatchResult.MatchScore += score; + requestMatchResult.TotalScore += score; - requestMatchResult.Total++; + requestMatchResult.TotalNumber++; return score; } diff --git a/src/WireMock.Net/Server/FluentMockServer.Admin.cs b/src/WireMock.Net/Server/FluentMockServer.Admin.cs index 2610aa44..b596eaf3 100644 --- a/src/WireMock.Net/Server/FluentMockServer.Admin.cs +++ b/src/WireMock.Net/Server/FluentMockServer.Admin.cs @@ -301,8 +301,10 @@ namespace WireMock.Server MappingGuid = logEntry.MappingGuid, RequestMatchResult = logEntry.RequestMatchResult != null ? new LogRequestMatchModel { - MatchScore = logEntry.RequestMatchResult.MatchScore, - Total = logEntry.RequestMatchResult.Total + TotalScore = logEntry.RequestMatchResult.TotalScore, + TotalNumber = logEntry.RequestMatchResult.TotalNumber, + IsPerfectMatch = logEntry.RequestMatchResult.IsPerfectMatch, + AverageTotalScore = logEntry.RequestMatchResult.AverageTotalScore } : null }; } diff --git a/src/WireMock.Net/Server/FluentMockServer.cs b/src/WireMock.Net/Server/FluentMockServer.cs index 5133abc1..62c77d79 100644 --- a/src/WireMock.Net/Server/FluentMockServer.cs +++ b/src/WireMock.Net/Server/FluentMockServer.cs @@ -11,6 +11,7 @@ using WireMock.Http; using WireMock.Logging; using WireMock.Matchers; using WireMock.Matchers.Request; +using WireMock.RequestBuilders; using WireMock.Validation; namespace WireMock.Server @@ -143,6 +144,18 @@ namespace WireMock.Server return new FluentMockServer(true, urls); } + /// + /// Adds the catch all mapping. + /// + [PublicAPI] + public void AddCatchAllMapping() + { + Given(Request.Create().WithPath("/*").UsingAnyVerb()) + .WithGuid(Guid.Parse("90008000-0000-4444-a17e-669cd84f1f05")) + .AtPriority(1000) + .RespondWith(new DynamicResponseProvider(request => new ResponseMessage { StatusCode = 404, Body = "No matching mapping found" })); + } + private FluentMockServer(bool startAdminInterface, int port, bool ssl) : this(startAdminInterface, (ssl ? "https" : "http") + "://localhost:" + port + "/") { } @@ -380,7 +393,7 @@ namespace WireMock.Server .ThenBy(m => m.Mapping.Priority) .ToList(); - var bestPartialMatch = partialMappings.FirstOrDefault(pm => pm.MatchResult.MatchPercentage > 0.0); + var bestPartialMatch = partialMappings.FirstOrDefault(pm => pm.MatchResult.AverageTotalScore > 0.0); targetMapping = bestPartialMatch?.Mapping; requestMatchResult = bestPartialMatch?.MatchResult; @@ -397,7 +410,7 @@ namespace WireMock.Server if (targetMapping == null) { - response = new ResponseMessage { StatusCode = 404, Body = "No mapping found" }; + response = new ResponseMessage { StatusCode = 404, Body = "No matching mapping found" }; return; } diff --git a/src/WireMock.Net/Server/IRespondWithAProvider.cs b/src/WireMock.Net/Server/IRespondWithAProvider.cs index b590ae31..a0c54131 100644 --- a/src/WireMock.Net/Server/IRespondWithAProvider.cs +++ b/src/WireMock.Net/Server/IRespondWithAProvider.cs @@ -14,6 +14,13 @@ namespace WireMock.Server /// The . IRespondWithAProvider WithGuid(Guid guid); + /// + /// Define a unique identifier for this mapping. + /// + /// The unique identifier. + /// The . + IRespondWithAProvider WithGuid(string guid); + /// /// Define the priority for this mapping. /// diff --git a/src/WireMock.Net/Server/RespondWithAProvider.cs b/src/WireMock.Net/Server/RespondWithAProvider.cs index 9cd7481c..afae9b22 100644 --- a/src/WireMock.Net/Server/RespondWithAProvider.cs +++ b/src/WireMock.Net/Server/RespondWithAProvider.cs @@ -44,6 +44,16 @@ namespace WireMock.Server _registrationCallback(new Mapping(mappingGuid, _requestMatcher, provider, _priority)); } + /// + /// Define a unique identifier for this mapping. + /// + /// The unique identifier. + /// The . + public IRespondWithAProvider WithGuid(string guid) + { + return WithGuid(Guid.Parse(guid)); + } + /// /// Define a unique identifier for this mapping. ///