GET "/__admin/mappings"

This commit is contained in:
Stef Heyenrath
2017-01-24 22:06:25 +01:00
parent 45aa83ee91
commit 3f84ba8b20
44 changed files with 805 additions and 302 deletions
@@ -19,11 +19,6 @@ namespace WireMock.Matchers.Request
/// </summary>
private readonly byte[] _bodyData;
/// <summary>
/// The matcher.
/// </summary>
private readonly IMatcher _matcher;
/// <summary>
/// The body function
/// </summary>
@@ -34,6 +29,11 @@ namespace WireMock.Matchers.Request
/// </summary>
private readonly Func<byte[], bool> _bodyDataFunc;
/// <summary>
/// The matcher.
/// </summary>
public readonly IMatcher Matcher;
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
/// </summary>
@@ -91,7 +91,7 @@ namespace WireMock.Matchers.Request
public RequestMessageBodyMatcher([NotNull] IMatcher matcher)
{
Check.NotNull(matcher, nameof(matcher));
_matcher = matcher;
Matcher = matcher;
}
/// <summary>
@@ -103,8 +103,8 @@ namespace WireMock.Matchers.Request
/// </returns>
public bool IsMatch(RequestMessage requestMessage)
{
if (_matcher != null)
return _matcher.IsMatch(requestMessage.Body);
if (Matcher != null)
return Matcher.IsMatch(requestMessage.Body);
if (_body != null)
return requestMessage.Body == _body;
@@ -1,13 +1,14 @@
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using WireMock.Validation;
namespace WireMock.Matchers.Request
{
/// <summary>
/// The composite request matcher.
/// </summary>
public class RequestMessageCompositeMatcher : IRequestMatcher
public abstract class RequestMessageCompositeMatcher : IRequestMatcher
{
private readonly CompositeMatcherType _type;
@@ -17,15 +18,17 @@ namespace WireMock.Matchers.Request
/// <value>
/// The request matchers.
/// </value>
public IEnumerable<IRequestMatcher> RequestMatchers { get; }
private IEnumerable<IRequestMatcher> RequestMatchers { get; }
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageCompositeMatcher"/> class.
/// </summary>
/// <param name="requestMatchers">The request matchers.</param>
/// <param name="type">The CompositeMatcherType type (Defaults to 'And')</param>
public RequestMessageCompositeMatcher([NotNull] IEnumerable<IRequestMatcher> requestMatchers, CompositeMatcherType type = CompositeMatcherType.And)
protected RequestMessageCompositeMatcher([NotNull] IEnumerable<IRequestMatcher> requestMatchers, CompositeMatcherType type = CompositeMatcherType.And)
{
Check.NotNull(requestMatchers, nameof(requestMatchers));
_type = type;
RequestMatchers = requestMatchers;
}
@@ -37,11 +40,11 @@ namespace WireMock.Matchers.Request
/// <returns>
/// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>.
/// </returns>
public bool IsMatch(RequestMessage requestMessage)
public virtual bool IsMatch(RequestMessage requestMessage)
{
return _type == CompositeMatcherType.And ?
RequestMatchers.All(spec => spec.IsMatch(requestMessage)) :
RequestMatchers.Any(spec => spec.IsMatch(requestMessage));
RequestMatchers.All(matcher => matcher.IsMatch(requestMessage)) :
RequestMatchers.Any(matcher => matcher.IsMatch(requestMessage));
}
}
}
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using WireMock.Validation;
@@ -10,12 +11,21 @@ namespace WireMock.Matchers.Request
/// </summary>
public class RequestMessageCookieMatcher : IRequestMatcher
{
private readonly string _name;
private readonly Func<IDictionary<string, string>, bool>[] _cookieFuncs;
private readonly IMatcher _matcher;
private readonly Func<IDictionary<string, string>, bool> _cookieFunc;
/// <summary>
/// The name
/// </summary>
public string Name { get; }
/// <summary>
/// Gets the matchers.
/// </summary>
/// <value>
/// The matchers.
/// </value>
public IMatcher[] Matchers { get; }
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageCookieMatcher"/> class.
/// </summary>
@@ -27,20 +37,18 @@ namespace WireMock.Matchers.Request
Check.NotNull(name, nameof(name));
Check.NotNull(pattern, nameof(pattern));
_name = name;
_matcher = new WildcardMatcher(pattern, ignoreCase);
Name = name;
Matchers = new IMatcher[] { new WildcardMatcher(pattern, ignoreCase) };
}
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageCookieMatcher"/> class.
/// </summary>
/// <param name="func">
/// The func.
/// </param>
public RequestMessageCookieMatcher([NotNull] Func<IDictionary<string, string>, bool> func)
/// <param name="funcs">The funcs.</param>
public RequestMessageCookieMatcher([NotNull] params Func<IDictionary<string, string>, bool>[] funcs)
{
Check.NotNull(func, nameof(func));
_cookieFunc = func;
Check.NotNull(funcs, nameof(funcs));
_cookieFuncs = funcs;
}
/// <summary>
@@ -52,14 +60,14 @@ namespace WireMock.Matchers.Request
/// </returns>
public bool IsMatch(RequestMessage requestMessage)
{
if (_cookieFunc != null)
return _cookieFunc(requestMessage.Cookies);
if (_cookieFuncs != null)
return _cookieFuncs.Any(cf => cf(requestMessage.Cookies));
if (requestMessage.Cookies == null)
return false;
string headerValue = requestMessage.Cookies[_name];
return _matcher.IsMatch(headerValue);
string headerValue = requestMessage.Cookies[Name];
return Matchers.Any(m => m.IsMatch(headerValue));
}
}
}
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using WireMock.Validation;
@@ -10,20 +11,20 @@ namespace WireMock.Matchers.Request
/// </summary>
public class RequestMessageHeaderMatcher : IRequestMatcher
{
/// <summary>
/// The name.
/// </summary>
private readonly string _name;
private readonly Func<IDictionary<string, string>, bool>[] _headerFuncs;
/// <summary>
/// The matcher.
/// The name
/// </summary>
private readonly IMatcher _matcher;
public string Name { get; }
/// <summary>
/// The header function
/// Gets the matchers.
/// </summary>
private readonly Func<IDictionary<string, string>, bool> _headerFunc;
/// <value>
/// The matchers.
/// </value>
public IMatcher[] Matchers { get; }
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageHeaderMatcher"/> class.
@@ -36,20 +37,18 @@ namespace WireMock.Matchers.Request
Check.NotNull(name, nameof(name));
Check.NotNull(pattern, nameof(pattern));
_name = name;
_matcher = new WildcardMatcher(pattern, ignoreCase);
Name = name;
Matchers = new IMatcher[] { new WildcardMatcher(pattern, ignoreCase) };
}
/// <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)
/// <param name="funcs">The funcs.</param>
public RequestMessageHeaderMatcher([NotNull] params Func<IDictionary<string, string>, bool>[] funcs)
{
Check.NotNull(func, nameof(func));
_headerFunc = func;
Check.NotNull(funcs, nameof(funcs));
_headerFuncs = funcs;
}
/// <summary>
@@ -61,14 +60,14 @@ namespace WireMock.Matchers.Request
/// </returns>
public bool IsMatch(RequestMessage requestMessage)
{
if (_headerFunc != null)
return _headerFunc(requestMessage.Headers);
if (_headerFuncs != null)
return _headerFuncs.Any(hf => hf(requestMessage.Headers));
if (requestMessage.Headers == null)
return false;
string headerValue = requestMessage.Headers[_name];
return _matcher.IsMatch(headerValue);
string headerValue = requestMessage.Headers[Name];
return Matchers.Any(m => m.IsMatch(headerValue));
}
}
}
@@ -12,17 +12,17 @@ namespace WireMock.Matchers.Request
/// </summary>
public class RequestMessageParamMatcher : IRequestMatcher
{
/// <summary>
/// The _key.
/// </summary>
private readonly string _key;
private readonly Func<IDictionary<string, WireMockList<string>>, bool>[] _funcs;
/// <summary>
/// The _values.
/// The key
/// </summary>
private readonly IEnumerable<string> _values;
public string Key { get; }
private readonly Func<IDictionary<string, WireMockList<string>>, bool> _func;
/// <summary>
/// The values
/// </summary>
public IEnumerable<string> Values { get; }
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageParamMatcher"/> class.
@@ -38,20 +38,18 @@ namespace WireMock.Matchers.Request
Check.NotNull(key, nameof(key));
Check.NotNull(values, nameof(values));
_key = key;
_values = 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, WireMockList<string>>, bool> func)
/// <param name="funcs">The funcs.</param>
public RequestMessageParamMatcher([NotNull] params Func<IDictionary<string, WireMockList<string>>, bool>[] funcs)
{
Check.NotNull(func, nameof(func));
_func = func;
Check.NotNull(funcs, nameof(funcs));
_funcs = funcs;
}
/// <summary>
@@ -63,12 +61,11 @@ namespace WireMock.Matchers.Request
/// </returns>
public bool IsMatch(RequestMessage requestMessage)
{
if (_func != null)
{
return _func(requestMessage.Query);
}
if (_funcs != null)
return _funcs.Any(f => f(requestMessage.Query));
return requestMessage.GetParameter(_key).Intersect(_values).Count() == _values.Count();
var values = requestMessage.GetParameter(Key);
return values?.Intersect(Values).Count() == Values.Count();
}
}
}
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using WireMock.Validation;
@@ -12,48 +14,39 @@ namespace WireMock.Matchers.Request
/// <summary>
/// The matcher.
/// </summary>
private readonly string _path;
public IReadOnlyList<IMatcher> Matchers { get; }
/// <summary>
/// The matcher.
/// The path functions
/// </summary>
private readonly IMatcher _matcher;
/// <summary>
/// The path function
/// </summary>
private readonly Func<string, bool> _pathFunc;
private readonly Func<string, bool>[] _pathFuncs;
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessagePathMatcher"/> class.
/// </summary>
/// <param name="path">The path.</param>
public RequestMessagePathMatcher([NotNull] string path)
/// <param name="paths">The paths.</param>
public RequestMessagePathMatcher([NotNull] params string[] paths) : this(paths.Select(path => new WildcardMatcher(path)).ToArray())
{
Check.NotNull(path, nameof(path));
_path = path;
}
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessagePathMatcher"/> class.
/// </summary>
/// <param name="matcher">The matcher.</param>
public RequestMessagePathMatcher([NotNull] IMatcher matcher)
/// <param name="matchers">The matchers.</param>
public RequestMessagePathMatcher([NotNull] params IMatcher[] matchers)
{
Check.NotNull(matcher, nameof(matcher));
_matcher = matcher;
Check.NotNull(matchers, nameof(matchers));
Matchers = matchers;
}
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessagePathMatcher"/> class.
/// </summary>
/// <param name="func">
/// The path func.
/// </param>
public RequestMessagePathMatcher([NotNull] Func<string, bool> func)
/// <param name="funcs">The path functions.</param>
public RequestMessagePathMatcher([NotNull] params Func<string, bool>[] funcs)
{
Check.NotNull(func, nameof(func));
_pathFunc = func;
Check.NotNull(funcs, nameof(funcs));
_pathFuncs = funcs;
}
/// <summary>
@@ -65,14 +58,11 @@ namespace WireMock.Matchers.Request
/// </returns>
public bool IsMatch(RequestMessage requestMessage)
{
if (_path != null)
return string.CompareOrdinal(_path, requestMessage.Path) == 0;
if (Matchers != null)
return Matchers.Any(matcher => matcher.IsMatch(requestMessage.Path));
if (_matcher != null)
return _matcher.IsMatch(requestMessage.Path);
if (_pathFunc != null)
return _pathFunc(requestMessage.Path);
if (_pathFuncs != null)
return _pathFuncs.Any(func => func(requestMessage.Path));
return false;
}
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using WireMock.Validation;
@@ -12,42 +14,39 @@ namespace WireMock.Matchers.Request
/// <summary>
/// The matcher.
/// </summary>
private readonly IMatcher _matcher;
public readonly IReadOnlyList<IMatcher> Matchers;
/// <summary>
/// The url function
/// The url functions
/// </summary>
private readonly Func<string, bool> _urlFunc;
private readonly Func<string, bool>[] _urlFuncs;
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageUrlMatcher"/> class.
/// </summary>
/// <param name="url">The url.</param>
public RequestMessageUrlMatcher([NotNull] string url) : this(new WildcardMatcher(url))
/// <param name="urls">The urls.</param>
public RequestMessageUrlMatcher([NotNull] params string[] urls) : this(urls.Select(url => new WildcardMatcher(url)).ToArray())
{
_matcher = new WildcardMatcher(url);
}
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageUrlMatcher"/> class.
/// </summary>
/// <param name="matcher">The matcher.</param>
public RequestMessageUrlMatcher([NotNull] IMatcher matcher)
/// <param name="matchers">The matchers.</param>
public RequestMessageUrlMatcher([NotNull] params IMatcher[] matchers)
{
Check.NotNull(matcher, nameof(matcher));
_matcher = matcher;
Check.NotNull(matchers, nameof(matchers));
Matchers = matchers;
}
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageUrlMatcher"/> class.
/// </summary>
/// <param name="func">
/// The url func.
/// </param>
public RequestMessageUrlMatcher([NotNull] Func<string, bool> func)
/// <param name="funcs">The url functions.</param>
public RequestMessageUrlMatcher([NotNull] params Func<string, bool>[] funcs)
{
Check.NotNull(func, nameof(func));
_urlFunc = func;
Check.NotNull(funcs, nameof(funcs));
_urlFuncs = funcs;
}
/// <summary>
@@ -59,11 +58,11 @@ namespace WireMock.Matchers.Request
/// </returns>
public bool IsMatch(RequestMessage requestMessage)
{
if (_matcher != null)
return _matcher.IsMatch(requestMessage.Path);
if (Matchers != null)
return Matchers.Any(matcher => matcher.IsMatch(requestMessage.Path));
if (_urlFunc != null)
return _urlFunc(requestMessage.Url);
if (_urlFuncs != null)
return _urlFuncs.Any(func => func(requestMessage.Url));
return false;
}
@@ -1,4 +1,5 @@
using JetBrains.Annotations;
using System.Linq;
using JetBrains.Annotations;
using WireMock.Validation;
namespace WireMock.Matchers.Request
@@ -9,20 +10,20 @@ namespace WireMock.Matchers.Request
internal class RequestMessageVerbMatcher : IRequestMatcher
{
/// <summary>
/// The _verb.
/// The verbs
/// </summary>
private readonly string _verb;
public string[] Verbs { get; }
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageVerbMatcher"/> class.
/// </summary>
/// <param name="verb">
/// <param name="verbs">
/// The verb.
/// </param>
public RequestMessageVerbMatcher([NotNull] string verb)
public RequestMessageVerbMatcher([NotNull] params string[] verbs)
{
Check.NotNull(verb, nameof(verb));
_verb = verb.ToLower();
Check.NotNull(verbs, nameof(verbs));
Verbs = verbs.Select(v => v.ToLower()).ToArray();
}
/// <summary>
@@ -34,7 +35,7 @@ namespace WireMock.Matchers.Request
/// </returns>
public bool IsMatch(RequestMessage requestMessage)
{
return requestMessage.Verb == _verb;
return Verbs.Contains(requestMessage.Verb);
}
}
}