WildcardMatcher and other fixes

This commit is contained in:
Stef Heyenrath
2017-01-20 23:06:59 +01:00
parent f4ce2dbeb3
commit 32f9171d01
16 changed files with 366 additions and 83 deletions

View File

@@ -46,7 +46,7 @@ namespace WireMock.Net.ConsoleApplication
.WithBody(@"{ ""result"": ""data posted with FUNC 201""}")); .WithBody(@"{ ""result"": ""data posted with FUNC 201""}"));
server server
.Given(Request.Create().WithUrl("/data").UsingPost()) .Given(Request.Create().WithUrl("/data", "/ax").UsingPost())
.RespondWith(Response.Create() .RespondWith(Response.Create()
.WithStatusCode(201) .WithStatusCode(201)
.WithHeader("Content-Type", "application/json") .WithHeader("Content-Type", "application/json")

View File

@@ -17,7 +17,7 @@ namespace WireMock.Matchers
/// Initializes a new instance of the <see cref="RegexMatcher"/> class. /// Initializes a new instance of the <see cref="RegexMatcher"/> class.
/// </summary> /// </summary>
/// <param name="pattern">The pattern.</param> /// <param name="pattern">The pattern.</param>
public RegexMatcher([NotNull] string pattern) public RegexMatcher([NotNull, RegexPattern] string pattern)
{ {
Check.NotNull(pattern, nameof(pattern)); Check.NotNull(pattern, nameof(pattern));

View File

@@ -0,0 +1,18 @@
namespace WireMock.Matchers.Request
{
/// <summary>
/// CompositeMatcherType
/// </summary>
public enum CompositeMatcherType
{
/// <summary>
/// And
/// </summary>
And = 0,
/// <summary>
/// Or
/// </summary>
Or = 1
}
}

View File

@@ -10,7 +10,12 @@ namespace WireMock.Matchers.Request
public class RequestMessageBodyMatcher : IRequestMatcher public class RequestMessageBodyMatcher : IRequestMatcher
{ {
/// <summary> /// <summary>
/// The bodyRegex. /// The body.
/// </summary>
private readonly string _body;
/// <summary>
/// The body as byte[].
/// </summary> /// </summary>
private readonly byte[] _bodyData; private readonly byte[] _bodyData;
@@ -35,10 +40,10 @@ namespace WireMock.Matchers.Request
/// <param name="body"> /// <param name="body">
/// The body Regex pattern. /// The body Regex pattern.
/// </param> /// </param>
public RequestMessageBodyMatcher([NotNull, RegexPattern] string body) public RequestMessageBodyMatcher([NotNull] string body)
{ {
Check.NotNull(body, nameof(body)); Check.NotNull(body, nameof(body));
_matcher = new RegexMatcher(body); _body = body;
} }
/// <summary> /// <summary>
@@ -101,6 +106,9 @@ namespace WireMock.Matchers.Request
if (_matcher != null) if (_matcher != null)
return _matcher.IsMatch(requestMessage.Body); return _matcher.IsMatch(requestMessage.Body);
if (_body != null)
return requestMessage.Body == _body;
if (_bodyData != null) if (_bodyData != null)
return requestMessage.BodyAsBytes == _bodyData; return requestMessage.BodyAsBytes == _bodyData;

View File

@@ -1,13 +1,16 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using JetBrains.Annotations;
namespace WireMock.Matchers.Request namespace WireMock.Matchers.Request
{ {
/// <summary> /// <summary>
/// The composite request matcher. /// The composite request matcher.
/// </summary> /// </summary>
public abstract class RequestMessageCompositeMatcher : IRequestMatcher public class RequestMessageCompositeMatcher : IRequestMatcher
{ {
private readonly CompositeMatcherType _type;
/// <summary> /// <summary>
/// Gets the request matchers. /// Gets the request matchers.
/// </summary> /// </summary>
@@ -17,14 +20,13 @@ namespace WireMock.Matchers.Request
public IEnumerable<IRequestMatcher> RequestMatchers { get; } public IEnumerable<IRequestMatcher> RequestMatchers { get; }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RequestMessageCompositeMatcher"/> class. /// Initializes a new instance of the <see cref="RequestMessageCompositeMatcher"/> class.
/// The constructor.
/// </summary> /// </summary>
/// <param name="requestMatchers"> /// <param name="requestMatchers">The request matchers.</param>
/// The <see cref="IEnumerable&lt;IRequestMatcher&gt;"/> request matchers. /// <param name="type">The CompositeMatcherType type (Defaults to 'And')</param>
/// </param> public RequestMessageCompositeMatcher([NotNull] IEnumerable<IRequestMatcher> requestMatchers, CompositeMatcherType type = CompositeMatcherType.And)
protected RequestMessageCompositeMatcher(IEnumerable<IRequestMatcher> requestMatchers)
{ {
_type = type;
RequestMatchers = requestMatchers; RequestMatchers = requestMatchers;
} }
@@ -37,7 +39,9 @@ namespace WireMock.Matchers.Request
/// </returns> /// </returns>
public bool IsMatch(RequestMessage requestMessage) public bool IsMatch(RequestMessage requestMessage)
{ {
return RequestMatchers.All(spec => spec.IsMatch(requestMessage)); return _type == CompositeMatcherType.And ?
RequestMatchers.All(spec => spec.IsMatch(requestMessage)) :
RequestMatchers.Any(spec => spec.IsMatch(requestMessage));
} }
} }
} }

View File

@@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.RegularExpressions;
using JetBrains.Annotations; using JetBrains.Annotations;
using WireMock.Validation; using WireMock.Validation;
@@ -17,9 +16,9 @@ namespace WireMock.Matchers.Request
private readonly string _name; private readonly string _name;
/// <summary> /// <summary>
/// The patternRegex. /// The matcher.
/// </summary> /// </summary>
private readonly Regex _patternRegex; private readonly IMatcher _matcher;
/// <summary> /// <summary>
/// The header function /// The header function
@@ -29,17 +28,16 @@ namespace WireMock.Matchers.Request
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RequestMessageHeaderMatcher"/> class. /// Initializes a new instance of the <see cref="RequestMessageHeaderMatcher"/> class.
/// </summary> /// </summary>
/// <param name="name"> /// <param name="name">The name.</param>
/// The name. /// <param name="pattern">The pattern.</param>
/// </param>
/// <param name="pattern">
/// The pattern.
/// </param>
/// <param name="ignoreCase">The ignoreCase.</param> /// <param name="ignoreCase">The ignoreCase.</param>
public RequestMessageHeaderMatcher([NotNull] string name, [NotNull, RegexPattern] string pattern, bool ignoreCase = true) public RequestMessageHeaderMatcher([NotNull] string name, [NotNull] string pattern, bool ignoreCase = true)
{ {
Check.NotNull(name, nameof(name));
Check.NotNull(pattern, nameof(pattern));
_name = name; _name = name;
_patternRegex = ignoreCase ? new Regex(pattern, RegexOptions.IgnoreCase) : new Regex(pattern); _matcher = new WildcardMatcher(pattern, ignoreCase);
} }
/// <summary> /// <summary>
@@ -63,11 +61,11 @@ namespace WireMock.Matchers.Request
/// </returns> /// </returns>
public bool IsMatch(RequestMessage requestMessage) public bool IsMatch(RequestMessage requestMessage)
{ {
if (_patternRegex == null) if (_headerFunc != null)
return _headerFunc(requestMessage.Headers); return _headerFunc(requestMessage.Headers);
string headerValue = requestMessage.Headers[_name]; string headerValue = requestMessage.Headers[_name];
return _patternRegex.IsMatch(headerValue); return _matcher.IsMatch(headerValue);
} }
} }
} }

View File

@@ -1,5 +1,4 @@
using System; using System;
using System.Text.RegularExpressions;
using JetBrains.Annotations; using JetBrains.Annotations;
using WireMock.Validation; using WireMock.Validation;
@@ -11,32 +10,45 @@ namespace WireMock.Matchers.Request
public class RequestMessagePathMatcher : IRequestMatcher public class RequestMessagePathMatcher : IRequestMatcher
{ {
/// <summary> /// <summary>
/// The pathRegex. /// The matcher.
/// </summary> /// </summary>
private readonly Regex _pathRegex; private readonly string _path;
/// <summary> /// <summary>
/// The url function /// The matcher.
/// </summary>
private readonly IMatcher _matcher;
/// <summary>
/// The path function
/// </summary> /// </summary>
private readonly Func<string, bool> _pathFunc; private readonly Func<string, bool> _pathFunc;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RequestMessagePathMatcher"/> class. /// Initializes a new instance of the <see cref="RequestMessagePathMatcher"/> class.
/// </summary> /// </summary>
/// <param name="path"> /// <param name="path">The path.</param>
/// The path Regex pattern. public RequestMessagePathMatcher([NotNull] string path)
/// </param>
public RequestMessagePathMatcher([NotNull, RegexPattern] string path)
{ {
Check.NotNull(path, nameof(path)); Check.NotNull(path, nameof(path));
_pathRegex = new Regex(path); _path = path;
}
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessagePathMatcher"/> class.
/// </summary>
/// <param name="matcher">The matcher.</param>
public RequestMessagePathMatcher([NotNull] IMatcher matcher)
{
Check.NotNull(matcher, nameof(matcher));
_matcher = matcher;
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RequestMessagePathMatcher"/> class. /// Initializes a new instance of the <see cref="RequestMessagePathMatcher"/> class.
/// </summary> /// </summary>
/// <param name="func"> /// <param name="func">
/// The url func. /// The path func.
/// </param> /// </param>
public RequestMessagePathMatcher([NotNull] Func<string, bool> func) public RequestMessagePathMatcher([NotNull] Func<string, bool> func)
{ {
@@ -53,7 +65,16 @@ namespace WireMock.Matchers.Request
/// </returns> /// </returns>
public bool IsMatch(RequestMessage requestMessage) public bool IsMatch(RequestMessage requestMessage)
{ {
return _pathRegex?.IsMatch(requestMessage.Path) ?? _pathFunc(requestMessage.Path); if (_path != null)
return string.CompareOrdinal(_path, requestMessage.Path) == 0;
if (_matcher != null)
return _matcher.IsMatch(requestMessage.Path);
if (_pathFunc != null)
return _pathFunc(requestMessage.Path);
return false;
} }
} }
} }

View File

@@ -1,5 +1,4 @@
using System; using System;
using System.Text.RegularExpressions;
using JetBrains.Annotations; using JetBrains.Annotations;
using WireMock.Validation; using WireMock.Validation;
@@ -11,9 +10,9 @@ namespace WireMock.Matchers.Request
public class RequestMessageUrlMatcher : IRequestMatcher public class RequestMessageUrlMatcher : IRequestMatcher
{ {
/// <summary> /// <summary>
/// The urlRegex. /// The matcher.
/// </summary> /// </summary>
private readonly Regex _urlRegex; private readonly IMatcher _matcher;
/// <summary> /// <summary>
/// The url function /// The url function
@@ -23,13 +22,20 @@ namespace WireMock.Matchers.Request
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RequestMessageUrlMatcher"/> class. /// Initializes a new instance of the <see cref="RequestMessageUrlMatcher"/> class.
/// </summary> /// </summary>
/// <param name="url"> /// <param name="url">The url.</param>
/// The url Regex pattern. public RequestMessageUrlMatcher([NotNull] string url) : this(new WildcardMatcher(url))
/// </param>
public RequestMessageUrlMatcher([NotNull, RegexPattern] string url)
{ {
Check.NotNull(url, nameof(url)); _matcher = new WildcardMatcher(url);
_urlRegex = new Regex(url); }
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageUrlMatcher"/> class.
/// </summary>
/// <param name="matcher">The matcher.</param>
public RequestMessageUrlMatcher([NotNull] IMatcher matcher)
{
Check.NotNull(matcher, nameof(matcher));
_matcher = matcher;
} }
/// <summary> /// <summary>
@@ -38,7 +44,7 @@ namespace WireMock.Matchers.Request
/// <param name="func"> /// <param name="func">
/// The url func. /// The url func.
/// </param> /// </param>
public RequestMessageUrlMatcher(Func<string, bool> func) public RequestMessageUrlMatcher([NotNull] Func<string, bool> func)
{ {
Check.NotNull(func, nameof(func)); Check.NotNull(func, nameof(func));
_urlFunc = func; _urlFunc = func;
@@ -53,7 +59,13 @@ namespace WireMock.Matchers.Request
/// </returns> /// </returns>
public bool IsMatch(RequestMessage requestMessage) public bool IsMatch(RequestMessage requestMessage)
{ {
return _urlRegex?.IsMatch(requestMessage.Url) ?? _urlFunc(requestMessage.Url); if (_matcher != null)
return _matcher.IsMatch(requestMessage.Path);
if (_urlFunc != null)
return _urlFunc(requestMessage.Url);
return false;
} }
} }
} }

View File

@@ -0,0 +1,102 @@
using JetBrains.Annotations;
using WireMock.Validation;
namespace WireMock.Matchers
{
/// <summary>
/// WildcardMatcher
/// </summary>
/// <seealso cref="WireMock.Matchers.IMatcher" />
public class WildcardMatcher : IMatcher
{
private readonly string _pattern;
private readonly bool _ignoreCase;
/// <summary>
/// Initializes a new instance of the <see cref="RegexMatcher"/> class.
/// </summary>
/// <param name="pattern">The pattern.</param>
/// <param name="ignoreCase">IgnoreCase</param>
public WildcardMatcher([NotNull] string pattern, bool ignoreCase = false)
{
Check.NotNull(pattern, nameof(pattern));
_pattern = pattern;
_ignoreCase = ignoreCase;
}
/// <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 MatchWildcardString(_pattern, input);
}
/// <summary>
/// Copy/paste from http://www.codeproject.com/Tips/57304/Use-wildcard-characters-and-to-compare-strings
/// </summary>
private bool MatchWildcardString(string pattern, string input)
{
if (input != null && _ignoreCase)
input = input.ToLower();
if (pattern != null && _ignoreCase)
pattern = pattern.ToLower();
if (string.CompareOrdinal(pattern, input) == 0)
{
return true;
}
if (string.IsNullOrEmpty(input))
{
return string.IsNullOrEmpty(pattern.Trim('*'));
}
if (pattern.Length == 0)
{
return false;
}
if (pattern[0] == '?')
{
return MatchWildcardString(pattern.Substring(1), input.Substring(1));
}
if (pattern[pattern.Length - 1] == '?')
{
return MatchWildcardString(pattern.Substring(0, pattern.Length - 1), input.Substring(0, input.Length - 1));
}
if (pattern[0] == '*')
{
if (MatchWildcardString(pattern.Substring(1), input))
{
return true;
}
return MatchWildcardString(pattern, input.Substring(1));
}
if (pattern[pattern.Length - 1] == '*')
{
if (MatchWildcardString(pattern.Substring(0, pattern.Length - 1), input))
{
return true;
}
return MatchWildcardString(pattern, input.Substring(0, input.Length - 1));
}
if (pattern[0] == input[0])
{
return MatchWildcardString(pattern.Substring(1), input.Substring(1));
}
return false;
}
}
}

View File

@@ -34,6 +34,6 @@ namespace WireMock.RequestBuilders
/// <returns> /// <returns>
/// The <see cref="IHeadersRequestBuilder"/>. /// The <see cref="IHeadersRequestBuilder"/>.
/// </returns> /// </returns>
IHeadersRequestBuilder WithHeader([NotNull] Func<IDictionary<string, string>, bool> func); IHeadersRequestBuilder WithHeader([NotNull] params Func<IDictionary<string, string>, bool>[] func);
} }
} }

