using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using JetBrains.Annotations;
using WireMock.Validation;
namespace WireMock.Matchers.Request
{
///
/// The request header matcher.
///
public class RequestMessageHeaderMatcher : IRequestMatcher
{
///
/// The name.
///
private readonly string _name;
///
/// The patternRegex.
///
private readonly Regex _patternRegex;
///
/// The header function
///
private readonly Func, bool> _headerFunc;
///
/// Initializes a new instance of the class.
///
///
/// The name.
///
///
/// The pattern.
///
/// The ignoreCase.
public RequestMessageHeaderMatcher([NotNull] string name, [NotNull, RegexPattern] string pattern, bool ignoreCase = true)
{
_name = name;
_patternRegex = ignoreCase ? new Regex(pattern, RegexOptions.IgnoreCase) : new Regex(pattern);
}
///
/// Initializes a new instance of the class.
///
///
/// The func.
///
public RequestMessageHeaderMatcher([NotNull] Func, bool> func)
{
Check.NotNull(func, nameof(func));
_headerFunc = func;
}
///
/// Determines whether the specified RequestMessage is match.
///
/// The RequestMessage.
///
/// true if the specified RequestMessage is match; otherwise, false.
///
public bool IsMatch(RequestMessage requestMessage)
{
if (_patternRegex == null)
return _headerFunc(requestMessage.Headers);
string headerValue = requestMessage.Headers[_name];
return _patternRegex.IsMatch(headerValue);
}
}
}