mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-19 23:11:20 +02:00
Increase code coverage (#107)
This commit is contained in:
@@ -2,9 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using JetBrains.Annotations;
|
||||
using SimMetrics.Net;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Matchers;
|
||||
using WireMock.Matchers.Request;
|
||||
using WireMock.RequestBuilders;
|
||||
using WireMock.ResponseBuilders;
|
||||
@@ -40,19 +38,19 @@ namespace WireMock.Serialization
|
||||
{
|
||||
ClientIP = clientIPMatchers != null && clientIPMatchers.Any() ? new ClientIPModel
|
||||
{
|
||||
Matchers = Map(clientIPMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers)),
|
||||
Matchers = MatcherMapper.Map(clientIPMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers)),
|
||||
Funcs = Map(clientIPMatchers.Where(m => m.Funcs != null).SelectMany(m => m.Funcs))
|
||||
} : null,
|
||||
|
||||
Path = pathMatchers != null && pathMatchers.Any() ? new PathModel
|
||||
{
|
||||
Matchers = Map(pathMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers)),
|
||||
Matchers = MatcherMapper.Map(pathMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers)),
|
||||
Funcs = Map(pathMatchers.Where(m => m.Funcs != null).SelectMany(m => m.Funcs))
|
||||
} : null,
|
||||
|
||||
Url = urlMatchers != null && urlMatchers.Any() ? new UrlModel
|
||||
{
|
||||
Matchers = Map(urlMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers)),
|
||||
Matchers = MatcherMapper.Map(urlMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers)),
|
||||
Funcs = Map(urlMatchers.Where(m => m.Funcs != null).SelectMany(m => m.Funcs))
|
||||
} : null,
|
||||
|
||||
@@ -61,14 +59,14 @@ namespace WireMock.Serialization
|
||||
Headers = headerMatchers != null && headerMatchers.Any() ? headerMatchers.Select(hm => new HeaderModel
|
||||
{
|
||||
Name = hm.Name,
|
||||
Matchers = Map(hm.Matchers),
|
||||
Matchers = MatcherMapper.Map(hm.Matchers),
|
||||
Funcs = Map(hm.Funcs)
|
||||
}).ToList() : null,
|
||||
|
||||
Cookies = cookieMatchers != null && cookieMatchers.Any() ? cookieMatchers.Select(cm => new CookieModel
|
||||
{
|
||||
Name = cm.Name,
|
||||
Matchers = Map(cm.Matchers),
|
||||
Matchers = MatcherMapper.Map(cm.Matchers),
|
||||
Funcs = Map(cm.Funcs)
|
||||
}).ToList() : null,
|
||||
|
||||
@@ -81,7 +79,7 @@ namespace WireMock.Serialization
|
||||
|
||||
Body = methodMatcher?.Methods != null && methodMatcher.Methods.Any(m => m == "get") ? null : new BodyModel
|
||||
{
|
||||
Matcher = bodyMatcher != null ? Map(bodyMatcher.Matcher) : null,
|
||||
Matcher = bodyMatcher != null ? MatcherMapper.Map(bodyMatcher.Matcher) : null,
|
||||
Func = bodyMatcher != null ? Map(bodyMatcher.Func) : null,
|
||||
DataFunc = bodyMatcher != null ? Map(bodyMatcher.DataFunc) : null
|
||||
}
|
||||
@@ -149,87 +147,16 @@ namespace WireMock.Serialization
|
||||
return newDictionary;
|
||||
}
|
||||
|
||||
private static MatcherModel[] Map([CanBeNull] IEnumerable<IMatcher> matchers)
|
||||
{
|
||||
if (matchers == null || !matchers.Any())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return matchers.Select(Map).Where(x => x != null).ToArray();
|
||||
}
|
||||
|
||||
private static MatcherModel Map([CanBeNull] IMatcher matcher)
|
||||
{
|
||||
if (matcher == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
IStringMatcher stringMatcher = matcher as IStringMatcher;
|
||||
string[] patterns = stringMatcher != null ? stringMatcher.GetPatterns() : new string[0];
|
||||
|
||||
return new MatcherModel
|
||||
{
|
||||
Name = matcher.GetName(),
|
||||
Pattern = patterns.Length == 1 ? patterns.First() : null,
|
||||
Patterns = patterns.Length > 1 ? patterns : null
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
private static string[] Map<T>([CanBeNull] IEnumerable<Func<T, bool>> funcs)
|
||||
{
|
||||
if (funcs == null || !funcs.Any())
|
||||
return null;
|
||||
|
||||
return funcs.Select(Map).Where(x => x != null).ToArray();
|
||||
return funcs?.Select(Map).Where(x => x != null).ToArray();
|
||||
}
|
||||
|
||||
private static string Map<T>([CanBeNull] Func<T, bool> func)
|
||||
{
|
||||
return func?.ToString();
|
||||
}
|
||||
|
||||
public static IMatcher Map([CanBeNull] MatcherModel matcher)
|
||||
{
|
||||
if (matcher == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var parts = matcher.Name.Split('.');
|
||||
string matcherName = parts[0];
|
||||
string matcherType = parts.Length > 1 ? parts[1] : null;
|
||||
|
||||
string[] patterns = matcher.Patterns ?? new[] { matcher.Pattern };
|
||||
|
||||
switch (matcherName)
|
||||
{
|
||||
case "ExactMatcher":
|
||||
return new ExactMatcher(patterns);
|
||||
|
||||
case "RegexMatcher":
|
||||
return new RegexMatcher(patterns);
|
||||
|
||||
case "JsonPathMatcher":
|
||||
return new JsonPathMatcher(patterns);
|
||||
|
||||
case "XPathMatcher":
|
||||
return new XPathMatcher(matcher.Pattern);
|
||||
|
||||
case "WildcardMatcher":
|
||||
return new WildcardMatcher(patterns, 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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
34
src/WireMock.Net/Serialization/MatcherMapper.cs
Normal file
34
src/WireMock.Net/Serialization/MatcherMapper.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using JetBrains.Annotations;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Matchers;
|
||||
|
||||
namespace WireMock.Serialization
|
||||
{
|
||||
internal static class MatcherMapper
|
||||
{
|
||||
public static MatcherModel[] Map([CanBeNull] IEnumerable<IMatcher> matchers)
|
||||
{
|
||||
return matchers?.Select(Map).Where(x => x != null).ToArray();
|
||||
}
|
||||
|
||||
public static MatcherModel Map([CanBeNull] IMatcher matcher)
|
||||
{
|
||||
if (matcher == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
IStringMatcher stringMatcher = matcher as IStringMatcher;
|
||||
string[] patterns = stringMatcher != null ? stringMatcher.GetPatterns() : new string[0];
|
||||
|
||||
return new MatcherModel
|
||||
{
|
||||
Name = matcher.GetName(),
|
||||
Pattern = patterns.Length == 1 ? patterns.First() : null,
|
||||
Patterns = patterns.Length > 1 ? patterns : null
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
55
src/WireMock.Net/Serialization/MatcherModelMapper.cs
Normal file
55
src/WireMock.Net/Serialization/MatcherModelMapper.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using JetBrains.Annotations;
|
||||
using SimMetrics.Net;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Matchers;
|
||||
|
||||
namespace WireMock.Serialization
|
||||
{
|
||||
internal static class MatcherModelMapper
|
||||
{
|
||||
public static IMatcher Map([CanBeNull] MatcherModel matcher)
|
||||
{
|
||||
if (matcher == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
string[] parts = matcher.Name.Split('.');
|
||||
string matcherName = parts[0];
|
||||
string matcherType = parts.Length > 1 ? parts[1] : null;
|
||||
|
||||
string[] patterns = matcher.Patterns ?? new[] { matcher.Pattern };
|
||||
|
||||
switch (matcherName)
|
||||
{
|
||||
case "ExactMatcher":
|
||||
return new ExactMatcher(patterns);
|
||||
|
||||
case "RegexMatcher":
|
||||
return new RegexMatcher(patterns, matcher.IgnoreCase == true);
|
||||
|
||||
case "JsonPathMatcher":
|
||||
return new JsonPathMatcher(patterns);
|
||||
|
||||
case "XPathMatcher":
|
||||
return new XPathMatcher(matcher.Pattern);
|
||||
|
||||
case "WildcardMatcher":
|
||||
return new WildcardMatcher(patterns, 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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user