This commit is contained in:
Stef Heyenrath
2017-02-14 21:38:44 +01:00
parent b25444d083
commit 571f434b9a
15 changed files with 65 additions and 34 deletions

View File

@@ -6,12 +6,12 @@
public class LogRequestMatchModel
{
/// <summary>
/// Gets or sets the number of matches.
/// Gets or sets the match-score.
/// </summary>
/// <value>
/// The number of matches.
/// The match-score.
/// </value>
public double MatchScore { get; set; }
public double TotalScore { get; set; }
/// <summary>
/// Gets or sets the total number of matches.
@@ -19,7 +19,7 @@
/// <value>
/// The total number of matches.
/// </value>
public int Total { get; set; }
public int TotalNumber { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this instance is perfect match.
@@ -27,7 +27,7 @@
/// <value>
/// <c>true</c> if this instance is perfect match; otherwise, <c>false</c>.
/// </value>
public bool IsPerfectMatch => MatchScore == Total;
public bool IsPerfectMatch { get; set; }
/// <summary>
/// Gets the match percentage.
@@ -35,6 +35,6 @@
/// <value>
/// The match percentage.
/// </value>
public double MatchPercentage => Total == 0 ? 100 : 100.0 * MatchScore / Total;
public double AverageTotalScore { get; set; }
}
}

View File

@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
namespace WireMock.Matchers

View File

@@ -8,12 +8,12 @@ namespace WireMock.Matchers.Request
public class RequestMatchResult : IComparable
{
/// <summary>
/// Gets or sets the matches score.
/// Gets or sets the match-score.
/// </summary>
/// <value>
/// The number of matches.
/// The match-score.
/// </value>
public double MatchScore { get; set; }
public double TotalScore { get; set; }
/// <summary>
/// Gets or sets the total number of matches.
@@ -21,7 +21,7 @@ namespace WireMock.Matchers.Request
/// <value>
/// The total number of matches.
/// </value>
public int Total { get; set; }
public int TotalNumber { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this instance is perfect match.
@@ -29,7 +29,7 @@ namespace WireMock.Matchers.Request
/// <value>
/// <c>true</c> if this instance is perfect match; otherwise, <c>false</c>.
/// </value>
public bool IsPerfectMatch => Math.Abs(MatchScore - Total) < MatchScores.Tolerance;
public bool IsPerfectMatch => Math.Abs(TotalScore - TotalNumber) < MatchScores.Tolerance;
/// <summary>
/// Gets the match percentage.
@@ -37,7 +37,7 @@ namespace WireMock.Matchers.Request
/// <value>
/// The match percentage.
/// </value>
public double MatchPercentage => Total == 0 ? 1.0 : MatchScore / Total;
public double AverageTotalScore => TotalNumber == 0 ? 0.0 : TotalScore / TotalNumber;
/// <summary>
/// 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);
}
}
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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
};
}

View File

@@ -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);
}
/// <summary>
/// Adds the catch all mapping.
/// </summary>
[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;
}

View File

@@ -14,6 +14,13 @@ namespace WireMock.Server
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
IRespondWithAProvider WithGuid(Guid guid);
/// <summary>
/// Define a unique identifier for this mapping.
/// </summary>
/// <param name="guid">The unique identifier.</param>
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
IRespondWithAProvider WithGuid(string guid);
/// <summary>
/// Define the priority for this mapping.
/// </summary>

View File

@@ -44,6 +44,16 @@ namespace WireMock.Server
_registrationCallback(new Mapping(mappingGuid, _requestMatcher, provider, _priority));
}
/// <summary>
/// Define a unique identifier for this mapping.
/// </summary>
/// <param name="guid">The unique identifier.</param>
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
public IRespondWithAProvider WithGuid(string guid)
{
return WithGuid(Guid.Parse(guid));
}
/// <summary>
/// Define a unique identifier for this mapping.
/// </summary>