Add XmlPath2 / RegEx matchers

Solves issue #5
This commit is contained in:
Stef Heyenrath
2017-01-19 21:51:22 +01:00
parent 1b2e5368a9
commit 72335d48d6
8 changed files with 214 additions and 44 deletions

View File

@@ -0,0 +1,17 @@
namespace WireMock.Matchers
{
/// <summary>
/// IMatcher
/// </summary>
public interface IMatcher
{
/// <summary>
/// Determines whether the specified input is match.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>
/// <c>true</c> if the specified input is match; otherwise, <c>false</c>.
/// </returns>
bool IsMatch(string input);
}
}

View File

@@ -0,0 +1,38 @@
using System.Text.RegularExpressions;
using JetBrains.Annotations;
using WireMock.Validation;
namespace WireMock.Matchers
{
/// <summary>
/// Regular Expression Matcher
/// </summary>
/// <seealso cref="WireMock.Matchers.IMatcher" />
public class RegexMatcher : IMatcher
{
private readonly Regex _expression;
/// <summary>
/// Initializes a new instance of the <see cref="RegexMatcher"/> class.
/// </summary>
/// <param name="pattern">The pattern.</param>
public RegexMatcher([NotNull] string pattern)
{
Check.NotNull(pattern, nameof(pattern));
_expression = new Regex(pattern, RegexOptions.Compiled);
}
/// <summary>
/// Determines whether the specified input is match.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>
/// <c>true</c> if the specified input is match; otherwise, <c>false</c>.
/// </returns>
public bool IsMatch(string input)
{
return input != null && _expression.IsMatch(input);
}
}
}

View File

@@ -0,0 +1,42 @@
using System.Xml;
using JetBrains.Annotations;
using WireMock.Validation;
using Wmhelp.XPath2;
namespace WireMock.Matchers
{
/// <summary>
/// XPath2Matcher
/// </summary>
/// <seealso cref="WireMock.Matchers.IMatcher" />
public class XPathMatcher : IMatcher
{
private readonly string _pattern;
/// <summary>
/// Initializes a new instance of the <see cref="XPathMatcher"/> class.
/// </summary>
/// <param name="pattern">The pattern.</param>
public XPathMatcher([NotNull] string pattern)
{
Check.NotNull(pattern, nameof(pattern));
_pattern = pattern;
}
/// <summary>
/// Determines whether the specified input is match.
/// </summary>
/// <param name="input">The input.</param>
/// <returns>
/// <c>true</c> if the specified input is match; otherwise, <c>false</c>.
/// </returns>
public bool IsMatch(string input)
{
var nav = new XmlDocument { InnerXml = input }.CreateNavigator();
object result = nav.XPath2Evaluate($"boolean({_pattern})");
return true.Equals(result);
}
}
}

View File

