Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json fixes (#91) #92

Merged
merged 3 commits into from
Feb 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions examples/WireMock.Net.ConsoleApplication/MainApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ public static void Run()
// .RespondWith(Response.Create()
// .WithProxy("http://restcountries.eu"));

server
.Given(Request
.Create()
.WithPath("/jsonthings")
.WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]"))
.UsingPut())
.RespondWith(Response.Create()
.WithBody(@"{ ""result"": ""JsonPathMatcher !!!""}"));

server
.Given(Request
.Create()
Expand Down
20 changes: 5 additions & 15 deletions src/WireMock.Net/Matchers/ExactMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace WireMock.Matchers
/// <summary>
/// ExactMatcher
/// </summary>
/// <seealso cref="IMatcher" />
public class ExactMatcher : IMatcher
/// <seealso cref="IStringMatcher" />
public class ExactMatcher : IStringMatcher
{
private readonly string[] _values;

Expand All @@ -23,29 +23,19 @@ public ExactMatcher([NotNull] params string[] values)
_values = values;
}

/// <summary>
/// Determines whether the specified input is match.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>A value between 0.0 - 1.0 of the similarity.</returns>
/// <inheritdoc cref="IStringMatcher.IsMatch"/>
public double IsMatch(string input)
{
return MatchScores.ToScore(_values.Select(value => value.Equals(input)));
}

/// <summary>
/// Gets the value.
/// </summary>
/// <returns>Patterns</returns>
/// <inheritdoc cref="IStringMatcher.GetPatterns"/>
public string[] GetPatterns()
{
return _values;
}

/// <summary>
/// Gets the name.
/// </summary>
/// <returns>Name</returns>
/// <inheritdoc cref="IMatcher.GetName"/>
public string GetName()
{
return "ExactMatcher";
Expand Down
46 changes: 46 additions & 0 deletions src/WireMock.Net/Matchers/ExactObjectMatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Linq;
using JetBrains.Annotations;

namespace WireMock.Matchers
{
/// <summary>
/// ExactMatcher
/// </summary>
/// <seealso cref="IObjectMatcher" />
public class ExactObjectMatcher : IObjectMatcher
{
private readonly object _object;
private readonly byte[] _bytes;

/// <summary>
/// Initializes a new instance of the <see cref="ExactMatcher"/> class.
/// </summary>
/// <param name="value">The value.</param>
public ExactObjectMatcher([NotNull] object value)
{
_object = value;
}

/// <summary>
/// Initializes a new instance of the <see cref="ExactMatcher"/> class.
/// </summary>
/// <param name="value">The value.</param>
public ExactObjectMatcher([NotNull] byte[] value)
{
_bytes = value;
}

/// <inheritdoc cref="IObjectMatcher.IsMatch"/>
public double IsMatch(object input)
{
bool equals = _object != null ? Equals(_object, input) : _bytes.SequenceEqual((byte[])input);
return MatchScores.ToScore(equals);
}

/// <inheritdoc cref="IMatcher.GetName"/>
public string GetName()
{
return "ExactObjectMatcher";
}
}
}
13 changes: 0 additions & 13 deletions src/WireMock.Net/Matchers/IMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,6 @@
/// </summary>
public interface IMatcher
{
/// <summary>
/// Determines whether the specified input is match.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>A value between 0.0 - 1.0 of the similarity.</returns>
double IsMatch(string input);

/// <summary>
/// Gets the patterns.
/// </summary>
/// <returns>Patterns</returns>
string[] GetPatterns();

/// <summary>
/// Gets the name.
/// </summary>
Expand Down
15 changes: 15 additions & 0 deletions src/WireMock.Net/Matchers/IObjectMatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace WireMock.Matchers
{
/// <summary>
/// IObjectMatcher
/// </summary>
public interface IObjectMatcher : IMatcher
{
/// <summary>
/// Determines whether the specified input is match.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>A value between 0.0 - 1.0 of the similarity.</returns>
double IsMatch(object input);
}
}
21 changes: 21 additions & 0 deletions src/WireMock.Net/Matchers/IStringMatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace WireMock.Matchers
{
/// <summary>
/// IStringMatcher
/// </summary>
public interface IStringMatcher : IMatcher
{
/// <summary>
/// Determines whether the specified input is match.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>A value between 0.0 - 1.0 of the similarity.</returns>
double IsMatch(string input);

/// <summary>
/// Gets the patterns.
/// </summary>
/// <returns>Patterns</returns>
string[] GetPatterns();
}
}
48 changes: 34 additions & 14 deletions src/WireMock.Net/Matchers/JSONPathMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ namespace WireMock.Matchers
/// JSONPathMatcher
/// </summary>
/// <seealso cref="IMatcher" />
public class JsonPathMatcher : IMatcher
public class JsonPathMatcher : IStringMatcher, IObjectMatcher
{
// private readonly object _jsonPattern;
private readonly string[] _patterns;

/// <summary>
Expand All @@ -25,15 +26,20 @@ public JsonPathMatcher([NotNull] params string[] patterns)
_patterns = patterns;
}

/// <summary>
/// Determines whether the specified input is match.
/// </summary>
/// <param name="input">The input string</param>
/// <returns>A value between 0.0 - 1.0 of the similarity.</returns>
//public JsonPathMatcher([NotNull] object jsonPattern)
//{
// Check.NotNull(jsonPattern, nameof(jsonPattern));

// _jsonPattern = jsonPattern;
//}

/// <inheritdoc cref="IStringMatcher.IsMatch"/>
public double IsMatch(string input)
{
if (input == null)
{
return MatchScores.Mismatch;
}

try
{
Expand All @@ -47,19 +53,33 @@ public double IsMatch(string input)
}
}

/// <summary>
/// Gets the patterns.
/// </summary>
/// <returns>Pattern</returns>
/// <inheritdoc cref="IObjectMatcher.IsMatch"/>
public double IsMatch(object input)
{
if (input == null)
{
return MatchScores.Mismatch;
}

try
{
var o = input as JObject ?? JObject.FromObject(input);

return MatchScores.ToScore(_patterns.Select(p => o.SelectToken(p) != null));
}
catch (Exception)
{
return MatchScores.Mismatch;
}
}

/// <inheritdoc cref="IStringMatcher.GetPatterns"/>
public string[] GetPatterns()
{
return _patterns;
}

/// <summary>
/// Gets the name.
/// </summary>
/// <returns>Name</returns>
/// <inheritdoc cref="IMatcher.GetName"/>
public string GetName()
{
return "JsonPathMatcher";
Expand Down
26 changes: 9 additions & 17 deletions src/WireMock.Net/Matchers/RegexMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace WireMock.Matchers
/// <summary>
/// Regular Expression Matcher
/// </summary>
/// <seealso cref="IMatcher" />
public class RegexMatcher : IMatcher
/// <seealso cref="IStringMatcher" />
public class RegexMatcher : IStringMatcher
{
private readonly string[] _patterns;
private readonly Regex[] _expressions;
Expand All @@ -37,20 +37,20 @@ public RegexMatcher([NotNull, RegexPattern] string[] patterns, bool ignoreCase =

RegexOptions options = RegexOptions.Compiled;
if (ignoreCase)
{
options |= RegexOptions.IgnoreCase;
}

_expressions = patterns.Select(p => new Regex(p, options)).ToArray();
}

/// <summary>
/// Determines whether the specified input is match.
/// </summary>
/// <param name="input">The input string</param>
/// <returns>A value between 0.0 - 1.0 of the similarity.</returns>
/// <inheritdoc cref="IStringMatcher.IsMatch"/>
public double IsMatch(string input)
{
if (input == null)
{
return MatchScores.Mismatch;
}

try
{
Expand All @@ -62,21 +62,13 @@ public double IsMatch(string input)
}
}

/// <summary>
/// Gets the patterns.
/// </summary>
/// <returns>Pattern</returns>
/// <inheritdoc cref="IStringMatcher.GetPatterns"/>
public virtual string[] GetPatterns()
{
return _patterns;
}

/// <summary>
/// Gets the name.
/// </summary>
/// <returns>
/// Name
/// </returns>
/// <inheritdoc cref="IMatcher.GetName"/>
public virtual string GetName()
{
return "RegexMatcher";
Expand Down
Loading