mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-25 02:41:53 +01:00
Partial
This commit is contained in:
@@ -31,6 +31,14 @@ namespace WireMock.Admin.Requests
|
||||
/// </value>
|
||||
public LogResponseModel Response { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the mapping unique identifier.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The mapping unique identifier.
|
||||
/// </value>
|
||||
public Guid? MappingGuid { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the request match result.
|
||||
/// </summary>
|
||||
|
||||
@@ -39,5 +39,13 @@ namespace WireMock.Logging
|
||||
/// The request match result.
|
||||
/// </value>
|
||||
public RequestMatchResult RequestMatchResult { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the mapping unique identifier.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The mapping unique identifier.
|
||||
/// </value>
|
||||
public Guid? MappingGuid { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -108,7 +108,7 @@ namespace WireMock.Matchers
|
||||
/// <returns>Name</returns>
|
||||
public string GetName()
|
||||
{
|
||||
return $"SimMetricsMatcher ({_simMetricType})";
|
||||
return $"SimMetricsMatcher.{_simMetricType}";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ using System.Linq;
|
||||
using JetBrains.Annotations;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using SimMetrics.Net;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Admin.Requests;
|
||||
using WireMock.Logging;
|
||||
@@ -11,6 +12,7 @@ using WireMock.Matchers;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.ResponseBuilders;
|
||||
using WireMock.Util;
|
||||
|
||||
namespace WireMock.Server
|
||||
{
|
||||
@@ -203,11 +205,12 @@ namespace WireMock.Server
|
||||
BodyOriginal = logEntry.ResponseMessage.BodyOriginal,
|
||||
Headers = logEntry.ResponseMessage.Headers
|
||||
},
|
||||
RequestMatchResult = new LogRequestMatchModel
|
||||
MappingGuid = logEntry.MappingGuid,
|
||||
RequestMatchResult = logEntry.RequestMatchResult != null ? new LogRequestMatchModel
|
||||
{
|
||||
MatchScore = logEntry.RequestMatchResult.MatchScore,
|
||||
Total = logEntry.RequestMatchResult.Total
|
||||
}
|
||||
} : null
|
||||
};
|
||||
}
|
||||
|
||||
@@ -222,26 +225,31 @@ namespace WireMock.Server
|
||||
private IRequestBuilder InitRequestBuilder(MappingModel mappingModel)
|
||||
{
|
||||
IRequestBuilder requestBuilder = Request.Create();
|
||||
string path = mappingModel.Request.Path as string;
|
||||
if (path != null)
|
||||
requestBuilder = requestBuilder.WithPath(path);
|
||||
else
|
||||
|
||||
if (mappingModel.Request.Path != null)
|
||||
{
|
||||
JToken pathToken = (JToken)mappingModel.Request.Path;
|
||||
PathModel pathModel = pathToken.ToObject<PathModel>();
|
||||
if (pathModel?.Matchers != null)
|
||||
requestBuilder = requestBuilder.WithPath(pathModel.Matchers.Select(Map).ToArray());
|
||||
string path = mappingModel.Request.Path as string;
|
||||
if (path != null)
|
||||
requestBuilder = requestBuilder.WithPath(path);
|
||||
else
|
||||
{
|
||||
var pathModel = JsonUtils.ParseJTokenToObject<PathModel>(mappingModel.Request.Path);
|
||||
if (pathModel?.Matchers != null)
|
||||
requestBuilder = requestBuilder.WithPath(pathModel.Matchers.Select(Map).ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
string url = mappingModel.Request.Url as string;
|
||||
if (url != null)
|
||||
requestBuilder = requestBuilder.WithUrl(url);
|
||||
else
|
||||
if (mappingModel.Request.Url != null)
|
||||
{
|
||||
JToken urlToken = (JToken)mappingModel.Request.Url;
|
||||
UrlModel urlModel = urlToken.ToObject<UrlModel>();
|
||||
if (urlModel?.Matchers != null)
|
||||
requestBuilder = requestBuilder.WithUrl(urlModel.Matchers.Select(Map).ToArray());
|
||||
string url = mappingModel.Request.Url as string;
|
||||
if (url != null)
|
||||
requestBuilder = requestBuilder.WithUrl(url);
|
||||
else
|
||||
{
|
||||
var urlModel = JsonUtils.ParseJTokenToObject<UrlModel>(mappingModel.Request.Url);
|
||||
if (urlModel?.Matchers != null)
|
||||
requestBuilder = requestBuilder.WithUrl(urlModel.Matchers.Select(Map).ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
if (mappingModel.Request.Methods != null)
|
||||
@@ -412,7 +420,11 @@ namespace WireMock.Server
|
||||
if (matcher == null)
|
||||
return null;
|
||||
|
||||
switch (matcher.Name)
|
||||
var parts = matcher.Name.Split('.');
|
||||
string matcherName = parts[0];
|
||||
string matcherType = parts.Length > 1 ? parts[1] : null;
|
||||
|
||||
switch (matcherName)
|
||||
{
|
||||
case "RegexMatcher":
|
||||
return new RegexMatcher(matcher.Pattern);
|
||||
@@ -423,8 +435,18 @@ namespace WireMock.Server
|
||||
case "XPathMatcher":
|
||||
return new XPathMatcher(matcher.Pattern);
|
||||
|
||||
default:
|
||||
case "WildcardMatcher":
|
||||
return new WildcardMatcher(matcher.Pattern, matcher.IgnoreCase == true);
|
||||
|
||||
case "SimMetricsMatcher":
|
||||
SimMetricType type = SimMetricType.Levenstein;
|
||||
if (!string.IsNullOrEmpty(matcherType) && !Enum.TryParse(matcherType, out type))
|
||||
throw new NotSupportedException($"Matcher '{matcherName}' with Type '{matcherType}' is not supported.");
|
||||
|
||||
return new SimMetricsMatcher(matcher.Pattern, type);
|
||||
|
||||
default:
|
||||
throw new NotSupportedException($"Matcher '{matcherName}' is not supported.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -331,6 +331,7 @@ namespace WireMock.Server
|
||||
var request = _requestMapper.Map(ctx.Request);
|
||||
|
||||
ResponseMessage response = null;
|
||||
Mapping targetMapping = null;
|
||||
RequestMatchResult requestMatchResult = null;
|
||||
try
|
||||
{
|
||||
@@ -338,15 +339,19 @@ namespace WireMock.Server
|
||||
.Select(m => new { Mapping = m, MatchResult = m.IsRequestHandled(request) })
|
||||
.ToList();
|
||||
|
||||
Mapping targetMapping;
|
||||
if (_allowPartialMapping)
|
||||
{
|
||||
var orderedMappings = possibleMatchingMappings
|
||||
.OrderBy(m => m.Mapping.Priority)
|
||||
.ThenBy(m => m.MatchResult)
|
||||
.Where(pm =>
|
||||
(pm.Mapping.Provider is DynamicResponseProvider && pm.MatchResult.IsPerfectMatch) ||
|
||||
!(pm.Mapping.Provider is DynamicResponseProvider)
|
||||
)
|
||||
.OrderBy(m => m.MatchResult)
|
||||
.ThenBy(m => m.Mapping.Priority)
|
||||
.ToList();
|
||||
|
||||
var bestPartialMatch = orderedMappings.FirstOrDefault();
|
||||
|
||||
targetMapping = bestPartialMatch?.Mapping;
|
||||
requestMatchResult = bestPartialMatch?.MatchResult;
|
||||
}
|
||||
@@ -388,6 +393,7 @@ namespace WireMock.Server
|
||||
Guid = Guid.NewGuid(),
|
||||
RequestMessage = request,
|
||||
ResponseMessage = response,
|
||||
MappingGuid = targetMapping?.Guid,
|
||||
RequestMatchResult = requestMatchResult
|
||||
};
|
||||
|
||||
|
||||
19
src/WireMock.Net/Util/JsonUtils.cs
Normal file
19
src/WireMock.Net/Util/JsonUtils.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace WireMock.Util
|
||||
{
|
||||
internal static class JsonUtils
|
||||
{
|
||||
public static T ParseJTokenToObject<T>(object value)
|
||||
{
|
||||
if (value == null)
|
||||
return default(T);
|
||||
|
||||
JToken token = value as JToken;
|
||||
if (token == null)
|
||||
return default(T);
|
||||
|
||||
return token.ToObject<T>();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user