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

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

View File

@@ -26,11 +26,25 @@ namespace WireMock.Net.Tests
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]
public void Should_specify_requests_matching_given_url_prefix()
{
// given
var spec = Request.Create().WithUrl("/foo*");
var spec = Request.Create().WithUrl(new RegexMatcher("^/foo"));
// when
string bodyAsString = "whatever";
@@ -230,7 +244,7 @@ namespace WireMock.Net.Tests
// when
string bodyAsString = "whatever";
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
Check.That(spec.IsMatch(request)).IsTrue();
@@ -252,10 +266,10 @@ namespace WireMock.Net.Tests
}
[Test]
public void Should_specify_requests_matching_given_body_as_regex()
public void Should_specify_requests_matching_given_body_as_wildcard()
{
// given
var spec = Request.Create().WithUrl("/foo").UsingAnyVerb().WithBody("H.*o");
var spec = Request.Create().WithUrl("/foo").UsingAnyVerb().WithBody(new WildcardMatcher("H*o*"));
// when
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="RequestMessageTests.cs" />
<Compile Include="ResponseTests.cs" />
<Compile Include="WildcardMatcherTest.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />