From c987a59ca83782a1eac6c9889dddf5664d9302e9 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Sun, 5 Feb 2017 18:04:48 +0100 Subject: [PATCH] Add ExactMatcher --- src/WireMock.Net/Matchers/ExactMatcher.cs | 53 ++++++++++++++++++++ test/WireMock.Net.Tests/RequestTests.cs | 60 ++++++++++++++++++++--- 2 files changed, 107 insertions(+), 6 deletions(-) create mode 100644 src/WireMock.Net/Matchers/ExactMatcher.cs diff --git a/src/WireMock.Net/Matchers/ExactMatcher.cs b/src/WireMock.Net/Matchers/ExactMatcher.cs new file mode 100644 index 00000000..789f6225 --- /dev/null +++ b/src/WireMock.Net/Matchers/ExactMatcher.cs @@ -0,0 +1,53 @@ +using JetBrains.Annotations; +using WireMock.Validation; + +namespace WireMock.Matchers +{ + /// + /// ExactMatcher + /// + /// + public class ExactMatcher : IMatcher + { + private readonly string _value; + + /// + /// Initializes a new instance of the class. + /// + /// The value. + public ExactMatcher([NotNull] string value) + { + Check.NotNull(value, nameof(value)); + + _value = value; + } + + /// + /// Determines whether the specified input is match. + /// + /// The input. + /// A value between 0.0 - 1.0 of the similarity. + public double IsMatch(string input) + { + return MatchScores.ToScore(_value.Equals(input)); + } + + /// + /// Gets the value. + /// + /// Pattern + public string GetPattern() + { + return _value; + } + + /// + /// Gets the name. + /// + /// Name + public string GetName() + { + return "ExactMatcher"; + } + } +} \ No newline at end of file diff --git a/test/WireMock.Net.Tests/RequestTests.cs b/test/WireMock.Net.Tests/RequestTests.cs index 71d54c39..839b32a0 100644 --- a/test/WireMock.Net.Tests/RequestTests.cs +++ b/test/WireMock.Net.Tests/RequestTests.cs @@ -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')]"));