mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-20 16:23:42 +01:00
* wip * fix * . * windows-2019 * <Target Name="CheckIfShouldKillVBCSCompiler" /> * <!--https://github.com/aspnet/RoslynCodeDomProvider/issues/51--> * AllowCSharpCodeMatcher * CSharpCodeMatcher : IObjectMatcher * TemplateForIsMatchWithDynamic * RequestMessageBodyMatcher_GetMatchingScore_BodyAsJson_CSharpCodeMatcher * fix * } * Better Exception Handling
133 lines
4.3 KiB
C#
133 lines
4.3 KiB
C#
using System.Linq;
|
|
using System.Linq.Dynamic.Core;
|
|
using JetBrains.Annotations;
|
|
using Newtonsoft.Json.Linq;
|
|
using WireMock.Util;
|
|
|
|
namespace WireMock.Matchers
|
|
{
|
|
/// <summary>
|
|
/// System.Linq.Dynamic.Core Expression Matcher
|
|
/// </summary>
|
|
/// <inheritdoc cref="IObjectMatcher"/>
|
|
/// <inheritdoc cref="IStringMatcher"/>
|
|
public class LinqMatcher : IObjectMatcher, IStringMatcher
|
|
{
|
|
private readonly string[] _patterns;
|
|
|
|
/// <inheritdoc cref="IMatcher.MatchBehaviour"/>
|
|
public MatchBehaviour MatchBehaviour { get; }
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="LinqMatcher"/> class.
|
|
/// </summary>
|
|
/// <param name="pattern">The pattern.</param>
|
|
public LinqMatcher([NotNull] string pattern) : this(new[] { pattern })
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="LinqMatcher"/> class.
|
|
/// </summary>
|
|
/// <param name="patterns">The patterns.</param>
|
|
public LinqMatcher([NotNull] string[] patterns) : this(MatchBehaviour.AcceptOnMatch, patterns)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="LinqMatcher"/> class.
|
|
/// </summary>
|
|
/// <param name="matchBehaviour">The match behaviour.</param>
|
|
/// <param name="pattern">The pattern.</param>
|
|
public LinqMatcher(MatchBehaviour matchBehaviour, [NotNull] string pattern) : this(matchBehaviour, new[] { pattern })
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="LinqMatcher"/> class.
|
|
/// </summary>
|
|
/// <param name="matchBehaviour">The match behaviour.</param>
|
|
/// <param name="patterns">The patterns.</param>
|
|
public LinqMatcher(MatchBehaviour matchBehaviour, [NotNull] string[] patterns)
|
|
{
|
|
MatchBehaviour = matchBehaviour;
|
|
_patterns = patterns;
|
|
}
|
|
|
|
/// <inheritdoc cref="IStringMatcher.IsMatch"/>
|
|
public double IsMatch(string input)
|
|
{
|
|
double match = MatchScores.Mismatch;
|
|
|
|
// Convert a single input string to a Queryable string-list with 1 entry.
|
|
IQueryable queryable = new[] { input }.AsQueryable();
|
|
|
|
try
|
|
{
|
|
// Use the Any(...) method to check if the result matches
|
|
match = MatchScores.ToScore(_patterns.Select(pattern => queryable.Any(pattern)));
|
|
|
|
return MatchBehaviourHelper.Convert(MatchBehaviour, match);
|
|
}
|
|
catch
|
|
{
|
|
// just ignore exception
|
|
// TODO add logging?
|
|
}
|
|
|
|
return MatchBehaviourHelper.Convert(MatchBehaviour, match);
|
|
}
|
|
|
|
/// <inheritdoc cref="IObjectMatcher.IsMatch"/>
|
|
public double IsMatch(object input)
|
|
{
|
|
double match = MatchScores.Mismatch;
|
|
|
|
JObject value;
|
|
switch (input)
|
|
{
|
|
case JObject valueAsJObject:
|
|
value = valueAsJObject;
|
|
break;
|
|
|
|
default:
|
|
value = JObject.FromObject(input);
|
|
break;
|
|
}
|
|
|
|
// Convert a single object to a Queryable JObject-list with 1 entry.
|
|
var queryable1 = new[] { value }.AsQueryable();
|
|
|
|
try
|
|
{
|
|
// Generate the DynamicLinq select statement.
|
|
string dynamicSelect = JsonUtils.GenerateDynamicLinqStatement(value);
|
|
|
|
// Execute DynamicLinq Select statement.
|
|
var queryable2 = queryable1.Select(dynamicSelect);
|
|
|
|
// Use the Any(...) method to check if the result matches.
|
|
match = MatchScores.ToScore(_patterns.Select(pattern => queryable2.Any(pattern)));
|
|
|
|
return MatchBehaviourHelper.Convert(MatchBehaviour, match);
|
|
}
|
|
catch
|
|
{
|
|
// just ignore exception
|
|
// TODO add logging?
|
|
}
|
|
|
|
return MatchBehaviourHelper.Convert(MatchBehaviour, match);
|
|
}
|
|
|
|
/// <inheritdoc cref="IStringMatcher.GetPatterns"/>
|
|
public string[] GetPatterns()
|
|
{
|
|
return _patterns;
|
|
}
|
|
|
|
/// <inheritdoc cref="IMatcher.Name"/>
|
|
public string Name => "LinqMatcher";
|
|
}
|
|
}
|