@@ -2,22 +2,9 @@ using System;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using JetBrains.Annotations; using JetBrains.Annotations;
using WireMock.Matchers;
using WireMock.Validation; using WireMock.Validation;
[module:
SuppressMessage("StyleCop.CSharp.ReadabilityRules",
"SA1101:PrefixLocalCallsWithThis",
Justification = "Reviewed. Suppression is OK here, as it conflicts with internal naming rules.")]
[module:
SuppressMessage("StyleCop.CSharp.NamingRules",
"SA1309:FieldNamesMustNotBeginWithUnderscore",
Justification = "Reviewed. Suppression is OK here, as it conflicts with internal naming rules.")]
[module:
SuppressMessage("StyleCop.CSharp.DocumentationRules",
"SA1633:FileMustHaveHeader",
Justification = "Reviewed. Suppression is OK here, as unknown copyright and company.")]
// ReSharper disable ArrangeThisQualifier
// ReSharper disable InconsistentNaming
namespace WireMock namespace WireMock
{ {
/// <summary> /// <summary>
@@ -28,22 +15,22 @@ namespace WireMock
/// <summary> /// <summary>
/// The bodyRegex. /// The bodyRegex.
/// </summary> /// </summary>
private readonly byte[] bodyData; private readonly byte[] _bodyData;
/// <summary> /// <summary>
/// The bodyRegex. /// The matcher.
/// </summary> /// </summary>
private readonly Regex bodyRegex; private readonly IMatcher _matcher;
/// <summary> /// <summary>
/// The body function /// The body function
/// </summary> /// </summary>
private readonly Func<string, bool> bodyFunc; private readonly Func<string, bool> _bodyFunc;
/// <summary> /// <summary>
/// The body data function /// The body data function
/// </summary> /// </summary>
private readonly Func<byte[], bool> bodyDataFunc; private readonly Func<byte[], bool> _bodyDataFunc;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RequestBodySpec"/> class. /// Initializes a new instance of the <see cref="RequestBodySpec"/> class.
@@ -54,7 +41,7 @@ namespace WireMock
public RequestBodySpec([NotNull, RegexPattern] string body) public RequestBodySpec([NotNull, RegexPattern] string body)
{ {
Check.NotNull(body, nameof(body)); Check.NotNull(body, nameof(body));
bodyRegex = new Regex(body); _matcher = new RegexMatcher(body);
} }
/// <summary> /// <summary>
@@ -66,7 +53,7 @@ namespace WireMock
public RequestBodySpec([NotNull] byte[] body) public RequestBodySpec([NotNull] byte[] body)
{ {
Check.NotNull(body, nameof(body)); Check.NotNull(body, nameof(body));
bodyData = body; _bodyData = body;
} }
/// <summary> /// <summary>
@@ -78,7 +65,7 @@ namespace WireMock
public RequestBodySpec([NotNull] Func<string, bool> func) public RequestBodySpec([NotNull] Func<string, bool> func)
{ {
Check.NotNull(func, nameof(func)); Check.NotNull(func, nameof(func));
bodyFunc = func; _bodyFunc = func;
} }
/// <summary> /// <summary>
@@ -90,7 +77,19 @@ namespace WireMock
public RequestBodySpec([NotNull] Func<byte[], bool> func) public RequestBodySpec([NotNull] Func<byte[], bool> func)
{ {
Check.NotNull(func, nameof(func)); Check.NotNull(func, nameof(func));
bodyDataFunc = func; _bodyDataFunc = func;
}
/// <summary>
/// Initializes a new instance of the <see cref="RequestBodySpec"/> class.
/// </summary>
/// <param name="matcher">
/// The body matcher.
/// </param>
public RequestBodySpec([NotNull] IMatcher matcher)
{
Check.NotNull(matcher, nameof(matcher));
_matcher = matcher;
} }
/// <summary> /// <summary>
@@ -104,17 +103,17 @@ namespace WireMock
/// </returns> /// </returns>
public bool IsSatisfiedBy(RequestMessage requestMessage) public bool IsSatisfiedBy(RequestMessage requestMessage)
{ {
if (bodyRegex != null) if (_matcher != null)
return bodyRegex.IsMatch(requestMessage.BodyAsString); return _matcher.IsMatch(requestMessage.BodyAsString);
if (bodyData != null) if (_bodyData != null)
return requestMessage.Body == bodyData; return requestMessage.Body == _bodyData;
if (bodyFunc != null) if (_bodyFunc != null)
return bodyFunc(requestMessage.BodyAsString); return _bodyFunc(requestMessage.BodyAsString);
if (bodyDataFunc != null) if (_bodyDataFunc != null)
return bodyDataFunc(requestMessage.Body); return _bodyDataFunc(requestMessage.Body);
return false; return false;
} }

View File

@@ -1,4 +1,6 @@
using System; using System;
using JetBrains.Annotations;
using WireMock.Matchers;
namespace WireMock.RequestBuilders namespace WireMock.RequestBuilders
{ {
@@ -7,6 +9,17 @@ namespace WireMock.RequestBuilders
/// </summary> /// </summary>
public interface IBodyRequestBuilder public interface IBodyRequestBuilder
{ {
/// <summary>
/// The with body.
/// </summary>
/// <param name="matcher">
/// The matcher.
/// </param>
/// <returns>
/// The <see cref="ISpecifyRequests"/>.
/// </returns>
ISpecifyRequests WithBody([NotNull] IMatcher matcher);
/// <summary> /// <summary>
/// The with body. /// The with body.
/// </summary> /// </summary>

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
using JetBrains.Annotations; using JetBrains.Annotations;
using WireMock.Matchers;
[module: [module:
SuppressMessage("StyleCop.CSharp.ReadabilityRules", SuppressMessage("StyleCop.CSharp.ReadabilityRules",
@@ -255,6 +256,19 @@ namespace WireMock.RequestBuilders
return this; return this;
} }
/// <summary>
/// The with body.
/// </summary>
/// <param name="matcher">The matcher.</param>
/// <returns>
/// The <see cref="ISpecifyRequests" />.
/// </returns>
public ISpecifyRequests WithBody(IMatcher matcher)
{
_requestSpecs.Add(new RequestBodySpec(matcher));
return this;
}
/// <summary> /// <summary>
/// The with parameters. /// The with parameters.
/// </summary> /// </summary>

View File

@@ -25,7 +25,8 @@
"JetBrains.Annotations": { "JetBrains.Annotations": {
"version": "10.2.1", "version": "10.2.1",
"type": "build" "type": "build"
} },
"XPath2": "1.0.3.1"
}, },
"frameworks": { "frameworks": {

View File

@@ -1,20 +1,11 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text; using System.Text;
using NFluent; using NFluent;
using NUnit.Framework; using NUnit.Framework;
using WireMock.RequestBuilders; using WireMock.RequestBuilders;
using WireMock.Matchers;
[module:
SuppressMessage("StyleCop.CSharp.DocumentationRules",
"SA1600:ElementsMustBeDocumented",
Justification = "Reviewed. Suppression is OK here, as it's a tests class.")]
[module:
SuppressMessage("StyleCop.CSharp.DocumentationRules",
"SA1633:FileMustHaveHeader",
Justification = "Reviewed. Suppression is OK here, as unknown copyright and company.")]
// ReSharper disable InconsistentNaming
namespace WireMock.Net.Tests namespace WireMock.Net.Tests
{ {
[TestFixture] [TestFixture]
@@ -249,7 +240,7 @@ namespace WireMock.Net.Tests
public void Should_specify_requests_matching_given_body() public void Should_specify_requests_matching_given_body()
{ {
// given // given
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithBody(".*Hello world!.*"); var spec = Request.WithUrl("/foo").UsingAnyVerb().WithBody("Hello world!");
// when // when
string bodyAsString = "Hello world!"; string bodyAsString = "Hello world!";
@@ -261,7 +252,7 @@ namespace WireMock.Net.Tests
} }
[Test] [Test]
public void Should_specify_requests_matching_given_body_as_wildcard() public void Should_specify_requests_matching_given_body_as_regex()
{ {
// given // given
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithBody("H.*o"); var spec = Request.WithUrl("/foo").UsingAnyVerb().WithBody("H.*o");
@@ -275,6 +266,61 @@ namespace WireMock.Net.Tests
Check.That(spec.IsSatisfiedBy(request)).IsTrue(); Check.That(spec.IsSatisfiedBy(request)).IsTrue();
} }
[Test]
public void Should_specify_requests_matching_given_body_as_regexmatcher()
{
// given
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithBody(new RegexMatcher("H.*o"));
// when
string bodyAsString = "Hello world!";
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString);
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_body_as_xpathmatcher_true()
{
// given
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithBody(new XPathMatcher("/todo-list[count(todo-item) = 3]"));
// when
string xmlBodyAsString = @"
<todo-list>
<todo-item id='a1'>abc</todo-item>
<todo-item id='a2'>def</todo-item>
<todo-item id='a3'>xyz</todo-item>
</todo-list>";
byte[] body = Encoding.UTF8.GetBytes(xmlBodyAsString);
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, xmlBodyAsString);
// then
Check.That(spec.IsSatisfiedBy(request)).IsTrue();
}
[Test]
public void Should_specify_requests_matching_given_body_as_xpathmatcher_false()
{
// given
var spec = Request.WithUrl("/foo").UsingAnyVerb().WithBody(new XPathMatcher("/todo-list[count(todo-item) = 99]"));
// when
string xmlBodyAsString = @"
<todo-list>
<todo-item id='a1'>abc</todo-item>
<todo-item id='a2'>def</todo-item>
<todo-item id='a3'>xyz</todo-item>
</todo-list>";
byte[] body = Encoding.UTF8.GetBytes(xmlBodyAsString);
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, xmlBodyAsString);
// then
Check.That(spec.IsSatisfiedBy(request)).IsFalse();
}
[Test] [Test]
public void Should_exclude_requests_not_matching_given_body() public void Should_exclude_requests_not_matching_given_body()
{ {