Move some classes and restructure.

This commit is contained in:
Stef Heyenrath
2017-01-20 12:07:29 +01:00
parent 847745c256
commit e2552f03b9
28 changed files with 471 additions and 583 deletions
@@ -0,0 +1,19 @@
using JetBrains.Annotations;
namespace WireMock.Matchers.Request
{
/// <summary>
/// The RequestMatcher interface.
/// </summary>
public interface IRequestMatcher
{
/// <summary>
/// Determines whether the specified RequestMessage is match.
/// </summary>
/// <param name="requestMessage">The RequestMessage.</param>
/// <returns>
/// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>.
/// </returns>
bool IsMatch([NotNull] RequestMessage requestMessage);
}
}
@@ -0,0 +1,116 @@
using System;
using JetBrains.Annotations;
using WireMock.Validation;
namespace WireMock.Matchers.Request
{
/// <summary>
/// The request body matcher.
/// </summary>
public class RequestMessageBodyMatcher : IRequestMatcher
{
/// <summary>
/// The bodyRegex.
/// </summary>
private readonly byte[] _bodyData;
/// <summary>
/// The matcher.
/// </summary>
private readonly IMatcher _matcher;
/// <summary>
/// The body function
/// </summary>
private readonly Func<string, bool> _bodyFunc;
/// <summary>
/// The body data function
/// </summary>
private readonly Func<byte[], bool> _bodyDataFunc;
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
/// </summary>
/// <param name="body">
/// The body Regex pattern.
/// </param>
public RequestMessageBodyMatcher([NotNull, RegexPattern] string body)
{
Check.NotNull(body, nameof(body));
_matcher = new RegexMatcher(body);
}
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
/// </summary>
/// <param name="body">
/// The body Regex pattern.
/// </param>
public RequestMessageBodyMatcher([NotNull] byte[] body)
{
Check.NotNull(body, nameof(body));
_bodyData = body;
}
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
/// </summary>
/// <param name="func">
/// The body func.
/// </param>
public RequestMessageBodyMatcher([NotNull] Func<string, bool> func)
{
Check.NotNull(func, nameof(func));
_bodyFunc = func;
}
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
/// </summary>
/// <param name="func">
/// The body func.
/// </param>
public RequestMessageBodyMatcher([NotNull] Func<byte[], bool> func)
{
Check.NotNull(func, nameof(func));
_bodyDataFunc = func;
}
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
/// </summary>
/// <param name="matcher">
/// The body matcher.
/// </param>
public RequestMessageBodyMatcher([NotNull] IMatcher matcher)
{
Check.NotNull(matcher, nameof(matcher));
_matcher = matcher;
}
/// <summary>
/// Determines whether the specified RequestMessage is match.
/// </summary>
/// <param name="requestMessage">The RequestMessage.</param>
/// <returns>
/// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>.
/// </returns>
public bool IsMatch(RequestMessage requestMessage)
{
if (_matcher != null)
return _matcher.IsMatch(requestMessage.BodyAsString);
if (_bodyData != null)
return requestMessage.Body == _bodyData;
if (_bodyFunc != null)
return _bodyFunc(requestMessage.BodyAsString);
if (_bodyDataFunc != null)
return _bodyDataFunc(requestMessage.Body);
return false;
}
}
}
@@ -0,0 +1,37 @@
using System.Collections.Generic;
using System.Linq;
namespace WireMock.Matchers.Request
{
/// <summary>
/// The composite request matcher.
/// </summary>
public class RequestMessageCompositeMatcher : IRequestMatcher
{
private readonly IEnumerable<IRequestMatcher> _requestMatchers;
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageCompositeMatcher"/> class.
/// The constructor.
/// </summary>
/// <param name="requestMatchers">
/// The <see cref="IEnumerable&lt;IRequestMatcher&gt;"/> request matchers.
/// </param>
public RequestMessageCompositeMatcher(IEnumerable<IRequestMatcher> requestMatchers)
{
_requestMatchers = requestMatchers;
}
/// <summary>
/// Determines whether the specified RequestMessage is match.
/// </summary>
/// <param name="requestMessage">The RequestMessage.</param>
/// <returns>
/// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>.
/// </returns>
public bool IsMatch(RequestMessage requestMessage)
{
return _requestMatchers.All(spec => spec.IsMatch(requestMessage));
}
}
}
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using JetBrains.Annotations;
using WireMock.Validation;
namespace WireMock.Matchers.Request
{
/// <summary>
/// The request header matcher.
/// </summary>
public class RequestMessageHeaderMatcher : IRequestMatcher
{
/// <summary>
/// The name.
/// </summary>
private readonly string _name;
/// <summary>
/// The patternRegex.
/// </summary>
private readonly Regex _patternRegex;
/// <summary>
/// The header function
/// </summary>
private readonly Func<IDictionary<string, string>, bool> _headerFunc;
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageHeaderMatcher"/> class.
/// </summary>
/// <param name="name">
/// The name.
/// </param>
/// <param name="pattern">
/// The pattern.
/// </param>
/// <param name="ignoreCase">The ignoreCase.</param>
public RequestMessageHeaderMatcher([NotNull] string name, [NotNull, RegexPattern] string pattern, bool ignoreCase = true)
{
_name = name;
_patternRegex = ignoreCase ? new Regex(pattern, RegexOptions.IgnoreCase) : new Regex(pattern);
}
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageHeaderMatcher"/> class.
/// </summary>
/// <param name="func">
/// The func.
/// </param>
public RequestMessageHeaderMatcher([NotNull] Func<IDictionary<string, string>, bool> func)
{
Check.NotNull(func, nameof(func));
_headerFunc = func;
}
/// <summary>
/// Determines whether the specified RequestMessage is match.
/// </summary>
/// <param name="requestMessage">The RequestMessage.</param>
/// <returns>
/// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>.
/// </returns>
public bool IsMatch(RequestMessage requestMessage)
{
if (_patternRegex == null)
return _headerFunc(requestMessage.Headers);
string headerValue = requestMessage.Headers[_name];
return _patternRegex.IsMatch(headerValue);
}
}
}
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using WireMock.Validation;
namespace WireMock.Matchers.Request
{
/// <summary>
/// The request parameters matcher.
/// </summary>
public class RequestMessageParamMatcher : IRequestMatcher
{
/// <summary>
/// The _key.
/// </summary>
private readonly string _key;
/// <summary>
/// The _values.
/// </summary>
private readonly IEnumerable<string> _values;
private readonly Func<IDictionary<string, List<string>>, bool> _func;
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageParamMatcher"/> class.
/// </summary>
/// <param name="key">
/// The key.
/// </param>
/// <param name="values">
/// The values.
/// </param>
public RequestMessageParamMatcher([NotNull] string key, [NotNull] IEnumerable<string> values)
{
Check.NotNull(key, nameof(key));
Check.NotNull(values, nameof(values));
_key = key;
_values = values;
}
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageParamMatcher"/> class.
/// </summary>
/// <param name="func">
/// The func.
/// </param>
public RequestMessageParamMatcher([NotNull] Func<IDictionary<string, List<string>>, bool> func)
{
Check.NotNull(func, nameof(func));
_func = func;
}
/// <summary>
/// Determines whether the specified RequestMessage is match.
/// </summary>
/// <param name="requestMessage">The RequestMessage.</param>
/// <returns>
/// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>.
/// </returns>
public bool IsMatch(RequestMessage requestMessage)
{
if (_func != null)
{
return _func(requestMessage.Parameters);
}
return requestMessage.GetParameter(_key).Intersect(_values).Count() == _values.Count();
}
}
}
@@ -0,0 +1,59 @@
using System;
using System.Text.RegularExpressions;
using JetBrains.Annotations;
using WireMock.Validation;
namespace WireMock.Matchers.Request
{
/// <summary>
/// The request path matcher.
/// </summary>
public class RequestMessagePathMatcher : IRequestMatcher
{
/// <summary>
/// The pathRegex.
/// </summary>
private readonly Regex _pathRegex;
/// <summary>
/// The url function
/// </summary>
private readonly Func<string, bool> _pathFunc;
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessagePathMatcher"/> class.
/// </summary>
/// <param name="path">
/// The path Regex pattern.
/// </param>
public RequestMessagePathMatcher([NotNull, RegexPattern] string path)
{
Check.NotNull(path, nameof(path));
_pathRegex = new Regex(path);
}
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessagePathMatcher"/> class.
/// </summary>
/// <param name="func">
/// The url func.
/// </param>
public RequestMessagePathMatcher([NotNull] Func<string, bool> func)
{
Check.NotNull(func, nameof(func));
_pathFunc = func;
}
/// <summary>
/// Determines whether the specified RequestMessage is match.
/// </summary>
/// <param name="requestMessage">The RequestMessage.</param>
/// <returns>
/// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>.
/// </returns>
public bool IsMatch(RequestMessage requestMessage)
{
return _pathRegex?.IsMatch(requestMessage.Path) ?? _pathFunc(requestMessage.Path);
}
}
}
@@ -0,0 +1,59 @@
using System;
using System.Text.RegularExpressions;
using JetBrains.Annotations;
using WireMock.Validation;
namespace WireMock.Matchers.Request
{
/// <summary>
/// The request url matcher.
/// </summary>
public class RequestMessageUrlMatcher : IRequestMatcher
{
/// <summary>
/// The urlRegex.
/// </summary>
private readonly Regex _urlRegex;
/// <summary>
/// The url function
/// </summary>
private readonly Func<string, bool> _urlFunc;
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageUrlMatcher"/> class.
/// </summary>
/// <param name="url">
/// The url Regex pattern.
/// </param>
public RequestMessageUrlMatcher([NotNull, RegexPattern] string url)
{
Check.NotNull(url, nameof(url));
_urlRegex = new Regex(url);
}
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageUrlMatcher"/> class.
/// </summary>
/// <param name="func">
/// The url func.
/// </param>
public RequestMessageUrlMatcher(Func<string, bool> func)
{
Check.NotNull(func, nameof(func));
_urlFunc = func;
}
/// <summary>
/// Determines whether the specified RequestMessage is match.
/// </summary>
/// <param name="requestMessage">The RequestMessage.</param>
/// <returns>
/// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>.
/// </returns>
public bool IsMatch(RequestMessage requestMessage)
{
return _urlRegex?.IsMatch(requestMessage.Url) ?? _urlFunc(requestMessage.Url);
}
}
}
@@ -0,0 +1,40 @@
using JetBrains.Annotations;
using WireMock.Validation;
namespace WireMock.Matchers.Request
{
/// <summary>
/// The request verb matcher.
/// </summary>
internal class RequestMessageVerbMatcher : IRequestMatcher
{
/// <summary>
/// The _verb.
/// </summary>
private readonly string _verb;
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageVerbMatcher"/> class.
/// </summary>
/// <param name="verb">
/// The verb.
/// </param>
public RequestMessageVerbMatcher([NotNull] string verb)
{
Check.NotNull(verb, nameof(verb));
_verb = verb.ToLower();
}
/// <summary>
/// Determines whether the specified RequestMessage is match.
/// </summary>
/// <param name="requestMessage">The RequestMessage.</param>
/// <returns>
/// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>.
/// </returns>
public bool IsMatch(RequestMessage requestMessage)
{
return requestMessage.Verb == _verb;
}
}
}