View File

@@ -1,5 +1,6 @@
using System; using System;
using JetBrains.Annotations; using JetBrains.Annotations;
using WireMock.Matchers;
namespace WireMock.RequestBuilders namespace WireMock.RequestBuilders
{ {
@@ -8,32 +9,46 @@ namespace WireMock.RequestBuilders
/// </summary> /// </summary>
public interface IUrlAndPathRequestBuilder : IVerbRequestBuilder public interface IUrlAndPathRequestBuilder : IVerbRequestBuilder
{ {
/// <summary>
/// The with url.
/// </summary>
/// <param name="matcher">The matcher.</param>
/// <returns>The <see cref="IUrlAndPathRequestBuilder"/>.</returns>
IUrlAndPathRequestBuilder WithUrl([NotNull] IMatcher matcher);
/// <summary> /// <summary>
/// The with url. /// The with url.
/// </summary> /// </summary>
/// <param name="url">The url.</param> /// <param name="url">The url.</param>
/// <returns>The <see cref="IUrlAndPathRequestBuilder"/>.</returns> /// <returns>The <see cref="IUrlAndPathRequestBuilder"/>.</returns>
IUrlAndPathRequestBuilder WithUrl([NotNull] string url); IUrlAndPathRequestBuilder WithUrl([NotNull] params string[] url);
/// <summary> /// <summary>
/// The with url. /// The with url.
/// </summary> /// </summary>
/// <param name="func">The url func.</param> /// <param name="func">The url func.</param>
/// <returns>The <see cref="IUrlAndPathRequestBuilder"/>.</returns> /// <returns>The <see cref="IUrlAndPathRequestBuilder"/>.</returns>
IUrlAndPathRequestBuilder WithUrl([NotNull] Func<string, bool> func); IUrlAndPathRequestBuilder WithUrl([NotNull] params Func<string, bool>[] func);
/// <summary>
/// The with path.
/// </summary>
/// <param name="matcher">The matcher.</param>
/// <returns>The <see cref="IUrlAndPathRequestBuilder"/>.</returns>
IUrlAndPathRequestBuilder WithPath([NotNull] IMatcher matcher);
/// <summary> /// <summary>
/// The with path. /// The with path.
/// </summary> /// </summary>
/// <param name="path">The path.</param> /// <param name="path">The path.</param>
/// <returns>The <see cref="IUrlAndPathRequestBuilder"/>.</returns> /// <returns>The <see cref="IUrlAndPathRequestBuilder"/>.</returns>
IUrlAndPathRequestBuilder WithPath([NotNull] string path); IUrlAndPathRequestBuilder WithPath([NotNull] params string[] path);
/// <summary> /// <summary>
/// The with path. /// The with path.
/// </summary> /// </summary>
/// <param name="func">The path func.</param> /// <param name="func">The path func.</param>
/// <returns>The <see cref="IUrlAndPathRequestBuilder"/>.</returns> /// <returns>The <see cref="IUrlAndPathRequestBuilder"/>.</returns>
IUrlAndPathRequestBuilder WithPath([NotNull] Func<string, bool> func); IUrlAndPathRequestBuilder WithPath([NotNull] params Func<string, bool>[] func);
} }
} }

View File

@@ -34,44 +34,76 @@ namespace WireMock.RequestBuilders
/// <summary> /// <summary>
/// The with url. /// The with url.
/// </summary> /// </summary>
/// <param name="url">The url.</param> /// <param name="matcher">The matcher.</param>
/// <returns>The <see cref="IUrlAndPathRequestBuilder"/>.</returns> /// <returns>The <see cref="IUrlAndPathRequestBuilder"/>.</returns>
public IUrlAndPathRequestBuilder WithUrl(string url) public IUrlAndPathRequestBuilder WithUrl(IMatcher matcher)
{ {
_requestMatchers.Add(new RequestMessageUrlMatcher(url)); _requestMatchers.Add(new RequestMessageUrlMatcher(matcher));
return this; return this;
} }
/// <summary> /// <summary>
/// The with url. /// The with url.
/// </summary> /// </summary>
/// <param name="func">The url func.</param> /// <param name="urls">The urls.</param>
/// <returns>The <see cref="IUrlAndPathRequestBuilder"/>.</returns> /// <returns>The <see cref="IUrlAndPathRequestBuilder"/>.</returns>
public IUrlAndPathRequestBuilder WithUrl(Func<string, bool> func) public IUrlAndPathRequestBuilder WithUrl(params string[] urls)
{ {
_requestMatchers.Add(new RequestMessageUrlMatcher(func)); var or = new RequestMessageCompositeMatcher(urls.Select(url => new RequestMessageUrlMatcher(url)), CompositeMatcherType.Or);
_requestMatchers.Add(or);
return this;
}
/// <summary>
/// The with url.
/// </summary>
/// <param name="funcs">The url func.</param>
/// <returns>The <see cref="IUrlAndPathRequestBuilder"/>.</returns>
public IUrlAndPathRequestBuilder WithUrl(params Func<string, bool>[] funcs)
{
var or = new RequestMessageCompositeMatcher(funcs.Select(func => new RequestMessageUrlMatcher(func)), CompositeMatcherType.Or);
_requestMatchers.Add(or);
return this;
}
/// <summary>
/// The with url.
/// </summary>
/// <param name="matcher">The matcher.</param>
/// <returns>The <see cref="IUrlAndPathRequestBuilder"/>.</returns>
public IUrlAndPathRequestBuilder WithPath(IMatcher matcher)
{
_requestMatchers.Add(new RequestMessagePathMatcher(matcher));
return this; return this;
} }
/// <summary> /// <summary>
/// The with path. /// The with path.
/// </summary> /// </summary>
/// <param name="path">The path.</param> /// <param name="paths">The path.</param>
/// <returns>The <see cref="IUrlAndPathRequestBuilder"/>.</returns> /// <returns>The <see cref="IUrlAndPathRequestBuilder"/>.</returns>
public IUrlAndPathRequestBuilder WithPath(string path) public IUrlAndPathRequestBuilder WithPath(params string[] paths)
{ {
_requestMatchers.Add(new RequestMessagePathMatcher(path)); var or = new RequestMessageCompositeMatcher(paths.Select(path => new RequestMessageUrlMatcher(path)), CompositeMatcherType.Or);
_requestMatchers.Add(or);
return this; return this;
} }
/// <summary> /// <summary>
/// The with path. /// The with path.
/// </summary> /// </summary>
/// <param name="func">The path func.</param> /// <param name="funcs">The path func.</param>
/// <returns>The <see cref="IUrlAndPathRequestBuilder"/>.</returns> /// <returns>The <see cref="IUrlAndPathRequestBuilder"/>.</returns>
public IUrlAndPathRequestBuilder WithPath(Func<string, bool> func) public IUrlAndPathRequestBuilder WithPath(params Func<string, bool>[] funcs)
{ {
_requestMatchers.Add(new RequestMessagePathMatcher(func)); var or = new RequestMessageCompositeMatcher(funcs.Select(func => new RequestMessageUrlMatcher(func)), CompositeMatcherType.Or);
_requestMatchers.Add(or);
return this; return this;
} }
@@ -295,15 +327,15 @@ namespace WireMock.RequestBuilders
/// <summary> /// <summary>
/// The with header. /// The with header.
/// </summary> /// </summary>
/// <param name="func"> /// <param name="funcs">The func.</param>
/// The func.
/// </param>
/// <returns> /// <returns>
/// The <see cref="IHeadersRequestBuilder"/>. /// The <see cref="IHeadersRequestBuilder"/>.
/// </returns> /// </returns>
public IHeadersRequestBuilder WithHeader(Func<IDictionary<string, string>, bool> func) public IHeadersRequestBuilder WithHeader(params Func<IDictionary<string, string>, bool>[] funcs)
{ {
_requestMatchers.Add(new RequestMessageHeaderMatcher(func)); var or = new RequestMessageCompositeMatcher(funcs.Select(func => new RequestMessageHeaderMatcher(func)), CompositeMatcherType.Or);
_requestMatchers.Add(or);
return this; return this;
} }
} }

