mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-24 09:18:27 +02:00
Fix MappingMatcher in case of an exception in LinqMatcher. (#322)
* Fix MappingMatcher in case of an exception in LinqMatcher. * update unit-tests
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
using WireMock.Matchers.Request;
|
||||
|
||||
namespace WireMock.Owin
|
||||
namespace WireMock.Owin
|
||||
{
|
||||
internal interface IMappingMatcher
|
||||
{
|
||||
(IMapping Mapping, RequestMatchResult RequestMatchResult) Match(RequestMessage request);
|
||||
MappingMatcherResult FindBestMatch(RequestMessage request);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.Linq;
|
||||
using WireMock.Matchers.Request;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using WireMock.Validation;
|
||||
|
||||
namespace WireMock.Owin
|
||||
@@ -15,34 +16,41 @@ namespace WireMock.Owin
|
||||
_options = options;
|
||||
}
|
||||
|
||||
public (IMapping Mapping, RequestMatchResult RequestMatchResult) Match(RequestMessage request)
|
||||
public MappingMatcherResult FindBestMatch(RequestMessage request)
|
||||
{
|
||||
var mappings = _options.Mappings.Values
|
||||
.Select(m => new
|
||||
var mappings = new List<MappingMatcherResult>();
|
||||
foreach (var mapping in _options.Mappings.Values)
|
||||
{
|
||||
try
|
||||
{
|
||||
Mapping = m,
|
||||
MatchResult = m.GetRequestMatchResult(request, m.Scenario != null && _options.Scenarios.ContainsKey(m.Scenario) ? _options.Scenarios[m.Scenario].NextState : null)
|
||||
})
|
||||
.ToList();
|
||||
string scenario = mapping.Scenario != null && _options.Scenarios.ContainsKey(mapping.Scenario) ? _options.Scenarios[mapping.Scenario].NextState : null;
|
||||
|
||||
mappings.Add(new MappingMatcherResult
|
||||
{
|
||||
Mapping = mapping,
|
||||
RequestMatchResult = mapping.GetRequestMatchResult(request, scenario)
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_options.Logger.Error($"Getting a Request MatchResult for Mapping '{mapping.Guid}' failed. This mapping will not be evaluated. Exception: {ex}");
|
||||
}
|
||||
}
|
||||
|
||||
if (_options.AllowPartialMapping)
|
||||
{
|
||||
var partialMappings = mappings
|
||||
.Where(pm => (pm.Mapping.IsAdminInterface && pm.MatchResult.IsPerfectMatch) || !pm.Mapping.IsAdminInterface)
|
||||
.OrderBy(m => m.MatchResult)
|
||||
.Where(pm => (pm.Mapping.IsAdminInterface && pm.RequestMatchResult.IsPerfectMatch) || !pm.Mapping.IsAdminInterface)
|
||||
.OrderBy(m => m.RequestMatchResult)
|
||||
.ThenBy(m => m.Mapping.Priority)
|
||||
.ToList();
|
||||
|
||||
var bestPartialMatch = partialMappings.FirstOrDefault(pm => pm.MatchResult.AverageTotalScore > 0.0);
|
||||
|
||||
return (bestPartialMatch?.Mapping, bestPartialMatch?.MatchResult);
|
||||
return partialMappings.FirstOrDefault(pm => pm.RequestMatchResult.AverageTotalScore > 0.0);
|
||||
}
|
||||
|
||||
var perfectMatch = mappings
|
||||
return mappings
|
||||
.OrderBy(m => m.Mapping.Priority)
|
||||
.FirstOrDefault(m => m.MatchResult.IsPerfectMatch);
|
||||
|
||||
return (perfectMatch?.Mapping, perfectMatch?.MatchResult);
|
||||
.FirstOrDefault(m => m.RequestMatchResult.IsPerfectMatch);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
src/WireMock.Net/Owin/MappingMatcherResult.cs
Normal file
11
src/WireMock.Net/Owin/MappingMatcherResult.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using WireMock.Matchers.Request;
|
||||
|
||||
namespace WireMock.Owin
|
||||
{
|
||||
internal class MappingMatcherResult
|
||||
{
|
||||
public IMapping Mapping { get; set; }
|
||||
|
||||
public RequestMatchResult RequestMatchResult { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using WireMock.Logging;
|
||||
using WireMock.Matchers.Request;
|
||||
using System.Linq;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Util;
|
||||
@@ -74,7 +73,7 @@ namespace WireMock.Owin
|
||||
|
||||
bool logRequest = false;
|
||||
ResponseMessage response = null;
|
||||
(IMapping TargetMapping, RequestMatchResult RequestMatchResult) result = (null, null);
|
||||
MappingMatcherResult result = null;
|
||||
try
|
||||
{
|
||||
foreach (var mapping in _options.Mappings.Values.Where(m => m?.Scenario != null))
|
||||
@@ -89,9 +88,9 @@ namespace WireMock.Owin
|
||||
}
|
||||
}
|
||||
|
||||
result = _mappingMatcher.Match(request);
|
||||
var targetMapping = result.TargetMapping;
|
||||
result = _mappingMatcher.FindBestMatch(request);
|
||||
|
||||
var targetMapping = result?.Mapping;
|
||||
if (targetMapping == null)
|
||||
{
|
||||
logRequest = true;
|
||||
@@ -129,7 +128,7 @@ namespace WireMock.Owin
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_options.Logger.Error($"Providing a Response for Mapping '{result.TargetMapping.Guid}' failed. HttpStatusCode set to 500. Exception: {ex}");
|
||||
_options.Logger.Error($"Providing a Response for Mapping '{result?.Mapping?.Guid}' failed. HttpStatusCode set to 500. Exception: {ex}");
|
||||
response = ResponseMessageBuilder.Create(JsonConvert.SerializeObject(ex), 500);
|
||||
}
|
||||
finally
|
||||
@@ -139,9 +138,9 @@ namespace WireMock.Owin
|
||||
Guid = Guid.NewGuid(),
|
||||
RequestMessage = request,
|
||||
ResponseMessage = response,
|
||||
MappingGuid = result.TargetMapping?.Guid,
|
||||
MappingTitle = result.TargetMapping?.Title,
|
||||
RequestMatchResult = result.RequestMatchResult
|
||||
MappingGuid = result?.Mapping?.Guid,
|
||||
MappingTitle = result?.Mapping?.Title,
|
||||
RequestMatchResult = result?.RequestMatchResult
|
||||
};
|
||||
|
||||
LogRequest(log, logRequest);
|
||||
|
||||
Reference in New Issue
Block a user