From d1aa517f99bf171bdb9c821c1b0f7e446f872255 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Fri, 3 Feb 2017 13:54:19 +0100 Subject: [PATCH] UrlModel + Funcs --- .../Program.cs | 2 +- src/WireMock.Net/Admin/Mappings/BodyModel.cs | 16 +++++ .../Admin/Mappings/CookieModel.cs | 8 +++ src/WireMock.Net/Admin/Mappings/FuncModel.cs | 7 --- .../Admin/Mappings/HeaderModel.cs | 8 +++ src/WireMock.Net/Admin/Mappings/ParamModel.cs | 8 +++ src/WireMock.Net/Admin/Mappings/PathModel.cs | 8 +++ .../Admin/Mappings/RequestModel.cs | 8 +++ src/WireMock.Net/Admin/Mappings/UrlModel.cs | 24 ++++++++ .../Request/RequestMessageBodyMatcher.cs | 18 +++--- .../Request/RequestMessageCookieMatcher.cs | 14 ++--- .../Request/RequestMessageHeaderMatcher.cs | 14 ++--- .../Request/RequestMessageParamMatcher.cs | 11 ++-- .../Request/RequestMessagePathMatcher.cs | 8 +-- .../Request/RequestMessageUrlMatcher.cs | 10 +-- .../Server/FluentMockServer.Admin.cs | 61 +++++++++++++++---- src/WireMock.Net/Server/FluentMockServer.cs | 6 +- 17 files changed, 173 insertions(+), 58 deletions(-) delete mode 100644 src/WireMock.Net/Admin/Mappings/FuncModel.cs create mode 100644 src/WireMock.Net/Admin/Mappings/UrlModel.cs diff --git a/examples/WireMock.Net.ConsoleApplication/Program.cs b/examples/WireMock.Net.ConsoleApplication/Program.cs index c588efdd..2d84e24c 100644 --- a/examples/WireMock.Net.ConsoleApplication/Program.cs +++ b/examples/WireMock.Net.ConsoleApplication/Program.cs @@ -19,7 +19,7 @@ namespace WireMock.Net.ConsoleApplication Console.WriteLine("FluentMockServer listening at {0}", string.Join(" and ", server.Urls)); server - .Given(Request.Create().WithPath(u => u.Contains("x")).UsingGet()) + .Given(Request.Create().WithPath(p => p.Contains("x")).UsingGet()) .AtPriority(4) .RespondWith(Response.Create() .WithStatusCode(200) diff --git a/src/WireMock.Net/Admin/Mappings/BodyModel.cs b/src/WireMock.Net/Admin/Mappings/BodyModel.cs index 565c3dfa..c31306a8 100644 --- a/src/WireMock.Net/Admin/Mappings/BodyModel.cs +++ b/src/WireMock.Net/Admin/Mappings/BodyModel.cs @@ -12,5 +12,21 @@ /// The matcher. /// public MatcherModel Matcher { get; set; } + + /// + /// Gets or sets the function. + /// + /// + /// The function. + /// + public string Func { get; set; } + + /// + /// Gets or sets the data function. + /// + /// + /// The data function. + /// + public string DataFunc { get; set; } } } \ No newline at end of file diff --git a/src/WireMock.Net/Admin/Mappings/CookieModel.cs b/src/WireMock.Net/Admin/Mappings/CookieModel.cs index dfbaef30..3a54f741 100644 --- a/src/WireMock.Net/Admin/Mappings/CookieModel.cs +++ b/src/WireMock.Net/Admin/Mappings/CookieModel.cs @@ -22,5 +22,13 @@ namespace WireMock.Admin.Mappings /// The matchers. /// public IList Matchers { get; set; } + + /// + /// Gets or sets the functions. + /// + /// + /// The functions. + /// + public string[] Funcs { get; set; } } } \ No newline at end of file diff --git a/src/WireMock.Net/Admin/Mappings/FuncModel.cs b/src/WireMock.Net/Admin/Mappings/FuncModel.cs deleted file mode 100644 index ecef8070..00000000 --- a/src/WireMock.Net/Admin/Mappings/FuncModel.cs +++ /dev/null @@ -1,7 +0,0 @@ -//namespace WireMock.Admin -//{ -// public class FuncModel -// { -// public string Name { get; set; } -// } -//} \ No newline at end of file diff --git a/src/WireMock.Net/Admin/Mappings/HeaderModel.cs b/src/WireMock.Net/Admin/Mappings/HeaderModel.cs index 855036a6..84eeed16 100644 --- a/src/WireMock.Net/Admin/Mappings/HeaderModel.cs +++ b/src/WireMock.Net/Admin/Mappings/HeaderModel.cs @@ -22,5 +22,13 @@ namespace WireMock.Admin.Mappings /// The matchers. /// public IList Matchers { get; set; } + + /// + /// Gets or sets the functions. + /// + /// + /// The functions. + /// + public string[] Funcs { get; set; } } } \ No newline at end of file diff --git a/src/WireMock.Net/Admin/Mappings/ParamModel.cs b/src/WireMock.Net/Admin/Mappings/ParamModel.cs index 28c3a58e..0135de2d 100644 --- a/src/WireMock.Net/Admin/Mappings/ParamModel.cs +++ b/src/WireMock.Net/Admin/Mappings/ParamModel.cs @@ -22,5 +22,13 @@ namespace WireMock.Admin.Mappings /// The values. /// public IList Values { get; set; } + + /// + /// Gets or sets the functions. + /// + /// + /// The functions. + /// + public string[] Funcs { get; set; } } } \ No newline at end of file diff --git a/src/WireMock.Net/Admin/Mappings/PathModel.cs b/src/WireMock.Net/Admin/Mappings/PathModel.cs index 4e7432f4..233a5f44 100644 --- a/src/WireMock.Net/Admin/Mappings/PathModel.cs +++ b/src/WireMock.Net/Admin/Mappings/PathModel.cs @@ -12,5 +12,13 @@ /// The matchers. /// public MatcherModel[] Matchers { get; set; } + + /// + /// Gets or sets the functions. + /// + /// + /// The functions. + /// + public string[] Funcs { get; set; } } } \ No newline at end of file diff --git a/src/WireMock.Net/Admin/Mappings/RequestModel.cs b/src/WireMock.Net/Admin/Mappings/RequestModel.cs index fa7574f8..24880c97 100644 --- a/src/WireMock.Net/Admin/Mappings/RequestModel.cs +++ b/src/WireMock.Net/Admin/Mappings/RequestModel.cs @@ -15,6 +15,14 @@ namespace WireMock.Admin.Mappings /// public object Path { get; set; } + /// + /// Gets or sets the Url. (Can be a string or a UrlModel) + /// + /// + /// The URL. + /// + public object Url { get; set; } + /// /// The methods /// diff --git a/src/WireMock.Net/Admin/Mappings/UrlModel.cs b/src/WireMock.Net/Admin/Mappings/UrlModel.cs new file mode 100644 index 00000000..0f5ce3af --- /dev/null +++ b/src/WireMock.Net/Admin/Mappings/UrlModel.cs @@ -0,0 +1,24 @@ +namespace WireMock.Admin.Mappings +{ + /// + /// UrlModel + /// + public class UrlModel + { + /// + /// Gets or sets the matchers. + /// + /// + /// The matchers. + /// + public MatcherModel[] Matchers { get; set; } + + /// + /// Gets or sets the functions. + /// + /// + /// The functions. + /// + public string[] Funcs { get; set; } + } +} diff --git a/src/WireMock.Net/Matchers/Request/RequestMessageBodyMatcher.cs b/src/WireMock.Net/Matchers/Request/RequestMessageBodyMatcher.cs index 70b3e82b..eb49c9b6 100644 --- a/src/WireMock.Net/Matchers/Request/RequestMessageBodyMatcher.cs +++ b/src/WireMock.Net/Matchers/Request/RequestMessageBodyMatcher.cs @@ -22,17 +22,17 @@ namespace WireMock.Matchers.Request /// /// The body function /// - private readonly Func _bodyFunc; + public Func Func { get; } /// /// The body data function /// - private readonly Func _bodyDataFunc; + public Func DataFunc { get; } /// /// The matcher. /// - public readonly IMatcher Matcher; + public IMatcher Matcher { get; } /// /// Initializes a new instance of the class. @@ -67,7 +67,7 @@ namespace WireMock.Matchers.Request public RequestMessageBodyMatcher([NotNull] Func func) { Check.NotNull(func, nameof(func)); - _bodyFunc = func; + Func = func; } /// @@ -79,7 +79,7 @@ namespace WireMock.Matchers.Request public RequestMessageBodyMatcher([NotNull] Func func) { Check.NotNull(func, nameof(func)); - _bodyDataFunc = func; + DataFunc = func; } /// @@ -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; } diff --git a/src/WireMock.Net/Matchers/Request/RequestMessageCookieMatcher.cs b/src/WireMock.Net/Matchers/Request/RequestMessageCookieMatcher.cs index f25f1f9c..e47b53c7 100644 --- a/src/WireMock.Net/Matchers/Request/RequestMessageCookieMatcher.cs +++ b/src/WireMock.Net/Matchers/Request/RequestMessageCookieMatcher.cs @@ -11,16 +11,16 @@ namespace WireMock.Matchers.Request /// public class RequestMessageCookieMatcher : IRequestMatcher { - private readonly Func, bool>[] _cookieFuncs; + /// + /// The funcs. + /// + public Func, bool>[] Funcs { get; } /// /// The name /// public string Name { get; } - /// - /// Gets the matchers. - /// /// /// The matchers. /// @@ -63,7 +63,7 @@ namespace WireMock.Matchers.Request { Check.NotNull(funcs, nameof(funcs)); - _cookieFuncs = funcs; + Funcs = funcs; } /// @@ -75,8 +75,8 @@ namespace WireMock.Matchers.Request /// 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; diff --git a/src/WireMock.Net/Matchers/Request/RequestMessageHeaderMatcher.cs b/src/WireMock.Net/Matchers/Request/RequestMessageHeaderMatcher.cs index a09b7223..dc15a941 100644 --- a/src/WireMock.Net/Matchers/Request/RequestMessageHeaderMatcher.cs +++ b/src/WireMock.Net/Matchers/Request/RequestMessageHeaderMatcher.cs @@ -11,16 +11,16 @@ namespace WireMock.Matchers.Request /// public class RequestMessageHeaderMatcher : IRequestMatcher { - private readonly Func, bool>[] _headerFuncs; + /// + /// The functions + /// + public Func, bool>[] Funcs { get; } /// /// The name /// public string Name { get; } - /// - /// Gets the matchers. - /// /// /// The matchers. /// @@ -63,7 +63,7 @@ namespace WireMock.Matchers.Request { Check.NotNull(funcs, nameof(funcs)); - _headerFuncs = funcs; + Funcs = funcs; } /// @@ -75,8 +75,8 @@ namespace WireMock.Matchers.Request /// 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; diff --git a/src/WireMock.Net/Matchers/Request/RequestMessageParamMatcher.cs b/src/WireMock.Net/Matchers/Request/RequestMessageParamMatcher.cs index eebb1574..489ade9e 100644 --- a/src/WireMock.Net/Matchers/Request/RequestMessageParamMatcher.cs +++ b/src/WireMock.Net/Matchers/Request/RequestMessageParamMatcher.cs @@ -12,7 +12,10 @@ namespace WireMock.Matchers.Request /// public class RequestMessageParamMatcher : IRequestMatcher { - private readonly Func>, bool>[] _funcs; + /// + /// The funcs + /// + public Func>, bool>[] Funcs { get; } /// /// The key @@ -49,7 +52,7 @@ namespace WireMock.Matchers.Request public RequestMessageParamMatcher([NotNull] params Func>, bool>[] funcs) { Check.NotNull(funcs, nameof(funcs)); - _funcs = funcs; + Funcs = funcs; } /// @@ -61,8 +64,8 @@ namespace WireMock.Matchers.Request /// 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(); diff --git a/src/WireMock.Net/Matchers/Request/RequestMessagePathMatcher.cs b/src/WireMock.Net/Matchers/Request/RequestMessagePathMatcher.cs index fe711ee9..51408c87 100644 --- a/src/WireMock.Net/Matchers/Request/RequestMessagePathMatcher.cs +++ b/src/WireMock.Net/Matchers/Request/RequestMessagePathMatcher.cs @@ -19,7 +19,7 @@ namespace WireMock.Matchers.Request /// /// The path functions /// - private readonly Func[] _pathFuncs; + public Func[] Funcs { get; } /// /// Initializes a new instance of the class. @@ -46,7 +46,7 @@ namespace WireMock.Matchers.Request public RequestMessagePathMatcher([NotNull] params Func[] funcs) { Check.NotNull(funcs, nameof(funcs)); - _pathFuncs = funcs; + Funcs = funcs; } /// @@ -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; } diff --git a/src/WireMock.Net/Matchers/Request/RequestMessageUrlMatcher.cs b/src/WireMock.Net/Matchers/Request/RequestMessageUrlMatcher.cs index 770bb2ec..56ee9771 100644 --- a/src/WireMock.Net/Matchers/Request/RequestMessageUrlMatcher.cs +++ b/src/WireMock.Net/Matchers/Request/RequestMessageUrlMatcher.cs @@ -14,12 +14,12 @@ namespace WireMock.Matchers.Request /// /// The matcher. /// - public readonly IReadOnlyList Matchers; + public IReadOnlyList Matchers { get; } /// /// The url functions /// - private readonly Func[] _urlFuncs; + public Func[] Funcs { get; } /// /// Initializes a new instance of the class. @@ -46,7 +46,7 @@ namespace WireMock.Matchers.Request public RequestMessageUrlMatcher([NotNull] params Func[] funcs) { Check.NotNull(funcs, nameof(funcs)); - _urlFuncs = funcs; + Funcs = funcs; } /// @@ -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; } diff --git a/src/WireMock.Net/Server/FluentMockServer.Admin.cs b/src/WireMock.Net/Server/FluentMockServer.Admin.cs index 91a05bea..11e22f3d 100644 --- a/src/WireMock.Net/Server/FluentMockServer.Admin.cs +++ b/src/WireMock.Net/Server/FluentMockServer.Admin.cs @@ -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(); + 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(); + var urlMatchers = request.GetRequestMessageMatchers(); var headerMatchers = request.GetRequestMessageMatchers(); var cookieMatchers = request.GetRequestMessageMatchers(); var paramsMatchers = request.GetRequestMessageMatchers(); @@ -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([CanBeNull] IEnumerable> funcs) + { + return funcs?.Select(Map).Where(x => x != null).ToArray(); + } + + private string Map([CanBeNull] Func func) + { + return func?.ToString(); + } + private IMatcher Map([CanBeNull] MatcherModel matcher) { if (matcher == null) diff --git a/src/WireMock.Net/Server/FluentMockServer.cs b/src/WireMock.Net/Server/FluentMockServer.cs index 9b6a2743..dd367f71 100644 --- a/src/WireMock.Net/Server/FluentMockServer.cs +++ b/src/WireMock.Net/Server/FluentMockServer.cs @@ -161,7 +161,7 @@ namespace WireMock.Server } /// - /// The reset. + /// Resets LogEntries and Mappings. /// public void Reset() { @@ -171,7 +171,7 @@ namespace WireMock.Server } /// - /// Resets the log entries. + /// Resets the LogEntries. /// public void ResetLogEntries() { @@ -203,7 +203,7 @@ namespace WireMock.Server } /// - /// Resets the mappings. + /// Resets the Mappings. /// public void ResetMappings() {