View File

@@ -6,6 +6,7 @@ using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using NFluent; using NFluent;
using NUnit.Framework; using NUnit.Framework;
using WireMock.Matchers;
using WireMock.RequestBuilders; using WireMock.RequestBuilders;
using WireMock.ResponseBuilders; using WireMock.ResponseBuilders;
using WireMock.Server; using WireMock.Server;
@@ -113,7 +114,7 @@ namespace WireMock.Net.Tests
await new HttpClient().GetAsync("http://localhost:" + _server.Port + "/bar"); await new HttpClient().GetAsync("http://localhost:" + _server.Port + "/bar");
// then // then
var result = _server.SearchLogsFor(Request.Create().WithUrl("/b.*")).ToList(); var result = _server.SearchLogsFor(Request.Create().WithUrl(new RegexMatcher("^/b.*"))).ToList();
Check.That(result).HasSize(1); Check.That(result).HasSize(1);
var requestLogged = result.First(); var requestLogged = result.First();
@@ -195,7 +196,7 @@ namespace WireMock.Net.Tests
.WithUrl("/*")) .WithUrl("/*"))
.RespondWith(Response.Create() .RespondWith(Response.Create()
.WithBody(@"{ msg: ""Hello world!""}") .WithBody(@"{ msg: ""Hello world!""}")
.WithDelay(TimeSpan.FromMilliseconds(2000))); .WithDelay(TimeSpan.FromMilliseconds(200)));
// when // when
var watch = new Stopwatch(); var watch = new Stopwatch();
@@ -204,7 +205,7 @@ namespace WireMock.Net.Tests
watch.Stop(); watch.Stop();
// then // then
Check.That(watch.ElapsedMilliseconds).IsGreaterThan(2000); Check.That(watch.ElapsedMilliseconds).IsGreaterThan(200);
} }
[Test] [Test]
@@ -212,12 +213,10 @@ namespace WireMock.Net.Tests
{ {
// given // given
_server = FluentMockServer.Start(); _server = FluentMockServer.Start();
_server.AddRequestProcessingDelay(TimeSpan.FromMilliseconds(2000)); _server.AddRequestProcessingDelay(TimeSpan.FromMilliseconds(200));
_server _server
.Given(Request.Create() .Given(Request.Create().WithUrl("/*"))
.WithUrl("/*")) .RespondWith(Response.Create().WithBody(@"{ msg: ""Hello world!""}"));
.RespondWith(Response.Create()
.WithBody(@"{ msg: ""Hello world!""}"));
// when // when
var watch = new Stopwatch(); var watch = new Stopwatch();
@@ -226,7 +225,7 @@ namespace WireMock.Net.Tests
watch.Stop(); watch.Stop();
// then // then
Check.That(watch.ElapsedMilliseconds).IsGreaterThan(2000); Check.That(watch.ElapsedMilliseconds).IsGreaterThan(200);
} }
[TearDown] [TearDown]

