diff --git a/GitReleaseNotes.txt b/GitReleaseNotes.txt index 70081d4bb..b4b12ecc9 100644 --- a/GitReleaseNotes.txt +++ b/GitReleaseNotes.txt @@ -1,5 +1,5 @@ https://github.com/GitTools/GitReleaseNotes -GitReleaseNotes.exe . /OutputFile CHANGELOG.md /Version 1.0.3.16 +GitReleaseNotes.exe . /OutputFile CHANGELOG.md /Version 1.0.3.17 GitReleaseNotes.exe . /OutputFile CHANGELOG.md /allTags diff --git a/examples/WireMock.Net.ConsoleApplication/MainApp.cs b/examples/WireMock.Net.ConsoleApplication/MainApp.cs index 0bf8fccd1..30cea7714 100644 --- a/examples/WireMock.Net.ConsoleApplication/MainApp.cs +++ b/examples/WireMock.Net.ConsoleApplication/MainApp.cs @@ -1,4 +1,5 @@ using System; +using System.Net; using Newtonsoft.Json; using WireMock.Matchers; using WireMock.RequestBuilders; @@ -150,6 +151,28 @@ public static void Run() .WithHeader("Content-Type", "application/json") .WithBody(@"{ ""result"": ""data deleted with 200""}")); + server + .Given(Request.Create() + .WithPath("/needs-a-key") + .UsingGet() + .WithHeader("api-key", "*", MatchBehaviour.AcceptOnMatch) + .UsingAnyMethod()) + .RespondWith(Response.Create() + .WithStatusCode(HttpStatusCode.OK) + .WithBody(@"{ ""result"": ""api-key found""}")); + + server + .Given(Request.Create() + .WithPath("/needs-a-key") + .UsingGet() + .WithHeader("api-key", "*", MatchBehaviour.RejectOnMatch) + .UsingAnyMethod()) + .RespondWith(Response.Create() + .WithStatusCode(HttpStatusCode.Unauthorized) + .WithBody(@"{ ""result"": ""api-key missing""}")); + + + server .Given(Request.Create().WithPath("/nobody").UsingGet()) .RespondWith(Response.Create().WithDelay(TimeSpan.FromSeconds(1)) diff --git a/src/WireMock.Net.StandAlone/WireMock.Net.StandAlone.csproj b/src/WireMock.Net.StandAlone/WireMock.Net.StandAlone.csproj index e8d40dded..0c462bc5f 100644 --- a/src/WireMock.Net.StandAlone/WireMock.Net.StandAlone.csproj +++ b/src/WireMock.Net.StandAlone/WireMock.Net.StandAlone.csproj @@ -3,7 +3,7 @@ Lightweight StandAlone Http Mocking Server for .Net. WireMock.Net.StandAlone - 1.0.3.16 + 1.0.3.17 Stef Heyenrath net452;net46;netstandard1.3;netstandard2.0 true diff --git a/src/WireMock.Net/Admin/Mappings/MatcherModel.cs b/src/WireMock.Net/Admin/Mappings/MatcherModel.cs index 54266b80e..5033757ed 100644 --- a/src/WireMock.Net/Admin/Mappings/MatcherModel.cs +++ b/src/WireMock.Net/Admin/Mappings/MatcherModel.cs @@ -24,5 +24,10 @@ public class MatcherModel /// Gets or sets the ignore case. /// public bool? IgnoreCase { get; set; } + + /// + /// Reject on match. + /// + public bool? RejectOnMatch { get; set; } } } diff --git a/src/WireMock.Net/Matchers/ExactMatcher.cs b/src/WireMock.Net/Matchers/ExactMatcher.cs index 002a0cc28..e3dcb183c 100644 --- a/src/WireMock.Net/Matchers/ExactMatcher.cs +++ b/src/WireMock.Net/Matchers/ExactMatcher.cs @@ -12,21 +12,34 @@ public class ExactMatcher : IStringMatcher { private readonly string[] _values; + /// + public MatchBehaviour MatchBehaviour { get; } + + /// + /// Initializes a new instance of the class. + /// + /// The values. + public ExactMatcher([NotNull] params string[] values) : this(MatchBehaviour.AcceptOnMatch, values) + { + } + /// /// Initializes a new instance of the class. /// + /// The match behaviour. /// The values. - public ExactMatcher([NotNull] params string[] values) + public ExactMatcher(MatchBehaviour matchBehaviour, [NotNull] params string[] values) { Check.HasNoNulls(values, nameof(values)); _values = values; + MatchBehaviour = matchBehaviour; } /// public double IsMatch(string input) { - return MatchScores.ToScore(_values.Select(value => value.Equals(input))); + return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(_values.Select(value => value.Equals(input)))); } /// @@ -35,10 +48,7 @@ public string[] GetPatterns() return _values; } - /// - public string GetName() - { - return "ExactMatcher"; - } + /// + public string Name => "ExactMatcher"; } } \ No newline at end of file diff --git a/src/WireMock.Net/Matchers/ExactObjectMatcher.cs b/src/WireMock.Net/Matchers/ExactObjectMatcher.cs index 8963bb5da..3f8ebfb7a 100644 --- a/src/WireMock.Net/Matchers/ExactObjectMatcher.cs +++ b/src/WireMock.Net/Matchers/ExactObjectMatcher.cs @@ -1,5 +1,6 @@ using System.Linq; using JetBrains.Annotations; +using WireMock.Validation; namespace WireMock.Matchers { @@ -12,35 +13,59 @@ public class ExactObjectMatcher : IObjectMatcher private readonly object _object; private readonly byte[] _bytes; + /// + public MatchBehaviour MatchBehaviour { get; } + + /// + /// Initializes a new instance of the class. + /// + /// The value. + public ExactObjectMatcher([NotNull] object value) : this(MatchBehaviour.AcceptOnMatch, value) + { + } + /// /// Initializes a new instance of the class. /// + /// The match behaviour. /// The value. - public ExactObjectMatcher([NotNull] object value) + public ExactObjectMatcher(MatchBehaviour matchBehaviour, [NotNull] object value) { + Check.NotNull(value, nameof(value)); + _object = value; + MatchBehaviour = matchBehaviour; } /// /// Initializes a new instance of the class. /// /// The value. - public ExactObjectMatcher([NotNull] byte[] value) + public ExactObjectMatcher([NotNull] byte[] value) : this(MatchBehaviour.AcceptOnMatch, value) { + } + + /// + /// Initializes a new instance of the class. + /// + /// The match behaviour. + /// The value. + public ExactObjectMatcher(MatchBehaviour matchBehaviour, [NotNull] byte[] value) + { + Check.NotNull(value, nameof(value)); + _bytes = value; + MatchBehaviour = matchBehaviour; } /// public double IsMatch(object input) { bool equals = _object != null ? Equals(_object, input) : _bytes.SequenceEqual((byte[])input); - return MatchScores.ToScore(equals); + return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(equals)); } - /// - public string GetName() - { - return "ExactObjectMatcher"; - } + /// + public string Name => "ExactObjectMatcher"; } } \ No newline at end of file diff --git a/src/WireMock.Net/Matchers/IIgnoreCaseMatcher.cs b/src/WireMock.Net/Matchers/IIgnoreCaseMatcher.cs index 0655982c4..b809dbd7c 100644 --- a/src/WireMock.Net/Matchers/IIgnoreCaseMatcher.cs +++ b/src/WireMock.Net/Matchers/IIgnoreCaseMatcher.cs @@ -3,10 +3,11 @@ /// /// IIgnoreCaseMatcher /// + /// public interface IIgnoreCaseMatcher : IMatcher { /// - /// Ignore the case. + /// Ignore the case from the pattern. /// bool IgnoreCase { get; } } diff --git a/src/WireMock.Net/Matchers/IMatcher.cs b/src/WireMock.Net/Matchers/IMatcher.cs index 7462c1554..8aba538b0 100644 --- a/src/WireMock.Net/Matchers/IMatcher.cs +++ b/src/WireMock.Net/Matchers/IMatcher.cs @@ -8,7 +8,12 @@ public interface IMatcher /// /// Gets the name. /// - /// Name - string GetName(); + string Name { get; } + + + /// + /// Gets the match behaviour. + /// + MatchBehaviour MatchBehaviour { get; } } } \ No newline at end of file diff --git a/src/WireMock.Net/Matchers/IStringMatcher.cs b/src/WireMock.Net/Matchers/IStringMatcher.cs index 7d1ab3fdb..c580a33e7 100644 --- a/src/WireMock.Net/Matchers/IStringMatcher.cs +++ b/src/WireMock.Net/Matchers/IStringMatcher.cs @@ -3,6 +3,7 @@ /// /// IStringMatcher /// + /// public interface IStringMatcher : IMatcher { /// diff --git a/src/WireMock.Net/Matchers/JSONPathMatcher.cs b/src/WireMock.Net/Matchers/JSONPathMatcher.cs index 64f9acf72..53ef69154 100644 --- a/src/WireMock.Net/Matchers/JSONPathMatcher.cs +++ b/src/WireMock.Net/Matchers/JSONPathMatcher.cs @@ -1,6 +1,6 @@ -using System; -using System.Linq; +using System.Linq; using JetBrains.Annotations; +using Newtonsoft.Json; using Newtonsoft.Json.Linq; using WireMock.Validation; @@ -14,54 +14,69 @@ public class JsonPathMatcher : IStringMatcher, IObjectMatcher { private readonly string[] _patterns; + /// + public MatchBehaviour MatchBehaviour { get; } + /// /// Initializes a new instance of the class. /// /// The patterns. - public JsonPathMatcher([NotNull] params string[] patterns) + public JsonPathMatcher([NotNull] params string[] patterns) : this(MatchBehaviour.AcceptOnMatch, patterns) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The match behaviour. + /// The patterns. + public JsonPathMatcher(MatchBehaviour matchBehaviour, [NotNull] params string[] patterns) { Check.NotNull(patterns, nameof(patterns)); + MatchBehaviour = matchBehaviour; _patterns = patterns; } /// public double IsMatch(string input) { - if (input == null) + double match = MatchScores.Mismatch; + if (input != null) { - return MatchScores.Mismatch; + try + { + var jtoken = JToken.Parse(input); + match = IsMatch(jtoken); + } + catch (JsonException) + { + // just ignore JsonException + } } - try - { - var jtoken = JToken.Parse(input); - return IsMatch(jtoken); - } - catch (Exception) - { - return MatchScores.Mismatch; - } + return MatchBehaviourHelper.Convert(MatchBehaviour, match); } /// public double IsMatch(object input) { - if (input == null) + double match = MatchScores.Mismatch; + if (input != null) { - return MatchScores.Mismatch; + try + { + // Check if JToken or object + JToken jtoken = input is JToken token ? token : JObject.FromObject(input); + match = IsMatch(jtoken); + } + catch (JsonException) + { + // just ignore JsonException + } } - try - { - // Check if JToken or object - JToken jtoken = input is JToken token ? token : JObject.FromObject(input); - return IsMatch(jtoken); - } - catch (Exception) - { - return MatchScores.Mismatch; - } + return MatchBehaviourHelper.Convert(MatchBehaviour, match); } /// @@ -70,11 +85,8 @@ public string[] GetPatterns() return _patterns; } - /// - public string GetName() - { - return "JsonPathMatcher"; - } + /// + public string Name => "JsonPathMatcher"; private double IsMatch(JToken jtoken) { diff --git a/src/WireMock.Net/Matchers/MatchBehaviour.cs b/src/WireMock.Net/Matchers/MatchBehaviour.cs new file mode 100644 index 000000000..874b88eba --- /dev/null +++ b/src/WireMock.Net/Matchers/MatchBehaviour.cs @@ -0,0 +1,18 @@ +namespace WireMock.Matchers +{ + /// + /// MatchBehaviour + /// + public enum MatchBehaviour + { + /// + /// Accept on match (default) + /// + AcceptOnMatch, + + /// + /// Reject on match + /// + RejectOnMatch + } +} \ No newline at end of file diff --git a/src/WireMock.Net/Matchers/MatchBehaviourHelper.cs b/src/WireMock.Net/Matchers/MatchBehaviourHelper.cs new file mode 100644 index 000000000..a45dafec6 --- /dev/null +++ b/src/WireMock.Net/Matchers/MatchBehaviourHelper.cs @@ -0,0 +1,28 @@ + +namespace WireMock.Matchers +{ + internal static class MatchBehaviourHelper + { + /// + /// Converts the specified match behaviour and match value to a new match value. + /// + /// if AcceptOnMatch --> return match (default) + /// if RejectOnMatch and match = 0.0 --> return 1.0 + /// if RejectOnMatch and match = 0.? --> return 0.0 + /// if RejectOnMatch and match = 1.0 --> return 0.0 + /// + /// + /// The match behaviour. + /// The match. + /// match value + internal static double Convert(MatchBehaviour matchBehaviour, double match) + { + if (matchBehaviour == MatchBehaviour.AcceptOnMatch) + { + return match; + } + + return match <= MatchScores.Tolerance ? MatchScores.Perfect : MatchScores.Mismatch; + } + } +} \ No newline at end of file diff --git a/src/WireMock.Net/Matchers/RegexMatcher.cs b/src/WireMock.Net/Matchers/RegexMatcher.cs index 388a6a368..4947cdc4b 100644 --- a/src/WireMock.Net/Matchers/RegexMatcher.cs +++ b/src/WireMock.Net/Matchers/RegexMatcher.cs @@ -9,32 +9,57 @@ namespace WireMock.Matchers /// /// Regular Expression Matcher /// - /// + /// + /// public class RegexMatcher : IStringMatcher, IIgnoreCaseMatcher { private readonly string[] _patterns; private readonly Regex[] _expressions; + /// + public MatchBehaviour MatchBehaviour { get; } + /// /// Initializes a new instance of the class. /// /// The pattern. - /// IgnoreCase - public RegexMatcher([NotNull, RegexPattern] string pattern, bool ignoreCase = false) : this(new [] { pattern }, ignoreCase ) + /// Ignore the case from the pattern. + public RegexMatcher([NotNull, RegexPattern] string pattern, bool ignoreCase = false) : this(new[] { pattern }, ignoreCase) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The match behaviour. + /// The pattern. + /// Ignore the case from the pattern. + public RegexMatcher(MatchBehaviour matchBehaviour, [NotNull, RegexPattern] string pattern, bool ignoreCase = false) : this(matchBehaviour, new[] { pattern }, ignoreCase) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The patterns. + /// Ignore the case from the pattern. + public RegexMatcher([NotNull, RegexPattern] string[] patterns, bool ignoreCase = false) : this(MatchBehaviour.AcceptOnMatch, patterns, ignoreCase) { } /// /// Initializes a new instance of the class. /// + /// The match behaviour. /// The patterns. - /// IgnoreCase - public RegexMatcher([NotNull, RegexPattern] string[] patterns, bool ignoreCase = false) + /// Ignore the case from the pattern. + public RegexMatcher(MatchBehaviour matchBehaviour, [NotNull, RegexPattern] string[] patterns, bool ignoreCase = false) { Check.NotNull(patterns, nameof(patterns)); _patterns = patterns; IgnoreCase = ignoreCase; + MatchBehaviour = matchBehaviour; RegexOptions options = RegexOptions.Compiled; if (ignoreCase) @@ -48,19 +73,20 @@ public RegexMatcher([NotNull, RegexPattern] string[] patterns, bool ignoreCase = /// public double IsMatch(string input) { - if (input == null) + double match = MatchScores.Mismatch; + if (input != null) { - return MatchScores.Mismatch; - } - - try - { - return MatchScores.ToScore(_expressions.Select(e => e.IsMatch(input))); - } - catch (Exception) - { - return MatchScores.Mismatch; + try + { + match = MatchScores.ToScore(_expressions.Select(e => e.IsMatch(input))); + } + catch (Exception) + { + // just ignore exception + } } + + return MatchBehaviourHelper.Convert(MatchBehaviour, match); } /// @@ -69,11 +95,8 @@ public virtual string[] GetPatterns() return _patterns; } - /// - public virtual string GetName() - { - return "RegexMatcher"; - } + /// + public virtual string Name => "RegexMatcher"; /// public bool IgnoreCase { get; } diff --git a/src/WireMock.Net/Matchers/Request/RequestMessageBodyMatcher.cs b/src/WireMock.Net/Matchers/Request/RequestMessageBodyMatcher.cs index 2753edd5e..ad25e470d 100644 --- a/src/WireMock.Net/Matchers/Request/RequestMessageBodyMatcher.cs +++ b/src/WireMock.Net/Matchers/Request/RequestMessageBodyMatcher.cs @@ -32,24 +32,27 @@ public class RequestMessageBodyMatcher : IRequestMatcher /// /// Initializes a new instance of the class. /// + /// The match behaviour. /// The body. - public RequestMessageBodyMatcher([NotNull] string body) : this(new SimMetricsMatcher(body)) + public RequestMessageBodyMatcher(MatchBehaviour matchBehaviour, [NotNull] string body) : this(new WildcardMatcher(matchBehaviour, body)) { } /// /// Initializes a new instance of the class. /// + /// The match behaviour. /// The body. - public RequestMessageBodyMatcher([NotNull] byte[] body) : this(new ExactObjectMatcher(body)) + public RequestMessageBodyMatcher(MatchBehaviour matchBehaviour, [NotNull] byte[] body) : this(new ExactObjectMatcher(matchBehaviour, body)) { } /// /// Initializes a new instance of the class. /// + /// The match behaviour. /// The body. - public RequestMessageBodyMatcher([NotNull] object body) : this(new ExactObjectMatcher(body)) + public RequestMessageBodyMatcher(MatchBehaviour matchBehaviour, [NotNull] object body) : this(new ExactObjectMatcher(matchBehaviour, body)) { } diff --git a/src/WireMock.Net/Matchers/Request/RequestMessageClientIPMatcher.cs b/src/WireMock.Net/Matchers/Request/RequestMessageClientIPMatcher.cs index 6b2d5d695..4ea2e2eb8 100644 --- a/src/WireMock.Net/Matchers/Request/RequestMessageClientIPMatcher.cs +++ b/src/WireMock.Net/Matchers/Request/RequestMessageClientIPMatcher.cs @@ -25,7 +25,8 @@ public class RequestMessageClientIPMatcher : IRequestMatcher /// Initializes a new instance of the class. /// /// The clientIPs. - public RequestMessageClientIPMatcher([NotNull] params string[] clientIPs) : this(clientIPs.Select(ip => new WildcardMatcher(ip)).Cast().ToArray()) + /// The match behaviour. + public RequestMessageClientIPMatcher(MatchBehaviour matchBehaviour, [NotNull] params string[] clientIPs) : this(clientIPs.Select(ip => new WildcardMatcher(matchBehaviour, ip)).Cast().ToArray()) { } diff --git a/src/WireMock.Net/Matchers/Request/RequestMessageCookieMatcher.cs b/src/WireMock.Net/Matchers/Request/RequestMessageCookieMatcher.cs index 795bd243a..8e5d9b4a0 100644 --- a/src/WireMock.Net/Matchers/Request/RequestMessageCookieMatcher.cs +++ b/src/WireMock.Net/Matchers/Request/RequestMessageCookieMatcher.cs @@ -11,6 +11,8 @@ namespace WireMock.Matchers.Request /// public class RequestMessageCookieMatcher : IRequestMatcher { + private readonly MatchBehaviour _matchBehaviour; + /// /// The funcs. /// @@ -29,16 +31,18 @@ public class RequestMessageCookieMatcher : IRequestMatcher /// /// Initializes a new instance of the class. /// + /// The match behaviour. /// The name. /// The pattern. /// The ignoreCase. - public RequestMessageCookieMatcher([NotNull] string name, [NotNull] string pattern, bool ignoreCase = true) + public RequestMessageCookieMatcher(MatchBehaviour matchBehaviour, [NotNull] string name, [NotNull] string pattern, bool ignoreCase = true) { Check.NotNull(name, nameof(name)); Check.NotNull(pattern, nameof(pattern)); + _matchBehaviour = matchBehaviour; Name = name; - Matchers = new IStringMatcher[] { new WildcardMatcher(pattern, ignoreCase) }; + Matchers = new IStringMatcher[] { new WildcardMatcher(matchBehaviour, pattern, ignoreCase) }; } /// @@ -77,7 +81,7 @@ private double IsMatch(RequestMessage requestMessage) { if (requestMessage.Cookies == null) { - return MatchScores.Mismatch; + return MatchBehaviourHelper.Convert(_matchBehaviour, MatchScores.Mismatch); } if (Funcs != null) @@ -92,7 +96,7 @@ private double IsMatch(RequestMessage requestMessage) if (!requestMessage.Cookies.ContainsKey(Name)) { - return MatchScores.Mismatch; + return MatchBehaviourHelper.Convert(_matchBehaviour, MatchScores.Mismatch); } string value = requestMessage.Cookies[Name]; diff --git a/src/WireMock.Net/Matchers/Request/RequestMessageHeaderMatcher.cs b/src/WireMock.Net/Matchers/Request/RequestMessageHeaderMatcher.cs index 94800509c..f8e16d5cc 100644 --- a/src/WireMock.Net/Matchers/Request/RequestMessageHeaderMatcher.cs +++ b/src/WireMock.Net/Matchers/Request/RequestMessageHeaderMatcher.cs @@ -13,6 +13,8 @@ namespace WireMock.Matchers.Request /// public class RequestMessageHeaderMatcher : IRequestMatcher { + private readonly MatchBehaviour _matchBehaviour; + /// /// The functions /// @@ -33,14 +35,16 @@ public class RequestMessageHeaderMatcher : IRequestMatcher /// /// The name. /// The pattern. - /// if set to true [ignore case]. - public RequestMessageHeaderMatcher([NotNull] string name, [NotNull] string pattern, bool ignoreCase = true) + /// Ignore the case from the pattern. + /// The match behaviour. + public RequestMessageHeaderMatcher(MatchBehaviour matchBehaviour, [NotNull] string name, [NotNull] string pattern, bool ignoreCase) { Check.NotNull(name, nameof(name)); Check.NotNull(pattern, nameof(pattern)); + _matchBehaviour = matchBehaviour; Name = name; - Matchers = new IStringMatcher[] { new WildcardMatcher(pattern, ignoreCase) }; + Matchers = new IStringMatcher[] { new WildcardMatcher(matchBehaviour, pattern, ignoreCase) }; } /// @@ -48,14 +52,16 @@ public RequestMessageHeaderMatcher([NotNull] string name, [NotNull] string patte /// /// The name. /// The patterns. - /// if set to true [ignore case]. - public RequestMessageHeaderMatcher([NotNull] string name, [NotNull] string[] patterns, bool ignoreCase = true) + /// Ignore the case from the pattern. + /// The match behaviour. + public RequestMessageHeaderMatcher(MatchBehaviour matchBehaviour, [NotNull] string name, [NotNull] string[] patterns, bool ignoreCase) { Check.NotNull(name, nameof(name)); Check.NotNull(patterns, nameof(patterns)); + _matchBehaviour = matchBehaviour; Name = name; - Matchers = patterns.Select(pattern => new WildcardMatcher(pattern, ignoreCase)).Cast().ToArray(); + Matchers = patterns.Select(pattern => new WildcardMatcher(matchBehaviour, pattern, ignoreCase)).Cast().ToArray(); } /// @@ -94,7 +100,7 @@ private double IsMatch(RequestMessage requestMessage) { if (requestMessage.Headers == null) { - return MatchScores.Mismatch; + return MatchBehaviourHelper.Convert(_matchBehaviour, MatchScores.Mismatch); } if (Funcs != null) @@ -109,7 +115,7 @@ private double IsMatch(RequestMessage requestMessage) if (!requestMessage.Headers.ContainsKey(Name)) { - return MatchScores.Mismatch; + return MatchBehaviourHelper.Convert(_matchBehaviour, MatchScores.Mismatch); } WireMockList list = requestMessage.Headers[Name]; diff --git a/src/WireMock.Net/Matchers/Request/RequestMessageMethodMatcher.cs b/src/WireMock.Net/Matchers/Request/RequestMessageMethodMatcher.cs index 105df40a1..59fec1d4b 100644 --- a/src/WireMock.Net/Matchers/Request/RequestMessageMethodMatcher.cs +++ b/src/WireMock.Net/Matchers/Request/RequestMessageMethodMatcher.cs @@ -9,6 +9,8 @@ namespace WireMock.Matchers.Request /// internal class RequestMessageMethodMatcher : IRequestMatcher { + private readonly MatchBehaviour _matchBehaviour; + /// /// The methods /// @@ -17,26 +19,20 @@ internal class RequestMessageMethodMatcher : IRequestMatcher /// /// Initializes a new instance of the class. /// - /// - /// The verb. - /// - public RequestMessageMethodMatcher([NotNull] params string[] methods) + /// The match behaviour. + /// The methods. + public RequestMessageMethodMatcher(MatchBehaviour matchBehaviour, [NotNull] params string[] methods) { Check.NotNull(methods, nameof(methods)); + _matchBehaviour = matchBehaviour; + Methods = methods.Select(v => v.ToLower()).ToArray(); } - /// - /// Determines whether the specified RequestMessage is match. - /// - /// The RequestMessage. - /// The RequestMatchResult. - /// - /// A value between 0.0 - 1.0 of the similarity. - /// + /// public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult) { - double score = IsMatch(requestMessage); + double score = MatchBehaviourHelper.Convert(_matchBehaviour, IsMatch(requestMessage)); return requestMatchResult.AddScore(GetType(), score); } diff --git a/src/WireMock.Net/Matchers/Request/RequestMessageParamMatcher.cs b/src/WireMock.Net/Matchers/Request/RequestMessageParamMatcher.cs index 604f0880d..be6fcee5f 100644 --- a/src/WireMock.Net/Matchers/Request/RequestMessageParamMatcher.cs +++ b/src/WireMock.Net/Matchers/Request/RequestMessageParamMatcher.cs @@ -12,6 +12,8 @@ namespace WireMock.Matchers.Request /// public class RequestMessageParamMatcher : IRequestMatcher { + private readonly MatchBehaviour _matchBehaviour; + /// /// The funcs /// @@ -30,20 +32,23 @@ public class RequestMessageParamMatcher : IRequestMatcher /// /// Initializes a new instance of the class. /// + /// The match behaviour. /// The key. - public RequestMessageParamMatcher([NotNull] string key) : this(key, null) + public RequestMessageParamMatcher(MatchBehaviour matchBehaviour, [NotNull] string key) : this(matchBehaviour, key, null) { } /// /// Initializes a new instance of the class. /// + /// The match behaviour. /// The key. /// The values. - public RequestMessageParamMatcher([NotNull] string key, [CanBeNull] IEnumerable values) + public RequestMessageParamMatcher(MatchBehaviour matchBehaviour, [NotNull] string key, [CanBeNull] IEnumerable values) { Check.NotNull(key, nameof(key)); + _matchBehaviour = matchBehaviour; Key = key; Values = values; } @@ -62,7 +67,7 @@ public RequestMessageParamMatcher([NotNull] params Func public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult) { - double score = IsMatch(requestMessage); + double score = MatchBehaviourHelper.Convert(_matchBehaviour, IsMatch(requestMessage)); return requestMatchResult.AddScore(GetType(), score); } diff --git a/src/WireMock.Net/Matchers/Request/RequestMessagePathMatcher.cs b/src/WireMock.Net/Matchers/Request/RequestMessagePathMatcher.cs index cec66a6af..0504e5f29 100644 --- a/src/WireMock.Net/Matchers/Request/RequestMessagePathMatcher.cs +++ b/src/WireMock.Net/Matchers/Request/RequestMessagePathMatcher.cs @@ -12,7 +12,7 @@ namespace WireMock.Matchers.Request public class RequestMessagePathMatcher : IRequestMatcher { /// - /// The matcher. + /// The matchers /// public IReadOnlyList Matchers { get; } @@ -24,8 +24,9 @@ public class RequestMessagePathMatcher : IRequestMatcher /// /// Initializes a new instance of the class. /// + /// The match behaviour. /// The paths. - public RequestMessagePathMatcher([NotNull] params string[] paths) : this(paths.Select(path => new WildcardMatcher(path)).Cast().ToArray()) + public RequestMessagePathMatcher(MatchBehaviour matchBehaviour, [NotNull] params string[] paths) : this(paths.Select(path => new WildcardMatcher(matchBehaviour, path)).Cast().ToArray()) { } @@ -36,6 +37,7 @@ public class RequestMessagePathMatcher : IRequestMatcher public RequestMessagePathMatcher([NotNull] params IStringMatcher[] matchers) { Check.NotNull(matchers, nameof(matchers)); + Matchers = matchers; } @@ -46,6 +48,7 @@ public RequestMessagePathMatcher([NotNull] params IStringMatcher[] matchers) public RequestMessagePathMatcher([NotNull] params Func[] funcs) { Check.NotNull(funcs, nameof(funcs)); + Funcs = funcs; } diff --git a/src/WireMock.Net/Matchers/Request/RequestMessageUrlMatcher.cs b/src/WireMock.Net/Matchers/Request/RequestMessageUrlMatcher.cs index ea256ddcc..47432cb20 100644 --- a/src/WireMock.Net/Matchers/Request/RequestMessageUrlMatcher.cs +++ b/src/WireMock.Net/Matchers/Request/RequestMessageUrlMatcher.cs @@ -24,8 +24,9 @@ public class RequestMessageUrlMatcher : IRequestMatcher /// /// Initializes a new instance of the class. /// + /// The match behaviour. /// The urls. - public RequestMessageUrlMatcher([NotNull] params string[] urls) : this(urls.Select(url => new WildcardMatcher(url)).Cast().ToArray()) + public RequestMessageUrlMatcher(MatchBehaviour matchBehaviour, [NotNull] params string[] urls) : this(urls.Select(url => new WildcardMatcher(matchBehaviour, url)).Cast().ToArray()) { } diff --git a/src/WireMock.Net/Matchers/SimMetricsMatcher.cs b/src/WireMock.Net/Matchers/SimMetricsMatcher.cs index 13e6ee028..4cf08b84d 100644 --- a/src/WireMock.Net/Matchers/SimMetricsMatcher.cs +++ b/src/WireMock.Net/Matchers/SimMetricsMatcher.cs @@ -16,12 +16,25 @@ public class SimMetricsMatcher : IStringMatcher private readonly string[] _patterns; private readonly SimMetricType _simMetricType; + /// + public MatchBehaviour MatchBehaviour { get; } + + /// + /// Initializes a new instance of the class. + /// + /// The pattern. + /// The SimMetric Type + public SimMetricsMatcher([NotNull] string pattern, SimMetricType simMetricType = SimMetricType.Levenstein) : this(new[] { pattern }, simMetricType) + { + } + /// /// Initializes a new instance of the class. /// + /// The match behaviour. /// The pattern. /// The SimMetric Type - public SimMetricsMatcher([NotNull] string pattern, SimMetricType simMetricType = SimMetricType.Levenstein) : this(new [] { pattern }, simMetricType) + public SimMetricsMatcher(MatchBehaviour matchBehaviour, [NotNull] string pattern, SimMetricType simMetricType = SimMetricType.Levenstein) : this(matchBehaviour, new[] { pattern }, simMetricType) { } @@ -30,10 +43,21 @@ public SimMetricsMatcher([NotNull] string pattern, SimMetricType simMetricType = /// /// The patterns. /// The SimMetric Type - public SimMetricsMatcher([NotNull] string[] patterns, SimMetricType simMetricType = SimMetricType.Levenstein) + public SimMetricsMatcher([NotNull] string[] patterns, SimMetricType simMetricType = SimMetricType.Levenstein) : this(MatchBehaviour.AcceptOnMatch, patterns, simMetricType) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The match behaviour. + /// The patterns. + /// The SimMetric Type + public SimMetricsMatcher(MatchBehaviour matchBehaviour, [NotNull] string[] patterns, SimMetricType simMetricType = SimMetricType.Levenstein) { Check.NotNullOrEmpty(patterns, nameof(patterns)); + MatchBehaviour = matchBehaviour; _patterns = patterns; _simMetricType = simMetricType; } @@ -43,7 +67,7 @@ public double IsMatch(string input) { IStringMetric m = GetStringMetricType(); - return MatchScores.ToScore(_patterns.Select(p => m.GetSimilarity(p, input))); + return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(_patterns.Select(p => m.GetSimilarity(p, input)))); } private IStringMetric GetStringMetricType() @@ -95,10 +119,7 @@ public string[] GetPatterns() return _patterns; } - /// - public string GetName() - { - return $"SimMetricsMatcher.{_simMetricType}"; - } + /// + public string Name => $"SimMetricsMatcher.{_simMetricType}"; } } \ No newline at end of file diff --git a/src/WireMock.Net/Matchers/WildcardMatcher.cs b/src/WireMock.Net/Matchers/WildcardMatcher.cs index caed77eca..5b735f52c 100644 --- a/src/WireMock.Net/Matchers/WildcardMatcher.cs +++ b/src/WireMock.Net/Matchers/WildcardMatcher.cs @@ -17,16 +17,36 @@ public class WildcardMatcher : RegexMatcher /// /// The pattern. /// IgnoreCase - public WildcardMatcher([NotNull] string pattern, bool ignoreCase = false) : this(new [] { pattern }, ignoreCase) + public WildcardMatcher([NotNull] string pattern, bool ignoreCase = false) : this(new[] { pattern }, ignoreCase) { } /// /// Initializes a new instance of the class. /// + /// The match behaviour. + /// The pattern. + /// IgnoreCase + public WildcardMatcher(MatchBehaviour matchBehaviour, [NotNull] string pattern, bool ignoreCase = false) : this(matchBehaviour, new[] { pattern }, ignoreCase) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The patterns. + /// IgnoreCase + public WildcardMatcher([NotNull] string[] patterns, bool ignoreCase = false) : this(MatchBehaviour.AcceptOnMatch, patterns, ignoreCase) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The match behaviour. /// The patterns. /// IgnoreCase - public WildcardMatcher([NotNull] string[] patterns, bool ignoreCase = false) : base(patterns.Select(pattern => "^" + Regex.Escape(pattern).Replace(@"\*", ".*").Replace(@"\?", ".") + "$").ToArray(), ignoreCase) + public WildcardMatcher(MatchBehaviour matchBehaviour, [NotNull] string[] patterns, bool ignoreCase = false) : base(matchBehaviour, patterns.Select(pattern => "^" + Regex.Escape(pattern).Replace(@"\*", ".*").Replace(@"\?", ".") + "$").ToArray(), ignoreCase) { _patterns = patterns; } @@ -37,10 +57,7 @@ public override string[] GetPatterns() return _patterns; } - /// - public override string GetName() - { - return "WildcardMatcher"; - } + /// + public override string Name => "WildcardMatcher"; } } \ No newline at end of file diff --git a/src/WireMock.Net/Matchers/XPathMatcher.cs b/src/WireMock.Net/Matchers/XPathMatcher.cs index 6586da898..ec09ffe11 100644 --- a/src/WireMock.Net/Matchers/XPathMatcher.cs +++ b/src/WireMock.Net/Matchers/XPathMatcher.cs @@ -17,38 +17,52 @@ public class XPathMatcher : IStringMatcher { private readonly string[] _patterns; + /// + public MatchBehaviour MatchBehaviour { get; } + + /// + /// Initializes a new instance of the class. + /// + /// The patterns. + public XPathMatcher([NotNull] params string[] patterns) : this(MatchBehaviour.AcceptOnMatch, patterns) + { + } + /// /// Initializes a new instance of the class. /// + /// The match behaviour. /// The patterns. - public XPathMatcher([NotNull] params string[] patterns) + public XPathMatcher(MatchBehaviour matchBehaviour, [NotNull] params string[] patterns) { Check.NotNull(patterns, nameof(patterns)); + MatchBehaviour = matchBehaviour; _patterns = patterns; } /// public double IsMatch(string input) { - if (input == null) + double match = MatchScores.Mismatch; + if (input != null) { - return MatchScores.Mismatch; - } - - try - { - var nav = new XmlDocument { InnerXml = input }.CreateNavigator(); + try + { + var nav = new XmlDocument { InnerXml = input }.CreateNavigator(); #if NETSTANDARD1_3 - return MatchScores.ToScore(_patterns.Select(p => true.Equals(nav.Evaluate($"boolean({p})")))); + match = MatchScores.ToScore(_patterns.Select(p => true.Equals(nav.Evaluate($"boolean({p})")))); #else - return MatchScores.ToScore(_patterns.Select(p => true.Equals(nav.XPath2Evaluate($"boolean({p})")))); + match = MatchScores.ToScore(_patterns.Select(p => true.Equals(nav.XPath2Evaluate($"boolean({p})")))); #endif + } + catch (Exception) + { + // just ignore exception + } } - catch (Exception) - { - return MatchScores.Mismatch; - } + + return MatchBehaviourHelper.Convert(MatchBehaviour, match); } /// @@ -57,10 +71,7 @@ public string[] GetPatterns() return _patterns; } - /// - public string GetName() - { - return "XPathMatcher"; - } + /// + public string Name => "XPathMatcher"; } } \ No newline at end of file diff --git a/src/WireMock.Net/RequestBuilders/IBodyRequestBuilder.cs b/src/WireMock.Net/RequestBuilders/IBodyRequestBuilder.cs index 49c4f78f4..b3e686f9a 100644 --- a/src/WireMock.Net/RequestBuilders/IBodyRequestBuilder.cs +++ b/src/WireMock.Net/RequestBuilders/IBodyRequestBuilder.cs @@ -20,39 +20,42 @@ public interface IBodyRequestBuilder /// WithBody: Body as string /// /// The body. + /// The match behaviour. /// The . - IRequestBuilder WithBody(string body); + IRequestBuilder WithBody(string body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch); /// /// WithBody: Body as byte[] /// /// The body. + /// The match behaviour. /// The . - IRequestBuilder WithBody(byte[] body); + IRequestBuilder WithBody(byte[] body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch); /// /// WithBody: Body as object /// /// The body. + /// The match behaviour. /// The . - IRequestBuilder WithBody(object body); + IRequestBuilder WithBody(object body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch); /// - ///WithBody: func (string) + /// WithBody: func (string) /// /// The function. /// The . IRequestBuilder WithBody([NotNull] Func func); /// - ///WithBody: func (byte[]) + /// WithBody: func (byte[]) /// /// The function. /// The . IRequestBuilder WithBody([NotNull] Func func); /// - ///WithBody: func (object) + /// WithBody: func (object) /// /// The function. /// The . diff --git a/src/WireMock.Net/RequestBuilders/IClientIPRequestBuilder.cs b/src/WireMock.Net/RequestBuilders/IClientIPRequestBuilder.cs index a830ba68a..a9bb8e4f2 100644 --- a/src/WireMock.Net/RequestBuilders/IClientIPRequestBuilder.cs +++ b/src/WireMock.Net/RequestBuilders/IClientIPRequestBuilder.cs @@ -10,21 +10,29 @@ namespace WireMock.RequestBuilders public interface IClientIPRequestBuilder : IUrlAndPathRequestBuilder { /// - /// The with ClientIP. + /// WithClientIP: add matching on ClientIP matchers. /// /// The matchers. /// The . IRequestBuilder WithClientIP([NotNull] params IStringMatcher[] matchers); /// - /// The with ClientIP. + /// WithClientIP: add matching on clientIPs. /// /// The clientIPs. /// The . IRequestBuilder WithClientIP([NotNull] params string[] clientIPs); /// - /// The with ClientIP. + /// WithClientIP: add matching on clientIPs and matchBehaviour. + /// + /// The match behaviour. + /// The clientIPs. + /// The . + IRequestBuilder WithClientIP(MatchBehaviour matchBehaviour, [NotNull] params string[] clientIPs); + + /// + /// WithClientIP: add matching on ClientIP funcs. /// /// The path funcs. /// The . diff --git a/src/WireMock.Net/RequestBuilders/IHeadersAndCookiesRequestBuilder.cs b/src/WireMock.Net/RequestBuilders/IHeadersAndCookiesRequestBuilder.cs index 78b210eda..a8b1b8533 100644 --- a/src/WireMock.Net/RequestBuilders/IHeadersAndCookiesRequestBuilder.cs +++ b/src/WireMock.Net/RequestBuilders/IHeadersAndCookiesRequestBuilder.cs @@ -12,25 +12,45 @@ namespace WireMock.RequestBuilders public interface IHeadersAndCookiesRequestBuilder : IBodyRequestBuilder, IRequestMatcher, IParamsRequestBuilder { /// - /// Add Header matching based on name, pattern and ignoreCase. + /// WithHeader: matching based on name, pattern and matchBehaviour. /// /// The name. /// The pattern. - /// ignore Case + /// The match behaviour. /// The . - IRequestBuilder WithHeader([NotNull] string name, string pattern, bool ignoreCase = true); + IRequestBuilder WithHeader([NotNull] string name, string pattern, MatchBehaviour matchBehaviour); /// - /// Add Header matching based on name, patterns and ignoreCase. + /// WithHeader: matching based on name, pattern, ignoreCase and matchBehaviour. + /// + /// The name. + /// The pattern. + /// Ignore the case from the pattern. + /// The match behaviour. + /// The . + IRequestBuilder WithHeader([NotNull] string name, string pattern, bool ignoreCase = true, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch); + + /// + /// WithHeader: matching based on name, patterns and matchBehaviour. + /// + /// The name. + /// The patterns. + /// The match behaviour. + /// The . + IRequestBuilder WithHeader([NotNull] string name, string[] patterns, MatchBehaviour matchBehaviour); + + /// + /// WithHeader: matching based on name, patterns, ignoreCase and matchBehaviour. /// /// The name. /// The patterns. - /// ignore Case + /// Ignore the case from the pattern. + /// The match behaviour. /// The . - IRequestBuilder WithHeader([NotNull] string name, string[] patterns, bool ignoreCase = true); + IRequestBuilder WithHeader([NotNull] string name, string[] patterns, bool ignoreCase = true, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch); /// - /// The with header. + /// WithHeader: matching based on name and IStringMatcher[]. /// /// The name. /// The matchers. @@ -38,23 +58,24 @@ public interface IHeadersAndCookiesRequestBuilder : IBodyRequestBuilder, IReques IRequestBuilder WithHeader([NotNull] string name, [NotNull] params IStringMatcher[] matchers); /// - /// The with header. + /// WithHeader: matching based on functions. /// /// The headers funcs. /// The . IRequestBuilder WithHeader([NotNull] params Func, bool>[] funcs); /// - /// The with cookie. + /// WithCookie: cookie matching based on name, pattern, ignoreCase and matchBehaviour. /// /// The name. /// The pattern. - /// ignore Case + /// Ignore the case from the pattern. + /// The match behaviour. /// The . - IRequestBuilder WithCookie([NotNull] string name, string pattern, bool ignoreCase = true); + IRequestBuilder WithCookie([NotNull] string name, string pattern, bool ignoreCase = true, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch); /// - /// The with cookie. + /// WithCookie: matching based on name and IStringMatcher[]. /// /// The name. /// The matchers. @@ -62,7 +83,7 @@ public interface IHeadersAndCookiesRequestBuilder : IBodyRequestBuilder, IReques IRequestBuilder WithCookie([NotNull] string name, [NotNull] params IStringMatcher[] matchers); /// - /// The with cookie. + /// WithCookie: matching based on functions. /// /// The funcs. /// The . diff --git a/src/WireMock.Net/RequestBuilders/IMethodRequestBuilder.cs b/src/WireMock.Net/RequestBuilders/IMethodRequestBuilder.cs index b5a43b904..a14b34441 100644 --- a/src/WireMock.Net/RequestBuilders/IMethodRequestBuilder.cs +++ b/src/WireMock.Net/RequestBuilders/IMethodRequestBuilder.cs @@ -1,4 +1,6 @@ -using JetBrains.Annotations; +using System; +using JetBrains.Annotations; +using WireMock.Matchers; namespace WireMock.RequestBuilders { @@ -8,66 +10,81 @@ namespace WireMock.RequestBuilders public interface IMethodRequestBuilder : IHeadersAndCookiesRequestBuilder { /// - /// The using delete. + /// UsingDelete: add HTTP Method matching on `delete` and matchBehaviour (optional). /// - /// - /// The . - /// - IRequestBuilder UsingDelete(); + /// The match behaviour. + /// The . + IRequestBuilder UsingDelete(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch); + + /// + /// UsingGet: add HTTP Method matching on `get` and matchBehaviour (optional). + /// + /// The match behaviour. + /// The . + IRequestBuilder UsingGet(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch); /// - /// The using get. + /// Add HTTP Method matching on `head` and matchBehaviour (optional). /// - /// - /// The . - /// - IRequestBuilder UsingGet(); + /// The match behaviour. + /// The . + IRequestBuilder UsingHead(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch); /// - /// The using head. + /// UsingPost: add HTTP Method matching on `post` and matchBehaviour (optional). /// - /// - /// The . - /// - IRequestBuilder UsingHead(); + /// The match behaviour. + /// The . + IRequestBuilder UsingPost(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch); /// - /// The using post. + /// UsingPatch: add HTTP Method matching on `patch` and matchBehaviour (optional). /// - /// - /// The . - /// - IRequestBuilder UsingPost(); + /// The match behaviour. + /// The . + IRequestBuilder UsingPatch(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch); /// - /// The using patch. + /// UsingPut: add HTTP Method matching on `put` and matchBehaviour (optional). /// - /// - /// The . - /// - IRequestBuilder UsingPatch(); + /// The match behaviour. + /// The . + IRequestBuilder UsingPut(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch); /// - /// The using put. + /// UsingAnyMethod: add HTTP Method matching on any method. /// - /// - /// The . - /// - IRequestBuilder UsingPut(); + /// The . + IRequestBuilder UsingAnyMethod(); /// - /// The using any verb. + /// UsingAnyVerb: add HTTP Method matching on any method. /// - /// - /// The . - /// + /// The . + [Obsolete] IRequestBuilder UsingAnyVerb(); /// - /// The using verb. + /// UsingMethod: add HTTP Method matching on any methods and matchBehaviour. + /// + /// The match behaviour. + /// The methods. + /// The . + IRequestBuilder UsingMethod(MatchBehaviour matchBehaviour, [NotNull] params string[] methods); + + /// + /// UsingMethod: add HTTP Method matching on any methods. + /// + /// The methods. + /// The . + IRequestBuilder UsingMethod([NotNull] params string[] methods); + + /// + /// UsingVerb: add HTTP Method matching on any methods. /// - /// The verb. + /// The methods. /// The . + [Obsolete] IRequestBuilder UsingVerb([NotNull] params string[] verbs); } } \ No newline at end of file diff --git a/src/WireMock.Net/RequestBuilders/IParamsRequestBuilder.cs b/src/WireMock.Net/RequestBuilders/IParamsRequestBuilder.cs index 3b1799aa3..0194de0cb 100644 --- a/src/WireMock.Net/RequestBuilders/IParamsRequestBuilder.cs +++ b/src/WireMock.Net/RequestBuilders/IParamsRequestBuilder.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using JetBrains.Annotations; +using WireMock.Matchers; using WireMock.Util; namespace WireMock.RequestBuilders @@ -11,14 +12,15 @@ namespace WireMock.RequestBuilders public interface IParamsRequestBuilder { /// - /// WithParam (key only) + /// WithParam: matching on key only. /// /// The key. + /// The match behaviour (optional). /// The . - IRequestBuilder WithParam([NotNull] string key); + IRequestBuilder WithParam([NotNull] string key, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch); /// - /// WithParam (values) + /// WithParam: matching on key and values. /// /// The key. /// The values. @@ -26,7 +28,16 @@ public interface IParamsRequestBuilder IRequestBuilder WithParam([NotNull] string key, [CanBeNull] params string[] values); /// - /// WithParam (funcs) + /// WithParam: matching on key, values and matchBehaviour. + /// + /// The key. + /// The values. + /// The match behaviour. + /// The . + IRequestBuilder WithParam([NotNull] string key, MatchBehaviour matchBehaviour, [CanBeNull] params string[] values); + + /// + /// WithParam: matching on functions. /// /// The funcs. /// The . diff --git a/src/WireMock.Net/RequestBuilders/IUrlAndPathRequestBuilder.cs b/src/WireMock.Net/RequestBuilders/IUrlAndPathRequestBuilder.cs index 1f760fa37..a3f979a7c 100644 --- a/src/WireMock.Net/RequestBuilders/IUrlAndPathRequestBuilder.cs +++ b/src/WireMock.Net/RequestBuilders/IUrlAndPathRequestBuilder.cs @@ -10,42 +10,58 @@ namespace WireMock.RequestBuilders public interface IUrlAndPathRequestBuilder : IMethodRequestBuilder { /// - /// The with path. + /// WithPath: add path matching based on IStringMatchers. /// /// The matchers. /// The . IRequestBuilder WithPath([NotNull] params IStringMatcher[] matchers); /// - /// The with path. + /// WithPath: add path matching based on paths. /// /// The paths. /// The . IRequestBuilder WithPath([NotNull] params string[] paths); /// - /// The with path. + /// WithPath: add path matching based on paths and matchBehaviour. + /// + /// The match behaviour. + /// The paths. + /// The . + IRequestBuilder WithPath(MatchBehaviour matchBehaviour, [NotNull] params string[] paths); + + /// + /// WithPath: add path matching based on functions. /// /// The path funcs. /// The . IRequestBuilder WithPath([NotNull] params Func[] funcs); /// - /// The with url. + /// WithUrl: add url matching based on IStringMatcher[]. /// /// The matchers. /// The . IRequestBuilder WithUrl([NotNull] params IStringMatcher[] matchers); /// - /// The with url. + /// WithUrl: add url matching based on urls. /// /// The urls. /// The . IRequestBuilder WithUrl([NotNull] params string[] urls); /// - /// The with path. + /// WithUrl: add url matching based on urls. + /// + /// The match behaviour. + /// The urls. + /// The . + IRequestBuilder WithUrl(MatchBehaviour matchBehaviour, [NotNull] params string[] urls); + + /// + /// WithUrl: add url matching based on functions. /// /// The path func. /// The . diff --git a/src/WireMock.Net/RequestBuilders/Request.cs b/src/WireMock.Net/RequestBuilders/Request.cs index f26e7ce7e..2cb369002 100644 --- a/src/WireMock.Net/RequestBuilders/Request.cs +++ b/src/WireMock.Net/RequestBuilders/Request.cs @@ -54,11 +54,7 @@ public T GetRequestMessageMatcher() where T : IRequestMatcher return _requestMatchers.Where(rm => rm is T).Cast().FirstOrDefault(); } - /// - /// The with clientIP. - /// - /// The matchers. - /// The . + /// public IRequestBuilder WithClientIP(params IStringMatcher[] matchers) { Check.NotNullOrEmpty(matchers, nameof(matchers)); @@ -67,24 +63,22 @@ public IRequestBuilder WithClientIP(params IStringMatcher[] matchers) return this; } - /// - /// The with clientIP. - /// - /// The ClientIPs. - /// The . + /// public IRequestBuilder WithClientIP(params string[] clientIPs) + { + return WithClientIP(MatchBehaviour.AcceptOnMatch, clientIPs); + } + + /// + public IRequestBuilder WithClientIP(MatchBehaviour matchBehaviour, params string[] clientIPs) { Check.NotNullOrEmpty(clientIPs, nameof(clientIPs)); - _requestMatchers.Add(new RequestMessageClientIPMatcher(clientIPs)); + _requestMatchers.Add(new RequestMessageClientIPMatcher(matchBehaviour, clientIPs)); return this; } - /// - /// The with clientIP. - /// - /// The clientIP funcs. - /// The . + /// public IRequestBuilder WithClientIP(params Func[] funcs) { Check.NotNullOrEmpty(funcs, nameof(funcs)); @@ -93,11 +87,7 @@ public IRequestBuilder WithClientIP(params Func[] funcs) return this; } - /// - /// The with path. - /// - /// The matchers. - /// The . + /// public IRequestBuilder WithPath(params IStringMatcher[] matchers) { Check.NotNullOrEmpty(matchers, nameof(matchers)); @@ -106,24 +96,22 @@ public IRequestBuilder WithPath(params IStringMatcher[] matchers) return this; } - /// - /// The with path. - /// - /// The paths. - /// The . + /// public IRequestBuilder WithPath(params string[] paths) + { + return WithPath(MatchBehaviour.AcceptOnMatch, paths); + } + + /// + public IRequestBuilder WithPath(MatchBehaviour matchBehaviour, params string[] paths) { Check.NotNullOrEmpty(paths, nameof(paths)); - _requestMatchers.Add(new RequestMessagePathMatcher(paths)); + _requestMatchers.Add(new RequestMessagePathMatcher(matchBehaviour, paths)); return this; } - /// - /// The with path. - /// - /// The path func. - /// The . + /// public IRequestBuilder WithPath(params Func[] funcs) { Check.NotNullOrEmpty(funcs, nameof(funcs)); @@ -132,11 +120,7 @@ public IRequestBuilder WithPath(params Func[] funcs) return this; } - /// - /// The with url. - /// - /// The matchers. - /// The . + /// public IRequestBuilder WithUrl(params IStringMatcher[] matchers) { Check.NotNullOrEmpty(matchers, nameof(matchers)); @@ -145,24 +129,22 @@ public IRequestBuilder WithUrl(params IStringMatcher[] matchers) return this; } - /// - /// The with url. - /// - /// The urls. - /// The . + /// public IRequestBuilder WithUrl(params string[] urls) + { + return WithUrl(MatchBehaviour.AcceptOnMatch, urls); + } + + /// + public IRequestBuilder WithUrl(MatchBehaviour matchBehaviour, params string[] urls) { Check.NotNullOrEmpty(urls, nameof(urls)); - _requestMatchers.Add(new RequestMessageUrlMatcher(urls)); + _requestMatchers.Add(new RequestMessageUrlMatcher(matchBehaviour, urls)); return this; } - /// - /// The with url. - /// - /// The url func. - /// The . + /// public IRequestBuilder WithUrl(params Func[] funcs) { Check.NotNullOrEmpty(funcs, nameof(funcs)); @@ -171,50 +153,50 @@ public IRequestBuilder WithUrl(params Func[] funcs) return this; } - /// - public IRequestBuilder UsingDelete() + /// + public IRequestBuilder UsingDelete(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) { - _requestMatchers.Add(new RequestMessageMethodMatcher("delete")); + _requestMatchers.Add(new RequestMessageMethodMatcher(matchBehaviour, "delete")); return this; } - /// - public IRequestBuilder UsingGet() + /// + public IRequestBuilder UsingGet(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) { - _requestMatchers.Add(new RequestMessageMethodMatcher("get")); + _requestMatchers.Add(new RequestMessageMethodMatcher(matchBehaviour, "get")); return this; } - /// - public IRequestBuilder UsingHead() + /// + public IRequestBuilder UsingHead(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) { - _requestMatchers.Add(new RequestMessageMethodMatcher("head")); + _requestMatchers.Add(new RequestMessageMethodMatcher(matchBehaviour, "head")); return this; } - /// - public IRequestBuilder UsingPost() + /// + public IRequestBuilder UsingPost(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) { - _requestMatchers.Add(new RequestMessageMethodMatcher("post")); + _requestMatchers.Add(new RequestMessageMethodMatcher(matchBehaviour, "post")); return this; } - /// - public IRequestBuilder UsingPatch() + /// + public IRequestBuilder UsingPatch(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) { - _requestMatchers.Add(new RequestMessageMethodMatcher("patch")); + _requestMatchers.Add(new RequestMessageMethodMatcher(matchBehaviour, "patch")); return this; } - /// - public IRequestBuilder UsingPut() + /// + public IRequestBuilder UsingPut(MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) { - _requestMatchers.Add(new RequestMessageMethodMatcher("put")); + _requestMatchers.Add(new RequestMessageMethodMatcher(matchBehaviour, "put")); return this; } - /// - public IRequestBuilder UsingAnyVerb() + /// + public IRequestBuilder UsingAnyMethod() { var matchers = _requestMatchers.Where(m => m is RequestMessageMethodMatcher).ToList(); foreach (var matcher in matchers) @@ -225,33 +207,51 @@ public IRequestBuilder UsingAnyVerb() return this; } - /// + /// + public IRequestBuilder UsingAnyVerb() + { + return UsingAnyMethod(); + } + + /// + public IRequestBuilder UsingMethod(params string[] methods) + { + return UsingMethod(MatchBehaviour.AcceptOnMatch, methods); + } + + /// public IRequestBuilder UsingVerb(params string[] verbs) { - Check.NotNullOrEmpty(verbs, nameof(verbs)); + return UsingMethod(verbs); + } + + /// + public IRequestBuilder UsingMethod(MatchBehaviour matchBehaviour, params string[] methods) + { + Check.NotNullOrEmpty(methods, nameof(methods)); - _requestMatchers.Add(new RequestMessageMethodMatcher(verbs)); + _requestMatchers.Add(new RequestMessageMethodMatcher(matchBehaviour, methods)); return this; } - /// - public IRequestBuilder WithBody(string body) + /// + public IRequestBuilder WithBody(string body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) { - _requestMatchers.Add(new RequestMessageBodyMatcher(body)); + _requestMatchers.Add(new RequestMessageBodyMatcher(matchBehaviour, body)); return this; } - /// - public IRequestBuilder WithBody(byte[] body) + /// + public IRequestBuilder WithBody(byte[] body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) { - _requestMatchers.Add(new RequestMessageBodyMatcher(body)); + _requestMatchers.Add(new RequestMessageBodyMatcher(matchBehaviour, body)); return this; } - /// - public IRequestBuilder WithBody(object body) + /// + public IRequestBuilder WithBody(object body, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) { - _requestMatchers.Add(new RequestMessageBodyMatcher(body)); + _requestMatchers.Add(new RequestMessageBodyMatcher(matchBehaviour, body)); return this; } @@ -291,21 +291,27 @@ public IRequestBuilder WithBody(Func func) return this; } - /// - public IRequestBuilder WithParam(string key) + /// + public IRequestBuilder WithParam(string key, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) { Check.NotNull(key, nameof(key)); - _requestMatchers.Add(new RequestMessageParamMatcher(key)); + _requestMatchers.Add(new RequestMessageParamMatcher(matchBehaviour, key)); return this; } /// public IRequestBuilder WithParam(string key, params string[] values) + { + return WithParam(key, MatchBehaviour.AcceptOnMatch, values); + } + + /// + public IRequestBuilder WithParam(string key, MatchBehaviour matchBehaviour, params string[] values) { Check.NotNull(key, nameof(key)); - _requestMatchers.Add(new RequestMessageParamMatcher(key, values)); + _requestMatchers.Add(new RequestMessageParamMatcher(matchBehaviour, key, values)); return this; } @@ -318,32 +324,39 @@ public IRequestBuilder WithParam(params Func - public IRequestBuilder WithHeader(string name, string pattern, bool ignoreCase = true) + /// + public IRequestBuilder WithHeader(string name, string pattern, MatchBehaviour matchBehaviour) + { + return WithHeader(name, pattern, true, matchBehaviour); + } + + /// + public IRequestBuilder WithHeader(string name, string pattern, bool ignoreCase = true, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) { Check.NotNull(name, nameof(name)); Check.NotNull(pattern, nameof(pattern)); - _requestMatchers.Add(new RequestMessageHeaderMatcher(name, pattern, ignoreCase)); + _requestMatchers.Add(new RequestMessageHeaderMatcher(matchBehaviour, name, pattern, ignoreCase)); return this; } - /// - public IRequestBuilder WithHeader(string name, string[] patterns, bool ignoreCase = true) + /// + public IRequestBuilder WithHeader(string name, string[] patterns, MatchBehaviour matchBehaviour) + { + return WithHeader(name, patterns, true, matchBehaviour); + } + + /// + public IRequestBuilder WithHeader(string name, string[] patterns, bool ignoreCase = true, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) { Check.NotNull(name, nameof(name)); Check.NotNull(patterns, nameof(patterns)); - _requestMatchers.Add(new RequestMessageHeaderMatcher(name, patterns, ignoreCase)); + _requestMatchers.Add(new RequestMessageHeaderMatcher(matchBehaviour, name, patterns, ignoreCase)); return this; } - /// - /// With header. - /// - /// The name. - /// The matchers. - /// The . + /// public IRequestBuilder WithHeader(string name, params IStringMatcher[] matchers) { Check.NotNull(name, nameof(name)); @@ -353,11 +366,7 @@ public IRequestBuilder WithHeader(string name, params IStringMatcher[] matchers) return this; } - /// - /// With header. - /// - /// The funcs. - /// The . + /// public IRequestBuilder WithHeader(params Func, bool>[] funcs) { Check.NotNullOrEmpty(funcs, nameof(funcs)); @@ -366,25 +375,14 @@ public IRequestBuilder WithHeader(params Func, boo return this; } - /// - /// With cookie. - /// - /// The name. - /// The pattern. - /// if set to true [ignore case]. - /// The . - public IRequestBuilder WithCookie(string name, string pattern, bool ignoreCase = true) + /// + public IRequestBuilder WithCookie(string name, string pattern, bool ignoreCase = true, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch) { - _requestMatchers.Add(new RequestMessageCookieMatcher(name, pattern, ignoreCase)); + _requestMatchers.Add(new RequestMessageCookieMatcher(matchBehaviour, name, pattern, ignoreCase)); return this; } - /// - /// With cookie. - /// - /// The name. - /// The matchers. - /// The . + /// public IRequestBuilder WithCookie(string name, params IStringMatcher[] matchers) { Check.NotNullOrEmpty(matchers, nameof(matchers)); @@ -393,11 +391,7 @@ public IRequestBuilder WithCookie(string name, params IStringMatcher[] matchers) return this; } - /// - /// With header. - /// - /// The funcs. - /// The . + /// public IRequestBuilder WithCookie(params Func, bool>[] funcs) { Check.NotNullOrEmpty(funcs, nameof(funcs)); diff --git a/src/WireMock.Net/Serialization/MatcherMapper.cs b/src/WireMock.Net/Serialization/MatcherMapper.cs index d23ca968c..b1f0f3761 100644 --- a/src/WireMock.Net/Serialization/MatcherMapper.cs +++ b/src/WireMock.Net/Serialization/MatcherMapper.cs @@ -22,11 +22,13 @@ public static MatcherModel Map([CanBeNull] IMatcher matcher) string[] patterns = matcher is IStringMatcher stringMatcher ? stringMatcher.GetPatterns() : new string[0]; bool? ignorecase = matcher is IIgnoreCaseMatcher ignoreCaseMatcher ? ignoreCaseMatcher.IgnoreCase : (bool?)null; + bool? rejectOnMatch = matcher.MatchBehaviour == MatchBehaviour.RejectOnMatch ? true : (bool?) null; return new MatcherModel { + RejectOnMatch = rejectOnMatch, IgnoreCase = ignorecase, - Name = matcher.GetName(), + Name = matcher.Name, Pattern = patterns.Length == 1 ? patterns.First() : null, Patterns = patterns.Length > 1 ? patterns : null }; diff --git a/src/WireMock.Net/Serialization/MatcherModelMapper.cs b/src/WireMock.Net/Serialization/MatcherModelMapper.cs index 78c1e630e..91852d140 100644 --- a/src/WireMock.Net/Serialization/MatcherModelMapper.cs +++ b/src/WireMock.Net/Serialization/MatcherModelMapper.cs @@ -20,23 +20,24 @@ public static IMatcher Map([CanBeNull] MatcherModel matcher) string matcherType = parts.Length > 1 ? parts[1] : null; string[] patterns = matcher.Patterns ?? new[] { matcher.Pattern }; + MatchBehaviour matchBehaviour = matcher.RejectOnMatch == true ? MatchBehaviour.RejectOnMatch : MatchBehaviour.AcceptOnMatch; switch (matcherName) { case "ExactMatcher": - return new ExactMatcher(patterns); + return new ExactMatcher(matchBehaviour, patterns); case "RegexMatcher": - return new RegexMatcher(patterns, matcher.IgnoreCase == true); + return new RegexMatcher(matchBehaviour, patterns, matcher.IgnoreCase == true); case "JsonPathMatcher": - return new JsonPathMatcher(patterns); + return new JsonPathMatcher(matchBehaviour, patterns); case "XPathMatcher": - return new XPathMatcher(matcher.Pattern); + return new XPathMatcher(matchBehaviour, matcher.Pattern); case "WildcardMatcher": - return new WildcardMatcher(patterns, matcher.IgnoreCase == true); + return new WildcardMatcher(matchBehaviour, patterns, matcher.IgnoreCase == true); case "SimMetricsMatcher": SimMetricType type = SimMetricType.Levenstein; @@ -45,7 +46,7 @@ public static IMatcher Map([CanBeNull] MatcherModel matcher) throw new NotSupportedException($"Matcher '{matcherName}' with Type '{matcherType}' is not supported."); } - return new SimMetricsMatcher(matcher.Pattern, type); + return new SimMetricsMatcher(matchBehaviour, matcher.Pattern, type); default: throw new NotSupportedException($"Matcher '{matcherName}' is not supported."); diff --git a/src/WireMock.Net/Server/FluentMockServer.Admin.cs b/src/WireMock.Net/Server/FluentMockServer.Admin.cs index 6a9170aed..124215ede 100644 --- a/src/WireMock.Net/Server/FluentMockServer.Admin.cs +++ b/src/WireMock.Net/Server/FluentMockServer.Admin.cs @@ -36,8 +36,8 @@ public partial class FluentMockServer private const string AdminRequests = "/__admin/requests"; private const string AdminSettings = "/__admin/settings"; private const string AdminScenarios = "/__admin/scenarios"; - private readonly RegexMatcher _adminMappingsGuidPathMatcher = new RegexMatcher(@"^\/__admin\/mappings\/(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$"); - private readonly RegexMatcher _adminRequestsGuidPathMatcher = new RegexMatcher(@"^\/__admin\/requests\/(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$"); + private readonly RegexMatcher _adminMappingsGuidPathMatcher = new RegexMatcher(MatchBehaviour.AcceptOnMatch, @"^\/__admin\/mappings\/(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$"); + private readonly RegexMatcher _adminRequestsGuidPathMatcher = new RegexMatcher(MatchBehaviour.AcceptOnMatch, @"^\/__admin\/requests\/(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$"); private readonly JsonSerializerSettings _settings = new JsonSerializerSettings { @@ -50,7 +50,7 @@ private void InitAdmin() { // __admin/settings Given(Request.Create().WithPath(AdminSettings).UsingGet()).RespondWith(new DynamicResponseProvider(SettingsGet)); - Given(Request.Create().WithPath(AdminSettings).UsingVerb("PUT", "POST").WithHeader(HttpKnownHeaderNames.ContentType, ContentTypeJson)).RespondWith(new DynamicResponseProvider(SettingsUpdate)); + Given(Request.Create().WithPath(AdminSettings).UsingMethod("PUT", "POST").WithHeader(HttpKnownHeaderNames.ContentType, ContentTypeJson)).RespondWith(new DynamicResponseProvider(SettingsUpdate)); // __admin/mappings @@ -196,7 +196,7 @@ public void ReadStaticMappingAndAddOrUpdate([NotNull] string path) private void InitProxyAndRecord(IProxyAndRecordSettings settings) { _httpClientForProxy = HttpClientHelper.CreateHttpClient(settings.ClientX509Certificate2ThumbprintOrSubjectName); - Given(Request.Create().WithPath("/*").UsingAnyVerb()).RespondWith(new ProxyAsyncResponseProvider(ProxyAndRecordAsync, settings)); + Given(Request.Create().WithPath("/*").UsingAnyMethod()).RespondWith(new ProxyAsyncResponseProvider(ProxyAndRecordAsync, settings)); } private async Task ProxyAndRecordAsync(RequestMessage requestMessage, IProxyAndRecordSettings settings) @@ -225,7 +225,7 @@ private Mapping ToMapping(RequestMessage requestMessage, ResponseMessage respons { var request = Request.Create(); request.WithPath(requestMessage.Path); - request.UsingVerb(requestMessage.Method); + request.UsingMethod(requestMessage.Method); requestMessage.Query.Loop((key, value) => request.WithParam(key, value.ToArray())); requestMessage.Cookies.Loop((key, value) => request.WithCookie(key, value)); @@ -241,7 +241,7 @@ private Mapping ToMapping(RequestMessage requestMessage, ResponseMessage respons if (requestMessage.Body != null) { - request.WithBody(new ExactMatcher(requestMessage.Body)); + request.WithBody(new ExactMatcher(MatchBehaviour.AcceptOnMatch, requestMessage.Body)); } var response = Response.Create(responseMessage); @@ -648,7 +648,7 @@ private IRequestBuilder InitRequestBuilder(RequestModel requestModel) if (requestModel.Methods != null) { - requestBuilder = requestBuilder.UsingVerb(requestModel.Methods); + requestBuilder = requestBuilder.UsingMethod(requestModel.Methods); } if (requestModel.Headers != null) diff --git a/src/WireMock.Net/Server/FluentMockServer.cs b/src/WireMock.Net/Server/FluentMockServer.cs index c7dfce82f..cd4148242 100644 --- a/src/WireMock.Net/Server/FluentMockServer.cs +++ b/src/WireMock.Net/Server/FluentMockServer.cs @@ -256,7 +256,7 @@ public void Stop() [PublicAPI] public void AddCatchAllMapping() { - Given(Request.Create().WithPath("/*").UsingAnyVerb()) + Given(Request.Create().WithPath("/*").UsingAnyMethod()) .WithGuid(Guid.Parse("90008000-0000-4444-a17e-669cd84f1f05")) .AtPriority(1000) .RespondWith(new DynamicResponseProvider(request => new ResponseMessage { StatusCode = 404, Body = "No matching mapping found" })); @@ -351,7 +351,7 @@ public void SetBasicAuthentication([NotNull] string username, [NotNull] string p Check.NotNull(password, nameof(password)); string authorization = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password)); - _options.AuthorizationMatcher = new RegexMatcher("^(?i)BASIC " + authorization + "$"); + _options.AuthorizationMatcher = new RegexMatcher(MatchBehaviour.AcceptOnMatch, "^(?i)BASIC " + authorization + "$"); } /// diff --git a/src/WireMock.Net/WireMock.Net.csproj b/src/WireMock.Net/WireMock.Net.csproj index 28618f8cd..00f63d1b9 100644 --- a/src/WireMock.Net/WireMock.Net.csproj +++ b/src/WireMock.Net/WireMock.Net.csproj @@ -3,7 +3,7 @@ Lightweight Http Mocking Server for .Net, inspired by WireMock from the Java landscape. WireMock.Net - 1.0.3.16 + 1.0.3.17 Stef Heyenrath net452;net46;netstandard1.3;netstandard2.0 true diff --git a/test/WireMock.Net.Tests/FluentMockServerTests.cs b/test/WireMock.Net.Tests/FluentMockServerTests.cs index aaf3288dd..d5bae3190 100644 --- a/test/WireMock.Net.Tests/FluentMockServerTests.cs +++ b/test/WireMock.Net.Tests/FluentMockServerTests.cs @@ -218,7 +218,7 @@ public async Task FluentMockServer_Should_respond_to_request_methodPatch() // given _server = FluentMockServer.Start(); - _server.Given(Request.Create().WithPath("/foo").UsingVerb("patch")) + _server.Given(Request.Create().WithPath("/foo").UsingMethod("patch")) .RespondWith(Response.Create().WithBody("hello patch")); // when @@ -268,7 +268,7 @@ public async Task FluentMockServer_Should_respond_to_request_BodyAsJson() _server = FluentMockServer.Start(); _server - .Given(Request.Create().UsingAnyVerb()) + .Given(Request.Create().UsingAnyMethod()) .RespondWith(Response.Create().WithBodyAsJson(new { message = "Hello" })); // Act @@ -285,7 +285,7 @@ public async Task FluentMockServer_Should_respond_to_request_BodyAsJson_Indented _server = FluentMockServer.Start(); _server - .Given(Request.Create().UsingAnyVerb()) + .Given(Request.Create().UsingAnyMethod()) .RespondWith(Response.Create().WithBodyAsJson(new { message = "Hello" }, true)); // Act diff --git a/test/WireMock.Net.Tests/MatchBehaviourHelperTests.cs b/test/WireMock.Net.Tests/MatchBehaviourHelperTests.cs new file mode 100644 index 000000000..191901890 --- /dev/null +++ b/test/WireMock.Net.Tests/MatchBehaviourHelperTests.cs @@ -0,0 +1,25 @@ +using NFluent; +using WireMock.Matchers; +using Xunit; + +namespace WireMock.Net.Tests +{ + public class MatchBehaviourHelperTests + { + [Fact] + public void MatchBehaviourHelper_Convert_AcceptOnMatch() + { + Check.That(MatchBehaviourHelper.Convert(MatchBehaviour.AcceptOnMatch, 0.0)).IsEqualTo(0.0); + Check.That(MatchBehaviourHelper.Convert(MatchBehaviour.AcceptOnMatch, 0.5)).IsEqualTo(0.5); + Check.That(MatchBehaviourHelper.Convert(MatchBehaviour.AcceptOnMatch, 1.0)).IsEqualTo(1.0); + } + + [Fact] + public void MatchBehaviourHelper_Convert_RejectOnMatch() + { + Check.That(MatchBehaviourHelper.Convert(MatchBehaviour.RejectOnMatch, 0.0)).IsEqualTo(1.0); + Check.That(MatchBehaviourHelper.Convert(MatchBehaviour.RejectOnMatch, 0.5)).IsEqualTo(0.0); + Check.That(MatchBehaviourHelper.Convert(MatchBehaviour.RejectOnMatch, 1.0)).IsEqualTo(0.0); + } + } +} diff --git a/test/WireMock.Net.Tests/Matchers/ExactMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/ExactMatcherTests.cs index 1cffc9bd5..1c903df0f 100644 --- a/test/WireMock.Net.Tests/Matchers/ExactMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/ExactMatcherTests.cs @@ -13,7 +13,7 @@ public void ExactMatcher_GetName() var matcher = new ExactMatcher("X"); // Act - string name = matcher.GetName(); + string name = matcher.Name; // Assert Check.That(name).Equals("ExactMatcher"); @@ -46,7 +46,7 @@ public void ExactMatcher_IsMatch_MultiplePatterns() } [Fact] - public void Request_WithBodyExactMatcher_false() + public void ExactMatcher_IsMatch_SinglePattern() { // Assign var matcher = new ExactMatcher("cat"); @@ -55,7 +55,33 @@ public void Request_WithBodyExactMatcher_false() double result = matcher.IsMatch("caR"); // Assert - Check.That(result).IsStrictlyLessThan(1.0); + Check.That(result).IsEqualTo(0.0); + } + + [Fact] + public void ExactMatcher_IsMatch_SinglePattern_AcceptOnMatch() + { + // Assign + var matcher = new ExactMatcher(MatchBehaviour.AcceptOnMatch, "cat"); + + // Act + double result = matcher.IsMatch("cat"); + + // Assert + Check.That(result).IsEqualTo(1.0); + } + + [Fact] + public void ExactMatcher_IsMatch_SinglePattern_RejectOnMatch() + { + // Assign + var matcher = new ExactMatcher(MatchBehaviour.RejectOnMatch, "cat"); + + // Act + double result = matcher.IsMatch("cat"); + + // Assert + Check.That(result).IsEqualTo(0.0); } } } \ No newline at end of file diff --git a/test/WireMock.Net.Tests/Matchers/ExactObjectMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/ExactObjectMatcherTests.cs index abd9266f8..fa7cd6b44 100644 --- a/test/WireMock.Net.Tests/Matchers/ExactObjectMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/ExactObjectMatcherTests.cs @@ -14,10 +14,52 @@ public void ExactObjectMatcher_GetName() // Act var matcher = new ExactObjectMatcher(obj); - string name = matcher.GetName(); + string name = matcher.Name; // Assert Check.That(name).Equals("ExactObjectMatcher"); } + + [Fact] + public void ExactObjectMatcher_IsMatch_ByteArray() + { + // Assign + object checkValue = new byte[] { 1, 2 }; + + // Act + var matcher = new ExactObjectMatcher(new byte[] { 1, 2 }); + double result = matcher.IsMatch(checkValue); + + // Assert + Check.That(result).IsEqualTo(1.0); + } + + [Fact] + public void ExactObjectMatcher_IsMatch_AcceptOnMatch() + { + // Assign + object obj = 1; + + // Act + var matcher = new ExactObjectMatcher(obj); + double result = matcher.IsMatch(1); + + // Assert + Check.That(result).IsEqualTo(1.0); + } + + [Fact] + public void ExactObjectMatcher_IsMatch_RejectOnMatch() + { + // Assign + object obj = 1; + + // Act + var matcher = new ExactObjectMatcher(MatchBehaviour.RejectOnMatch, obj); + double result = matcher.IsMatch(1); + + // Assert + Check.That(result).IsEqualTo(0.0); + } } } \ No newline at end of file diff --git a/test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs index 6834001b6..8d25cc1b5 100644 --- a/test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/JsonPathMatcherTests.cs @@ -14,7 +14,7 @@ public void JsonPathMatcher_GetName() var matcher = new JsonPathMatcher("X"); // Act - string name = matcher.GetName(); + string name = matcher.Name; // Assert Check.That(name).Equals("JsonPathMatcher"); @@ -78,10 +78,10 @@ public void JsonPathMatcher_IsMatch_String_Exception_Mismatch() public void JsonPathMatcher_IsMatch_Object_Exception_Mismatch() { // Assign - var matcher = new JsonPathMatcher("xxx"); + var matcher = new JsonPathMatcher(""); // Act - double match = matcher.IsMatch(""); + double match = matcher.IsMatch("x"); // Assert Check.That(match).IsEqualTo(0); @@ -131,5 +131,18 @@ public void JsonPathMatcher_IsMatch_JObject_Parsed() // Assert Check.That(match).IsEqualTo(1); } + + [Fact] + public void JsonPathMatcher_IsMatch_RejectOnMatch() + { + // Assign + var matcher = new JsonPathMatcher(MatchBehaviour.RejectOnMatch, "$..[?(@.Id == 1)]"); + + // Act + double match = matcher.IsMatch(JObject.Parse("{\"Id\":1,\"Name\":\"Test\"}")); + + // Assert + Check.That(match).IsEqualTo(0.0); + } } } \ No newline at end of file diff --git a/test/WireMock.Net.Tests/Matchers/RegexMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/RegexMatcherTests.cs index 0c1446f49..6f2c7aee0 100644 --- a/test/WireMock.Net.Tests/Matchers/RegexMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/RegexMatcherTests.cs @@ -13,7 +13,7 @@ public void RegexMatcher_GetName() var matcher = new RegexMatcher(""); // Act - string name = matcher.GetName(); + string name = matcher.Name; // Assert Check.That(name).Equals("RegexMatcher"); @@ -32,6 +32,18 @@ public void RegexMatcher_GetPatterns() Check.That(patterns).ContainsExactly("X"); } + [Fact] + public void RegexMatcher_GetIgnoreCase() + { + // Act + bool case1 = new RegexMatcher("X").IgnoreCase; + bool case2 = new RegexMatcher("X", true).IgnoreCase; + + // Assert + Check.That(case1).IsFalse(); + Check.That(case2).IsTrue(); + } + [Fact] public void RegexMatcher_IsMatch() { @@ -65,10 +77,23 @@ public void RegexMatcher_IsMatch_IgnoreCase() var matcher = new RegexMatcher("H.*o", true); // Act - double result = matcher.IsMatch("hello world!"); + double result = matcher.IsMatch("hello"); // Assert Check.That(result).IsEqualTo(1.0d); } + + [Fact] + public void RegexMatcher_IsMatch_RejectOnMatch() + { + // Assign + var matcher = new RegexMatcher(MatchBehaviour.RejectOnMatch, "h.*o"); + + // Act + double result = matcher.IsMatch("hello"); + + // Assert + Check.That(result).IsEqualTo(0.0); + } } } \ No newline at end of file diff --git a/test/WireMock.Net.Tests/Matchers/SimMetricsMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/SimMetricsMatcherTests.cs index bc7a9a573..eaa8c8193 100644 --- a/test/WireMock.Net.Tests/Matchers/SimMetricsMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/SimMetricsMatcherTests.cs @@ -13,7 +13,7 @@ public void SimMetricsMatcher_GetName() var matcher = new SimMetricsMatcher("X"); // Act - string name = matcher.GetName(); + string name = matcher.Name; // Assert Check.That(name).Equals("SimMetricsMatcher.Levenstein"); @@ -57,5 +57,31 @@ public void SimMetricsMatcher_IsMatch_2() // Assert Check.That(result).IsStrictlyLessThan(0.1).And.IsStrictlyGreaterThan(0.05); } + + [Fact] + public void SimMetricsMatcher_IsMatch_AcceptOnMatch() + { + // Assign + var matcher = new SimMetricsMatcher("test"); + + // Act + double result = matcher.IsMatch("test"); + + // Assert + Check.That(result).IsEqualTo(1.0); + } + + [Fact] + public void SimMetricsMatcher_IsMatch_RejectOnMatch() + { + // Assign + var matcher = new SimMetricsMatcher(MatchBehaviour.RejectOnMatch, "test"); + + // Act + double result = matcher.IsMatch("test"); + + // Assert + Check.That(result).IsEqualTo(0.0); + } } } \ No newline at end of file diff --git a/test/WireMock.Net.Tests/Matchers/WildcardMatcherTest.cs b/test/WireMock.Net.Tests/Matchers/WildcardMatcherTest.cs index c399db14b..e2d5e6363 100644 --- a/test/WireMock.Net.Tests/Matchers/WildcardMatcherTest.cs +++ b/test/WireMock.Net.Tests/Matchers/WildcardMatcherTest.cs @@ -65,7 +65,7 @@ public void WildcardMatcher_GetName() var matcher = new WildcardMatcher("x"); // Act - string name = matcher.GetName(); + string name = matcher.Name; // Assert Check.That(name).Equals("WildcardMatcher"); @@ -83,5 +83,17 @@ public void WildcardMatcher_GetPatterns() // Assert Check.That(patterns).ContainsExactly("x"); } + + [Fact] + public void WildcardMatcher_IsMatch_RejectOnMatch() + { + // Assign + var matcher = new WildcardMatcher(MatchBehaviour.RejectOnMatch, "m"); + + // Act + double result = matcher.IsMatch("m"); + + Check.That(result).IsEqualTo(0.0); + } } } \ No newline at end of file diff --git a/test/WireMock.Net.Tests/Matchers/XPathMatcherTests.cs b/test/WireMock.Net.Tests/Matchers/XPathMatcherTests.cs index 91eef8582..9102cf6f8 100644 --- a/test/WireMock.Net.Tests/Matchers/XPathMatcherTests.cs +++ b/test/WireMock.Net.Tests/Matchers/XPathMatcherTests.cs @@ -13,7 +13,7 @@ public void XPathMatcher_GetName() var matcher = new XPathMatcher("X"); // Act - string name = matcher.GetName(); + string name = matcher.Name; // Assert Check.That(name).Equals("XPathMatcher"); @@ -31,5 +31,39 @@ public void XPathMatcher_GetPatterns() // Assert Check.That(patterns).ContainsExactly("X"); } + + [Fact] + public void XPathMatcher_IsMatch_AcceptOnMatch() + { + // Assign + string xml = @" + + abc + "; + var matcher = new XPathMatcher("/todo-list[count(todo-item) = 1]"); + + // Act + double result = matcher.IsMatch(xml); + + // Assert + Check.That(result).IsEqualTo(1.0); + } + + [Fact] + public void XPathMatcher_IsMatch_RejectOnMatch() + { + // Assign + string xml = @" + + abc + "; + var matcher = new XPathMatcher(MatchBehaviour.RejectOnMatch, "/todo-list[count(todo-item) = 1]"); + + // Act + double result = matcher.IsMatch(xml); + + // Assert + Check.That(result).IsEqualTo(0.0); + } } } \ No newline at end of file diff --git a/test/WireMock.Net.Tests/RequestCookieTests.cs b/test/WireMock.Net.Tests/RequestCookieTests.cs index 2b88c8820..20dbc890b 100644 --- a/test/WireMock.Net.Tests/RequestCookieTests.cs +++ b/test/WireMock.Net.Tests/RequestCookieTests.cs @@ -15,7 +15,7 @@ public class RequestCookieTests public void Request_WithCookie_OK() { // given - var spec = Request.Create().UsingAnyVerb().WithCookie("session", "a*"); + var spec = Request.Create().UsingAnyMethod().WithCookie("session", "a*"); // when var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, null, null, null, null, new Dictionary { { "session", "abc" } }); diff --git a/test/WireMock.Net.Tests/RequestMatchers/RequestMessageCookieMatcherTests.cs b/test/WireMock.Net.Tests/RequestMatchers/RequestMessageCookieMatcherTests.cs index 97335b50e..c955c29b0 100644 --- a/test/WireMock.Net.Tests/RequestMatchers/RequestMessageCookieMatcherTests.cs +++ b/test/WireMock.Net.Tests/RequestMatchers/RequestMessageCookieMatcherTests.cs @@ -9,6 +9,99 @@ namespace WireMock.Net.Tests.RequestMatchers { public class RequestMessageCookieMatcherTests { + [Fact] + public void RequestMessageCookieMatcher_GetMatchingScore_AcceptOnMatch_CookieDoesNotExists() + { + // Assign + var requestMessage = new RequestMessage(new Uri("http://localhost"), "GET", "127.0.0.1"); + var matcher = new RequestMessageCookieMatcher(MatchBehaviour.AcceptOnMatch, "c", "x"); + + // Act + var result = new RequestMatchResult(); + double score = matcher.GetMatchingScore(requestMessage, result); + + // Assert + Check.That(score).IsEqualTo(0.0d); + } + + [Fact] + public void RequestMessageCookieMatcher_GetMatchingScore_RejectOnMatch_CookieDoesNotExists() + { + // Assign + var requestMessage = new RequestMessage(new Uri("http://localhost"), "GET", "127.0.0.1"); + var matcher = new RequestMessageCookieMatcher(MatchBehaviour.RejectOnMatch, "c", "x"); + + // Act + var result = new RequestMatchResult(); + double score = matcher.GetMatchingScore(requestMessage, result); + + // Assert + Check.That(score).IsEqualTo(1.0d); + } + + [Fact] + public void RequestMessageCookieMatcher_GetMatchingScore_AcceptOnMatch_CookieDoesNotMatchPattern() + { + // Assign + var cookies = new Dictionary { { "c", "x" } }; + var requestMessage = new RequestMessage(new Uri("http://localhost"), "GET", "127.0.0.1", null, null, cookies); + var matcher = new RequestMessageCookieMatcher(MatchBehaviour.AcceptOnMatch, "no-match", "123"); + + // Act + var result = new RequestMatchResult(); + double score = matcher.GetMatchingScore(requestMessage, result); + + // Assert + Check.That(score).IsEqualTo(0.0d); + } + [Fact] + public void RequestMessageCookieMatcher_GetMatchingScore_RejectOnMatch_CookieDoesNotMatchPattern() + { + // Assign + var cookies = new Dictionary { { "h", "x" } }; + var requestMessage = new RequestMessage(new Uri("http://localhost"), "GET", "127.0.0.1", null, null, cookies); + var matcher = new RequestMessageCookieMatcher(MatchBehaviour.RejectOnMatch, "no-match", "123"); + + // Act + var result = new RequestMatchResult(); + double score = matcher.GetMatchingScore(requestMessage, result); + + // Assert + Check.That(score).IsEqualTo(1.0d); + } + + [Fact] + public void RequestMessageCookieMatcher_GetMatchingScore_AcceptOnMatch() + { + // Assign + var cookies = new Dictionary { { "h", "x" } }; + var requestMessage = new RequestMessage(new Uri("http://localhost"), "GET", "127.0.0.1", null, null, cookies); + var matcher = new RequestMessageCookieMatcher(MatchBehaviour.AcceptOnMatch, "h", "x"); + + // Act + var result = new RequestMatchResult(); + double score = matcher.GetMatchingScore(requestMessage, result); + + // Assert + Check.That(score).IsEqualTo(1.0d); + } + + [Fact] + public void RequestMessageCookieMatcher_GetMatchingScore_RejectOnMatch() + { + // Assign + var cookies = new Dictionary { { "h", "x" } }; + var requestMessage = new RequestMessage(new Uri("http://localhost"), "GET", "127.0.0.1", null, null, cookies); + var matcher = new RequestMessageCookieMatcher(MatchBehaviour.RejectOnMatch, "h", "x"); + + // Act + var result = new RequestMatchResult(); + double score = matcher.GetMatchingScore(requestMessage, result); + + // Assert + Check.That(score).IsEqualTo(0.0d); + } + [Fact] public void RequestMessageCookieMatcher_GetMatchingScore_IStringMatcher_Match() { diff --git a/test/WireMock.Net.Tests/RequestMatchers/RequestMessageHeaderMatcherTests.cs b/test/WireMock.Net.Tests/RequestMatchers/RequestMessageHeaderMatcherTests.cs index c273a882c..d0804830b 100644 --- a/test/WireMock.Net.Tests/RequestMatchers/RequestMessageHeaderMatcherTests.cs +++ b/test/WireMock.Net.Tests/RequestMatchers/RequestMessageHeaderMatcherTests.cs @@ -9,11 +9,104 @@ namespace WireMock.Net.Tests.RequestMatchers { public class RequestMessageHeaderMatcherTests { + [Fact] + public void RequestMessageHeaderMatcher_GetMatchingScore_AcceptOnMatch_HeaderDoesNotExists() + { + // Assign + var requestMessage = new RequestMessage(new Uri("http://localhost"), "GET", "127.0.0.1"); + var matcher = new RequestMessageHeaderMatcher(MatchBehaviour.AcceptOnMatch, "h", "x", true); + + // Act + var result = new RequestMatchResult(); + double score = matcher.GetMatchingScore(requestMessage, result); + + // Assert + Check.That(score).IsEqualTo(0.0d); + } + + [Fact] + public void RequestMessageHeaderMatcher_GetMatchingScore_RejectOnMatch_HeaderDoesNotExists() + { + // Assign + var requestMessage = new RequestMessage(new Uri("http://localhost"), "GET", "127.0.0.1"); + var matcher = new RequestMessageHeaderMatcher(MatchBehaviour.RejectOnMatch, "h", "x", true); + + // Act + var result = new RequestMatchResult(); + double score = matcher.GetMatchingScore(requestMessage, result); + + // Assert + Check.That(score).IsEqualTo(1.0d); + } + + [Fact] + public void RequestMessageHeaderMatcher_GetMatchingScore_AcceptOnMatch_HeaderDoesNotMatchPattern() + { + // Assign + var headers = new Dictionary { { "h", new[] { "x" } } }; + var requestMessage = new RequestMessage(new Uri("http://localhost"), "GET", "127.0.0.1", null, headers); + var matcher = new RequestMessageHeaderMatcher(MatchBehaviour.AcceptOnMatch, "no-match", "123", true); + + // Act + var result = new RequestMatchResult(); + double score = matcher.GetMatchingScore(requestMessage, result); + + // Assert + Check.That(score).IsEqualTo(0.0d); + } + [Fact] + public void RequestMessageHeaderMatcher_GetMatchingScore_RejectOnMatch_HeaderDoesNotMatchPattern() + { + // Assign + var headers = new Dictionary { { "h", new[] { "x" } } }; + var requestMessage = new RequestMessage(new Uri("http://localhost"), "GET", "127.0.0.1", null, headers); + var matcher = new RequestMessageHeaderMatcher(MatchBehaviour.RejectOnMatch, "no-match", "123", true); + + // Act + var result = new RequestMatchResult(); + double score = matcher.GetMatchingScore(requestMessage, result); + + // Assert + Check.That(score).IsEqualTo(1.0d); + } + + [Fact] + public void RequestMessageHeaderMatcher_GetMatchingScore_AcceptOnMatch() + { + // Assign + var headers = new Dictionary { { "h", new[] { "x" } } }; + var requestMessage = new RequestMessage(new Uri("http://localhost"), "GET", "127.0.0.1", null, headers); + var matcher = new RequestMessageHeaderMatcher(MatchBehaviour.AcceptOnMatch, "h", "x", true); + + // Act + var result = new RequestMatchResult(); + double score = matcher.GetMatchingScore(requestMessage, result); + + // Assert + Check.That(score).IsEqualTo(1.0d); + } + + [Fact] + public void RequestMessageHeaderMatcher_GetMatchingScore_RejectOnMatch() + { + // Assign + var headers = new Dictionary { { "h", new[] { "x" } } }; + var requestMessage = new RequestMessage(new Uri("http://localhost"), "GET", "127.0.0.1", null, headers); + var matcher = new RequestMessageHeaderMatcher(MatchBehaviour.RejectOnMatch, "h", "x", true); + + // Act + var result = new RequestMatchResult(); + double score = matcher.GetMatchingScore(requestMessage, result); + + // Assert + Check.That(score).IsEqualTo(0.0d); + } + [Fact] public void RequestMessageHeaderMatcher_GetMatchingScore_IStringMatcher_Match() { // Assign - var headers = new Dictionary { { "h", new [] { "x" } } }; + var headers = new Dictionary { { "h", new[] { "x" } } }; var requestMessage = new RequestMessage(new Uri("http://localhost"), "GET", "127.0.0.1", null, headers); var matcher = new RequestMessageHeaderMatcher("h", new ExactMatcher("x")); diff --git a/test/WireMock.Net.Tests/RequestMatchers/RequestMessageParamMatcherTests.cs b/test/WireMock.Net.Tests/RequestMatchers/RequestMessageParamMatcherTests.cs index 87560bafa..fa1196625 100644 --- a/test/WireMock.Net.Tests/RequestMatchers/RequestMessageParamMatcherTests.cs +++ b/test/WireMock.Net.Tests/RequestMatchers/RequestMessageParamMatcherTests.cs @@ -1,5 +1,6 @@ using System; using NFluent; +using WireMock.Matchers; using WireMock.Matchers.Request; using Xunit; @@ -12,7 +13,7 @@ public void RequestMessageParamMatcher_GetMatchingScore_AllMatch() { // Assign var requestMessage = new RequestMessage(new Uri("http://localhost?key=test1,test2"), "GET", "127.0.0.1"); - var matcher = new RequestMessageParamMatcher("key", new[] { "test1", "test2" }); + var matcher = new RequestMessageParamMatcher(MatchBehaviour.AcceptOnMatch, "key", new[] { "test1", "test2" }); // Act var result = new RequestMatchResult(); @@ -27,7 +28,7 @@ public void RequestMessageParamMatcher_GetMatchingScore_PartialMatch() { // Assign var requestMessage = new RequestMessage(new Uri("http://localhost?key=test0,test2"), "GET", "127.0.0.1"); - var matcher = new RequestMessageParamMatcher("key", new[] { "test1", "test2" }); + var matcher = new RequestMessageParamMatcher(MatchBehaviour.AcceptOnMatch, "key", new[] { "test1", "test2" }); // Act var result = new RequestMatchResult(); @@ -42,7 +43,7 @@ public void RequestMessageParamMatcher_GetMatchingScore_OnlyKeyPresent() { // Assign var requestMessage = new RequestMessage(new Uri("http://localhost?key"), "GET", "127.0.0.1"); - var matcher = new RequestMessageParamMatcher("key", new[] { "test1", "test2" }); + var matcher = new RequestMessageParamMatcher(MatchBehaviour.AcceptOnMatch, "key", new[] { "test1", "test2" }); // Act var result = new RequestMatchResult(); @@ -57,7 +58,7 @@ public void RequestMessageParamMatcher_GetMatchingScore_OnlyKeyPresent_WithNull( { // Assign var requestMessage = new RequestMessage(new Uri("http://localhost?key"), "GET", "127.0.0.1"); - var matcher = new RequestMessageParamMatcher("key"); + var matcher = new RequestMessageParamMatcher(MatchBehaviour.AcceptOnMatch, "key"); // Act var result = new RequestMatchResult(); @@ -72,7 +73,7 @@ public void RequestMessageParamMatcher_GetMatchingScore_OnlyKeyPresent_WithEmpty { // Assign var requestMessage = new RequestMessage(new Uri("http://localhost?key"), "GET", "127.0.0.1"); - var matcher = new RequestMessageParamMatcher("key", new string[] { }); + var matcher = new RequestMessageParamMatcher(MatchBehaviour.AcceptOnMatch, "key", new string[] { }); // Act var result = new RequestMatchResult(); diff --git a/test/WireMock.Net.Tests/RequestTests.cs b/test/WireMock.Net.Tests/RequestTests.cs index dbb40711b..cfdea63e4 100644 --- a/test/WireMock.Net.Tests/RequestTests.cs +++ b/test/WireMock.Net.Tests/RequestTests.cs @@ -30,7 +30,7 @@ public void Should_exclude_requests_matching_given_http_method_but_not_url() public void Should_exclude_requests_not_matching_given_headers() { // given - var spec = Request.Create().UsingAnyVerb().WithHeader("X-toto", "tatata"); + var spec = Request.Create().UsingAnyMethod().WithHeader("X-toto", "tatata"); // when string bodyAsString = "whatever"; @@ -46,7 +46,7 @@ public void Should_exclude_requests_not_matching_given_headers() public void Should_exclude_requests_not_matching_given_headers_ignorecase() { // given - var spec = Request.Create().UsingAnyVerb().WithHeader("X-toto", "abc", false); + var spec = Request.Create().UsingAnyMethod().WithHeader("X-toto", "abc", false); // when string bodyAsString = "whatever"; @@ -62,7 +62,7 @@ public void Should_exclude_requests_not_matching_given_headers_ignorecase() public void Should_specify_requests_matching_given_header_prefix() { // given - var spec = Request.Create().UsingAnyVerb().WithHeader("X-toto", "tata*"); + var spec = Request.Create().UsingAnyMethod().WithHeader("X-toto", "tata*"); // when string bodyAsString = "whatever"; @@ -80,7 +80,7 @@ public void Should_specify_requests_matching_given_header_prefix() public void Should_specify_requests_matching_given_body() { // given - var spec = Request.Create().UsingAnyVerb().WithBody("Hello world!"); + var spec = Request.Create().UsingAnyMethod().WithBody("Hello world!"); // when string bodyAsString = "Hello world!"; @@ -97,7 +97,7 @@ public void Should_specify_requests_matching_given_body() public void Should_exclude_requests_not_matching_given_body() { // given - var spec = Request.Create().UsingAnyVerb().WithBody(" Hello world! "); + var spec = Request.Create().UsingAnyMethod().WithBody(" Hello world! "); // when string bodyAsString = "xxx"; @@ -127,7 +127,7 @@ public void Should_specify_requests_matching_given_param() public void Should_specify_requests_matching_given_param_func() { // given - var spec = Request.Create().UsingAnyVerb().WithParam(p => p.ContainsKey("bar")); + var spec = Request.Create().UsingAnyMethod().WithParam(p => p.ContainsKey("bar")); // when var request = new RequestMessage(new Uri("http://localhost/foo?bar=1&bar=2"), "PUT", ClientIp); diff --git a/test/WireMock.Net.Tests/RequestWithBodyTests.cs b/test/WireMock.Net.Tests/RequestWithBodyTests.cs index 6e3736c58..1aae75514 100644 --- a/test/WireMock.Net.Tests/RequestWithBodyTests.cs +++ b/test/WireMock.Net.Tests/RequestWithBodyTests.cs @@ -19,7 +19,7 @@ public class RequestWithBodyTests public void Request_WithBody_FuncString() { // Assign - var requestBuilder = Request.Create().UsingAnyVerb().WithBody(b => b.Contains("b")); + var requestBuilder = Request.Create().UsingAnyMethod().WithBody(b => b.Contains("b")); // Act var body = new BodyData @@ -37,7 +37,7 @@ public void Request_WithBody_FuncString() public void Request_WithBody_FuncJson() { // Assign - var requestBuilder = Request.Create().UsingAnyVerb().WithBody(b => b != null); + var requestBuilder = Request.Create().UsingAnyMethod().WithBody(b => b != null); // Act var body = new BodyData @@ -55,7 +55,7 @@ public void Request_WithBody_FuncJson() public void Request_WithBody_FuncByteArray() { // Assign - var requestBuilder = Request.Create().UsingAnyVerb().WithBody((byte[] b) => b != null); + var requestBuilder = Request.Create().UsingAnyMethod().WithBody((byte[] b) => b != null); // Act var body = new BodyData @@ -73,7 +73,7 @@ public void Request_WithBody_FuncByteArray() public void Request_WithBodyExactMatcher() { // given - var requestBuilder = Request.Create().UsingAnyVerb().WithBody(new ExactMatcher("cat")); + var requestBuilder = Request.Create().UsingAnyMethod().WithBody(new ExactMatcher("cat")); // when string bodyAsString = "cat"; @@ -89,7 +89,7 @@ public void Request_WithBodyExactMatcher() public void Request_WithBodyWildcardMatcher() { // given - var spec = Request.Create().WithPath("/foo").UsingAnyVerb().WithBody(new WildcardMatcher("H*o*")); + var spec = Request.Create().WithPath("/foo").UsingAnyMethod().WithBody(new WildcardMatcher("H*o*")); // when string bodyAsString = "Hello world!"; @@ -105,7 +105,7 @@ public void Request_WithBodyWildcardMatcher() public void Request_WithBodyXPathMatcher_true() { // given - var spec = Request.Create().UsingAnyVerb().WithBody(new XPathMatcher("/todo-list[count(todo-item) = 3]")); + var spec = Request.Create().UsingAnyMethod().WithBody(new XPathMatcher("/todo-list[count(todo-item) = 3]")); // when string xmlBodyAsString = @" @@ -126,7 +126,7 @@ public void Request_WithBodyXPathMatcher_true() public void Request_WithBodyXPathMatcher_false() { // given - var spec = Request.Create().UsingAnyVerb().WithBody(new XPathMatcher("/todo-list[count(todo-item) = 99]")); + var spec = Request.Create().UsingAnyMethod().WithBody(new XPathMatcher("/todo-list[count(todo-item) = 99]")); // when string xmlBodyAsString = @" @@ -147,7 +147,7 @@ public void Request_WithBodyXPathMatcher_false() public void Request_WithBodyJsonPathMatcher_true() { // given - var spec = Request.Create().UsingAnyVerb().WithBody(new JsonPathMatcher("$..things[?(@.name == 'RequiredThing')]")); + var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$..things[?(@.name == 'RequiredThing')]")); // when string bodyAsString = "{ \"things\": [ { \"name\": \"RequiredThing\" }, { \"name\": \"Wiremock\" } ] }"; @@ -163,7 +163,7 @@ public void Request_WithBodyJsonPathMatcher_true() public void Request_WithBodyJsonPathMatcher_false() { // given - var spec = Request.Create().UsingAnyVerb().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]")); + var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]")); // when string bodyAsString = "{ \"things\": { \"name\": \"Wiremock\" } }"; @@ -179,7 +179,7 @@ public void Request_WithBodyJsonPathMatcher_false() public void Request_WithBodyAsJson_Object_JsonPathMatcher_true() { // given - var spec = Request.Create().UsingAnyVerb().WithBody(new JsonPathMatcher("$..things[?(@.name == 'RequiredThing')]")); + var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$..things[?(@.name == 'RequiredThing')]")); // when string jsonString = "{ \"things\": [ { \"name\": \"RequiredThing\" }, { \"name\": \"Wiremock\" } ] }"; @@ -199,7 +199,7 @@ public void Request_WithBodyAsJson_Object_JsonPathMatcher_true() public void Request_WithBodyAsJson_Array_JsonPathMatcher_1() { // given - var spec = Request.Create().UsingAnyVerb().WithBody(new JsonPathMatcher("$..books[?(@.price < 10)]")); + var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$..books[?(@.price < 10)]")); // when string jsonString = "{ \"books\": [ { \"category\": \"test1\", \"price\": 8.95 }, { \"category\": \"test2\", \"price\": 20 } ] }"; @@ -220,7 +220,7 @@ public void Request_WithBodyAsJson_Array_JsonPathMatcher_1() public void Request_WithBodyAsJson_Array_JsonPathMatcher_2() { // given - var spec = Request.Create().UsingAnyVerb().WithBody(new JsonPathMatcher("$..[?(@.Id == 1)]")); + var spec = Request.Create().UsingAnyMethod().WithBody(new JsonPathMatcher("$..[?(@.Id == 1)]")); // when string jsonString = "{ \"Id\": 1, \"Name\": \"Test\" }"; @@ -243,7 +243,7 @@ public void Request_WithBodyAsObject_ExactObjectMatcher_true() { // Assign object body = DateTime.MinValue; - var requestBuilder = Request.Create().UsingAnyVerb().WithBody(body); + var requestBuilder = Request.Create().UsingAnyMethod().WithBody(body); var bodyData = new BodyData { @@ -263,7 +263,7 @@ public void Request_WithBodyAsBytes_ExactObjectMatcher_true() { // Assign byte[] body = { 123 }; - var requestBuilder = Request.Create().UsingAnyVerb().WithBody(body); + var requestBuilder = Request.Create().UsingAnyMethod().WithBody(body); var bodyData = new BodyData { diff --git a/test/WireMock.Net.Tests/RequestWithPathTests.cs b/test/WireMock.Net.Tests/RequestWithPathTests.cs index 6c89b09fa..2693d3c09 100644 --- a/test/WireMock.Net.Tests/RequestWithPathTests.cs +++ b/test/WireMock.Net.Tests/RequestWithPathTests.cs @@ -17,7 +17,7 @@ public class RequestWithPathTests public void Request_WithPath_WithHeader_Match() { // given - var spec = Request.Create().WithPath("/foo").UsingAnyVerb().WithHeader("X-toto", "tata"); + var spec = Request.Create().WithPath("/foo").UsingAnyMethod().WithHeader("X-toto", "tata"); // when string bodyAsString = "whatever"; diff --git a/test/WireMock.Net.Tests/Serialization/MatcherMapperTests.cs b/test/WireMock.Net.Tests/Serialization/MatcherMapperTests.cs index ae43f5d35..fd3060313 100644 --- a/test/WireMock.Net.Tests/Serialization/MatcherMapperTests.cs +++ b/test/WireMock.Net.Tests/Serialization/MatcherMapperTests.cs @@ -47,7 +47,7 @@ public void MatcherMapper_Map_IStringMatcher() { // Assign var matcherMock = new Mock(); - matcherMock.Setup(m => m.GetName()).Returns("test"); + matcherMock.Setup(m => m.Name).Returns("test"); matcherMock.Setup(m => m.GetPatterns()).Returns(new[] { "p1", "p2" }); // Act