mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-22 17:19:00 +01:00
* Create WireMock.Net.MimePart project * . * REFACTOR * ILRepack * -- * ... * x * x * . * fix * public class MimePartMatcher * shared * min * . * <!--<DelaySign>true</DelaySign>--> * Update README.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
97 lines
3.5 KiB
C#
97 lines
3.5 KiB
C#
// Copyright © WireMock.Net
|
|
|
|
using System.Linq;
|
|
using System.Text.RegularExpressions;
|
|
using AnyOfTypes;
|
|
using Stef.Validation;
|
|
using WireMock.Extensions;
|
|
using WireMock.Models;
|
|
using WireMock.Util;
|
|
|
|
namespace WireMock.Matchers;
|
|
|
|
/// <summary>
|
|
/// WildcardMatcher
|
|
/// </summary>
|
|
/// <seealso cref="RegexMatcher" />
|
|
public class WildcardMatcher : RegexMatcher
|
|
{
|
|
private readonly AnyOf<string, StringPattern>[] _patterns;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="WildcardMatcher"/> class.
|
|
/// </summary>
|
|
/// <param name="pattern">The pattern.</param>
|
|
/// <param name="ignoreCase">IgnoreCase</param>
|
|
public WildcardMatcher(AnyOf<string, StringPattern> pattern, bool ignoreCase = false) : this(new[] { pattern }, ignoreCase)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="WildcardMatcher"/> class.
|
|
/// </summary>
|
|
/// <param name="matchBehaviour">The match behaviour.</param>
|
|
/// <param name="pattern">The pattern.</param>
|
|
/// <param name="ignoreCase">IgnoreCase</param>
|
|
public WildcardMatcher(MatchBehaviour matchBehaviour, AnyOf<string, StringPattern> pattern, bool ignoreCase = false) : this(matchBehaviour, new[] { pattern }, ignoreCase)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="WildcardMatcher"/> class.
|
|
/// </summary>
|
|
/// <param name="patterns">The patterns.</param>
|
|
/// <param name="ignoreCase">IgnoreCase</param>
|
|
public WildcardMatcher(AnyOf<string, StringPattern>[] patterns, bool ignoreCase = false) : this(MatchBehaviour.AcceptOnMatch, patterns, ignoreCase)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="WildcardMatcher"/> class.
|
|
/// </summary>
|
|
/// <param name="matchBehaviour">The match behaviour.</param>
|
|
/// <param name="patterns">The patterns.</param>
|
|
/// <param name="ignoreCase">IgnoreCase</param>
|
|
/// <param name="matchOperator">The <see cref="MatchOperator"/> to use. (default = "Or")</param>
|
|
public WildcardMatcher(
|
|
MatchBehaviour matchBehaviour,
|
|
AnyOf<string, StringPattern>[] patterns,
|
|
bool ignoreCase = false,
|
|
MatchOperator matchOperator = MatchOperator.Or) : base(matchBehaviour, CreateArray(patterns), ignoreCase, true, matchOperator)
|
|
{
|
|
_patterns = Guard.NotNull(patterns);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override AnyOf<string, StringPattern>[] GetPatterns()
|
|
{
|
|
return _patterns;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override string Name => nameof(WildcardMatcher);
|
|
|
|
/// <inheritdoc />
|
|
public override string GetCSharpCodeArguments()
|
|
{
|
|
return $"new {Name}" +
|
|
$"(" +
|
|
$"{MatchBehaviour.GetFullyQualifiedEnumValue()}, " +
|
|
$"{MappingConverterUtils.ToCSharpCodeArguments(_patterns)}, " +
|
|
$"{CSharpFormatter.ToCSharpBooleanLiteral(IgnoreCase)}, " +
|
|
$"{MatchOperator.GetFullyQualifiedEnumValue()}" +
|
|
$")";
|
|
}
|
|
|
|
private static AnyOf<string, StringPattern>[] CreateArray(AnyOf<string, StringPattern>[] patterns)
|
|
{
|
|
return patterns
|
|
.Select(pattern => new AnyOf<string, StringPattern>(
|
|
new StringPattern
|
|
{
|
|
Pattern = "^" + Regex.Escape(pattern.GetPattern()).Replace(@"\*", ".*").Replace(@"\?", ".") + "$",
|
|
PatternAsFile = pattern.IsSecond ? pattern.Second.PatternAsFile : null
|
|
}))
|
|
.ToArray();
|
|
}
|
|
} |