| | | 1 | | using System.Linq; |
| | | 2 | | using WireMock.Matchers.Request; |
| | | 3 | | using WireMock.Validation; |
| | | 4 | | |
| | | 5 | | namespace WireMock.Owin |
| | | 6 | | { |
| | | 7 | | internal class MappingMatcher : IMappingMatcher |
| | | 8 | | { |
| | | 9 | | private readonly IWireMockMiddlewareOptions _options; |
| | | 10 | | |
| | 50 | 11 | | public MappingMatcher(IWireMockMiddlewareOptions options) |
| | 50 | 12 | | { |
| | 50 | 13 | | Check.NotNull(options, nameof(options)); |
| | | 14 | | |
| | 50 | 15 | | _options = options; |
| | 50 | 16 | | } |
| | | 17 | | |
| | | 18 | | public (IMapping Mapping, RequestMatchResult RequestMatchResult) Match(RequestMessage request) |
| | 49 | 19 | | { |
| | 49 | 20 | | var mappings = _options.Mappings.Values |
| | 332 | 21 | | .Select(m => new |
| | 332 | 22 | | { |
| | 332 | 23 | | Mapping = m, |
| | 332 | 24 | | MatchResult = m.GetRequestMatchResult(request, m.Scenario != null && _options.Scenarios.ContainsKey( |
| | 332 | 25 | | }) |
| | 49 | 26 | | .ToList(); |
| | | 27 | | |
| | 49 | 28 | | if (_options.AllowPartialMapping) |
| | 1 | 29 | | { |
| | 1 | 30 | | var partialMappings = mappings |
| | 3 | 31 | | .Where(pm => (pm.Mapping.IsAdminInterface && pm.MatchResult.IsPerfectMatch) || !pm.Mapping.IsAdminIn |
| | 3 | 32 | | .OrderBy(m => m.MatchResult) |
| | 3 | 33 | | .ThenBy(m => m.Mapping.Priority) |
| | 1 | 34 | | .ToList(); |
| | | 35 | | |
| | 2 | 36 | | var bestPartialMatch = partialMappings.FirstOrDefault(pm => pm.MatchResult.AverageTotalScore > 0.0); |
| | | 37 | | |
| | 1 | 38 | | return (bestPartialMatch?.Mapping, bestPartialMatch?.MatchResult); |
| | | 39 | | } |
| | | 40 | | |
| | 48 | 41 | | var perfectMatch = mappings |
| | 85 | 42 | | .OrderBy(m => m.Mapping.Priority) |
| | 329 | 43 | | .FirstOrDefault(m => m.MatchResult.IsPerfectMatch); |
| | | 44 | | |
| | 48 | 45 | | return (perfectMatch?.Mapping, perfectMatch?.MatchResult); |
| | 49 | 46 | | } |
| | | 47 | | } |
| | | 48 | | } |