UrlModel + Funcs

This commit is contained in:
Stef Heyenrath
2017-02-03 13:54:19 +01:00
parent 84901ab1e4
commit d1aa517f99
17 changed files with 173 additions and 58 deletions
@@ -19,7 +19,7 @@ namespace WireMock.Net.ConsoleApplication
Console.WriteLine("FluentMockServer listening at {0}", string.Join(" and ", server.Urls)); Console.WriteLine("FluentMockServer listening at {0}", string.Join(" and ", server.Urls));
server server
.Given(Request.Create().WithPath(u => u.Contains("x")).UsingGet()) .Given(Request.Create().WithPath(p => p.Contains("x")).UsingGet())
.AtPriority(4) .AtPriority(4)
.RespondWith(Response.Create() .RespondWith(Response.Create()
.WithStatusCode(200) .WithStatusCode(200)
@@ -12,5 +12,21 @@
/// The matcher. /// The matcher.
/// </value> /// </value>
public MatcherModel Matcher { get; set; } public MatcherModel Matcher { get; set; }
/// <summary>
/// Gets or sets the function.
/// </summary>
/// <value>
/// The function.
/// </value>
public string Func { get; set; }
/// <summary>
/// Gets or sets the data function.
/// </summary>
/// <value>
/// The data function.
/// </value>
public string DataFunc { get; set; }
} }
} }
@@ -22,5 +22,13 @@ namespace WireMock.Admin.Mappings
/// The matchers. /// The matchers.
/// </value> /// </value>
public IList<MatcherModel> Matchers { get; set; } public IList<MatcherModel> Matchers { get; set; }
/// <summary>
/// Gets or sets the functions.
/// </summary>
/// <value>
/// The functions.
/// </value>
public string[] Funcs { get; set; }
} }
} }
@@ -1,7 +0,0 @@
//namespace WireMock.Admin
//{
// public class FuncModel
// {
// public string Name { get; set; }
// }
//}
@@ -22,5 +22,13 @@ namespace WireMock.Admin.Mappings
/// The matchers. /// The matchers.
/// </value> /// </value>
public IList<MatcherModel> Matchers { get; set; } public IList<MatcherModel> Matchers { get; set; }
/// <summary>
/// Gets or sets the functions.
/// </summary>
/// <value>
/// The functions.
/// </value>
public string[] Funcs { get; set; }
} }
} }
@@ -22,5 +22,13 @@ namespace WireMock.Admin.Mappings
/// The values. /// The values.
/// </value> /// </value>
public IList<string> Values { get; set; } public IList<string> Values { get; set; }
/// <summary>
/// Gets or sets the functions.
/// </summary>
/// <value>
/// The functions.
/// </value>
public string[] Funcs { get; set; }
} }
} }
@@ -12,5 +12,13 @@
/// The matchers. /// The matchers.
/// </value> /// </value>
public MatcherModel[] Matchers { get; set; } public MatcherModel[] Matchers { get; set; }
/// <summary>
/// Gets or sets the functions.
/// </summary>
/// <value>
/// The functions.
/// </value>
public string[] Funcs { get; set; }
} }
} }
@@ -15,6 +15,14 @@ namespace WireMock.Admin.Mappings
/// </value> /// </value>
public object Path { get; set; } public object Path { get; set; }
/// <summary>
/// Gets or sets the Url. (Can be a string or a UrlModel)
/// </summary>
/// <value>
/// The URL.
/// </value>
public object Url { get; set; }
/// <summary> /// <summary>
/// The methods /// The methods
/// </summary> /// </summary>
@@ -0,0 +1,24 @@
namespace WireMock.Admin.Mappings
{
/// <summary>
/// UrlModel
/// </summary>
public class UrlModel
{
/// <summary>
/// Gets or sets the matchers.
/// </summary>
/// <value>
/// The matchers.
/// </value>
public MatcherModel[] Matchers { get; set; }
/// <summary>
/// Gets or sets the functions.
/// </summary>
/// <value>
/// The functions.
/// </value>
public string[] Funcs { get; set; }
}
}
@@ -22,17 +22,17 @@ namespace WireMock.Matchers.Request
/// <summary> /// <summary>
/// The body function /// The body function
/// </summary> /// </summary>
private readonly Func<string, bool> _bodyFunc; public Func<string, bool> Func { get; }
/// <summary> /// <summary>
/// The body data function /// The body data function
/// </summary> /// </summary>
private readonly Func<byte[], bool> _bodyDataFunc; public Func<byte[], bool> DataFunc { get; }
/// <summary> /// <summary>
/// The matcher. /// The matcher.
/// </summary> /// </summary>
public readonly IMatcher Matcher; public IMatcher Matcher { get; }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class. /// Initializes a new instance of the <see cref="RequestMessageBodyMatcher"/> class.
@@ -67,7 +67,7 @@ namespace WireMock.Matchers.Request
public RequestMessageBodyMatcher([NotNull] Func<string, bool> func) public RequestMessageBodyMatcher([NotNull] Func<string, bool> func)
{ {
Check.NotNull(func, nameof(func)); Check.NotNull(func, nameof(func));
_bodyFunc = func; Func = func;
} }
/// <summary> /// <summary>
@@ -79,7 +79,7 @@ namespace WireMock.Matchers.Request
public RequestMessageBodyMatcher([NotNull] Func<byte[], bool> func) public RequestMessageBodyMatcher([NotNull] Func<byte[], bool> func)
{ {
Check.NotNull(func, nameof(func)); Check.NotNull(func, nameof(func));
_bodyDataFunc = func; DataFunc = func;
} }
/// <summary> /// <summary>
@@ -112,11 +112,11 @@ namespace WireMock.Matchers.Request
if (_bodyData != null) if (_bodyData != null)
return requestMessage.BodyAsBytes == _bodyData; return requestMessage.BodyAsBytes == _bodyData;
if (_bodyFunc != null) if (Func != null)
return _bodyFunc(requestMessage.Body); return Func(requestMessage.Body);
if (_bodyDataFunc != null) if (DataFunc != null)
return _bodyDataFunc(requestMessage.BodyAsBytes); return DataFunc(requestMessage.BodyAsBytes);
return false; return false;
} }
@@ -11,16 +11,16 @@ namespace WireMock.Matchers.Request
/// </summary> /// </summary>
public class RequestMessageCookieMatcher : IRequestMatcher public class RequestMessageCookieMatcher : IRequestMatcher
{ {
private readonly Func<IDictionary<string, string>, bool>[] _cookieFuncs; /// <value>
/// The funcs.
/// </value>
public Func<IDictionary<string, string>, bool>[] Funcs { get; }
/// <summary> /// <summary>
/// The name /// The name
/// </summary> /// </summary>
public string Name { get; } public string Name { get; }
/// <summary>
/// Gets the matchers.
/// </summary>
/// <value> /// <value>
/// The matchers. /// The matchers.
/// </value> /// </value>
@@ -63,7 +63,7 @@ namespace WireMock.Matchers.Request
{ {
Check.NotNull(funcs, nameof(funcs)); Check.NotNull(funcs, nameof(funcs));
_cookieFuncs = funcs; Funcs = funcs;
} }
/// <summary> /// <summary>
@@ -75,8 +75,8 @@ namespace WireMock.Matchers.Request
/// </returns> /// </returns>
public bool IsMatch(RequestMessage requestMessage) public bool IsMatch(RequestMessage requestMessage)
{ {
if (_cookieFuncs != null) if (Funcs != null)
return _cookieFuncs.Any(cf => cf(requestMessage.Cookies)); return Funcs.Any(cf => cf(requestMessage.Cookies));
if (requestMessage.Cookies == null) if (requestMessage.Cookies == null)
return false; return false;
@@ -11,16 +11,16 @@ namespace WireMock.Matchers.Request
/// </summary> /// </summary>
public class RequestMessageHeaderMatcher : IRequestMatcher public class RequestMessageHeaderMatcher : IRequestMatcher
{ {
private readonly Func<IDictionary<string, string>, bool>[] _headerFuncs; /// <summary>
/// The functions
/// </summary>
public Func<IDictionary<string, string>, bool>[] Funcs { get; }
/// <summary> /// <summary>
/// The name /// The name
/// </summary> /// </summary>
public string Name { get; } public string Name { get; }
/// <summary>
/// Gets the matchers.
/// </summary>
/// <value> /// <value>
/// The matchers. /// The matchers.
/// </value> /// </value>
@@ -63,7 +63,7 @@ namespace WireMock.Matchers.Request
{ {
Check.NotNull(funcs, nameof(funcs)); Check.NotNull(funcs, nameof(funcs));
_headerFuncs = funcs; Funcs = funcs;
} }
/// <summary> /// <summary>
@@ -75,8 +75,8 @@ namespace WireMock.Matchers.Request
/// </returns> /// </returns>
public bool IsMatch(RequestMessage requestMessage) public bool IsMatch(RequestMessage requestMessage)
{ {
if (_headerFuncs != null) if (Funcs != null)
return _headerFuncs.Any(hf => hf(requestMessage.Headers)); return Funcs.Any(hf => hf(requestMessage.Headers));
if (requestMessage.Headers == null) if (requestMessage.Headers == null)
return false; return false;
@@ -12,7 +12,10 @@ namespace WireMock.Matchers.Request
/// </summary> /// </summary>
public class RequestMessageParamMatcher : IRequestMatcher public class RequestMessageParamMatcher : IRequestMatcher
{ {
private readonly Func<IDictionary<string, WireMockList<string>>, bool>[] _funcs; /// <summary>
/// The funcs
/// </summary>
public Func<IDictionary<string, WireMockList<string>>, bool>[] Funcs { get; }
/// <summary> /// <summary>
/// The key /// The key
@@ -49,7 +52,7 @@ namespace WireMock.Matchers.Request
public RequestMessageParamMatcher([NotNull] params Func<IDictionary<string, WireMockList<string>>, bool>[] funcs) public RequestMessageParamMatcher([NotNull] params Func<IDictionary<string, WireMockList<string>>, bool>[] funcs)
{ {
Check.NotNull(funcs, nameof(funcs)); Check.NotNull(funcs, nameof(funcs));
_funcs = funcs; Funcs = funcs;
} }
/// <summary> /// <summary>
@@ -61,8 +64,8 @@ namespace WireMock.Matchers.Request
/// </returns> /// </returns>
public bool IsMatch(RequestMessage requestMessage) public bool IsMatch(RequestMessage requestMessage)
{ {
if (_funcs != null) if (Funcs != null)
return _funcs.Any(f => f(requestMessage.Query)); return Funcs.Any(f => f(requestMessage.Query));
var values = requestMessage.GetParameter(Key); var values = requestMessage.GetParameter(Key);
return values?.Intersect(Values).Count() == Values.Count(); return values?.Intersect(Values).Count() == Values.Count();
@@ -19,7 +19,7 @@ namespace WireMock.Matchers.Request
/// <summary> /// <summary>
/// The path functions /// The path functions
/// </summary> /// </summary>
private readonly Func<string, bool>[] _pathFuncs; public Func<string, bool>[] Funcs { get; }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RequestMessagePathMatcher"/> class. /// Initializes a new instance of the <see cref="RequestMessagePathMatcher"/> class.
@@ -46,7 +46,7 @@ namespace WireMock.Matchers.Request
public RequestMessagePathMatcher([NotNull] params Func<string, bool>[] funcs) public RequestMessagePathMatcher([NotNull] params Func<string, bool>[] funcs)
{ {
Check.NotNull(funcs, nameof(funcs)); Check.NotNull(funcs, nameof(funcs));
_pathFuncs = funcs; Funcs = funcs;
} }
/// <summary> /// <summary>
@@ -61,8 +61,8 @@ namespace WireMock.Matchers.Request
if (Matchers != null) if (Matchers != null)
return Matchers.Any(matcher => matcher.IsMatch(requestMessage.Path)); return Matchers.Any(matcher => matcher.IsMatch(requestMessage.Path));
if (_pathFuncs != null) if (Funcs != null)
return _pathFuncs.Any(func => func(requestMessage.Path)); return Funcs.Any(func => func(requestMessage.Path));
return false; return false;
} }
@@ -14,12 +14,12 @@ namespace WireMock.Matchers.Request
/// <summary> /// <summary>
/// The matcher. /// The matcher.
/// </summary> /// </summary>
public readonly IReadOnlyList<IMatcher> Matchers; public IReadOnlyList<IMatcher> Matchers { get; }
/// <summary> /// <summary>
/// The url functions /// The url functions
/// </summary> /// </summary>
private readonly Func<string, bool>[] _urlFuncs; public Func<string, bool>[] Funcs { get; }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RequestMessageUrlMatcher"/> class. /// Initializes a new instance of the <see cref="RequestMessageUrlMatcher"/> class.
@@ -46,7 +46,7 @@ namespace WireMock.Matchers.Request
public RequestMessageUrlMatcher([NotNull] params Func<string, bool>[] funcs) public RequestMessageUrlMatcher([NotNull] params Func<string, bool>[] funcs)
{ {
Check.NotNull(funcs, nameof(funcs)); Check.NotNull(funcs, nameof(funcs));
_urlFuncs = funcs; Funcs = funcs;
} }
/// <summary> /// <summary>
@@ -61,8 +61,8 @@ namespace WireMock.Matchers.Request
if (Matchers != null) if (Matchers != null)
return Matchers.Any(matcher => matcher.IsMatch(requestMessage.Url)); return Matchers.Any(matcher => matcher.IsMatch(requestMessage.Url));
if (_urlFuncs != null) if (Funcs != null)
return _urlFuncs.Any(func => func(requestMessage.Url)); return Funcs.Any(func => func(requestMessage.Url));
return false; return false;
} }
@@ -228,6 +228,17 @@ namespace WireMock.Server
requestBuilder = requestBuilder.WithPath(pathModel.Matchers.Select(Map).ToArray()); requestBuilder = requestBuilder.WithPath(pathModel.Matchers.Select(Map).ToArray());
} }
string url = mappingModel.Request.Url as string;
if (url != null)
requestBuilder = requestBuilder.WithUrl(url);
else
{
JToken urlToken = (JToken)mappingModel.Request.Url;
UrlModel urlModel = urlToken.ToObject<UrlModel>();
if (urlModel?.Matchers != null)
requestBuilder = requestBuilder.WithUrl(urlModel.Matchers.Select(Map).ToArray());
}
if (mappingModel.Request.Methods != null) if (mappingModel.Request.Methods != null)
requestBuilder = requestBuilder.UsingVerb(mappingModel.Request.Methods); requestBuilder = requestBuilder.UsingVerb(mappingModel.Request.Methods);
else else
@@ -298,6 +309,7 @@ namespace WireMock.Server
var response = (Response)mapping.Provider; var response = (Response)mapping.Provider;
var pathMatchers = request.GetRequestMessageMatchers<RequestMessagePathMatcher>(); var pathMatchers = request.GetRequestMessageMatchers<RequestMessagePathMatcher>();
var urlMatchers = request.GetRequestMessageMatchers<RequestMessageUrlMatcher>();
var headerMatchers = request.GetRequestMessageMatchers<RequestMessageHeaderMatcher>(); var headerMatchers = request.GetRequestMessageMatchers<RequestMessageHeaderMatcher>();
var cookieMatchers = request.GetRequestMessageMatchers<RequestMessageCookieMatcher>(); var cookieMatchers = request.GetRequestMessageMatchers<RequestMessageCookieMatcher>();
var paramsMatchers = request.GetRequestMessageMatchers<RequestMessageParamMatcher>(); var paramsMatchers = request.GetRequestMessageMatchers<RequestMessageParamMatcher>();
@@ -310,29 +322,46 @@ namespace WireMock.Server
Priority = mapping.Priority, Priority = mapping.Priority,
Request = new RequestModel Request = new RequestModel
{ {
Path = new PathModel Path = pathMatchers != null ? new PathModel
{ {
Matchers = pathMatchers != null ? Map(pathMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers)) : null Matchers = Map(pathMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers)),
}, Funcs = Map(pathMatchers.Where(m => m.Funcs != null).SelectMany(m => m.Funcs))
} : null,
Url = urlMatchers != null ? new UrlModel
{
Matchers = Map(urlMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers)),
Funcs = Map(urlMatchers.Where(m => m.Funcs != null).SelectMany(m => m.Funcs))
} : null,
Methods = methodMatcher != null ? methodMatcher.Methods : new[] { "any" }, Methods = methodMatcher != null ? methodMatcher.Methods : new[] { "any" },
Headers = headerMatchers?.Select(hm => new HeaderModel Headers = headerMatchers?.Select(hm => new HeaderModel
{ {
Name = hm.Name, Name = hm.Name,
Matchers = Map(hm.Matchers) Matchers = Map(hm.Matchers),
Funcs = Map(hm.Funcs)
}).ToList(), }).ToList(),
Cookies = cookieMatchers?.Select(hm => new CookieModel
Cookies = cookieMatchers?.Select(cm => new CookieModel
{ {
Name = hm.Name, Name = cm.Name,
Matchers = Map(hm.Matchers) Matchers = Map(cm.Matchers),
Funcs = Map(cm.Funcs)
}).ToList(), }).ToList(),
Params = paramsMatchers?.Select(hm => new ParamModel
Params = paramsMatchers?.Select(pm => new ParamModel
{ {
Name = hm.Key, Name = pm.Key,
Values = hm.Values?.ToList() Values = pm.Values?.ToList(),
Funcs = Map(pm.Funcs)
}).ToList(), }).ToList(),
Body = new BodyModel Body = new BodyModel
{ {
Matcher = bodyMatcher != null ? Map(bodyMatcher.Matcher) : null Matcher = bodyMatcher != null ? Map(bodyMatcher.Matcher) : null,
Func = bodyMatcher != null ? Map(bodyMatcher.Func) : null,
DataFunc = bodyMatcher != null ? Map(bodyMatcher.DataFunc) : null
} }
}, },
Response = new ResponseModel Response = new ResponseModel
@@ -363,6 +392,16 @@ namespace WireMock.Server
}; };
} }
private string[] Map<T>([CanBeNull] IEnumerable<Func<T, bool>> funcs)
{
return funcs?.Select(Map).Where(x => x != null).ToArray();
}
private string Map<T>([CanBeNull] Func<T, bool> func)
{
return func?.ToString();
}
private IMatcher Map([CanBeNull] MatcherModel matcher) private IMatcher Map([CanBeNull] MatcherModel matcher)
{ {
if (matcher == null) if (matcher == null)
+3 -3
View File
@@ -161,7 +161,7 @@ namespace WireMock.Server
} }
/// <summary> /// <summary>
/// The reset. /// Resets LogEntries and Mappings.
/// </summary> /// </summary>
public void Reset() public void Reset()
{ {
@@ -171,7 +171,7 @@ namespace WireMock.Server
} }
/// <summary> /// <summary>
/// Resets the log entries. /// Resets the LogEntries.
/// </summary> /// </summary>
public void ResetLogEntries() public void ResetLogEntries()
{ {
@@ -203,7 +203,7 @@ namespace WireMock.Server
} }
/// <summary> /// <summary>
/// Resets the mappings. /// Resets the Mappings.
/// </summary> /// </summary>
public void ResetMappings() public void ResetMappings()
{ {