Add ExactMatcher

This commit is contained in:
Stef Heyenrath
2017-02-05 18:04:48 +01:00
parent 1adc7340d1
commit c987a59ca8
2 changed files with 107 additions and 6 deletions

View File

@@ -0,0 +1,53 @@
using JetBrains.Annotations;
using WireMock.Validation;
namespace WireMock.Matchers
{
/// <summary>
/// ExactMatcher
/// </summary>
/// <seealso cref="IMatcher" />
public class ExactMatcher : IMatcher
{
private readonly string _value;
/// <summary>
/// Initializes a new instance of the <see cref="ExactMatcher"/> class.
/// </summary>
/// <param name="value">The value.</param>
public ExactMatcher([NotNull] string value)
{
Check.NotNull(value, nameof(value));
_value = value;
}
/// <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>
public double IsMatch(string input)
{
return MatchScores.ToScore(_value.Equals(input));
}
/// <summary>
/// Gets the value.
/// </summary>
/// <returns>Pattern</returns>
public string GetPattern()
{
return _value;
}
/// <summary>
/// Gets the name.
/// </summary>
/// <returns>Name</returns>
public string GetName()
{
return "ExactMatcher";
}
}
}

View File

@@ -290,7 +290,55 @@ namespace WireMock.Net.Tests
}
[Test]
public void Should_specify_requests_matching_given_body_as_wildcard()
public void Should_specify_requests_matching_given_body_using_ExactMatcher_true()
{
// given
var requestBuilder = Request.Create().WithPath("/foo").UsingAnyVerb().WithBody(new ExactMatcher("cat"));
// when
string bodyAsString = "cat";
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString);
// then
var requestMatchResult = new RequestMatchResult();
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
}
[Test]
public void Should_specify_requests_matching_given_body_using_ExactMatcher_false()
{
// given
var requestBuilder = Request.Create().WithPath("/foo").UsingAnyVerb().WithBody(new ExactMatcher("cat"));
// when
string bodyAsString = "caR";
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString);
// then
var requestMatchResult = new RequestMatchResult();
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsLessThan(1.0);
}
[Test]
public void Should_specify_requests_matching_given_body_using_SimMetricsMatcher()
{
// given
var requestBuilder = Request.Create().WithPath("/foo").UsingAnyVerb().WithBody("The cat walks in the street.");
// when
string bodyAsString = "The car drives in the street.";
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString);
// then
var requestMatchResult = new RequestMatchResult();
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsLessThan(1.0).And.IsGreaterThan(0.5);
}
[Test]
public void Should_specify_requests_matching_given_body_using_WildcardMatcher()
{
// given
var spec = Request.Create().WithPath("/foo").UsingAnyVerb().WithBody(new WildcardMatcher("H*o*"));
@@ -306,7 +354,7 @@ namespace WireMock.Net.Tests
}
[Test]
public void Should_specify_requests_matching_given_body_as_regexmatcher()
public void Should_specify_requests_matching_given_body_using_RegexMatcher()
{
// given
var spec = Request.Create().WithPath("/foo").UsingAnyVerb().WithBody(new RegexMatcher("H.*o"));
@@ -322,7 +370,7 @@ namespace WireMock.Net.Tests
}
[Test]
public void Should_specify_requests_matching_given_body_as_xpathmatcher_true()
public void Should_specify_requests_matching_given_body_using_XPathMatcher_true()
{
// given
var spec = Request.Create().WithPath("/foo").UsingAnyVerb().WithBody(new XPathMatcher("/todo-list[count(todo-item) = 3]"));
@@ -343,7 +391,7 @@ namespace WireMock.Net.Tests
}
[Test]
public void Should_specify_requests_matching_given_body_as_xpathmatcher_false()
public void Should_specify_requests_matching_given_body_using_XPathMatcher_false()
{
// given
var spec = Request.Create().WithPath("/foo").UsingAnyVerb().WithBody(new XPathMatcher("/todo-list[count(todo-item) = 99]"));
@@ -364,7 +412,7 @@ namespace WireMock.Net.Tests
}
[Test]
public void Should_specify_requests_matching_given_body_as_jsonpathmatcher_true()
public void Should_specify_requests_matching_given_body_using_JsonPathMatcher_true()
{
// given
var spec = Request.Create().WithPath("/foo").UsingAnyVerb().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]"));
@@ -380,7 +428,7 @@ namespace WireMock.Net.Tests
}
[Test]
public void Should_specify_requests_matching_given_body_as_jsonpathmatcher_false()
public void Should_specify_requests_matching_given_body_using_JsonPathMatcher_false()
{
// given
var spec = Request.Create().WithPath("/foo").UsingAnyVerb().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]"));