using System.Linq;
using System.Text.RegularExpressions;
using AnyOfTypes;
using JetBrains.Annotations;
using WireMock.Extensions;
using WireMock.Models;
namespace WireMock.Matchers
{
///
/// WildcardMatcher
///
///
public class WildcardMatcher : RegexMatcher
{
private readonly AnyOf[] _patterns;
///
/// Initializes a new instance of the class.
///
/// The pattern.
/// IgnoreCase
public WildcardMatcher([NotNull] AnyOf pattern, bool ignoreCase = false) : this(new[] { pattern }, ignoreCase)
{
}
///
/// Initializes a new instance of the class.
///
/// The match behaviour.
/// The pattern.
/// IgnoreCase
public WildcardMatcher(MatchBehaviour matchBehaviour, [NotNull] AnyOf pattern, bool ignoreCase = false) : this(matchBehaviour, new[] { pattern }, ignoreCase)
{
}
///
/// Initializes a new instance of the class.
///
/// The patterns.
/// IgnoreCase
public WildcardMatcher([NotNull] AnyOf[] patterns, bool ignoreCase = false) : this(MatchBehaviour.AcceptOnMatch, patterns, ignoreCase)
{
}
///
/// Initializes a new instance of the class.
///
/// The match behaviour.
/// The patterns.
/// IgnoreCase
/// Throw an exception when the internal matching fails because of invalid input.
public WildcardMatcher(MatchBehaviour matchBehaviour, [NotNull] AnyOf[] patterns, bool ignoreCase = false, bool throwException = false) :
base(matchBehaviour, CreateArray(patterns), ignoreCase, throwException)
{
_patterns = patterns;
}
///
public override AnyOf[] GetPatterns()
{
return _patterns;
}
///
public override string Name => "WildcardMatcher";
private static AnyOf[] CreateArray(AnyOf[] patterns)
{
return patterns.Select(pattern => new AnyOf(
new StringPattern
{
Pattern = "^" + Regex.Escape(pattern.GetPattern()).Replace(@"\*", ".*").Replace(@"\?", ".") + "$",
PatternAsFile = pattern.IsSecond ? pattern.Second.PatternAsFile : null
}))
.ToArray();
}
}
}