using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using WireMock.Validation;
namespace WireMock.Matchers.Request
{
///
/// The request url matcher.
///
public class RequestMessageUrlMatcher : IRequestMatcher
{
///
/// The matcher.
///
public IReadOnlyList Matchers { get; }
///
/// The url functions
///
public Func[] Funcs { get; }
///
/// Initializes a new instance of the class.
///
/// The urls.
public RequestMessageUrlMatcher([NotNull] params string[] urls) : this(urls.Select(url => new WildcardMatcher(url)).ToArray())
{
}
///
/// Initializes a new instance of the class.
///
/// The matchers.
public RequestMessageUrlMatcher([NotNull] params IMatcher[] matchers)
{
Check.NotNull(matchers, nameof(matchers));
Matchers = matchers;
}
///
/// Initializes a new instance of the class.
///
/// The url functions.
public RequestMessageUrlMatcher([NotNull] params Func[] funcs)
{
Check.NotNull(funcs, nameof(funcs));
Funcs = funcs;
}
///
/// Determines whether the specified RequestMessage is match.
///
/// The RequestMessage.
/// The RequestMatchResult.
///
/// true if the specified RequestMessage is match; otherwise, false.
///
public bool IsMatch(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
{
bool isMatch = IsMatch(requestMessage);
if (isMatch)
requestMatchResult.Matched++;
requestMatchResult.Total++;
return isMatch;
}
private bool IsMatch(RequestMessage requestMessage)
{
if (Matchers != null)
return Matchers.Any(matcher => matcher.IsMatch(requestMessage.Url));
if (Funcs != null)
return requestMessage.Url != null && Funcs.Any(func => func(requestMessage.Url));
return false;
}
}
}