mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-17 23:14:23 +01:00
UrlModel + Funcs
This commit is contained in:
@@ -12,5 +12,21 @@
|
||||
/// The matcher.
|
||||
/// </value>
|
||||
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.
|
||||
/// </value>
|
||||
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.
|
||||
/// </value>
|
||||
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.
|
||||
/// </value>
|
||||
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.
|
||||
/// </value>
|
||||
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>
|
||||
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>
|
||||
/// The methods
|
||||
/// </summary>
|
||||
|
||||
24
src/WireMock.Net/Admin/Mappings/UrlModel.cs
Normal file
24
src/WireMock.Net/Admin/Mappings/UrlModel.cs
Normal file
@@ -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>
|
||||
/// The body function
|
||||
/// </summary>
|
||||
private readonly Func<string, bool> _bodyFunc;
|
||||
public Func<string, bool> Func { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The body data function
|
||||
/// </summary>
|
||||
private readonly Func<byte[], bool> _bodyDataFunc;
|
||||
public Func<byte[], bool> DataFunc { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The matcher.
|
||||
/// </summary>
|
||||
public readonly IMatcher Matcher;
|
||||
public IMatcher Matcher { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 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)
|
||||
{
|
||||
Check.NotNull(func, nameof(func));
|
||||
_bodyFunc = func;
|
||||
Func = func;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -79,7 +79,7 @@ namespace WireMock.Matchers.Request
|
||||
public RequestMessageBodyMatcher([NotNull] Func<byte[], bool> func)
|
||||
{
|
||||
Check.NotNull(func, nameof(func));
|
||||
_bodyDataFunc = func;
|
||||
DataFunc = func;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -112,11 +112,11 @@ namespace WireMock.Matchers.Request
|
||||
if (_bodyData != null)
|
||||
return requestMessage.BodyAsBytes == _bodyData;
|
||||
|
||||
if (_bodyFunc != null)
|
||||
return _bodyFunc(requestMessage.Body);
|
||||
if (Func != null)
|
||||
return Func(requestMessage.Body);
|
||||
|
||||
if (_bodyDataFunc != null)
|
||||
return _bodyDataFunc(requestMessage.BodyAsBytes);
|
||||
if (DataFunc != null)
|
||||
return DataFunc(requestMessage.BodyAsBytes);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -11,16 +11,16 @@ namespace WireMock.Matchers.Request
|
||||
/// </summary>
|
||||
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>
|
||||
/// The name
|
||||
/// </summary>
|
||||
public string Name { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the matchers.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The matchers.
|
||||
/// </value>
|
||||
@@ -63,7 +63,7 @@ namespace WireMock.Matchers.Request
|
||||
{
|
||||
Check.NotNull(funcs, nameof(funcs));
|
||||
|
||||
_cookieFuncs = funcs;
|
||||
Funcs = funcs;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -75,8 +75,8 @@ namespace WireMock.Matchers.Request
|
||||
/// </returns>
|
||||
public bool IsMatch(RequestMessage requestMessage)
|
||||
{
|
||||
if (_cookieFuncs != null)
|
||||
return _cookieFuncs.Any(cf => cf(requestMessage.Cookies));
|
||||
if (Funcs != null)
|
||||
return Funcs.Any(cf => cf(requestMessage.Cookies));
|
||||
|
||||
if (requestMessage.Cookies == null)
|
||||
return false;
|
||||
|
||||
@@ -11,16 +11,16 @@ namespace WireMock.Matchers.Request
|
||||
/// </summary>
|
||||
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>
|
||||
/// The name
|
||||
/// </summary>
|
||||
public string Name { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the matchers.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The matchers.
|
||||
/// </value>
|
||||
@@ -63,7 +63,7 @@ namespace WireMock.Matchers.Request
|
||||
{
|
||||
Check.NotNull(funcs, nameof(funcs));
|
||||
|
||||
_headerFuncs = funcs;
|
||||
Funcs = funcs;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -75,8 +75,8 @@ namespace WireMock.Matchers.Request
|
||||
/// </returns>
|
||||
public bool IsMatch(RequestMessage requestMessage)
|
||||
{
|
||||
if (_headerFuncs != null)
|
||||
return _headerFuncs.Any(hf => hf(requestMessage.Headers));
|
||||
if (Funcs != null)
|
||||
return Funcs.Any(hf => hf(requestMessage.Headers));
|
||||
|
||||
if (requestMessage.Headers == null)
|
||||
return false;
|
||||
|
||||
@@ -12,7 +12,10 @@ namespace WireMock.Matchers.Request
|
||||
/// </summary>
|
||||
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>
|
||||
/// The key
|
||||
@@ -49,7 +52,7 @@ namespace WireMock.Matchers.Request
|
||||
public RequestMessageParamMatcher([NotNull] params Func<IDictionary<string, WireMockList<string>>, bool>[] funcs)
|
||||
{
|
||||
Check.NotNull(funcs, nameof(funcs));
|
||||
_funcs = funcs;
|
||||
Funcs = funcs;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -61,8 +64,8 @@ namespace WireMock.Matchers.Request
|
||||
/// </returns>
|
||||
public bool IsMatch(RequestMessage requestMessage)
|
||||
{
|
||||
if (_funcs != null)
|
||||
return _funcs.Any(f => f(requestMessage.Query));
|
||||
if (Funcs != null)
|
||||
return Funcs.Any(f => f(requestMessage.Query));
|
||||
|
||||
var values = requestMessage.GetParameter(Key);
|
||||
return values?.Intersect(Values).Count() == Values.Count();
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace WireMock.Matchers.Request
|
||||
/// <summary>
|
||||
/// The path functions
|
||||
/// </summary>
|
||||
private readonly Func<string, bool>[] _pathFuncs;
|
||||
public Func<string, bool>[] Funcs { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 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)
|
||||
{
|
||||
Check.NotNull(funcs, nameof(funcs));
|
||||
_pathFuncs = funcs;
|
||||
Funcs = funcs;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -61,8 +61,8 @@ namespace WireMock.Matchers.Request
|
||||
if (Matchers != null)
|
||||
return Matchers.Any(matcher => matcher.IsMatch(requestMessage.Path));
|
||||
|
||||
if (_pathFuncs != null)
|
||||
return _pathFuncs.Any(func => func(requestMessage.Path));
|
||||
if (Funcs != null)
|
||||
return Funcs.Any(func => func(requestMessage.Path));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -14,12 +14,12 @@ namespace WireMock.Matchers.Request
|
||||
/// <summary>
|
||||
/// The matcher.
|
||||
/// </summary>
|
||||
public readonly IReadOnlyList<IMatcher> Matchers;
|
||||
public IReadOnlyList<IMatcher> Matchers { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The url functions
|
||||
/// </summary>
|
||||
private readonly Func<string, bool>[] _urlFuncs;
|
||||
public Func<string, bool>[] Funcs { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 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)
|
||||
{
|
||||
Check.NotNull(funcs, nameof(funcs));
|
||||
_urlFuncs = funcs;
|
||||
Funcs = funcs;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -61,8 +61,8 @@ namespace WireMock.Matchers.Request
|
||||
if (Matchers != null)
|
||||
return Matchers.Any(matcher => matcher.IsMatch(requestMessage.Url));
|
||||
|
||||
if (_urlFuncs != null)
|
||||
return _urlFuncs.Any(func => func(requestMessage.Url));
|
||||
if (Funcs != null)
|
||||
return Funcs.Any(func => func(requestMessage.Url));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -228,6 +228,17 @@ namespace WireMock.Server
|
||||
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)
|
||||
requestBuilder = requestBuilder.UsingVerb(mappingModel.Request.Methods);
|
||||
else
|
||||
@@ -298,6 +309,7 @@ namespace WireMock.Server
|
||||
var response = (Response)mapping.Provider;
|
||||
|
||||
var pathMatchers = request.GetRequestMessageMatchers<RequestMessagePathMatcher>();
|
||||
var urlMatchers = request.GetRequestMessageMatchers<RequestMessageUrlMatcher>();
|
||||
var headerMatchers = request.GetRequestMessageMatchers<RequestMessageHeaderMatcher>();
|
||||
var cookieMatchers = request.GetRequestMessageMatchers<RequestMessageCookieMatcher>();
|
||||
var paramsMatchers = request.GetRequestMessageMatchers<RequestMessageParamMatcher>();
|
||||
@@ -310,29 +322,46 @@ namespace WireMock.Server
|
||||
Priority = mapping.Priority,
|
||||
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" },
|
||||
|
||||
Headers = headerMatchers?.Select(hm => new HeaderModel
|
||||
{
|
||||
Name = hm.Name,
|
||||
Matchers = Map(hm.Matchers)
|
||||
Matchers = Map(hm.Matchers),
|
||||
Funcs = Map(hm.Funcs)
|
||||
}).ToList(),
|
||||
Cookies = cookieMatchers?.Select(hm => new CookieModel
|
||||
|
||||
Cookies = cookieMatchers?.Select(cm => new CookieModel
|
||||
{
|
||||
Name = hm.Name,
|
||||
Matchers = Map(hm.Matchers)
|
||||
Name = cm.Name,
|
||||
Matchers = Map(cm.Matchers),
|
||||
Funcs = Map(cm.Funcs)
|
||||
}).ToList(),
|
||||
Params = paramsMatchers?.Select(hm => new ParamModel
|
||||
|
||||
Params = paramsMatchers?.Select(pm => new ParamModel
|
||||
{
|
||||
Name = hm.Key,
|
||||
Values = hm.Values?.ToList()
|
||||
Name = pm.Key,
|
||||
Values = pm.Values?.ToList(),
|
||||
Funcs = Map(pm.Funcs)
|
||||
}).ToList(),
|
||||
|
||||
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
|
||||
@@ -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)
|
||||
{
|
||||
if (matcher == null)
|
||||
|
||||
@@ -161,7 +161,7 @@ namespace WireMock.Server
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The reset.
|
||||
/// Resets LogEntries and Mappings.
|
||||
/// </summary>
|
||||
public void Reset()
|
||||
{
|
||||
@@ -171,7 +171,7 @@ namespace WireMock.Server
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets the log entries.
|
||||
/// Resets the LogEntries.
|
||||
/// </summary>
|
||||
public void ResetLogEntries()
|
||||
{
|
||||
@@ -203,7 +203,7 @@ namespace WireMock.Server
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets the mappings.
|
||||
/// Resets the Mappings.
|
||||
/// </summary>
|
||||
public void ResetMappings()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user