View File

@@ -26,11 +26,25 @@ namespace WireMock.Net.Tests
Check.That(spec.IsMatch(request)).IsTrue(); Check.That(spec.IsMatch(request)).IsTrue();
} }
[Test]
public void Should_specify_requests_matching_given_urls()
{
var requestBuilder = Request.Create().WithUrl("/x1", "/x2");
string bodyAsString = "whatever";
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
var request1 = new RequestMessage(new Uri("http://localhost/x1"), "blabla", body, bodyAsString);
var request2 = new RequestMessage(new Uri("http://localhost/x2"), "blabla", body, bodyAsString);
Check.That(requestBuilder.IsMatch(request1)).IsTrue();
Check.That(requestBuilder.IsMatch(request2)).IsTrue();
}
[Test] [Test]
public void Should_specify_requests_matching_given_url_prefix() public void Should_specify_requests_matching_given_url_prefix()
{ {
// given // given
var spec = Request.Create().WithUrl("/foo*"); var spec = Request.Create().WithUrl(new RegexMatcher("^/foo"));
// when // when
string bodyAsString = "whatever"; string bodyAsString = "whatever";
@@ -230,7 +244,7 @@ namespace WireMock.Net.Tests
// when // when
string bodyAsString = "whatever"; string bodyAsString = "whatever";
byte[] body = Encoding.UTF8.GetBytes(bodyAsString); byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "TaTaTa" } }); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "TaTa" } });
// then // then
Check.That(spec.IsMatch(request)).IsTrue(); Check.That(spec.IsMatch(request)).IsTrue();
@@ -252,10 +266,10 @@ namespace WireMock.Net.Tests
} }
[Test] [Test]
public void Should_specify_requests_matching_given_body_as_regex() public void Should_specify_requests_matching_given_body_as_wildcard()
{ {
// given // given
var spec = Request.Create().WithUrl("/foo").UsingAnyVerb().WithBody("H.*o"); var spec = Request.Create().WithUrl("/foo").UsingAnyVerb().WithBody(new WildcardMatcher("H*o*"));
// when // when
string bodyAsString = "Hello world!"; string bodyAsString = "Hello world!";

View File

@@ -0,0 +1,59 @@
using NUnit.Framework;
using WireMock.Matchers;
namespace WireMock.Net.Tests
{
[TestFixture]
public class WildcardMatcherTest
{
[Test]
public void WildcardMatcher_patterns_positive()
{
var tests = new[]
{
new { p = "*", i = "" },
new { p = "?", i = " " },
new { p = "*", i = "a" },
new { p = "*", i = "ab" },
new { p = "?", i = "a" },
new { p = "*?", i = "abc" },
new { p = "?*", i = "abc" },
new { p = "abc", i = "abc" },
new { p = "abc*", i = "abc" },
new { p = "abc*", i = "abcd" },
new { p = "*abc*", i = "abc" },
new { p = "*a*bc*", i = "abc" },
new { p = "*a*b?", i = "aXXXbc" }
};
foreach (var test in tests)
{
var matcher = new WildcardMatcher(test.p);
Assert.IsTrue(matcher.IsMatch(test.i), "p = " + test.p + ", i = " + test.i);
}
}
[Test]
public void WildcardMatcher_patterns_negative()
{
var tests = new[]
{
new { p = "*a", i = ""},
new { p = "a*", i = ""},
new { p = "?", i = ""},
new { p = "*b*", i = "a"},
new { p = "b*a", i = "ab"},
new { p = "??", i = "a"},
new { p = "*?", i = ""},
new { p = "??*", i = "a"},
new { p = "*abc", i = "abX"},
new { p = "*abc*", i = "Xbc"},
new { p = "*a*bc*", i = "ac"}
};
foreach (var test in tests)
{
var matcher = new WildcardMatcher(test.p);
Assert.IsFalse(matcher.IsMatch(test.i), "p = " + test.p + ", i = " + test.i);
}
}
}
}

View File

@@ -67,6 +67,7 @@
<Compile Include="RequestTests.cs" /> <Compile Include="RequestTests.cs" />
<Compile Include="RequestMessageTests.cs" /> <Compile Include="RequestMessageTests.cs" />
<Compile Include="ResponseTests.cs" /> <Compile Include="ResponseTests.cs" />
<Compile Include="WildcardMatcherTest.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="packages.config" /> <None Include="packages.config" />