using System;
using JetBrains.Annotations;
using WireMock.Validation;
namespace WireMock.Matchers.Request
{
///
/// The request body matcher.
///
public class RequestMessageBodyMatcher : IRequestMatcher
{
///
/// The bodyRegex.
///
private readonly byte[] _bodyData;
///
/// The matcher.
///
private readonly IMatcher _matcher;
///
/// The body function
///
private readonly Func _bodyFunc;
///
/// The body data function
///
private readonly Func _bodyDataFunc;
///
/// Initializes a new instance of the class.
///
///
/// The body Regex pattern.
///
public RequestMessageBodyMatcher([NotNull, RegexPattern] string body)
{
Check.NotNull(body, nameof(body));
_matcher = new RegexMatcher(body);
}
///
/// Initializes a new instance of the class.
///
///
/// The body Regex pattern.
///
public RequestMessageBodyMatcher([NotNull] byte[] body)
{
Check.NotNull(body, nameof(body));
_bodyData = body;
}
///
/// Initializes a new instance of the class.
///
///
/// The body func.
///
public RequestMessageBodyMatcher([NotNull] Func func)
{
Check.NotNull(func, nameof(func));
_bodyFunc = func;
}
///
/// Initializes a new instance of the class.
///
///
/// The body func.
///
public RequestMessageBodyMatcher([NotNull] Func func)
{
Check.NotNull(func, nameof(func));
_bodyDataFunc = func;
}
///
/// Initializes a new instance of the class.
///
///
/// The body matcher.
///
public RequestMessageBodyMatcher([NotNull] IMatcher matcher)
{
Check.NotNull(matcher, nameof(matcher));
_matcher = matcher;
}
///
/// Determines whether the specified RequestMessage is match.
///
/// The RequestMessage.
///
/// true if the specified RequestMessage is match; otherwise, false.
///
public bool IsMatch(RequestMessage requestMessage)
{
if (_matcher != null)
return _matcher.IsMatch(requestMessage.Body);
if (_bodyData != null)
return requestMessage.BodyAsBytes == _bodyData;
if (_bodyFunc != null)
return _bodyFunc(requestMessage.Body);
if (_bodyDataFunc != null)
return _bodyDataFunc(requestMessage.BodyAsBytes);
return false;
}
}
}