Compare commits

..

8 Commits

Author SHA1 Message Date
Stef Heyenrath
9665729e58 1.2.18 2020-08-13 09:02:51 +02:00
Stef Heyenrath
06be3aff95 Add ThrowExceptionWhenMatcherFails option to all Matchers (#500)
* .

* ut

* IMatcher.ThrowException

* settings

* more tests

* linq matcher throw

* .

* .
2020-08-13 08:58:18 +02:00
Stef Heyenrath
0d102f3af4 Fix MyGet badges 2020-08-09 16:28:20 +02:00
Stef Heyenrath
b55435ddac HandleRequestsSynchronously (#496) 2020-08-07 08:08:54 +02:00
Stef Heyenrath
c4ee91c614 1.2.17 2020-08-01 18:42:50 +02:00
Stef Heyenrath
4d0373d4ca Scenario : stay on current state for a number of times (#495)
* state

* xml comment
2020-08-01 18:40:35 +02:00
Stef Heyenrath
6c27820659 1.2.16 2020-07-27 19:26:03 +02:00
Stef Heyenrath
9a532108b8 Mark BlacklistedHeaders and BlacklistedCookies as obsolete (#492)
* #489

* .

* FluentMockServerSettings
2020-07-27 17:24:24 +02:00
55 changed files with 2068 additions and 1675 deletions

View File

@@ -1,3 +1,16 @@
# 1.2.18 (13 August 2020)
- [#496](https://github.com/WireMock-Net/WireMock.Net/pull/496) - Add setting to handle requests synchronously [feature] contributed by [StefH](https://github.com/StefH)
- [#500](https://github.com/WireMock-Net/WireMock.Net/pull/500) - Add ThrowExceptionWhenMatcherFails option to all Matchers [feature] contributed by [StefH](https://github.com/StefH)
- [#478](https://github.com/WireMock-Net/WireMock.Net/issues/478) - Sometimes returns status code 0 in unit tests with xunit test fixture (flaky test) [bug]
# 1.2.17 (01 August 2020)
- [#495](https://github.com/WireMock-Net/WireMock.Net/pull/495) - Scenario : stay on current state for a number of times contributed by [StefH](https://github.com/StefH)
- [#494](https://github.com/WireMock-Net/WireMock.Net/issues/494) - Stay in Current State for specified number of requests [feature]
# 1.2.16 (27 July 2020)
- [#492](https://github.com/WireMock-Net/WireMock.Net/pull/492) - Mark FluentMockServer, FluentMockServerSettings, BlacklistedHeaders and BlacklistedCookies as obsolete [feature] contributed by [StefH](https://github.com/StefH)
- [#489](https://github.com/WireMock-Net/WireMock.Net/issues/489) - Change "blacklist" and "whitelist" terms [feature]
# 1.2.15 (19 July 2020)
- [#485](https://github.com/WireMock-Net/WireMock.Net/pull/485) - Add fluent assertions for headers [test] contributed by [akamud](https://github.com/akamud)

View File

@@ -4,7 +4,7 @@
</PropertyGroup>
<PropertyGroup>
<VersionPrefix>1.2.15</VersionPrefix>
<VersionPrefix>1.2.18</VersionPrefix>
<PackageReleaseNotes>See CHANGELOG.md</PackageReleaseNotes>
<PackageIconUrl>https://raw.githubusercontent.com/WireMock-Net/WireMock.Net/master/WireMock.Net-Logo.png</PackageIconUrl>
<PackageProjectUrl>https://github.com/WireMock-Net/WireMock.Net</PackageProjectUrl>

View File

@@ -1,3 +1,3 @@
https://github.com/StefH/GitHubReleaseNotes
GitHubReleaseNotes.exe --output CHANGELOG.md --skip-empty-releases --exclude-labels question invalid doc --version 1.2.15
GitHubReleaseNotes.exe --output CHANGELOG.md --skip-empty-releases --exclude-labels question invalid doc --version 1.2.18

View File

@@ -29,10 +29,10 @@ A C# .NET version based on [mock4net](https://github.com/alexvictoor/mock4net) w
| | Official | Preview |
| - | - | - |
| &nbsp;&nbsp;**WireMock.Net** | [![NuGet Badge WireMock.Net](https://buildstats.info/nuget/WireMock.Net)](https://www.nuget.org/packages/WireMock.Net) | [![MyGet Badge WireMock.Net](https://buildstats.info/myget/wiremock-net/WireMock.Net)](https://www.myget.org/feed/wiremock-net/package/nuget/WireMock.Net)
| &nbsp;&nbsp;**WireMock.Net.StandAlone** | [![NuGet Badge WireMock.Net](https://buildstats.info/nuget/WireMock.Net.StandAlone)](https://www.nuget.org/packages/WireMock.Net.StandAlone) | [![MyGet Badge WireMock.Net.StandAlone](https://buildstats.info/myget/wiremock-net/WireMock.Net.StandAlone)](https://www.myget.org/feed/wiremock-net/package/nuget/WireMock.Net.StandAlone)
| &nbsp;&nbsp;**WireMock.Net.FluentAssertions** | [![NuGet Badge WireMock.Net.FluentAssertions](https://buildstats.info/nuget/WireMock.Net.FluentAssertions)](https://www.nuget.org/packages/WireMock.Net.FluentAssertions) | [![MyGet Badge WireMock.Net.FluentAssertions](https://buildstats.info/myget/wiremock-net/WireMock.Net.FluentAssertions)](https://www.myget.org/feed/wiremock-net/package/nuget/WireMock.Net.FluentAssertions)
| &nbsp;&nbsp;**WireMock.Net.RestClient** | [![NuGet Badge WireMock.Net.RestClient](https://buildstats.info/nuget/WireMock.Net.RestClient)](https://www.nuget.org/packages/WireMock.Net.RestClient) | [![MyGet Badge WireMock.Net.RestClient](https://buildstats.info/myget/wiremock-net/WireMock.Net.RestClient)](https://www.myget.org/feed/wiremock-net/package/nuget/WireMock.Net.RestClient)
| &nbsp;&nbsp;**WireMock.Net** | [![NuGet Badge WireMock.Net](https://buildstats.info/nuget/WireMock.Net)](https://www.nuget.org/packages/WireMock.Net) | [![MyGet Badge WireMock.Net](https://buildstats.info/myget/wiremock-net/WireMock.Net?includePreReleases=true)](https://www.myget.org/feed/wiremock-net/package/nuget/WireMock.Net)
| &nbsp;&nbsp;**WireMock.Net.StandAlone** | [![NuGet Badge WireMock.Net](https://buildstats.info/nuget/WireMock.Net.StandAlone)](https://www.nuget.org/packages/WireMock.Net.StandAlone) | [![MyGet Badge WireMock.Net.StandAlone](https://buildstats.info/myget/wiremock-net/WireMock.Net.StandAlone?includePreReleases=true)](https://www.myget.org/feed/wiremock-net/package/nuget/WireMock.Net.StandAlone)
| &nbsp;&nbsp;**WireMock.Net.FluentAssertions** | [![NuGet Badge WireMock.Net.FluentAssertions](https://buildstats.info/nuget/WireMock.Net.FluentAssertions)](https://www.nuget.org/packages/WireMock.Net.FluentAssertions) | [![MyGet Badge WireMock.Net.FluentAssertions](https://buildstats.info/myget/wiremock-net/WireMock.Net.FluentAssertions?includePreReleases=true)](https://www.myget.org/feed/wiremock-net/package/nuget/WireMock.Net.FluentAssertions)
| &nbsp;&nbsp;**WireMock.Net.RestClient** | [![NuGet Badge WireMock.Net.RestClient](https://buildstats.info/nuget/WireMock.Net.RestClient)](https://www.nuget.org/packages/WireMock.Net.RestClient) | [![MyGet Badge WireMock.Net.RestClient](https://buildstats.info/myget/wiremock-net/WireMock.Net.RestClient?includePreReleases=true)](https://www.myget.org/feed/wiremock-net/package/nuget/WireMock.Net.RestClient)
## Development
For the supported frameworks and build information, see [this](https://github.com/WireMock-Net/WireMock.Net/wiki/Development-Information) page.

View File

@@ -24,5 +24,10 @@
/// Gets or sets a value indicating whether this <see cref="ScenarioStateModel"/> is finished.
/// </summary>
public bool Finished { get; set; }
/// <summary>
/// Gets or sets the state counter.
/// </summary>
public int Counter { get; set; }
}
}

View File

@@ -26,8 +26,18 @@
public int? MaxRequestLogCount { get; set; }
/// <summary>
/// Gets or sets wether to allow a body for all HTTP methods.
/// Allow a Body for all HTTP Methods. (default set to false).
/// </summary>
public bool? AllowBodyForAllHttpMethods { get; set; }
/// <summary>
/// Handle all requests synchronously. (default set to false).
/// </summary>
public bool? HandleRequestsSynchronously { get; set; }
/// <summary>
/// Throw an exception when the Matcher fails because of invalid input. (default set to false).
/// </summary>
public bool? ThrowExceptionWhenMatcherFails { get; set; }
}
}

View File

@@ -27,9 +27,9 @@ namespace WireMock.Server
/// </summary>
IEnumerable<MappingModel> MappingModels { get; }
/// <summary>
/// Gets the mappings.
/// </summary>
// <summary>
// Gets the mappings.
// </summary>
//[PublicAPI]
//IEnumerable<IMapping> Mappings { get; }

View File

@@ -13,9 +13,9 @@ namespace WireMock.Net.StandAlone
public static class StandAloneApp
{
/// <summary>
/// Start WireMock.Net standalone Server based on the FluentMockServerSettings.
/// Start WireMock.Net standalone Server based on the IWireMockServerSettings.
/// </summary>
/// <param name="settings">The FluentMockServerSettings</param>
/// <param name="settings">The IWireMockServerSettings</param>
[PublicAPI]
public static WireMockServer Start([NotNull] IWireMockServerSettings settings)
{

View File

@@ -51,6 +51,12 @@ namespace WireMock
[CanBeNull]
string NextState { get; }
/// <summary>
/// The number of times this match should be matched before the state will be changed to the next state.
/// </summary>
[CanBeNull]
int? StateTimes { get; }
/// <summary>
/// The Request matcher.
/// </summary>

View File

@@ -33,6 +33,9 @@ namespace WireMock
/// <inheritdoc cref="IMapping.NextState" />
public string NextState { get; }
/// <inheritdoc cref="IMapping.StateTimes" />
public int? StateTimes { get; }
/// <inheritdoc cref="IMapping.RequestMatcher" />
public IRequestMatcher RequestMatcher { get; }
@@ -64,9 +67,10 @@ namespace WireMock
/// <param name="scenario">The scenario. [Optional]</param>
/// <param name="executionConditionState">State in which the current mapping can occur. [Optional]</param>
/// <param name="nextState">The next state which will occur after the current mapping execution. [Optional]</param>
/// <param name="stateTimes">Only when the current state is executed this number, the next state which will occur. [Optional]</param>
public Mapping(Guid guid, [CanBeNull] string title, [CanBeNull] string path,
[NotNull] IWireMockServerSettings settings, [NotNull] IRequestMatcher requestMatcher, [NotNull] IResponseProvider provider,
int priority, [CanBeNull] string scenario, [CanBeNull] string executionConditionState, [CanBeNull] string nextState)
int priority, [CanBeNull] string scenario, [CanBeNull] string executionConditionState, [CanBeNull] string nextState, [CanBeNull] int? stateTimes)
{
Guid = guid;
Title = title;
@@ -78,6 +82,7 @@ namespace WireMock
Scenario = scenario;
ExecutionConditionState = executionConditionState;
NextState = nextState;
StateTimes = stateTimes;
}
/// <inheritdoc cref="IMapping.ProvideResponseAsync" />

View File

@@ -29,6 +29,9 @@ namespace WireMock.Matchers
public MatchBehaviour MatchBehaviour { get; }
/// <inheritdoc cref="IMatcher.ThrowException"/>
public bool ThrowException { get; }
private readonly string[] _patterns;
/// <summary>
@@ -49,6 +52,7 @@ namespace WireMock.Matchers
Check.NotNull(patterns, nameof(patterns));
MatchBehaviour = matchBehaviour;
ThrowException = false;
_patterns = patterns;
}
@@ -156,7 +160,7 @@ namespace WireMock.Matchers
#if NETSTANDARD2_0
script = csscript.GenericExtensions.CreateObject(assembly, "*");
#else
script = CSScriptLib.ReflectionExtensions.CreateObject(assembly,"*");
script = CSScriptLib.ReflectionExtensions.CreateObject(assembly, "*");
#endif
}
catch (Exception ex)

View File

@@ -1,73 +1,75 @@
using System.Net.Http.Headers;
using JetBrains.Annotations;
namespace WireMock.Matchers
{
/// <summary>
/// ContentTypeMatcher which accepts also all charsets
/// </summary>
/// <seealso cref="RegexMatcher" />
public class ContentTypeMatcher : WildcardMatcher
{
private readonly string[] _patterns;
/// <summary>
/// Initializes a new instance of the <see cref="ContentTypeMatcher"/> class.
/// </summary>
/// <param name="pattern">The pattern.</param>
/// <param name="ignoreCase">IgnoreCase (default false)</param>
public ContentTypeMatcher([NotNull] string pattern, bool ignoreCase = false) : this(new[] { pattern }, ignoreCase)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ContentTypeMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="pattern">The pattern.</param>
/// <param name="ignoreCase">IgnoreCase (default false)</param>
public ContentTypeMatcher(MatchBehaviour matchBehaviour, [NotNull] string pattern, bool ignoreCase = false) : this(matchBehaviour, new[] { pattern }, ignoreCase)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ContentTypeMatcher"/> class.
/// </summary>
/// <param name="patterns">The patterns.</param>
/// <param name="ignoreCase">IgnoreCase (default false)</param>
public ContentTypeMatcher([NotNull] string[] patterns, bool ignoreCase = false) : this(MatchBehaviour.AcceptOnMatch, patterns, ignoreCase)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ContentTypeMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="patterns">The patterns.</param>
/// <param name="ignoreCase">IgnoreCase (default false)</param>
public ContentTypeMatcher(MatchBehaviour matchBehaviour, [NotNull] string[] patterns, bool ignoreCase = false) : base(matchBehaviour, patterns, ignoreCase)
{
_patterns = patterns;
}
/// <inheritdoc cref="RegexMatcher.IsMatch"/>
public override double IsMatch(string input)
{
if (string.IsNullOrEmpty(input) || !MediaTypeHeaderValue.TryParse(input, out MediaTypeHeaderValue contentType))
{
return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.Mismatch);
}
return base.IsMatch(contentType.MediaType);
}
/// <inheritdoc cref="IStringMatcher.GetPatterns"/>
public override string[] GetPatterns()
{
return _patterns;
}
/// <inheritdoc cref="IMatcher.Name"/>
public override string Name => "ContentTypeMatcher";
}
using System.Net.Http.Headers;
using JetBrains.Annotations;
namespace WireMock.Matchers
{
/// <summary>
/// ContentTypeMatcher which accepts also all charsets
/// </summary>
/// <seealso cref="RegexMatcher" />
public class ContentTypeMatcher : WildcardMatcher
{
private readonly string[] _patterns;
/// <summary>
/// Initializes a new instance of the <see cref="ContentTypeMatcher"/> class.
/// </summary>
/// <param name="pattern">The pattern.</param>
/// <param name="ignoreCase">IgnoreCase (default false)</param>
public ContentTypeMatcher([NotNull] string pattern, bool ignoreCase = false) : this(new[] { pattern }, ignoreCase)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ContentTypeMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="pattern">The pattern.</param>
/// <param name="ignoreCase">IgnoreCase (default false)</param>
public ContentTypeMatcher(MatchBehaviour matchBehaviour, [NotNull] string pattern, bool ignoreCase = false) : this(matchBehaviour, new[] { pattern }, ignoreCase)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ContentTypeMatcher"/> class.
/// </summary>
/// <param name="patterns">The patterns.</param>
/// <param name="ignoreCase">IgnoreCase (default false)</param>
public ContentTypeMatcher([NotNull] string[] patterns, bool ignoreCase = false) : this(MatchBehaviour.AcceptOnMatch, patterns, ignoreCase)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ContentTypeMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="patterns">The patterns.</param>
/// <param name="ignoreCase">IgnoreCase (default false)</param>
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
public ContentTypeMatcher(MatchBehaviour matchBehaviour, [NotNull] string[] patterns, bool ignoreCase = false, bool throwException = false) :
base(matchBehaviour, patterns, ignoreCase, throwException)
{
_patterns = patterns;
}
/// <inheritdoc cref="RegexMatcher.IsMatch"/>
public override double IsMatch(string input)
{
if (string.IsNullOrEmpty(input) || !MediaTypeHeaderValue.TryParse(input, out MediaTypeHeaderValue contentType))
{
return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.Mismatch);
}
return base.IsMatch(contentType.MediaType);
}
/// <inheritdoc cref="IStringMatcher.GetPatterns"/>
public override string[] GetPatterns()
{
return _patterns;
}
/// <inheritdoc cref="IMatcher.Name"/>
public override string Name => "ContentTypeMatcher";
}
}

View File

@@ -1,59 +1,64 @@
using System.Linq;
using JetBrains.Annotations;
using WireMock.Validation;
namespace WireMock.Matchers
{
/// <summary>
/// ExactMatcher
/// </summary>
/// <seealso cref="IStringMatcher" />
public class ExactMatcher : IStringMatcher
{
private readonly string[] _values;
/// <inheritdoc cref="IMatcher.MatchBehaviour"/>
public MatchBehaviour MatchBehaviour { get; }
/// <summary>
/// Initializes a new instance of the <see cref="ExactMatcher"/> class.
/// </summary>
/// <param name="values">The values.</param>
public ExactMatcher([NotNull] params string[] values) : this(MatchBehaviour.AcceptOnMatch, values)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ExactMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="values">The values.</param>
public ExactMatcher(MatchBehaviour matchBehaviour, [NotNull] params string[] values)
{
Check.HasNoNulls(values, nameof(values));
_values = values;
MatchBehaviour = matchBehaviour;
}
/// <inheritdoc cref="IStringMatcher.IsMatch"/>
public double IsMatch(string input)
{
if (_values.Length == 1)
{
return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(_values[0] == input));
}
return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(_values.Contains(input)));
}
/// <inheritdoc cref="IStringMatcher.GetPatterns"/>
public string[] GetPatterns()
{
return _values;
}
/// <inheritdoc cref="IMatcher.Name"/>
public string Name => "ExactMatcher";
}
using System.Linq;
using JetBrains.Annotations;
using WireMock.Validation;
namespace WireMock.Matchers
{
/// <summary>
/// ExactMatcher
/// </summary>
/// <seealso cref="IStringMatcher" />
public class ExactMatcher : IStringMatcher
{
private readonly string[] _values;
/// <inheritdoc cref="IMatcher.MatchBehaviour"/>
public MatchBehaviour MatchBehaviour { get; }
/// <inheritdoc cref="IMatcher.ThrowException"/>
public bool ThrowException { get; }
/// <summary>
/// Initializes a new instance of the <see cref="ExactMatcher"/> class.
/// </summary>
/// <param name="values">The values.</param>
public ExactMatcher([NotNull] params string[] values) : this(MatchBehaviour.AcceptOnMatch, false, values)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ExactMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
/// <param name="values">The values.</param>
public ExactMatcher(MatchBehaviour matchBehaviour, bool throwException = false, [NotNull] params string[] values)
{
Check.HasNoNulls(values, nameof(values));
MatchBehaviour = matchBehaviour;
ThrowException = throwException;
_values = values;
}
/// <inheritdoc cref="IStringMatcher.IsMatch"/>
public double IsMatch(string input)
{
if (_values.Length == 1)
{
return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(_values[0] == input));
}
return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(_values.Contains(input)));
}
/// <inheritdoc cref="IStringMatcher.GetPatterns"/>
public string[] GetPatterns()
{
return _values;
}
/// <inheritdoc cref="IMatcher.Name"/>
public string Name => "ExactMatcher";
}
}

View File

@@ -1,78 +1,83 @@
using JetBrains.Annotations;
using System.Linq;
using WireMock.Validation;
namespace WireMock.Matchers
{
/// <summary>
/// ExactObjectMatcher
/// </summary>
/// <seealso cref="IObjectMatcher" />
public class ExactObjectMatcher : IObjectMatcher
{
/// <summary>
/// Gets the value as object.
/// </summary>
public object ValueAsObject { get; }
/// <summary>
/// Gets the value as byte[].
/// </summary>
public byte[] ValueAsBytes { get; }
/// <inheritdoc cref="IMatcher.MatchBehaviour"/>
public MatchBehaviour MatchBehaviour { get; }
/// <summary>
/// Initializes a new instance of the <see cref="ExactObjectMatcher"/> class.
/// </summary>
/// <param name="value">The value.</param>
public ExactObjectMatcher([NotNull] object value) : this(MatchBehaviour.AcceptOnMatch, value)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ExactObjectMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="value">The value.</param>
public ExactObjectMatcher(MatchBehaviour matchBehaviour, [NotNull] object value)
{
Check.NotNull(value, nameof(value));
ValueAsObject = value;
MatchBehaviour = matchBehaviour;
}
/// <summary>
/// Initializes a new instance of the <see cref="ExactObjectMatcher"/> class.
/// </summary>
/// <param name="value">The value.</param>
public ExactObjectMatcher([NotNull] byte[] value) : this(MatchBehaviour.AcceptOnMatch, value)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ExactObjectMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="value">The value.</param>
public ExactObjectMatcher(MatchBehaviour matchBehaviour, [NotNull] byte[] value)
{
Check.NotNull(value, nameof(value));
ValueAsBytes = value;
MatchBehaviour = matchBehaviour;
}
/// <inheritdoc cref="IObjectMatcher.IsMatch"/>
public double IsMatch(object input)
{
bool equals = ValueAsObject != null ? Equals(ValueAsObject, input) : ValueAsBytes.SequenceEqual((byte[])input);
return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(equals));
}
/// <inheritdoc cref="IMatcher.Name"/>
public string Name => "ExactObjectMatcher";
}
using JetBrains.Annotations;
using System.Linq;
using WireMock.Validation;
namespace WireMock.Matchers
{
/// <summary>
/// ExactObjectMatcher
/// </summary>
/// <seealso cref="IObjectMatcher" />
public class ExactObjectMatcher : IObjectMatcher
{
/// <summary>
/// Gets the value as object.
/// </summary>
public object ValueAsObject { get; }
/// <summary>
/// Gets the value as byte[].
/// </summary>
public byte[] ValueAsBytes { get; }
/// <inheritdoc cref="IMatcher.MatchBehaviour"/>
public MatchBehaviour MatchBehaviour { get; }
/// <inheritdoc cref="IMatcher.ThrowException"/>
public bool ThrowException { get; }
/// <summary>
/// Initializes a new instance of the <see cref="ExactObjectMatcher"/> class.
/// </summary>
/// <param name="value">The value.</param>
public ExactObjectMatcher([NotNull] object value) : this(MatchBehaviour.AcceptOnMatch, value)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ExactObjectMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="value">The value.</param>
public ExactObjectMatcher(MatchBehaviour matchBehaviour, [NotNull] object value)
{
Check.NotNull(value, nameof(value));
ValueAsObject = value;
MatchBehaviour = matchBehaviour;
}
/// <summary>
/// Initializes a new instance of the <see cref="ExactObjectMatcher"/> class.
/// </summary>
/// <param name="value">The value.</param>
public ExactObjectMatcher([NotNull] byte[] value) : this(MatchBehaviour.AcceptOnMatch, value)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ExactObjectMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
/// <param name="value">The value.</param>
public ExactObjectMatcher(MatchBehaviour matchBehaviour, [NotNull] byte[] value, bool throwException = false)
{
Check.NotNull(value, nameof(value));
MatchBehaviour = matchBehaviour;
ThrowException = throwException;
ValueAsBytes = value;
}
/// <inheritdoc cref="IObjectMatcher.IsMatch"/>
public double IsMatch(object input)
{
bool equals = ValueAsObject != null ? Equals(ValueAsObject, input) : ValueAsBytes.SequenceEqual((byte[])input);
return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(equals));
}
/// <inheritdoc cref="IMatcher.Name"/>
public string Name => "ExactObjectMatcher";
}
}

View File

@@ -1,18 +1,23 @@
namespace WireMock.Matchers
{
/// <summary>
/// IMatcher
/// </summary>
public interface IMatcher
{
/// <summary>
/// Gets the name.
/// </summary>
string Name { get; }
/// <summary>
/// Gets the match behaviour.
/// </summary>
MatchBehaviour MatchBehaviour { get; }
}
namespace WireMock.Matchers
{
/// <summary>
/// IMatcher
/// </summary>
public interface IMatcher
{
/// <summary>
/// Gets the name.
/// </summary>
string Name { get; }
/// <summary>
/// Gets the match behaviour.
/// </summary>
MatchBehaviour MatchBehaviour { get; }
/// <summary>
/// Should this matcher throw an exception?
/// </summary>
bool ThrowException { get; }
}
}

View File

@@ -1,99 +1,110 @@
using System.Linq;
using JetBrains.Annotations;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using WireMock.Validation;
namespace WireMock.Matchers
{
/// <summary>
/// JsonPathMatcher
/// </summary>
/// <seealso cref="IMatcher" />
/// <seealso cref="IObjectMatcher" />
public class JsonPathMatcher : IStringMatcher, IObjectMatcher
{
private readonly string[] _patterns;
/// <inheritdoc cref="IMatcher.MatchBehaviour"/>
public MatchBehaviour MatchBehaviour { get; }
/// <summary>
/// Initializes a new instance of the <see cref="JsonPathMatcher"/> class.
/// </summary>
/// <param name="patterns">The patterns.</param>
public JsonPathMatcher([NotNull] params string[] patterns) : this(MatchBehaviour.AcceptOnMatch, patterns)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="JsonPathMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="patterns">The patterns.</param>
public JsonPathMatcher(MatchBehaviour matchBehaviour, [NotNull] params string[] patterns)
{
Check.NotNull(patterns, nameof(patterns));
MatchBehaviour = matchBehaviour;
_patterns = patterns;
}
/// <inheritdoc cref="IStringMatcher.IsMatch"/>
public double IsMatch(string input)
{
double match = MatchScores.Mismatch;
if (input != null)
{
try
{
var jtoken = JToken.Parse(input);
match = IsMatch(jtoken);
}
catch (JsonException)
{
// just ignore JsonException
}
}
return MatchBehaviourHelper.Convert(MatchBehaviour, match);
}
/// <inheritdoc cref="IObjectMatcher.IsMatch"/>
public double IsMatch(object input)
{
double match = MatchScores.Mismatch;
// When input is null or byte[], return Mismatch.
if (input != null && !(input is byte[]))
{
try
{
// Check if JToken or object
JToken jtoken = input is JToken token ? token : JObject.FromObject(input);
match = IsMatch(jtoken);
}
catch (JsonException)
{
// just ignore JsonException
}
}
return MatchBehaviourHelper.Convert(MatchBehaviour, match);
}
/// <inheritdoc cref="IStringMatcher.GetPatterns"/>
public string[] GetPatterns()
{
return _patterns;
}
/// <inheritdoc cref="IMatcher.Name"/>
public string Name => "JsonPathMatcher";
private double IsMatch(JToken jtoken)
{
return MatchScores.ToScore(_patterns.Select(pattern => jtoken.SelectToken(pattern) != null));
}
}
using System.Linq;
using JetBrains.Annotations;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using WireMock.Validation;
namespace WireMock.Matchers
{
/// <summary>
/// JsonPathMatcher
/// </summary>
/// <seealso cref="IMatcher" />
/// <seealso cref="IObjectMatcher" />
public class JsonPathMatcher : IStringMatcher, IObjectMatcher
{
private readonly string[] _patterns;
/// <inheritdoc cref="IMatcher.MatchBehaviour"/>
public MatchBehaviour MatchBehaviour { get; }
/// <inheritdoc cref="IMatcher.ThrowException"/>
public bool ThrowException { get; }
/// <summary>
/// Initializes a new instance of the <see cref="JsonPathMatcher"/> class.
/// </summary>
/// <param name="patterns">The patterns.</param>
public JsonPathMatcher([NotNull] params string[] patterns) : this(MatchBehaviour.AcceptOnMatch, false, patterns)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="JsonPathMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
/// <param name="patterns">The patterns.</param>
public JsonPathMatcher(MatchBehaviour matchBehaviour, bool throwException = false, [NotNull] params string[] patterns)
{
Check.NotNull(patterns, nameof(patterns));
MatchBehaviour = matchBehaviour;
ThrowException = throwException;
_patterns = patterns;
}
/// <inheritdoc cref="IStringMatcher.IsMatch"/>
public double IsMatch(string input)
{
double match = MatchScores.Mismatch;
if (input != null)
{
try
{
var jtoken = JToken.Parse(input);
match = IsMatch(jtoken);
}
catch (JsonException)
{
if (ThrowException)
{
throw;
}
}
}
return MatchBehaviourHelper.Convert(MatchBehaviour, match);
}
/// <inheritdoc cref="IObjectMatcher.IsMatch"/>
public double IsMatch(object input)
{
double match = MatchScores.Mismatch;
// When input is null or byte[], return Mismatch.
if (input != null && !(input is byte[]))
{
try
{
// Check if JToken or object
JToken jtoken = input is JToken token ? token : JObject.FromObject(input);
match = IsMatch(jtoken);
}
catch (JsonException)
{
if (ThrowException)
{
throw;
}
}
}
return MatchBehaviourHelper.Convert(MatchBehaviour, match);
}
/// <inheritdoc cref="IStringMatcher.GetPatterns"/>
public string[] GetPatterns()
{
return _patterns;
}
/// <inheritdoc cref="IMatcher.Name"/>
public string Name => "JsonPathMatcher";
private double IsMatch(JToken jtoken)
{
return MatchScores.ToScore(_patterns.Select(pattern => jtoken.SelectToken(pattern) != null));
}
}
}

View File

@@ -1,83 +1,100 @@
using DevLab.JmesPath;
using JetBrains.Annotations;
using Newtonsoft.Json;
using System.Linq;
using WireMock.Validation;
namespace WireMock.Matchers
{
/// <summary>
/// http://jmespath.org/
/// </summary>
public class JmesPathMatcher : IStringMatcher, IObjectMatcher
{
private readonly string[] _patterns;
/// <inheritdoc cref="IMatcher.MatchBehaviour"/>
public MatchBehaviour MatchBehaviour { get; }
/// <summary>
/// Initializes a new instance of the <see cref="JmesPathMatcher"/> class.
/// </summary>
/// <param name="patterns">The patterns.</param>
public JmesPathMatcher([NotNull] params string[] patterns) : this(MatchBehaviour.AcceptOnMatch, patterns)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="JmesPathMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="patterns">The patterns.</param>
public JmesPathMatcher(MatchBehaviour matchBehaviour, [NotNull] params string[] patterns)
{
Check.NotNull(patterns, nameof(patterns));
MatchBehaviour = matchBehaviour;
_patterns = patterns;
}
/// <inheritdoc cref="IStringMatcher.IsMatch"/>
public double IsMatch(string input)
{
double match = MatchScores.Mismatch;
if (input != null)
{
try
{
match = MatchScores.ToScore(_patterns.Select(pattern => bool.Parse(new JmesPath().Transform(input, pattern))));
}
catch (JsonException)
{
// just ignore JsonException
}
}
return MatchBehaviourHelper.Convert(MatchBehaviour, match);
}
/// <inheritdoc cref="IObjectMatcher.IsMatch"/>
public double IsMatch(object input)
{
double match = MatchScores.Mismatch;
// When input is null or byte[], return Mismatch.
if (input != null && !(input is byte[]))
{
string inputAsString = JsonConvert.SerializeObject(input);
return IsMatch(inputAsString);
}
return MatchBehaviourHelper.Convert(MatchBehaviour, match);
}
/// <inheritdoc cref="IStringMatcher.GetPatterns"/>
public string[] GetPatterns()
{
return _patterns;
}
/// <inheritdoc cref="IMatcher.Name"/>
public string Name => "JmesPathMatcher";
}
using DevLab.JmesPath;
using JetBrains.Annotations;
using Newtonsoft.Json;
using System.Linq;
using WireMock.Validation;
namespace WireMock.Matchers
{
/// <summary>
/// http://jmespath.org/
/// </summary>
public class JmesPathMatcher : IStringMatcher, IObjectMatcher
{
private readonly string[] _patterns;
/// <inheritdoc cref="IMatcher.MatchBehaviour"/>
public MatchBehaviour MatchBehaviour { get; }
/// <inheritdoc cref="IMatcher.ThrowException"/>
public bool ThrowException { get; }
/// <summary>
/// Initializes a new instance of the <see cref="JmesPathMatcher"/> class.
/// </summary>
/// <param name="patterns">The patterns.</param>
public JmesPathMatcher([NotNull] params string[] patterns) : this(MatchBehaviour.AcceptOnMatch, false, patterns)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="JmesPathMatcher"/> class.
/// </summary>
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
/// <param name="patterns">The patterns.</param>
public JmesPathMatcher(bool throwException = false, [NotNull] params string[] patterns) : this(MatchBehaviour.AcceptOnMatch, throwException, patterns)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="JmesPathMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
/// <param name="patterns">The patterns.</param>
public JmesPathMatcher(MatchBehaviour matchBehaviour, bool throwException = false, [NotNull] params string[] patterns)
{
Check.NotNull(patterns, nameof(patterns));
MatchBehaviour = matchBehaviour;
ThrowException = throwException;
_patterns = patterns;
}
/// <inheritdoc cref="IStringMatcher.IsMatch"/>
public double IsMatch(string input)
{
double match = MatchScores.Mismatch;
if (input != null)
{
try
{
match = MatchScores.ToScore(_patterns.Select(pattern => bool.Parse(new JmesPath().Transform(input, pattern))));
}
catch (JsonException)
{
if (ThrowException)
{
throw;
}
}
}
return MatchBehaviourHelper.Convert(MatchBehaviour, match);
}
/// <inheritdoc cref="IObjectMatcher.IsMatch"/>
public double IsMatch(object input)
{
double match = MatchScores.Mismatch;
// When input is null or byte[], return Mismatch.
if (input != null && !(input is byte[]))
{
string inputAsString = JsonConvert.SerializeObject(input);
return IsMatch(inputAsString);
}
return MatchBehaviourHelper.Convert(MatchBehaviour, match);
}
/// <inheritdoc cref="IStringMatcher.GetPatterns"/>
public string[] GetPatterns()
{
return _patterns;
}
/// <inheritdoc cref="IMatcher.Name"/>
public string Name => "JmesPathMatcher";
}
}

View File

@@ -1,169 +1,165 @@
using JetBrains.Annotations;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections;
using System.Linq;
using WireMock.Util;
using WireMock.Validation;
namespace WireMock.Matchers
{
/// <summary>
/// JsonMatcher
/// </summary>
public class JsonMatcher : IValueMatcher, IIgnoreCaseMatcher
{
/// <inheritdoc cref="IValueMatcher.Value"/>
public object Value { get; }
/// <inheritdoc cref="IMatcher.Name"/>
public string Name => "JsonMatcher";
/// <inheritdoc cref="IMatcher.MatchBehaviour"/>
public MatchBehaviour MatchBehaviour { get; }
/// <inheritdoc cref="IIgnoreCaseMatcher.IgnoreCase"/>
public bool IgnoreCase { get; }
/// <summary>
/// Initializes a new instance of the <see cref="JsonMatcher"/> class.
/// </summary>
/// <param name="value">The string value to check for equality.</param>
/// <param name="ignoreCase">Ignore the case from the PropertyName and PropertyValue (string only).</param>
public JsonMatcher([NotNull] string value, bool ignoreCase = false) : this(MatchBehaviour.AcceptOnMatch, value, ignoreCase)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="JsonMatcher"/> class.
/// </summary>
/// <param name="value">The object value to check for equality.</param>
/// <param name="ignoreCase">Ignore the case from the PropertyName and PropertyValue (string only).</param>
public JsonMatcher([NotNull] object value, bool ignoreCase = false) : this(MatchBehaviour.AcceptOnMatch, value, ignoreCase)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="JsonMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="value">The string value to check for equality.</param>
/// <param name="ignoreCase">Ignore the case from the PropertyName and PropertyValue (string only).</param>
public JsonMatcher(MatchBehaviour matchBehaviour, [NotNull] string value, bool ignoreCase = false)
{
Check.NotNull(value, nameof(value));
MatchBehaviour = matchBehaviour;
Value = value;
IgnoreCase = ignoreCase;
}
/// <summary>
/// Initializes a new instance of the <see cref="JsonMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="value">The object value to check for equality.</param>
/// <param name="ignoreCase">Ignore the case from the PropertyName and PropertyValue (string only).</param>
public JsonMatcher(MatchBehaviour matchBehaviour, [NotNull] object value, bool ignoreCase = false)
{
Check.NotNull(value, nameof(value));
MatchBehaviour = matchBehaviour;
Value = value;
IgnoreCase = ignoreCase;
}
/// <inheritdoc cref="IObjectMatcher.IsMatch"/>
public double IsMatch(object input)
{
bool match = false;
// When input is null or byte[], return Mismatch.
if (input != null && !(input is byte[]))
{
try
{
// Check if JToken or object
JToken jtokenInput = input is JToken tokenInput ? tokenInput : JObject.FromObject(input);
// Check if JToken, string, IEnumerable or object
JToken jtokenValue;
switch (Value)
{
case JToken tokenValue:
jtokenValue = tokenValue;
break;
case string stringValue:
jtokenValue = JsonUtils.Parse(stringValue);
break;
case IEnumerable enumerableValue:
jtokenValue = JArray.FromObject(enumerableValue);
break;
default:
jtokenValue = JObject.FromObject(Value);
break;
}
match = DeepEquals(jtokenValue, jtokenInput);
}
catch (JsonException)
{
// just ignore JsonException
}
}
return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(match));
}
private bool DeepEquals(JToken value, JToken input)
{
if (!IgnoreCase)
{
return JToken.DeepEquals(value, input);
}
JToken renamedValue = Rename(value);
JToken renamedInput = Rename(input);
return JToken.DeepEquals(renamedValue, renamedInput);
}
private static string ToUpper(string input)
{
return input?.ToUpperInvariant();
}
// https://stackoverflow.com/questions/11679804/json-net-rename-properties
private static JToken Rename(JToken json)
{
if (json is JProperty property)
{
JToken propertyValue = property.Value;
if (propertyValue.Type == JTokenType.String)
{
string stringValue = propertyValue.Value<string>();
propertyValue = ToUpper(stringValue);
}
return new JProperty(ToUpper(property.Name), Rename(propertyValue));
}
if (json is JArray array)
{
var renamedValues = array.Select(Rename);
return new JArray(renamedValues);
}
if (json is JObject obj)
{
var renamedProperties = obj.Properties().Select(Rename);
return new JObject(renamedProperties);
}
return json;
}
}
using System.Collections;
using System.Linq;
using JetBrains.Annotations;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using WireMock.Util;
using WireMock.Validation;
namespace WireMock.Matchers
{
/// <summary>
/// JsonMatcher
/// </summary>
public class JsonMatcher : IValueMatcher, IIgnoreCaseMatcher
{
/// <inheritdoc cref="IValueMatcher.Value"/>
public object Value { get; }
/// <inheritdoc cref="IMatcher.Name"/>
public string Name => "JsonMatcher";
/// <inheritdoc cref="IMatcher.MatchBehaviour"/>
public MatchBehaviour MatchBehaviour { get; }
/// <inheritdoc cref="IIgnoreCaseMatcher.IgnoreCase"/>
public bool IgnoreCase { get; }
/// <inheritdoc cref="IMatcher.ThrowException"/>
public bool ThrowException { get; }
private readonly JToken _valueAsJToken;
/// <summary>
/// Initializes a new instance of the <see cref="JsonMatcher"/> class.
/// </summary>
/// <param name="value">The string value to check for equality.</param>
/// <param name="ignoreCase">Ignore the case from the PropertyName and PropertyValue (string only).</param>
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
public JsonMatcher([NotNull] string value, bool ignoreCase = false, bool throwException = false) : this(MatchBehaviour.AcceptOnMatch, value, ignoreCase, throwException)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="JsonMatcher"/> class.
/// </summary>
/// <param name="value">The object value to check for equality.</param>
/// <param name="ignoreCase">Ignore the case from the PropertyName and PropertyValue (string only).</param>
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
public JsonMatcher([NotNull] object value, bool ignoreCase = false, bool throwException = false) : this(MatchBehaviour.AcceptOnMatch, value, ignoreCase, throwException)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="JsonMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="value">The value to check for equality.</param>
/// <param name="ignoreCase">Ignore the case from the PropertyName and PropertyValue (string only).</param>
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
public JsonMatcher(MatchBehaviour matchBehaviour, [NotNull] object value, bool ignoreCase = false, bool throwException = false)
{
Check.NotNull(value, nameof(value));
MatchBehaviour = matchBehaviour;
IgnoreCase = ignoreCase;
ThrowException = throwException;
Value = value;
_valueAsJToken = ConvertValueToJToken(value);
}
/// <inheritdoc cref="IObjectMatcher.IsMatch"/>
public double IsMatch(object input)
{
bool match = false;
// When input is null or byte[], return Mismatch.
if (input != null && !(input is byte[]))
{
try
{
var inputAsJToken = ConvertValueToJToken(input);
match = DeepEquals(_valueAsJToken, inputAsJToken);
}
catch (JsonException)
{
if (ThrowException)
{
throw;
}
}
}
return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(match));
}
private static JToken ConvertValueToJToken(object value)
{
// Check if JToken, string, IEnumerable or object
switch (value)
{
case JToken tokenValue:
return tokenValue;
case string stringValue:
return JsonUtils.Parse(stringValue);
case IEnumerable enumerableValue:
return JArray.FromObject(enumerableValue);
default:
return JObject.FromObject(value);
}
}
private bool DeepEquals(JToken value, JToken input)
{
if (!IgnoreCase)
{
return JToken.DeepEquals(value, input);
}
JToken renamedValue = Rename(value);
JToken renamedInput = Rename(input);
return JToken.DeepEquals(renamedValue, renamedInput);
}
private static string ToUpper(string input)
{
return input?.ToUpperInvariant();
}
// https://stackoverflow.com/questions/11679804/json-net-rename-properties
private static JToken Rename(JToken json)
{
if (json is JProperty property)
{
JToken propertyValue = property.Value;
if (propertyValue.Type == JTokenType.String)
{
string stringValue = propertyValue.Value<string>();
propertyValue = ToUpper(stringValue);
}
return new JProperty(ToUpper(property.Name), Rename(propertyValue));
}
if (json is JArray array)
{
var renamedValues = array.Select(Rename);
return new JArray(renamedValues);
}
if (json is JObject obj)
{
var renamedProperties = obj.Properties().Select(Rename);
return new JObject(renamedProperties);
}
return json;
}
}
}

View File

@@ -1,132 +1,140 @@
using System.Linq;
using System.Linq.Dynamic.Core;
using JetBrains.Annotations;
using Newtonsoft.Json.Linq;
using WireMock.Util;
namespace WireMock.Matchers
{
/// <summary>
/// System.Linq.Dynamic.Core Expression Matcher
/// </summary>
/// <inheritdoc cref="IObjectMatcher"/>
/// <inheritdoc cref="IStringMatcher"/>
public class LinqMatcher : IObjectMatcher, IStringMatcher
{
private readonly string[] _patterns;
/// <inheritdoc cref="IMatcher.MatchBehaviour"/>
public MatchBehaviour MatchBehaviour { get; }
/// <summary>
/// Initializes a new instance of the <see cref="LinqMatcher"/> class.
/// </summary>
/// <param name="pattern">The pattern.</param>
public LinqMatcher([NotNull] string pattern) : this(new[] { pattern })
{
}
/// <summary>
/// Initializes a new instance of the <see cref="LinqMatcher"/> class.
/// </summary>
/// <param name="patterns">The patterns.</param>
public LinqMatcher([NotNull] string[] patterns) : this(MatchBehaviour.AcceptOnMatch, patterns)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="LinqMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="pattern">The pattern.</param>
public LinqMatcher(MatchBehaviour matchBehaviour, [NotNull] string pattern) : this(matchBehaviour, new[] { pattern })
{
}
/// <summary>
/// Initializes a new instance of the <see cref="LinqMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="patterns">The patterns.</param>
public LinqMatcher(MatchBehaviour matchBehaviour, [NotNull] string[] patterns)
{
MatchBehaviour = matchBehaviour;
_patterns = patterns;
}
/// <inheritdoc cref="IStringMatcher.IsMatch"/>
public double IsMatch(string input)
{
double match = MatchScores.Mismatch;
// Convert a single input string to a Queryable string-list with 1 entry.
IQueryable queryable = new[] { input }.AsQueryable();
try
{
// Use the Any(...) method to check if the result matches
match = MatchScores.ToScore(_patterns.Select(pattern => queryable.Any(pattern)));
return MatchBehaviourHelper.Convert(MatchBehaviour, match);
}
catch
{
// just ignore exception
// TODO add logging?
}
return MatchBehaviourHelper.Convert(MatchBehaviour, match);
}
/// <inheritdoc cref="IObjectMatcher.IsMatch"/>
public double IsMatch(object input)
{
double match = MatchScores.Mismatch;
JObject value;
switch (input)
{
case JObject valueAsJObject:
value = valueAsJObject;
break;
default:
value = JObject.FromObject(input);
break;
}
// Convert a single object to a Queryable JObject-list with 1 entry.
var queryable1 = new[] { value }.AsQueryable();
try
{
// Generate the DynamicLinq select statement.
string dynamicSelect = JsonUtils.GenerateDynamicLinqStatement(value);
// Execute DynamicLinq Select statement.
var queryable2 = queryable1.Select(dynamicSelect);
// Use the Any(...) method to check if the result matches.
match = MatchScores.ToScore(_patterns.Select(pattern => queryable2.Any(pattern)));
return MatchBehaviourHelper.Convert(MatchBehaviour, match);
}
catch
{
// just ignore exception
// TODO add logging?
}
return MatchBehaviourHelper.Convert(MatchBehaviour, match);
}
/// <inheritdoc cref="IStringMatcher.GetPatterns"/>
public string[] GetPatterns()
{
return _patterns;
}
/// <inheritdoc cref="IMatcher.Name"/>
public string Name => "LinqMatcher";
}
}
using System.Linq;
using System.Linq.Dynamic.Core;
using JetBrains.Annotations;
using Newtonsoft.Json.Linq;
using WireMock.Util;
namespace WireMock.Matchers
{
/// <summary>
/// System.Linq.Dynamic.Core Expression Matcher
/// </summary>
/// <inheritdoc cref="IObjectMatcher"/>
/// <inheritdoc cref="IStringMatcher"/>
public class LinqMatcher : IObjectMatcher, IStringMatcher
{
private readonly string[] _patterns;
/// <inheritdoc cref="IMatcher.MatchBehaviour"/>
public MatchBehaviour MatchBehaviour { get; }
/// <inheritdoc cref="IMatcher.ThrowException"/>
public bool ThrowException { get; }
/// <summary>
/// Initializes a new instance of the <see cref="LinqMatcher"/> class.
/// </summary>
/// <param name="pattern">The pattern.</param>
public LinqMatcher([NotNull] string pattern) : this(new[] { pattern })
{
}
/// <summary>
/// Initializes a new instance of the <see cref="LinqMatcher"/> class.
/// </summary>
/// <param name="patterns">The patterns.</param>
public LinqMatcher([NotNull] params string[] patterns) : this(MatchBehaviour.AcceptOnMatch, false, patterns)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="LinqMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="pattern">The pattern.</param>
public LinqMatcher(MatchBehaviour matchBehaviour, [NotNull] string pattern) : this(matchBehaviour, false, pattern)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="LinqMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="patterns">The patterns.</param>
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
public LinqMatcher(MatchBehaviour matchBehaviour, bool throwException = false, [NotNull] params string[] patterns)
{
MatchBehaviour = matchBehaviour;
ThrowException = throwException;
_patterns = patterns;
}
/// <inheritdoc cref="IStringMatcher.IsMatch"/>
public double IsMatch(string input)
{
double match = MatchScores.Mismatch;
// Convert a single input string to a Queryable string-list with 1 entry.
IQueryable queryable = new[] { input }.AsQueryable();
try
{
// Use the Any(...) method to check if the result matches
match = MatchScores.ToScore(_patterns.Select(pattern => queryable.Any(pattern)));
return MatchBehaviourHelper.Convert(MatchBehaviour, match);
}
catch
{
if (ThrowException)
{
throw;
}
}
return MatchBehaviourHelper.Convert(MatchBehaviour, match);
}
/// <inheritdoc cref="IObjectMatcher.IsMatch"/>
public double IsMatch(object input)
{
double match = MatchScores.Mismatch;
JObject value;
switch (input)
{
case JObject valueAsJObject:
value = valueAsJObject;
break;
default:
value = JObject.FromObject(input);
break;
}
// Convert a single object to a Queryable JObject-list with 1 entry.
var queryable1 = new[] { value }.AsQueryable();
try
{
// Generate the DynamicLinq select statement.
string dynamicSelect = JsonUtils.GenerateDynamicLinqStatement(value);
// Execute DynamicLinq Select statement.
var queryable2 = queryable1.Select(dynamicSelect);
// Use the Any(...) method to check if the result matches.
match = MatchScores.ToScore(_patterns.Select(pattern => queryable2.Any(pattern)));
return MatchBehaviourHelper.Convert(MatchBehaviour, match);
}
catch
{
if (ThrowException)
{
throw;
}
}
return MatchBehaviourHelper.Convert(MatchBehaviour, match);
}
/// <inheritdoc cref="IStringMatcher.GetPatterns"/>
public string[] GetPatterns()
{
return _patterns;
}
/// <inheritdoc cref="IMatcher.Name"/>
public string Name => "LinqMatcher";
}
}

View File

@@ -19,6 +19,9 @@ namespace WireMock.Matchers
/// <inheritdoc cref="IMatcher.MatchBehaviour"/>
public MatchBehaviour MatchBehaviour { get; }
/// <inheritdoc cref="IMatcher.ThrowException"/>
public bool ThrowException { get; }
/// <summary>
/// Initializes a new instance of the <see cref="RegexMatcher"/> class.
/// </summary>
@@ -53,13 +56,15 @@ namespace WireMock.Matchers
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="patterns">The patterns.</param>
/// <param name="ignoreCase">Ignore the case from the pattern.</param>
public RegexMatcher(MatchBehaviour matchBehaviour, [NotNull, RegexPattern] string[] patterns, bool ignoreCase = false)
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
public RegexMatcher(MatchBehaviour matchBehaviour, [NotNull, RegexPattern] string[] patterns, bool ignoreCase = false, bool throwException = false)
{
Check.NotNull(patterns, nameof(patterns));
_patterns = patterns;
IgnoreCase = ignoreCase;
MatchBehaviour = matchBehaviour;
ThrowException = throwException;
RegexOptions options = RegexOptions.Compiled | RegexOptions.Multiline;
@@ -83,10 +88,13 @@ namespace WireMock.Matchers
}
catch (Exception)
{
// just ignore exception
if (ThrowException)
{
throw;
}
}
}
}
return MatchBehaviourHelper.Convert(MatchBehaviour, match);
}

View File

@@ -51,7 +51,7 @@ namespace WireMock.Matchers.Request
/// <param name="key">The key.</param>
/// <param name="ignoreCase">Defines if the key should be matched using case-ignore.</param>
/// <param name="values">The values.</param>
public RequestMessageParamMatcher(MatchBehaviour matchBehaviour, [NotNull] string key, bool ignoreCase, [CanBeNull] string[] values) : this(matchBehaviour, key, ignoreCase, values?.Select(value => new ExactMatcher(matchBehaviour, value)).Cast<IStringMatcher>().ToArray())
public RequestMessageParamMatcher(MatchBehaviour matchBehaviour, [NotNull] string key, bool ignoreCase, [CanBeNull] string[] values) : this(matchBehaviour, key, ignoreCase, values?.Select(value => new ExactMatcher(matchBehaviour, false, value)).Cast<IStringMatcher>().ToArray())
{
}

View File

@@ -1,125 +1,131 @@
using System.Linq;
using JetBrains.Annotations;
using SimMetrics.Net;
using SimMetrics.Net.API;
using SimMetrics.Net.Metric;
using WireMock.Validation;
namespace WireMock.Matchers
{
/// <summary>
/// SimMetricsMatcher
/// </summary>
/// <seealso cref="IStringMatcher" />
public class SimMetricsMatcher : IStringMatcher
{
private readonly string[] _patterns;
private readonly SimMetricType _simMetricType;
/// <inheritdoc cref="IMatcher.MatchBehaviour"/>
public MatchBehaviour MatchBehaviour { get; }
/// <summary>
/// Initializes a new instance of the <see cref="SimMetricsMatcher"/> class.
/// </summary>
/// <param name="pattern">The pattern.</param>
/// <param name="simMetricType">The SimMetric Type</param>
public SimMetricsMatcher([NotNull] string pattern, SimMetricType simMetricType = SimMetricType.Levenstein) : this(new[] { pattern }, simMetricType)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SimMetricsMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="pattern">The pattern.</param>
/// <param name="simMetricType">The SimMetric Type</param>
public SimMetricsMatcher(MatchBehaviour matchBehaviour, [NotNull] string pattern, SimMetricType simMetricType = SimMetricType.Levenstein) : this(matchBehaviour, new[] { pattern }, simMetricType)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SimMetricsMatcher"/> class.
/// </summary>
/// <param name="patterns">The patterns.</param>
/// <param name="simMetricType">The SimMetric Type</param>
public SimMetricsMatcher([NotNull] string[] patterns, SimMetricType simMetricType = SimMetricType.Levenstein) : this(MatchBehaviour.AcceptOnMatch, patterns, simMetricType)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SimMetricsMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="patterns">The patterns.</param>
/// <param name="simMetricType">The SimMetric Type</param>
public SimMetricsMatcher(MatchBehaviour matchBehaviour, [NotNull] string[] patterns, SimMetricType simMetricType = SimMetricType.Levenstein)
{
Check.NotNullOrEmpty(patterns, nameof(patterns));
MatchBehaviour = matchBehaviour;
_patterns = patterns;
_simMetricType = simMetricType;
}
/// <inheritdoc cref="IStringMatcher.IsMatch"/>
public double IsMatch(string input)
{
IStringMetric m = GetStringMetricType();
return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(_patterns.Select(p => m.GetSimilarity(p, input))));
}
private IStringMetric GetStringMetricType()
{
switch (_simMetricType)
{
case SimMetricType.BlockDistance:
return new BlockDistance();
case SimMetricType.ChapmanLengthDeviation:
return new ChapmanLengthDeviation();
case SimMetricType.CosineSimilarity:
return new CosineSimilarity();
case SimMetricType.DiceSimilarity:
return new DiceSimilarity();
case SimMetricType.EuclideanDistance:
return new EuclideanDistance();
case SimMetricType.JaccardSimilarity:
return new JaccardSimilarity();
case SimMetricType.Jaro:
return new Jaro();
case SimMetricType.JaroWinkler:
return new JaroWinkler();
case SimMetricType.MatchingCoefficient:
return new MatchingCoefficient();
case SimMetricType.MongeElkan:
return new MongeElkan();
case SimMetricType.NeedlemanWunch:
return new NeedlemanWunch();
case SimMetricType.OverlapCoefficient:
return new OverlapCoefficient();
case SimMetricType.QGramsDistance:
return new QGramsDistance();
case SimMetricType.SmithWaterman:
return new SmithWaterman();
case SimMetricType.SmithWatermanGotoh:
return new SmithWatermanGotoh();
case SimMetricType.SmithWatermanGotohWindowedAffine:
return new SmithWatermanGotohWindowedAffine();
case SimMetricType.ChapmanMeanLength:
return new ChapmanMeanLength();
default:
return new Levenstein();
}
}
/// <inheritdoc cref="IStringMatcher.GetPatterns"/>
public string[] GetPatterns()
{
return _patterns;
}
/// <inheritdoc cref="IMatcher.Name"/>
public string Name => $"SimMetricsMatcher.{_simMetricType}";
}
using System.Linq;
using JetBrains.Annotations;
using SimMetrics.Net;
using SimMetrics.Net.API;
using SimMetrics.Net.Metric;
using WireMock.Validation;
namespace WireMock.Matchers
{
/// <summary>
/// SimMetricsMatcher
/// </summary>
/// <seealso cref="IStringMatcher" />
public class SimMetricsMatcher : IStringMatcher
{
private readonly string[] _patterns;
private readonly SimMetricType _simMetricType;
/// <inheritdoc cref="IMatcher.MatchBehaviour"/>
public MatchBehaviour MatchBehaviour { get; }
/// <inheritdoc cref="IMatcher.ThrowException"/>
public bool ThrowException { get; }
/// <summary>
/// Initializes a new instance of the <see cref="SimMetricsMatcher"/> class.
/// </summary>
/// <param name="pattern">The pattern.</param>
/// <param name="simMetricType">The SimMetric Type</param>
public SimMetricsMatcher([NotNull] string pattern, SimMetricType simMetricType = SimMetricType.Levenstein) : this(new[] { pattern }, simMetricType)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SimMetricsMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="pattern">The pattern.</param>
/// <param name="simMetricType">The SimMetric Type</param>
public SimMetricsMatcher(MatchBehaviour matchBehaviour, [NotNull] string pattern, SimMetricType simMetricType = SimMetricType.Levenstein) : this(matchBehaviour, new[] { pattern }, simMetricType)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SimMetricsMatcher"/> class.
/// </summary>
/// <param name="patterns">The patterns.</param>
/// <param name="simMetricType">The SimMetric Type</param>
public SimMetricsMatcher([NotNull] string[] patterns, SimMetricType simMetricType = SimMetricType.Levenstein) : this(MatchBehaviour.AcceptOnMatch, patterns, simMetricType)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SimMetricsMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="patterns">The patterns.</param>
/// <param name="simMetricType">The SimMetric Type</param>
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
public SimMetricsMatcher(MatchBehaviour matchBehaviour, [NotNull] string[] patterns, SimMetricType simMetricType = SimMetricType.Levenstein, bool throwException = false)
{
Check.NotNullOrEmpty(patterns, nameof(patterns));
MatchBehaviour = matchBehaviour;
ThrowException = throwException;
_patterns = patterns;
_simMetricType = simMetricType;
}
/// <inheritdoc cref="IStringMatcher.IsMatch"/>
public double IsMatch(string input)
{
IStringMetric m = GetStringMetricType();
return MatchBehaviourHelper.Convert(MatchBehaviour, MatchScores.ToScore(_patterns.Select(p => m.GetSimilarity(p, input))));
}
private IStringMetric GetStringMetricType()
{
switch (_simMetricType)
{
case SimMetricType.BlockDistance:
return new BlockDistance();
case SimMetricType.ChapmanLengthDeviation:
return new ChapmanLengthDeviation();
case SimMetricType.CosineSimilarity:
return new CosineSimilarity();
case SimMetricType.DiceSimilarity:
return new DiceSimilarity();
case SimMetricType.EuclideanDistance:
return new EuclideanDistance();
case SimMetricType.JaccardSimilarity:
return new JaccardSimilarity();
case SimMetricType.Jaro:
return new Jaro();
case SimMetricType.JaroWinkler:
return new JaroWinkler();
case SimMetricType.MatchingCoefficient:
return new MatchingCoefficient();
case SimMetricType.MongeElkan:
return new MongeElkan();
case SimMetricType.NeedlemanWunch:
return new NeedlemanWunch();
case SimMetricType.OverlapCoefficient:
return new OverlapCoefficient();
case SimMetricType.QGramsDistance:
return new QGramsDistance();
case SimMetricType.SmithWaterman:
return new SmithWaterman();
case SimMetricType.SmithWatermanGotoh:
return new SmithWatermanGotoh();
case SimMetricType.SmithWatermanGotohWindowedAffine:
return new SmithWatermanGotohWindowedAffine();
case SimMetricType.ChapmanMeanLength:
return new ChapmanMeanLength();
default:
return new Levenstein();
}
}
/// <inheritdoc cref="IStringMatcher.GetPatterns"/>
public string[] GetPatterns()
{
return _patterns;
}
/// <inheritdoc cref="IMatcher.Name"/>
public string Name => $"SimMetricsMatcher.{_simMetricType}";
}
}

View File

@@ -1,63 +1,65 @@
using System.Linq;
using System.Text.RegularExpressions;
using JetBrains.Annotations;
namespace WireMock.Matchers
{
/// <summary>
/// WildcardMatcher
/// </summary>
/// <seealso cref="RegexMatcher" />
public class WildcardMatcher : RegexMatcher
{
private readonly string[] _patterns;
/// <summary>
/// Initializes a new instance of the <see cref="WildcardMatcher"/> class.
/// </summary>
/// <param name="pattern">The pattern.</param>
/// <param name="ignoreCase">IgnoreCase</param>
public WildcardMatcher([NotNull] string pattern, bool ignoreCase = false) : this(new[] { pattern }, ignoreCase)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="WildcardMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="pattern">The pattern.</param>
/// <param name="ignoreCase">IgnoreCase</param>
public WildcardMatcher(MatchBehaviour matchBehaviour, [NotNull] string pattern, bool ignoreCase = false) : this(matchBehaviour, new[] { pattern }, ignoreCase)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="WildcardMatcher"/> class.
/// </summary>
/// <param name="patterns">The patterns.</param>
/// <param name="ignoreCase">IgnoreCase</param>
public WildcardMatcher([NotNull] string[] patterns, bool ignoreCase = false) : this(MatchBehaviour.AcceptOnMatch, patterns, ignoreCase)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="WildcardMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="patterns">The patterns.</param>
/// <param name="ignoreCase">IgnoreCase</param>
public WildcardMatcher(MatchBehaviour matchBehaviour, [NotNull] string[] patterns, bool ignoreCase = false) : base(matchBehaviour, patterns.Select(pattern => "^" + Regex.Escape(pattern).Replace(@"\*", ".*").Replace(@"\?", ".") + "$").ToArray(), ignoreCase)
{
_patterns = patterns;
}
/// <inheritdoc cref="IStringMatcher.GetPatterns"/>
public override string[] GetPatterns()
{
return _patterns;
}
/// <inheritdoc cref="IMatcher.Name"/>
public override string Name => "WildcardMatcher";
}
using System.Linq;
using System.Text.RegularExpressions;
using JetBrains.Annotations;
namespace WireMock.Matchers
{
/// <summary>
/// WildcardMatcher
/// </summary>
/// <seealso cref="RegexMatcher" />
public class WildcardMatcher : RegexMatcher
{
private readonly string[] _patterns;
/// <summary>
/// Initializes a new instance of the <see cref="WildcardMatcher"/> class.
/// </summary>
/// <param name="pattern">The pattern.</param>
/// <param name="ignoreCase">IgnoreCase</param>
public WildcardMatcher([NotNull] string pattern, bool ignoreCase = false) : this(new[] { pattern }, ignoreCase)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="WildcardMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="pattern">The pattern.</param>
/// <param name="ignoreCase">IgnoreCase</param>
public WildcardMatcher(MatchBehaviour matchBehaviour, [NotNull] string pattern, bool ignoreCase = false) : this(matchBehaviour, new[] { pattern }, ignoreCase)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="WildcardMatcher"/> class.
/// </summary>
/// <param name="patterns">The patterns.</param>
/// <param name="ignoreCase">IgnoreCase</param>
public WildcardMatcher([NotNull] string[] patterns, bool ignoreCase = false) : this(MatchBehaviour.AcceptOnMatch, patterns, ignoreCase)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="WildcardMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="patterns">The patterns.</param>
/// <param name="ignoreCase">IgnoreCase</param>
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
public WildcardMatcher(MatchBehaviour matchBehaviour, [NotNull] string[] patterns, bool ignoreCase = false, bool throwException = false) :
base(matchBehaviour, patterns.Select(pattern => "^" + Regex.Escape(pattern).Replace(@"\*", ".*").Replace(@"\?", ".") + "$").ToArray(), ignoreCase, throwException)
{
_patterns = patterns;
}
/// <inheritdoc cref="IStringMatcher.GetPatterns"/>
public override string[] GetPatterns()
{
return _patterns;
}
/// <inheritdoc cref="IMatcher.Name"/>
public override string Name => "WildcardMatcher";
}
}

View File

@@ -1,78 +1,85 @@
using System;
using System.Linq;
using System.Xml;
using JetBrains.Annotations;
using WireMock.Validation;
#if !NETSTANDARD1_3
using Wmhelp.XPath2;
#endif
namespace WireMock.Matchers
{
/// <summary>
/// XPath2Matcher
/// </summary>
/// <seealso cref="IStringMatcher" />
public class XPathMatcher : IStringMatcher
{
private readonly string[] _patterns;
/// <inheritdoc cref="IMatcher.MatchBehaviour"/>
public MatchBehaviour MatchBehaviour { get; }
/// <summary>
/// Initializes a new instance of the <see cref="XPathMatcher"/> class.
/// </summary>
/// <param name="patterns">The patterns.</param>
public XPathMatcher([NotNull] params string[] patterns) : this(MatchBehaviour.AcceptOnMatch, patterns)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="XPathMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="patterns">The patterns.</param>
public XPathMatcher(MatchBehaviour matchBehaviour, [NotNull] params string[] patterns)
{
Check.NotNull(patterns, nameof(patterns));
MatchBehaviour = matchBehaviour;
_patterns = patterns;
}
/// <inheritdoc cref="IStringMatcher.IsMatch"/>
public double IsMatch(string input)
{
double match = MatchScores.Mismatch;
if (input != null)
{
try
{
var nav = new XmlDocument { InnerXml = input }.CreateNavigator();
#if NETSTANDARD1_3
match = MatchScores.ToScore(_patterns.Select(p => true.Equals(nav.Evaluate($"boolean({p})"))));
#else
match = MatchScores.ToScore(_patterns.Select(p => true.Equals(nav.XPath2Evaluate($"boolean({p})"))));
#endif
}
catch (Exception)
{
// just ignore exception
// TODO add logging?
}
}
return MatchBehaviourHelper.Convert(MatchBehaviour, match);
}
/// <inheritdoc cref="IStringMatcher.GetPatterns"/>
public string[] GetPatterns()
{
return _patterns;
}
/// <inheritdoc cref="IMatcher.Name"/>
public string Name => "XPathMatcher";
}
using System;
using System.Linq;
using System.Xml;
using JetBrains.Annotations;
using WireMock.Validation;
#if !NETSTANDARD1_3
using Wmhelp.XPath2;
#endif
namespace WireMock.Matchers
{
/// <summary>
/// XPath2Matcher
/// </summary>
/// <seealso cref="IStringMatcher" />
public class XPathMatcher : IStringMatcher
{
private readonly string[] _patterns;
/// <inheritdoc cref="IMatcher.MatchBehaviour"/>
public MatchBehaviour MatchBehaviour { get; }
/// <inheritdoc cref="IMatcher.ThrowException"/>
public bool ThrowException { get; }
/// <summary>
/// Initializes a new instance of the <see cref="XPathMatcher"/> class.
/// </summary>
/// <param name="patterns">The patterns.</param>
public XPathMatcher([NotNull] params string[] patterns) : this(MatchBehaviour.AcceptOnMatch, false, patterns)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="XPathMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour.</param>
/// <param name="throwException">Throw an exception when the internal matching fails because of invalid input.</param>
/// <param name="patterns">The patterns.</param>
public XPathMatcher(MatchBehaviour matchBehaviour, bool throwException = false, [NotNull] params string[] patterns)
{
Check.NotNull(patterns, nameof(patterns));
MatchBehaviour = matchBehaviour;
ThrowException = throwException;
_patterns = patterns;
}
/// <inheritdoc cref="IStringMatcher.IsMatch"/>
public double IsMatch(string input)
{
double match = MatchScores.Mismatch;
if (input != null)
{
try
{
var nav = new XmlDocument { InnerXml = input }.CreateNavigator();
#if NETSTANDARD1_3
match = MatchScores.ToScore(_patterns.Select(p => true.Equals(nav.Evaluate($"boolean({p})"))));
#else
match = MatchScores.ToScore(_patterns.Select(p => true.Equals(nav.XPath2Evaluate($"boolean({p})"))));
#endif
}
catch (Exception)
{
if (ThrowException)
{
throw;
}
}
}
return MatchBehaviourHelper.Convert(MatchBehaviour, match);
}
/// <inheritdoc cref="IStringMatcher.GetPatterns"/>
public string[] GetPatterns()
{
return _patterns;
}
/// <inheritdoc cref="IMatcher.Name"/>
public string Name => "XPathMatcher";
}
}

View File

@@ -45,5 +45,7 @@ namespace WireMock.Owin
bool? DisableJsonBodyParsing { get; set; }
bool? DisableRequestBodyDecompressing { get; set; }
bool? HandleRequestsSynchronously { get; set; }
}
}

View File

@@ -23,12 +23,12 @@ namespace WireMock.Owin
{
try
{
string scenario = mapping.Scenario != null && _options.Scenarios.ContainsKey(mapping.Scenario) ? _options.Scenarios[mapping.Scenario].NextState : null;
string nextState = GetNextState(mapping);
mappings.Add(new MappingMatcherResult
{
Mapping = mapping,
RequestMatchResult = mapping.GetRequestMatchResult(request, scenario)
RequestMatchResult = mapping.GetRequestMatchResult(request, nextState)
});
}
catch (Exception ex)
@@ -56,5 +56,18 @@ namespace WireMock.Owin
return (match, partialMatch);
}
private string GetNextState(IMapping mapping)
{
// If the mapping does not have a scenario or _options.Scenarios does not contain this scenario from the mapping,
// just return null to indicate that there is no next state.
if (mapping.Scenario == null || !_options.Scenarios.ContainsKey(mapping.Scenario))
{
return null;
}
// Else just return the next state
return _options.Scenarios[mapping.Scenario].NextState;
}
}
}

View File

@@ -25,6 +25,7 @@ namespace WireMock.Owin
{
internal class WireMockMiddleware : OwinMiddleware
{
private readonly object _lock = new object();
private static readonly Task CompletedTask = Task.FromResult(false);
private readonly IWireMockMiddlewareOptions _options;
private readonly IOwinRequestMapper _requestMapper;
@@ -65,7 +66,17 @@ namespace WireMock.Owin
public Task Invoke(IContext ctx)
#endif
{
return InvokeInternal(ctx);
if (_options.HandleRequestsSynchronously.GetValueOrDefault(true))
{
lock (_lock)
{
return InvokeInternal(ctx);
}
}
else
{
return InvokeInternal(ctx);
}
}
private async Task InvokeInternal(IContext ctx)
@@ -79,7 +90,7 @@ namespace WireMock.Owin
{
foreach (var mapping in _options.Mappings.Values.Where(m => m?.Scenario != null))
{
// Set start
// Set scenario start
if (!_options.Scenarios.ContainsKey(mapping.Scenario) && mapping.IsStartState)
{
_options.Scenarios.TryAdd(mapping.Scenario, new ScenarioState
@@ -122,9 +133,7 @@ namespace WireMock.Owin
if (targetMapping.Scenario != null)
{
_options.Scenarios[targetMapping.Scenario].NextState = targetMapping.NextState;
_options.Scenarios[targetMapping.Scenario].Started = true;
_options.Scenarios[targetMapping.Scenario].Finished = targetMapping.NextState == null;
UpdateScenarioState(targetMapping);
}
}
catch (Exception ex)
@@ -157,6 +166,25 @@ namespace WireMock.Owin
await CompletedTask;
}
private void UpdateScenarioState(IMapping mapping)
{
var scenario = _options.Scenarios[mapping.Scenario];
// Increase the number of times this state has been executed
scenario.Counter++;
// Only if the number of times this state is executed equals the required StateTimes, proceed to next state and reset the counter to 0
if (scenario.Counter == (mapping.StateTimes ?? 1))
{
scenario.NextState = mapping.NextState;
scenario.Counter = 0;
}
// Else just update Started and Finished
scenario.Started = true;
scenario.Finished = mapping.NextState == null;
}
private void LogRequest(LogEntry entry, bool addRequest)
{
_options.Logger.DebugRequestResponse(LogEntryMapper.Map(entry), entry.RequestMessage.Path.StartsWith("/__admin/"));

View File

@@ -51,5 +51,8 @@ namespace WireMock.Owin
/// <inheritdoc cref="IWireMockMiddlewareOptions.DisableRequestBodyDecompressing"/>
public bool? DisableRequestBodyDecompressing { get; set; }
/// <inheritdoc cref="IWireMockMiddlewareOptions.HandleRequestsSynchronously"/>
public bool? HandleRequestsSynchronously { get; set; }
}
}

View File

@@ -24,5 +24,10 @@
/// Gets or sets a value indicating whether this <see cref="ScenarioState"/> is finished.
/// </summary>
public bool Finished { get; set; }
/// <summary>
/// Gets or sets the state counter.
/// </summary>
public int Counter { get; set; }
}
}

View File

@@ -38,6 +38,8 @@ namespace WireMock.Serialization
string[] stringPatterns = matcher.Patterns != null ? matcher.Patterns.OfType<string>().ToArray() : new[] { matcher.Pattern as string };
MatchBehaviour matchBehaviour = matcher.RejectOnMatch == true ? MatchBehaviour.RejectOnMatch : MatchBehaviour.AcceptOnMatch;
bool ignoreCase = matcher.IgnoreCase == true;
bool throwExceptionWhenMatcherFails = _settings.ThrowExceptionWhenMatcherFails == true;
switch (matcherName)
{
@@ -50,34 +52,34 @@ namespace WireMock.Serialization
throw new NotSupportedException("It's not allowed to use the 'CSharpCodeMatcher' because IWireMockServerSettings.AllowCSharpCodeMatcher is not set to 'true'.");
case "LinqMatcher":
return new LinqMatcher(matchBehaviour, stringPatterns);
return new LinqMatcher(matchBehaviour, throwExceptionWhenMatcherFails, stringPatterns);
case "ExactMatcher":
return new ExactMatcher(matchBehaviour, stringPatterns);
return new ExactMatcher(matchBehaviour, throwExceptionWhenMatcherFails, stringPatterns);
case "ExactObjectMatcher":
return CreateExactObjectMatcher(matchBehaviour, stringPatterns[0]);
return CreateExactObjectMatcher(matchBehaviour, stringPatterns[0], throwExceptionWhenMatcherFails);
case "RegexMatcher":
return new RegexMatcher(matchBehaviour, stringPatterns, matcher.IgnoreCase == true);
return new RegexMatcher(matchBehaviour, stringPatterns, ignoreCase, throwExceptionWhenMatcherFails);
case "JsonMatcher":
return new JsonMatcher(matchBehaviour, matcher.Pattern, matcher.IgnoreCase == true);
return new JsonMatcher(matchBehaviour, stringPatterns, ignoreCase, throwExceptionWhenMatcherFails);
case "JsonPathMatcher":
return new JsonPathMatcher(matchBehaviour, stringPatterns);
return new JsonPathMatcher(matchBehaviour, throwExceptionWhenMatcherFails, stringPatterns);
case "JmesPathMatcher":
return new JmesPathMatcher(matchBehaviour, stringPatterns);
return new JmesPathMatcher(matchBehaviour, throwExceptionWhenMatcherFails, stringPatterns);
case "XPathMatcher":
return new XPathMatcher(matchBehaviour, stringPatterns);
return new XPathMatcher(matchBehaviour, throwExceptionWhenMatcherFails, stringPatterns);
case "WildcardMatcher":
return new WildcardMatcher(matchBehaviour, stringPatterns, matcher.IgnoreCase == true);
return new WildcardMatcher(matchBehaviour, stringPatterns, ignoreCase, throwExceptionWhenMatcherFails);
case "ContentTypeMatcher":
return new ContentTypeMatcher(matchBehaviour, stringPatterns, matcher.IgnoreCase == true);
return new ContentTypeMatcher(matchBehaviour, stringPatterns, ignoreCase, throwExceptionWhenMatcherFails);
case "SimMetricsMatcher":
SimMetricType type = SimMetricType.Levenstein;
@@ -86,14 +88,14 @@ namespace WireMock.Serialization
throw new NotSupportedException($"Matcher '{matcherName}' with Type '{matcherType}' is not supported.");
}
return new SimMetricsMatcher(matchBehaviour, stringPatterns, type);
return new SimMetricsMatcher(matchBehaviour, stringPatterns, type, throwExceptionWhenMatcherFails);
default:
throw new NotSupportedException($"Matcher '{matcherName}' is not supported.");
}
}
private ExactObjectMatcher CreateExactObjectMatcher(MatchBehaviour matchBehaviour, string stringPattern)
private ExactObjectMatcher CreateExactObjectMatcher(MatchBehaviour matchBehaviour, string stringPattern, bool throwException)
{
byte[] bytePattern;
try
@@ -105,7 +107,7 @@ namespace WireMock.Serialization
throw new ArgumentException($"Matcher 'ExactObjectMatcher' has invalid pattern. The pattern value '{stringPattern}' is not a Base64String.", nameof(stringPattern));
}
return new ExactObjectMatcher(matchBehaviour, bytePattern);
return new ExactObjectMatcher(matchBehaviour, bytePattern, throwException);
}
public MatcherModel[] Map([CanBeNull] IEnumerable<IMatcher> matchers)

View File

@@ -1,11 +1,13 @@
using WireMock.Settings;
namespace WireMock.Server
{
public class FluentMockServer : WireMockServer
{
public FluentMockServer(IFluentMockServerSettings settings) : base((IWireMockServerSettings) settings)
{
}
}
using System;
using WireMock.Settings;
namespace WireMock.Server
{
[Obsolete("Use WireMockServer. This will removed in next version (1.3.x)")]
public class FluentMockServer : WireMockServer
{
public FluentMockServer(IFluentMockServerSettings settings) : base((IWireMockServerSettings) settings)
{
}
}
}

View File

@@ -86,14 +86,16 @@ namespace WireMock.Server
/// Once this mapping is executed the state will be changed to specified one.
/// </summary>
/// <param name="state">Any object which identifies the new state</param>
/// <param name="times">The number of times this match should be matched before the state will be changed to the specified one. Default value is 1.</param>
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
IRespondWithAProvider WillSetStateTo(string state);
IRespondWithAProvider WillSetStateTo(string state, int? times = 1);
/// <summary>
/// Once this mapping is executed the state will be changed to specified one.
/// </summary>
/// <param name="state">Any object which identifies the new state</param>
/// <param name="times">The number of times this match should be matched before the state will be changed to the specified one. Default value is 1.</param>
/// <returns>The <see cref="IRespondWithAProvider"/>.</returns>
IRespondWithAProvider WillSetStateTo(int state);
IRespondWithAProvider WillSetStateTo(int state, int? times = 1);
}
}

View File

@@ -18,6 +18,7 @@ namespace WireMock.Server
private string _executionConditionState;
private string _nextState;
private string _scenario;
private int _timesInSameState = 1;
private readonly RegistrationCallback _registrationCallback;
private readonly IRequestMatcher _requestMatcher;
private readonly IWireMockServerSettings _settings;
@@ -46,7 +47,7 @@ namespace WireMock.Server
/// <param name="provider">The provider.</param>
public void RespondWith(IResponseProvider provider)
{
_registrationCallback(new Mapping(Guid, _title, _path, _settings, _requestMatcher, provider, _priority, _scenario, _executionConditionState, _nextState), _saveToFile);
_registrationCallback(new Mapping(Guid, _title, _path, _settings, _requestMatcher, provider, _priority, _scenario, _executionConditionState, _nextState, _timesInSameState), _saveToFile);
}
/// <see cref="IRespondWithAProvider.WithGuid(string)"/>
@@ -120,8 +121,8 @@ namespace WireMock.Server
return WhenStateIs(state.ToString());
}
/// <see cref="IRespondWithAProvider.WillSetStateTo(string)"/>
public IRespondWithAProvider WillSetStateTo(string state)
/// <see cref="IRespondWithAProvider.WillSetStateTo(string, int?)"/>
public IRespondWithAProvider WillSetStateTo(string state, int? times = 1)
{
if (string.IsNullOrEmpty(_scenario))
{
@@ -129,14 +130,15 @@ namespace WireMock.Server
}
_nextState = state;
_timesInSameState = times ?? 1;
return this;
}
/// <see cref="IRespondWithAProvider.WillSetStateTo(int)"/>
public IRespondWithAProvider WillSetStateTo(int state)
/// <see cref="IRespondWithAProvider.WillSetStateTo(int, int?)"/>
public IRespondWithAProvider WillSetStateTo(int state, int? times = 1)
{
return WillSetStateTo(state.ToString());
return WillSetStateTo(state.ToString(), times);
}
}
}

View File

@@ -1,6 +1,3 @@
using JetBrains.Annotations;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
@@ -9,6 +6,9 @@ using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using WireMock.Admin.Mappings;
using WireMock.Admin.Scenarios;
using WireMock.Admin.Settings;
@@ -274,7 +274,12 @@ namespace WireMock.Server
if (HttpStatusRangeParser.IsMatch(settings.ProxyAndRecordSettings.SaveMappingForStatusCodePattern, responseMessage.StatusCode) &&
(settings.ProxyAndRecordSettings.SaveMapping || settings.ProxyAndRecordSettings.SaveMappingToFile))
{
var mapping = ToMapping(requestMessage, responseMessage, settings.ProxyAndRecordSettings.BlackListedHeaders ?? new string[] { }, settings.ProxyAndRecordSettings.BlackListedCookies ?? new string[] { });
var mapping = ToMapping(
requestMessage,
responseMessage,
settings.ProxyAndRecordSettings.BlackListedHeaders ?? new string[] { },
settings.ProxyAndRecordSettings.BlackListedCookies ?? new string[] { }
);
if (settings.ProxyAndRecordSettings.SaveMapping)
{
@@ -290,7 +295,7 @@ namespace WireMock.Server
return responseMessage;
}
private IMapping ToMapping(RequestMessage requestMessage, ResponseMessage responseMessage, string[] blacklistedHeaders, string[] blacklistedCookies)
private IMapping ToMapping(RequestMessage requestMessage, ResponseMessage responseMessage, string[] excludedHeaders, string[] excludedCookies)
{
var request = Request.Create();
request.WithPath(requestMessage.Path);
@@ -299,39 +304,40 @@ namespace WireMock.Server
requestMessage.Query.Loop((key, value) => request.WithParam(key, false, value.ToArray()));
requestMessage.Cookies.Loop((key, value) =>
{
if (!blacklistedCookies.Contains(key, StringComparer.OrdinalIgnoreCase))
if (!excludedCookies.Contains(key, StringComparer.OrdinalIgnoreCase))
{
request.WithCookie(key, value);
}
});
var allBlackListedHeaders = new List<string>(blacklistedHeaders) { "Cookie" };
var allExcludedHeaders = new List<string>(excludedHeaders) { "Cookie" };
requestMessage.Headers.Loop((key, value) =>
{
if (!allBlackListedHeaders.Contains(key, StringComparer.OrdinalIgnoreCase))
if (!allExcludedHeaders.Contains(key, StringComparer.OrdinalIgnoreCase))
{
request.WithHeader(key, value.ToArray());
}
});
bool throwExceptionWhenMatcherFails = _settings.ThrowExceptionWhenMatcherFails == true;
switch (requestMessage.BodyData?.DetectedBodyType)
{
case BodyType.Json:
request.WithBody(new JsonMatcher(MatchBehaviour.AcceptOnMatch, requestMessage.BodyData.BodyAsJson));
request.WithBody(new JsonMatcher(MatchBehaviour.AcceptOnMatch, requestMessage.BodyData.BodyAsJson, true, throwExceptionWhenMatcherFails));
break;
case BodyType.String:
request.WithBody(new ExactMatcher(MatchBehaviour.AcceptOnMatch, requestMessage.BodyData.BodyAsString));
request.WithBody(new ExactMatcher(MatchBehaviour.AcceptOnMatch, throwExceptionWhenMatcherFails, requestMessage.BodyData.BodyAsString));
break;
case BodyType.Bytes:
request.WithBody(new ExactObjectMatcher(MatchBehaviour.AcceptOnMatch, requestMessage.BodyData.BodyAsBytes));
request.WithBody(new ExactObjectMatcher(MatchBehaviour.AcceptOnMatch, requestMessage.BodyData.BodyAsBytes, throwExceptionWhenMatcherFails));
break;
}
var response = Response.Create(responseMessage);
return new Mapping(Guid.NewGuid(), string.Empty, null, _settings, request, response, 0, null, null, null);
return new Mapping(Guid.NewGuid(), string.Empty, null, _settings, request, response, 0, null, null, null, null);
}
#endregion
@@ -340,11 +346,13 @@ namespace WireMock.Server
{
var model = new SettingsModel
{
AllowPartialMapping = _options.AllowPartialMapping,
MaxRequestLogCount = _options.MaxRequestLogCount,
RequestLogExpirationDuration = _options.RequestLogExpirationDuration,
AllowPartialMapping = _settings.AllowPartialMapping,
MaxRequestLogCount = _settings.MaxRequestLogCount,
RequestLogExpirationDuration = _settings.RequestLogExpirationDuration,
GlobalProcessingDelay = (int?)_options.RequestProcessingDelay?.TotalMilliseconds,
AllowBodyForAllHttpMethods = _options.AllowBodyForAllHttpMethods
AllowBodyForAllHttpMethods = _settings.AllowBodyForAllHttpMethods,
HandleRequestsSynchronously = _settings.HandleRequestsSynchronously,
ThrowExceptionWhenMatcherFails = _settings.ThrowExceptionWhenMatcherFails
};
return ToJson(model);
@@ -371,6 +379,16 @@ namespace WireMock.Server
_options.AllowBodyForAllHttpMethods = settings.AllowBodyForAllHttpMethods.Value;
}
if (settings.HandleRequestsSynchronously != null)
{
_options.HandleRequestsSynchronously = settings.HandleRequestsSynchronously.Value;
}
if (settings.ThrowExceptionWhenMatcherFails != null)
{
_settings.ThrowExceptionWhenMatcherFails = settings.ThrowExceptionWhenMatcherFails.Value;
}
return ResponseMessageBuilder.Create("Settings updated");
}
#endregion Settings
@@ -706,7 +724,8 @@ namespace WireMock.Server
Name = s.Name,
NextState = s.NextState,
Started = s.Started,
Finished = s.Finished
Finished = s.Finished,
Counter = s.Counter
});
return ToJson(scenariosStates, true);

View File

@@ -217,6 +217,7 @@ namespace WireMock.Server
_options.PostWireMockMiddlewareInit = _settings.PostWireMockMiddlewareInit;
_options.Logger = _settings.Logger;
_options.DisableJsonBodyParsing = _settings.DisableJsonBodyParsing;
_options.HandleRequestsSynchronously = settings.HandleRequestsSynchronously;
_matcherMapper = new MatcherMapper(_settings);
_mappingConverter = new MappingConverter(_matcherMapper);

View File

@@ -1,8 +1,11 @@
namespace WireMock.Settings
using System;
namespace WireMock.Settings
{
/// <summary>
/// FluentMockServerSettings
/// </summary>
[Obsolete("Use WireMockServerSettings. This will removed in next version (1.3.x)")]
public class FluentMockServerSettings : WireMockServerSettings
{
}

View File

@@ -1,8 +1,11 @@
namespace WireMock.Settings
using System;
namespace WireMock.Settings
{
/// <summary>
/// IFluentMockServerSettings
/// </summary>
[Obsolete("Use IWireMockServerSettings. This will removed in next version (1.3.x)")]
public interface IFluentMockServerSettings : IWireMockServerSettings
{
}

View File

@@ -1,4 +1,5 @@
using JetBrains.Annotations;
using System;
using JetBrains.Annotations;
namespace WireMock.Settings
{
@@ -36,13 +37,15 @@ namespace WireMock.Settings
string ClientX509Certificate2ThumbprintOrSubjectName { get; set; }
/// <summary>
/// Defines a list from headers which will excluded from the saved mappings.
/// Defines a list from headers which will be excluded from the saved mappings.
/// </summary>
[Obsolete("Will be renamed to ExcludedHeaders in next version (1.3.x)")]
string[] BlackListedHeaders { get; set; }
/// <summary>
/// Defines a list of cookies which will excluded from the saved mappings.
/// Defines a list of cookies which will be excluded from the saved mappings.
/// </summary>
[Obsolete("Will be renamed to ExcludedCookies in next version (1.3.x)")]
string[] BlackListedCookies { get; set; }
/// <summary>

View File

@@ -3,6 +3,7 @@ using HandlebarsDotNet;
using JetBrains.Annotations;
using WireMock.Handlers;
using WireMock.Logging;
using WireMock.Matchers;
namespace WireMock.Settings
{
@@ -143,7 +144,7 @@ namespace WireMock.Settings
/// - false : also null, 0, empty or invalid HttpStatus codes are allowed.
/// - true : only codes defined in <see cref="System.Net.HttpStatusCode"/> are allowed.
/// </summary>
/// [PublicAPI]
[PublicAPI]
bool? AllowOnlyDefinedHttpStatusCodeInResponse { get; set; }
/// <summary>
@@ -157,5 +158,17 @@ namespace WireMock.Settings
/// </summary>
[PublicAPI]
bool? DisableRequestBodyDecompressing { get; set; }
/// <summary>
/// Handle all requests synchronously. (default set to false).
/// </summary>
[PublicAPI]
bool? HandleRequestsSynchronously { get; set; }
/// <summary>
/// Throw an exception when the <see cref="IMatcher"/> fails because of invalid input. (default set to false).
/// </summary>
[PublicAPI]
bool? ThrowExceptionWhenMatcherFails { get; set; }
}
}

View File

@@ -1,4 +1,5 @@
using JetBrains.Annotations;
using System;
using JetBrains.Annotations;
namespace WireMock.Settings
{
@@ -39,16 +40,14 @@ namespace WireMock.Settings
[PublicAPI]
public string ClientX509Certificate2ThumbprintOrSubjectName { get; set; }
/// <summary>
/// Defines a list from headers which will excluded from the saved mappings.
/// </summary>
/// <inheritdoc cref="IProxyAndRecordSettings.BlackListedHeaders"/>
[PublicAPI]
[Obsolete("Will be renamed to ExcludedHeaders in next version (1.3.x)")]
public string[] BlackListedHeaders { get; set; }
/// <summary>
/// Defines a list of cookies which will excluded from the saved mappings.
/// </summary>
/// <inheritdoc cref="IProxyAndRecordSettings.BlackListedCookies"/>
[PublicAPI]
[Obsolete("Will be renamed to ExcludedCookies in next version (1.3.x)")]
public string[] BlackListedCookies { get; set; }
/// <inheritdoc cref="IProxyAndRecordSettings.WebProxySettings"/>

View File

@@ -113,5 +113,13 @@ namespace WireMock.Settings
/// <inheritdoc cref="IWireMockServerSettings.DisableRequestBodyDecompressing"/>
[PublicAPI]
public bool? DisableRequestBodyDecompressing { get; set; }
/// <inheritdoc cref="IWireMockServerSettings.HandleRequestsSynchronously"/>
[PublicAPI]
public bool? HandleRequestsSynchronously { get; set; }
/// <inheritdoc cref="IWireMockServerSettings.ThrowExceptionWhenMatcherFails"/>
[PublicAPI]
public bool? ThrowExceptionWhenMatcherFails { get; set; }
}
}

View File

@@ -36,7 +36,9 @@ namespace WireMock.Settings
AllowCSharpCodeMatcher = parser.GetBoolValue("AllowCSharpCodeMatcher"),
AllowBodyForAllHttpMethods = parser.GetBoolValue("AllowBodyForAllHttpMethods"),
AllowOnlyDefinedHttpStatusCodeInResponse = parser.GetBoolValue("AllowOnlyDefinedHttpStatusCodeInResponse"),
DisableJsonBodyParsing = parser.GetBoolValue("DisableJsonBodyParsing")
DisableJsonBodyParsing = parser.GetBoolValue("DisableJsonBodyParsing"),
HandleRequestsSynchronously = parser.GetBoolValue("HandleRequestsSynchronously"),
ThrowExceptionWhenMatcherFails = parser.GetBoolValue("ThrowExceptionWhenMatcherFails")
};
if (logger != null)

View File

@@ -1,5 +1,5 @@
using HandlebarsDotNet;
using System;
using System;
using HandlebarsDotNet;
using WireMock.Handlers;
namespace WireMock.Transformers

View File

@@ -1,11 +1,10 @@
using HandlebarsDotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using HandlebarsDotNet;
using JetBrains.Annotations;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WireMock.Types;
using WireMock.Util;
using WireMock.Validation;

View File

@@ -1,92 +1,92 @@
using NFluent;
using WireMock.Matchers;
using Xunit;
namespace WireMock.Net.Tests.Matchers
{
public class CSharpCodeMatcherTests
{
[Fact]
public void CSharpCodeMatcher_For_String_SinglePattern_IsMatch_Positive()
{
// Assign
string input = "x";
// Act
var matcher = new CSharpCodeMatcher("return it == \"x\";");
// Assert
Check.That(matcher.IsMatch(input)).IsEqualTo(1.0d);
}
[Fact]
public void CSharpCodeMatcher_For_String_IsMatch_Negative()
{
// Assign
string input = "y";
// Act
var matcher = new CSharpCodeMatcher("return it == \"x\";");
// Assert
Check.That(matcher.IsMatch(input)).IsEqualTo(0.0d);
}
[Fact]
public void CSharpCodeMatcher_For_String_IsMatch_RejectOnMatch()
{
// Assign
string input = "x";
// Act
var matcher = new CSharpCodeMatcher(MatchBehaviour.RejectOnMatch, "return it == \"x\";");
// Assert
Check.That(matcher.IsMatch(input)).IsEqualTo(0.0d);
}
[Fact]
public void CSharpCodeMatcher_For_Object_IsMatch()
{
// Assign
var input = new
{
Id = 9,
Name = "Test"
};
// Act
var matcher = new CSharpCodeMatcher("return it.Id > 1 && it.Name == \"Test\";");
double match = matcher.IsMatch(input);
// Assert
Assert.Equal(1.0, match);
}
[Fact]
public void CSharpCodeMatcher_GetName()
{
// Assign
var matcher = new CSharpCodeMatcher("x");
// Act
string name = matcher.Name;
// Assert
Check.That(name).Equals("CSharpCodeMatcher");
}
[Fact]
public void CSharpCodeMatcher_GetPatterns()
{
// Assign
var matcher = new CSharpCodeMatcher("x");
// Act
string[] patterns = matcher.GetPatterns();
// Assert
Check.That(patterns).ContainsExactly("x");
}
}
using NFluent;
using WireMock.Matchers;
using Xunit;
namespace WireMock.Net.Tests.Matchers
{
public class CSharpCodeMatcherTests
{
[Fact]
public void CSharpCodeMatcher_For_String_SinglePattern_IsMatch_Positive()
{
// Assign
string input = "x";
// Act
var matcher = new CSharpCodeMatcher("return it == \"x\";");
// Assert
Check.That(matcher.IsMatch(input)).IsEqualTo(1.0d);
}
[Fact]
public void CSharpCodeMatcher_For_String_IsMatch_Negative()
{
// Assign
string input = "y";
// Act
var matcher = new CSharpCodeMatcher("return it == \"x\";");
// Assert
Check.That(matcher.IsMatch(input)).IsEqualTo(0.0d);
}
[Fact]
public void CSharpCodeMatcher_For_String_IsMatch_RejectOnMatch()
{
// Assign
string input = "x";
// Act
var matcher = new CSharpCodeMatcher(MatchBehaviour.RejectOnMatch, "return it == \"x\";");
// Assert
Check.That(matcher.IsMatch(input)).IsEqualTo(0.0d);
}
[Fact]
public void CSharpCodeMatcher_For_Object_IsMatch()
{
// Assign
var input = new
{
Id = 9,
Name = "Test"
};
// Act
var matcher = new CSharpCodeMatcher("return it.Id > 1 && it.Name == \"Test\";");
double match = matcher.IsMatch(input);
// Assert
Assert.Equal(1.0, match);
}
[Fact]
public void CSharpCodeMatcher_GetName()
{
// Assign
var matcher = new CSharpCodeMatcher("x");
// Act
string name = matcher.Name;
// Assert
Check.That(name).Equals("CSharpCodeMatcher");
}
[Fact]
public void CSharpCodeMatcher_GetPatterns()
{
// Assign
var matcher = new CSharpCodeMatcher("x");
// Act
string[] patterns = matcher.GetPatterns();
// Assert
Check.That(patterns).ContainsExactly("x");
}
}
}

View File

@@ -1,113 +1,113 @@
using NFluent;
using WireMock.Matchers;
using Xunit;
namespace WireMock.Net.Tests.Matchers
{
public class ExactMatcherTests
{
[Fact]
public void ExactMatcher_GetName()
{
// Assign
var matcher = new ExactMatcher("X");
// Act
string name = matcher.Name;
// Assert
Check.That(name).Equals("ExactMatcher");
}
[Fact]
public void ExactMatcher_GetPatterns()
{
// Assign
var matcher = new ExactMatcher("X");
// Act
string[] patterns = matcher.GetPatterns();
// Assert
Check.That(patterns).ContainsExactly("X");
}
[Fact]
public void ExactMatcher_IsMatch_WithSinglePattern_ReturnsMatch1_0()
{
// Assign
var matcher = new ExactMatcher("x");
// Act
double result = matcher.IsMatch("x");
// Assert
Check.That(result).IsEqualTo(1.0);
}
[Fact]
public void ExactMatcher_IsMatch_WithSinglePattern_ReturnsMatch0_0()
{
// Assign
var matcher = new ExactMatcher("x");
// Act
double result = matcher.IsMatch("y");
// Assert
Check.That(result).IsEqualTo(0.0);
}
[Fact]
public void ExactMatcher_IsMatch_WithMultiplePatterns_ReturnsMatch0_5()
{
// Assign
var matcher = new ExactMatcher("x", "y");
// Act
double result = matcher.IsMatch("x");
// Assert
Check.That(result).IsEqualTo(1.0);
}
[Fact]
public void ExactMatcher_IsMatch_SinglePattern()
{
// Assign
var matcher = new ExactMatcher("cat");
// Act
double result = matcher.IsMatch("caR");
// Assert
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);
}
}
using NFluent;
using WireMock.Matchers;
using Xunit;
namespace WireMock.Net.Tests.Matchers
{
public class ExactMatcherTests
{
[Fact]
public void ExactMatcher_GetName()
{
// Assign
var matcher = new ExactMatcher("X");
// Act
string name = matcher.Name;
// Assert
Check.That(name).Equals("ExactMatcher");
}
[Fact]
public void ExactMatcher_GetPatterns()
{
// Assign
var matcher = new ExactMatcher("X");
// Act
string[] patterns = matcher.GetPatterns();
// Assert
Check.That(patterns).ContainsExactly("X");
}
[Fact]
public void ExactMatcher_IsMatch_WithSinglePattern_ReturnsMatch1_0()
{
// Assign
var matcher = new ExactMatcher("x");
// Act
double result = matcher.IsMatch("x");
// Assert
Check.That(result).IsEqualTo(1.0);
}
[Fact]
public void ExactMatcher_IsMatch_WithSinglePattern_ReturnsMatch0_0()
{
// Assign
var matcher = new ExactMatcher("x");
// Act
double result = matcher.IsMatch("y");
// Assert
Check.That(result).IsEqualTo(0.0);
}
[Fact]
public void ExactMatcher_IsMatch_WithMultiplePatterns_ReturnsMatch0_5()
{
// Assign
var matcher = new ExactMatcher("x", "y");
// Act
double result = matcher.IsMatch("x");
// Assert
Check.That(result).IsEqualTo(1.0);
}
[Fact]
public void ExactMatcher_IsMatch_SinglePattern()
{
// Assign
var matcher = new ExactMatcher("cat");
// Act
double result = matcher.IsMatch("caR");
// Assert
Check.That(result).IsEqualTo(0.0);
}
[Fact]
public void ExactMatcher_IsMatch_SinglePattern_AcceptOnMatch()
{
// Assign
var matcher = new ExactMatcher(MatchBehaviour.AcceptOnMatch, false, "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, false, "cat");
// Act
double result = matcher.IsMatch("cat");
// Assert
Check.That(result).IsEqualTo(0.0);
}
}
}

View File

@@ -1,166 +1,166 @@
using Newtonsoft.Json.Linq;
using NFluent;
using WireMock.Matchers;
using Xunit;
namespace WireMock.Net.Tests.Matchers
{
public class JmesPathMatcherTests
{
[Fact]
public void JmesPathMatcher_GetName()
{
// Assign
var matcher = new JmesPathMatcher("X");
// Act
string name = matcher.Name;
// Assert
Check.That(name).Equals("JmesPathMatcher");
}
[Fact]
public void JmesPathMatcher_GetPatterns()
{
// Assign
var matcher = new JmesPathMatcher("X");
// Act
string[] patterns = matcher.GetPatterns();
// Assert
Check.That(patterns).ContainsExactly("X");
}
[Fact]
public void JmesPathMatcher_IsMatch_ByteArray()
{
// Assign
var bytes = new byte[0];
var matcher = new JmesPathMatcher("");
// Act
double match = matcher.IsMatch(bytes);
// Assert
Check.That(match).IsEqualTo(0);
}
[Fact]
public void JmesPathMatcher_IsMatch_NullString()
{
// Assign
string s = null;
var matcher = new JmesPathMatcher("");
// Act
double match = matcher.IsMatch(s);
// Assert
Check.That(match).IsEqualTo(0);
}
[Fact]
public void JmesPathMatcher_IsMatch_NullObject()
{
// Assign
object o = null;
var matcher = new JmesPathMatcher("");
// Act
double match = matcher.IsMatch(o);
// Assert
Check.That(match).IsEqualTo(0);
}
[Fact]
public void JmesPathMatcher_IsMatch_String_Exception_Mismatch()
{
// Assign
var matcher = new JmesPathMatcher("xxx");
// Act
double match = matcher.IsMatch("");
// Assert
Check.That(match).IsEqualTo(0);
}
[Fact]
public void JmesPathMatcher_IsMatch_Object_Exception_Mismatch()
{
// Assign
var matcher = new JmesPathMatcher("");
// Act
double match = matcher.IsMatch("x");
// Assert
Check.That(match).IsEqualTo(0);
}
[Fact]
public void JmesPathMatcher_IsMatch_AnonymousObject()
{
// Assign
var matcher = new JmesPathMatcher("things.name == 'RequiredThing'");
// Act
double match = matcher.IsMatch(new { things = new { name = "RequiredThing" } });
// Assert
Check.That(match).IsEqualTo(1);
}
[Fact]
public void JmesPathMatcher_IsMatch_JObject()
{
// Assign
string[] patterns = { "things.x == 'RequiredThing'" };
var matcher = new JmesPathMatcher(patterns);
// Act
var sub = new JObject
{
{ "x", new JValue("RequiredThing") }
};
var jobject = new JObject
{
{ "Id", new JValue(1) },
{ "things", sub }
};
double match = matcher.IsMatch(jobject);
// Assert
Check.That(match).IsEqualTo(1);
}
[Fact]
public void JmesPathMatcher_IsMatch_JObject_Parsed()
{
// Assign
var matcher = new JmesPathMatcher("things.x == 'RequiredThing'");
// Act
double match = matcher.IsMatch(JObject.Parse("{ \"things\": { \"x\": \"RequiredThing\" } }"));
// Assert
Check.That(match).IsEqualTo(1);
}
[Fact]
public void JmesPathMatcher_IsMatch_RejectOnMatch()
{
// Assign
var matcher = new JmesPathMatcher(MatchBehaviour.RejectOnMatch, "things.x == 'RequiredThing'");
// Act
double match = matcher.IsMatch(JObject.Parse("{ \"things\": { \"x\": \"RequiredThing\" } }"));
// Assert
Check.That(match).IsEqualTo(0.0);
}
}
using Newtonsoft.Json.Linq;
using NFluent;
using WireMock.Matchers;
using Xunit;
namespace WireMock.Net.Tests.Matchers
{
public class JmesPathMatcherTests
{
[Fact]
public void JmesPathMatcher_GetName()
{
// Assign
var matcher = new JmesPathMatcher("X");
// Act
string name = matcher.Name;
// Assert
Check.That(name).Equals("JmesPathMatcher");
}
[Fact]
public void JmesPathMatcher_GetPatterns()
{
// Assign
var matcher = new JmesPathMatcher("X");
// Act
string[] patterns = matcher.GetPatterns();
// Assert
Check.That(patterns).ContainsExactly("X");
}
[Fact]
public void JmesPathMatcher_IsMatch_ByteArray()
{
// Assign
var bytes = new byte[0];
var matcher = new JmesPathMatcher("");
// Act
double match = matcher.IsMatch(bytes);
// Assert
Check.That(match).IsEqualTo(0);
}
[Fact]
public void JmesPathMatcher_IsMatch_NullString()
{
// Assign
string s = null;
var matcher = new JmesPathMatcher("");
// Act
double match = matcher.IsMatch(s);
// Assert
Check.That(match).IsEqualTo(0);
}
[Fact]
public void JmesPathMatcher_IsMatch_NullObject()
{
// Assign
object o = null;
var matcher = new JmesPathMatcher("");
// Act
double match = matcher.IsMatch(o);
// Assert
Check.That(match).IsEqualTo(0);
}
[Fact]
public void JmesPathMatcher_IsMatch_String_Exception_Mismatch()
{
// Assign
var matcher = new JmesPathMatcher("xxx");
// Act
double match = matcher.IsMatch("");
// Assert
Check.That(match).IsEqualTo(0);
}
[Fact]
public void JmesPathMatcher_IsMatch_Object_Exception_Mismatch()
{
// Assign
var matcher = new JmesPathMatcher("");
// Act
double match = matcher.IsMatch("x");
// Assert
Check.That(match).IsEqualTo(0);
}
[Fact]
public void JmesPathMatcher_IsMatch_AnonymousObject()
{
// Assign
var matcher = new JmesPathMatcher("things.name == 'RequiredThing'");
// Act
double match = matcher.IsMatch(new { things = new { name = "RequiredThing" } });
// Assert
Check.That(match).IsEqualTo(1);
}
[Fact]
public void JmesPathMatcher_IsMatch_JObject()
{
// Assign
string[] patterns = { "things.x == 'RequiredThing'" };
var matcher = new JmesPathMatcher(patterns);
// Act
var sub = new JObject
{
{ "x", new JValue("RequiredThing") }
};
var jobject = new JObject
{
{ "Id", new JValue(1) },
{ "things", sub }
};
double match = matcher.IsMatch(jobject);
// Assert
Check.That(match).IsEqualTo(1);
}
[Fact]
public void JmesPathMatcher_IsMatch_JObject_Parsed()
{
// Assign
var matcher = new JmesPathMatcher("things.x == 'RequiredThing'");
// Act
double match = matcher.IsMatch(JObject.Parse("{ \"things\": { \"x\": \"RequiredThing\" } }"));
// Assert
Check.That(match).IsEqualTo(1);
}
[Fact]
public void JmesPathMatcher_IsMatch_RejectOnMatch()
{
// Assign
var matcher = new JmesPathMatcher(MatchBehaviour.RejectOnMatch, false, "things.x == 'RequiredThing'");
// Act
double match = matcher.IsMatch(JObject.Parse("{ \"things\": { \"x\": \"RequiredThing\" } }"));
// Assert
Check.That(match).IsEqualTo(0.0);
}
}
}

View File

@@ -1,4 +1,8 @@
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using FluentAssertions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NFluent;
using WireMock.Matchers;
using Xunit;
@@ -33,6 +37,52 @@ namespace WireMock.Net.Tests.Matchers
Check.That(value).Equals("{}");
}
[Fact]
public void JsonMatcher_WithInvalidStringValue_Should_ThrowException()
{
// Act
Action action = () => new JsonMatcher(MatchBehaviour.AcceptOnMatch, "{ \"Id\"");
// Assert
action.Should().Throw<JsonException>();
}
[Fact]
public void JsonMatcher_WithInvalidObjectValue_Should_ThrowException()
{
// Act
Action action = () => new JsonMatcher(MatchBehaviour.AcceptOnMatch, new MemoryStream());
// Assert
action.Should().Throw<JsonException>();
}
[Fact]
public void JsonMatcher_IsMatch_WithInvalidValue_And_ThrowExceptionIsFalse_Should_ReturnMismatch()
{
// Assign
var matcher = new JsonMatcher("");
// Act
double match = matcher.IsMatch(new MemoryStream());
// Assert
Check.That(match).IsEqualTo(0);
}
[Fact]
public void JsonMatcher_IsMatch_WithInvalidValue_And_ThrowExceptionIsTrue_Should_ReturnMismatch()
{
// Assign
var matcher = new JsonMatcher("", false, true);
// Act
Action action = () => matcher.IsMatch(new MemoryStream());
// Assert
action.Should().Throw<JsonException>();
}
[Fact]
public void JsonMatcher_IsMatch_ByteArray()
{

View File

@@ -1,162 +1,162 @@
using Newtonsoft.Json.Linq;
using NFluent;
using WireMock.Matchers;
using Xunit;
namespace WireMock.Net.Tests.Matchers
{
public class JsonPathMatcherTests
{
[Fact]
public void JsonPathMatcher_GetName()
{
// Assign
var matcher = new JsonPathMatcher("X");
// Act
string name = matcher.Name;
// Assert
Check.That(name).Equals("JsonPathMatcher");
}
[Fact]
public void JsonPathMatcher_GetPatterns()
{
// Assign
var matcher = new JsonPathMatcher("X");
// Act
string[] patterns = matcher.GetPatterns();
// Assert
Check.That(patterns).ContainsExactly("X");
}
[Fact]
public void JsonPathMatcher_IsMatch_ByteArray()
{
// Assign
var bytes = new byte[0];
var matcher = new JsonPathMatcher("");
// Act
double match = matcher.IsMatch(bytes);
// Assert
Check.That(match).IsEqualTo(0);
}
[Fact]
public void JsonPathMatcher_IsMatch_NullString()
{
// Assign
string s = null;
var matcher = new JsonPathMatcher("");
// Act
double match = matcher.IsMatch(s);
// Assert
Check.That(match).IsEqualTo(0);
}
[Fact]
public void JsonPathMatcher_IsMatch_NullObject()
{
// Assign
object o = null;
var matcher = new JsonPathMatcher("");
// Act
double match = matcher.IsMatch(o);
// Assert
Check.That(match).IsEqualTo(0);
}
[Fact]
public void JsonPathMatcher_IsMatch_String_Exception_Mismatch()
{
// Assign
var matcher = new JsonPathMatcher("xxx");
// Act
double match = matcher.IsMatch("");
// Assert
Check.That(match).IsEqualTo(0);
}
[Fact]
public void JsonPathMatcher_IsMatch_Object_Exception_Mismatch()
{
// Assign
var matcher = new JsonPathMatcher("");
// Act
double match = matcher.IsMatch("x");
// Assert
Check.That(match).IsEqualTo(0);
}
[Fact]
public void JsonPathMatcher_IsMatch_AnonymousObject()
{
// Assign
var matcher = new JsonPathMatcher("$..[?(@.Id == 1)]");
// Act
double match = matcher.IsMatch(new { Id = 1, Name = "Test" });
// Assert
Check.That(match).IsEqualTo(1);
}
[Fact]
public void JsonPathMatcher_IsMatch_JObject()
{
// Assign
string[] patterns = { "$..[?(@.Id == 1)]" };
var matcher = new JsonPathMatcher(patterns);
// Act
var jobject = new JObject
{
{ "Id", new JValue(1) },
{ "Name", new JValue("Test") }
};
double match = matcher.IsMatch(jobject);
// Assert
Check.That(match).IsEqualTo(1);
}
[Fact]
public void JsonPathMatcher_IsMatch_JObject_Parsed()
{
// Assign
var matcher = new JsonPathMatcher("$..[?(@.Id == 1)]");
// Act
double match = matcher.IsMatch(JObject.Parse("{\"Id\":1,\"Name\":\"Test\"}"));
// 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);
}
}
using Newtonsoft.Json.Linq;
using NFluent;
using WireMock.Matchers;
using Xunit;
namespace WireMock.Net.Tests.Matchers
{
public class JsonPathMatcherTests
{
[Fact]
public void JsonPathMatcher_GetName()
{
// Assign
var matcher = new JsonPathMatcher("X");
// Act
string name = matcher.Name;
// Assert
Check.That(name).Equals("JsonPathMatcher");
}
[Fact]
public void JsonPathMatcher_GetPatterns()
{
// Assign
var matcher = new JsonPathMatcher("X");
// Act
string[] patterns = matcher.GetPatterns();
// Assert
Check.That(patterns).ContainsExactly("X");
}
[Fact]
public void JsonPathMatcher_IsMatch_ByteArray()
{
// Assign
var bytes = new byte[0];
var matcher = new JsonPathMatcher("");
// Act
double match = matcher.IsMatch(bytes);
// Assert
Check.That(match).IsEqualTo(0);
}
[Fact]
public void JsonPathMatcher_IsMatch_NullString()
{
// Assign
string s = null;
var matcher = new JsonPathMatcher("");
// Act
double match = matcher.IsMatch(s);
// Assert
Check.That(match).IsEqualTo(0);
}
[Fact]
public void JsonPathMatcher_IsMatch_NullObject()
{
// Assign
object o = null;
var matcher = new JsonPathMatcher("");
// Act
double match = matcher.IsMatch(o);
// Assert
Check.That(match).IsEqualTo(0);
}
[Fact]
public void JsonPathMatcher_IsMatch_String_Exception_Mismatch()
{
// Assign
var matcher = new JsonPathMatcher("xxx");
// Act
double match = matcher.IsMatch("");
// Assert
Check.That(match).IsEqualTo(0);
}
[Fact]
public void JsonPathMatcher_IsMatch_Object_Exception_Mismatch()
{
// Assign
var matcher = new JsonPathMatcher("");
// Act
double match = matcher.IsMatch("x");
// Assert
Check.That(match).IsEqualTo(0);
}
[Fact]
public void JsonPathMatcher_IsMatch_AnonymousObject()
{
// Assign
var matcher = new JsonPathMatcher("$..[?(@.Id == 1)]");
// Act
double match = matcher.IsMatch(new { Id = 1, Name = "Test" });
// Assert
Check.That(match).IsEqualTo(1);
}
[Fact]
public void JsonPathMatcher_IsMatch_JObject()
{
// Assign
string[] patterns = { "$..[?(@.Id == 1)]" };
var matcher = new JsonPathMatcher(patterns);
// Act
var jobject = new JObject
{
{ "Id", new JValue(1) },
{ "Name", new JValue("Test") }
};
double match = matcher.IsMatch(jobject);
// Assert
Check.That(match).IsEqualTo(1);
}
[Fact]
public void JsonPathMatcher_IsMatch_JObject_Parsed()
{
// Assign
var matcher = new JsonPathMatcher("$..[?(@.Id == 1)]");
// Act
double match = matcher.IsMatch(JObject.Parse("{\"Id\":1,\"Name\":\"Test\"}"));
// Assert
Check.That(match).IsEqualTo(1);
}
[Fact]
public void JsonPathMatcher_IsMatch_RejectOnMatch()
{
// Assign
var matcher = new JsonPathMatcher(MatchBehaviour.RejectOnMatch, false, "$..[?(@.Id == 1)]");
// Act
double match = matcher.IsMatch(JObject.Parse("{\"Id\":1,\"Name\":\"Test\"}"));
// Assert
Check.That(match).IsEqualTo(0.0);
}
}
}

View File

@@ -1,69 +1,69 @@
using NFluent;
using WireMock.Matchers;
using Xunit;
namespace WireMock.Net.Tests.Matchers
{
public class XPathMatcherTests
{
[Fact]
public void XPathMatcher_GetName()
{
// Assign
var matcher = new XPathMatcher("X");
// Act
string name = matcher.Name;
// Assert
Check.That(name).Equals("XPathMatcher");
}
[Fact]
public void XPathMatcher_GetPatterns()
{
// Assign
var matcher = new XPathMatcher("X");
// Act
string[] patterns = matcher.GetPatterns();
// Assert
Check.That(patterns).ContainsExactly("X");
}
[Fact]
public void XPathMatcher_IsMatch_AcceptOnMatch()
{
// Assign
string xml = @"
<todo-list>
<todo-item id='a1'>abc</todo-item>
</todo-list>";
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 = @"
<todo-list>
<todo-item id='a1'>abc</todo-item>
</todo-list>";
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);
}
}
using NFluent;
using WireMock.Matchers;
using Xunit;
namespace WireMock.Net.Tests.Matchers
{
public class XPathMatcherTests
{
[Fact]
public void XPathMatcher_GetName()
{
// Assign
var matcher = new XPathMatcher("X");
// Act
string name = matcher.Name;
// Assert
Check.That(name).Equals("XPathMatcher");
}
[Fact]
public void XPathMatcher_GetPatterns()
{
// Assign
var matcher = new XPathMatcher("X");
// Act
string[] patterns = matcher.GetPatterns();
// Assert
Check.That(patterns).ContainsExactly("X");
}
[Fact]
public void XPathMatcher_IsMatch_AcceptOnMatch()
{
// Assign
string xml = @"
<todo-list>
<todo-item id='a1'>abc</todo-item>
</todo-list>";
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 = @"
<todo-list>
<todo-item id='a1'>abc</todo-item>
</todo-list>";
var matcher = new XPathMatcher(MatchBehaviour.RejectOnMatch, false, "/todo-list[count(todo-item) = 1]");
// Act
double result = matcher.IsMatch(xml);
// Assert
Check.That(result).IsEqualTo(0.0);
}
}
}

View File

@@ -11,7 +11,7 @@ namespace WireMock.Net.Tests.ResponseBuilders
{
public class ResponseWithStatusCodeTests
{
private readonly Mock<IFluentMockServerSettings> _settingsMock = new Mock<IFluentMockServerSettings>();
private readonly Mock<IWireMockServerSettings> _settingsMock = new Mock<IWireMockServerSettings>();
private const string ClientIp = "::1";
[Theory]

View File

@@ -25,7 +25,7 @@ namespace WireMock.Net.Tests.Serialization
// Assign
var request = Request.Create();
var response = Response.Create();
var mapping = new Mapping(Guid.NewGuid(), "", null, _settings, request, response, 0, null, null, null);
var mapping = new Mapping(Guid.NewGuid(), "", null, _settings, request, response, 0, null, null, null, null);
// Act
var model = _sut.ToMappingModel(mapping);
@@ -44,7 +44,7 @@ namespace WireMock.Net.Tests.Serialization
// Assign
var request = Request.Create();
var response = Response.Create().WithBodyAsJson(new { x = "x" }).WithTransformer();
var mapping = new Mapping(Guid.NewGuid(), "", null, _settings, request, response, 42, null, null, null);
var mapping = new Mapping(Guid.NewGuid(), "", null, _settings, request, response, 42, null, null, null, null);
// Act
var model = _sut.ToMappingModel(mapping);

View File

@@ -1,4 +1,5 @@
using System;
using FluentAssertions;
using NFluent;
using WireMock.Admin.Mappings;
using WireMock.Matchers;
@@ -43,7 +44,8 @@ namespace WireMock.Net.Tests.Serialization
var matcher = (ExactMatcher)_sut.Map(model);
// Assert
Check.That(matcher.GetPatterns()).ContainsExactly("x");
matcher.GetPatterns().Should().ContainSingle("x");
matcher.ThrowException.Should().BeFalse();
}
[Fact]
@@ -63,6 +65,39 @@ namespace WireMock.Net.Tests.Serialization
Check.That(matcher.GetPatterns()).ContainsExactly("x", "y");
}
[Theory]
[InlineData(nameof(LinqMatcher))]
[InlineData(nameof(ExactMatcher))]
[InlineData(nameof(ExactObjectMatcher))]
[InlineData(nameof(RegexMatcher))]
[InlineData(nameof(JsonMatcher))]
[InlineData(nameof(JsonPathMatcher))]
[InlineData(nameof(JmesPathMatcher))]
[InlineData(nameof(XPathMatcher))]
[InlineData(nameof(WildcardMatcher))]
[InlineData(nameof(ContentTypeMatcher))]
[InlineData(nameof(SimMetricsMatcher))]
public void MatcherModelMapper_Map_ThrowExceptionWhenMatcherFails_True(string name)
{
// Assign
var settings = new WireMockServerSettings
{
ThrowExceptionWhenMatcherFails = true
};
var sut = new MatcherMapper(settings);
var model = new MatcherModel
{
Name = name,
Patterns = new[] { "" }
};
// Act
var matcher = sut.Map(model);
// Assert
matcher.ThrowException.Should().BeTrue();
}
[Fact]
public void MatcherModelMapper_Map_ExactObjectMatcher_ValidBase64StringPattern()
{

View File

@@ -3,6 +3,7 @@ using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using FluentAssertions;
using NFluent;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
@@ -61,6 +62,89 @@ namespace WireMock.Net.Tests
Check.That(responseWithState).Equals("Test state msg");
}
[Fact]
public async Task Scenarios_With_Same_Path_Should_Use_Times_When_Moving_To_Next_State()
{
// given
const int times = 2;
string path = $"/foo_{Guid.NewGuid()}";
string body1 = "Scenario S1, No State, Setting State T2";
string body2 = "Scenario S1, State T2, End";
var server = WireMockServer.Start();
server
.Given(Request.Create().WithPath(path).UsingGet())
.InScenario(1)
.WillSetStateTo(2, times)
.RespondWith(Response.Create().WithBody(body1));
server
.Given(Request.Create().WithPath(path).UsingGet())
.InScenario(1)
.WhenStateIs(2)
.RespondWith(Response.Create().WithBody(body2));
// when
var client = new HttpClient();
var responseScenario1 = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path);
var responseScenario2 = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path);
var responseWithState = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path);
// then
responseScenario1.Should().Be(body1);
responseScenario2.Should().Be(body1);
responseWithState.Should().Be(body2);
}
[Fact]
public async Task Scenarios_With_Different_Paths_Should_Use_Times_When_Moving_To_Next_State()
{
// given
const int times = 2;
string path1 = $"/a_{Guid.NewGuid()}";
string path2 = $"/b_{Guid.NewGuid()}";
string path3 = $"/c_{Guid.NewGuid()}";
string body1 = "Scenario S1, No State, Setting State T2";
string body2 = "Scenario S1, State T2, Setting State T3";
string body3 = "Scenario S1, State T3, End";
var server = WireMockServer.Start();
server
.Given(Request.Create().WithPath(path1).UsingGet())
.InScenario("S1")
.WillSetStateTo("T2", times)
.RespondWith(Response.Create().WithBody(body1));
server
.Given(Request.Create().WithPath(path2).UsingGet())
.InScenario("S1")
.WhenStateIs("T2")
.WillSetStateTo("T3", times)
.RespondWith(Response.Create().WithBody(body2));
server
.Given(Request.Create().WithPath(path3).UsingGet())
.InScenario("S1")
.WhenStateIs("T3")
.RespondWith(Response.Create().WithBody(body3));
// when
var client = new HttpClient();
var t1a = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path1);
var t1b = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path1);
var t2a = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path2);
var t2b = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path2);
var t3 = await client.GetStringAsync("http://localhost:" + server.Ports[0] + path3);
// then
t1a.Should().Be(body1);
t1b.Should().Be(body1);
t2a.Should().Be(body2);
t2b.Should().Be(body2);
t3.Should().Be(body3);
}
[Fact]
public async Task Scenarios_Should_Respect_Int_Valued_Scenarios_and_States()
{

View File

@@ -33,9 +33,9 @@ namespace WireMock.Net.Tests
SaveMappingToFile = false
}
};
var server = WireMockServer.Start(settings);
// Act
var server = WireMockServer.Start(settings);
// Act
var requestMessage = new HttpRequestMessage
{
Method = HttpMethod.Get,
@@ -47,8 +47,8 @@ namespace WireMock.Net.Tests
// Assert
Check.That(server.Mappings).HasSize(2);
Check.That(server.LogEntries).HasSize(1);
}
}
[Fact]
public async Task WireMockServer_Proxy_Should_proxy_responses()
{
@@ -123,7 +123,7 @@ namespace WireMock.Net.Tests
}
[Fact]
public async Task WireMockServer_Proxy_Should_exclude_blacklisted_content_header_in_mapping()
public async Task WireMockServer_Proxy_Should_exclude_ExcludedHeaders_in_mapping()
{
// Assign
string path = $"/prx_{Guid.NewGuid()}";
@@ -139,7 +139,7 @@ namespace WireMock.Net.Tests
Url = serverForProxyForwarding.Urls[0],
SaveMapping = true,
SaveMappingToFile = false,
BlackListedHeaders = new[] { "blacklisted" }
BlackListedHeaders = new[] { "excluded-header-X" }
}
};
var server = WireMockServer.Start(settings);
@@ -152,7 +152,7 @@ namespace WireMock.Net.Tests
RequestUri = new Uri($"{server.Urls[0]}{path}"),
Content = new StringContent("stringContent")
};
requestMessage.Headers.Add("blacklisted", "exact_match");
requestMessage.Headers.Add("foobar", "exact_match");
requestMessage.Headers.Add("ok", "ok-value");
await new HttpClient().SendAsync(requestMessage);
@@ -160,12 +160,12 @@ namespace WireMock.Net.Tests
var mapping = server.Mappings.FirstOrDefault(m => m.Guid != defaultMapping.Guid);
Check.That(mapping).IsNotNull();
var matchers = ((Request)mapping.RequestMatcher).GetRequestMessageMatchers<RequestMessageHeaderMatcher>().Select(m => m.Name).ToList();
Check.That(matchers).Not.Contains("blacklisted");
Check.That(matchers).Not.Contains("excluded-header-X");
Check.That(matchers).Contains("ok");
}
[Fact]
public async Task WireMockServer_Proxy_Should_exclude_blacklisted_cookies_in_mapping()
public async Task WireMockServer_Proxy_Should_exclude_ExcludedCookies_in_mapping()
{
// Assign
string path = $"/prx_{Guid.NewGuid()}";
@@ -515,7 +515,7 @@ namespace WireMock.Net.Tests
content.Should().Contain("known"); // On Linux it's "Name or service not known". On Windows it's "No such host is known.".
server.LogEntries.Should().HaveCount(1);
((StatusModel) server.LogEntries.First().ResponseMessage.BodyData.BodyAsJson).Status.Should().Contain("known");
((StatusModel)server.LogEntries.First().ResponseMessage.BodyData.BodyAsJson).Status.Should().Contain("known");
}
}
}