Partial matching

This commit is contained in:
Stef Heyenrath
2017-02-04 17:30:16 +01:00
parent 9b99a7f26b
commit ec2d105db2
19 changed files with 343 additions and 83 deletions

View File

@@ -18,6 +18,8 @@ namespace WireMock.Net.ConsoleApplication
var server = FluentMockServer.StartWithAdminInterface(url1, url2, url3); var server = FluentMockServer.StartWithAdminInterface(url1, url2, url3);
Console.WriteLine("FluentMockServer listening at {0}", string.Join(" and ", server.Urls)); Console.WriteLine("FluentMockServer listening at {0}", string.Join(" and ", server.Urls));
server.AllowPartialMapping();
server server
.Given(Request.Create().WithPath(p => p.Contains("x")).UsingGet()) .Given(Request.Create().WithPath(p => p.Contains("x")).UsingGet())
.AtPriority(4) .AtPriority(4)
@@ -26,20 +28,6 @@ namespace WireMock.Net.ConsoleApplication
.WithHeader("Content-Type", "application/json") .WithHeader("Content-Type", "application/json")
.WithBody(@"{ ""result"": ""Contains x with FUNC 200""}")); .WithBody(@"{ ""result"": ""Contains x with FUNC 200""}"));
// http://localhost:8080/gffgfgf/sddsds?start=1000&stop=1&stop=2
server
.Given(Request.Create().WithPath("/*").UsingGet().WithParam("start"))
.WithGuid(Guid.Parse("90356dba-b36c-469a-a17e-669cd84f1f05"))
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithHeader("Content-Type", "application/json")
.WithHeader("Transformed-Postman-Token", "token is {{request.headers.Postman-Token}}")
.WithBody(@"{""msg"": ""Hello world, {{request.path}}, bykey={{request.query.start}}, bykey={{request.query.stop}}, byidx0={{request.query.stop.[0]}}, byidx1={{request.query.stop.[1]}}"" }")
.WithTransformer()
.WithDelay(1000)
.WithDelay(TimeSpan.FromMilliseconds(100))
);
server server
.Given(Request.Create().WithPath("/data").UsingPost().WithBody(b => b.Contains("e"))) .Given(Request.Create().WithPath("/data").UsingPost().WithBody(b => b.Contains("e")))
.RespondWith(Response.Create() .RespondWith(Response.Create()
@@ -80,6 +68,24 @@ namespace WireMock.Net.ConsoleApplication
.RespondWith(Response.Create().WithDelay(TimeSpan.FromSeconds(1)) .RespondWith(Response.Create().WithDelay(TimeSpan.FromSeconds(1))
.WithStatusCode(200)); .WithStatusCode(200));
server
.Given(Request.Create().WithPath("/partial").UsingGet().WithHeader("p", "p"))
.RespondWith(Response.Create().WithStatusCode(200).WithBody("partial = 200"));
// http://localhost:8080/any/any?start=1000&stop=1&stop=2
server
.Given(Request.Create().WithPath("/*").UsingGet())
.WithGuid(Guid.Parse("90356dba-b36c-469a-a17e-669cd84f1f05"))
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithHeader("Content-Type", "application/json")
.WithHeader("Transformed-Postman-Token", "token is {{request.headers.Postman-Token}}")
.WithBody(@"{""msg"": ""Hello world CATCH-ALL on /*, {{request.path}}, bykey={{request.query.start}}, bykey={{request.query.stop}}, byidx0={{request.query.stop.[0]}}, byidx1={{request.query.stop.[1]}}"" }")
.WithTransformer()
.WithDelay(1000)
.WithDelay(TimeSpan.FromMilliseconds(100))
);
Console.WriteLine("Press any key to stop the server"); Console.WriteLine("Press any key to stop the server");
Console.ReadKey(); Console.ReadKey();

View File

@@ -30,5 +30,13 @@ namespace WireMock.Admin.Requests
/// The response. /// The response.
/// </value> /// </value>
public LogResponseModel Response { get; set; } public LogResponseModel Response { get; set; }
/// <summary>
/// Gets or sets the request match result.
/// </summary>
/// <value>
/// The request match result.
/// </value>
public LogRequestMatchModel RequestMatchResult { get; set; }
} }
} }

View File

@@ -0,0 +1,40 @@
namespace WireMock.Admin.Requests
{
/// <summary>
/// LogRequestMatchModel
/// </summary>
public class LogRequestMatchModel
{
/// <summary>
/// Gets or sets the number of matches.
/// </summary>
/// <value>
/// The number of matches.
/// </value>
public int Matched { get; set; }
/// <summary>
/// Gets or sets the total number of matches.
/// </summary>
/// <value>
/// The total number of matches.
/// </value>
public int Total { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this instance is perfect match.
/// </summary>
/// <value>
/// <c>true</c> if this instance is perfect match; otherwise, <c>false</c>.
/// </value>
public bool IsPerfectMatch => Matched == Total;
/// <summary>
/// Gets the match percentage.
/// </summary>
/// <value>
/// The match percentage.
/// </value>
public double MatchPercentage => Total == 0 ? 100 : 100.0 * Matched / Total;
}
}

View File

@@ -1,4 +1,5 @@
using System; using System;
using WireMock.Matchers.Request;
namespace WireMock.Logging namespace WireMock.Logging
{ {
@@ -30,5 +31,13 @@ namespace WireMock.Logging
/// The response message. /// The response message.
/// </value> /// </value>
public ResponseMessage ResponseMessage { get; set; } public ResponseMessage ResponseMessage { get; set; }
/// <summary>
/// Gets or sets the request match result.
/// </summary>
/// <value>
/// The request match result.
/// </value>
public RequestMatchResult RequestMatchResult { get; set; }
} }
} }

View File

@@ -64,12 +64,14 @@ namespace WireMock
/// Determines whether the RequestMessage is handled. /// Determines whether the RequestMessage is handled.
/// </summary> /// </summary>
/// <param name="requestMessage">The request message.</param> /// <param name="requestMessage">The request message.</param>
/// <returns> /// <returns>The <see cref="RequestMatchResult"/>.</returns>
/// <c>true</c> if RequestMessage is handled; otherwise, <c>false</c>. public RequestMatchResult IsRequestHandled(RequestMessage requestMessage)
/// </returns>
public bool IsRequestHandled(RequestMessage requestMessage)
{ {
return RequestMatcher.IsMatch(requestMessage); var result = new RequestMatchResult();
RequestMatcher.IsMatch(requestMessage, result);
return result;
} }
} }
} }

View File

@@ -11,9 +11,10 @@ namespace WireMock.Matchers.Request
/// Determines whether the specified RequestMessage is match. /// Determines whether the specified RequestMessage is match.
/// </summary> /// </summary>
/// <param name="requestMessage">The RequestMessage.</param> /// <param name="requestMessage">The RequestMessage.</param>
/// <param name="requestMatchResult">The RequestMatchResult.</param>
/// <returns> /// <returns>
/// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>. /// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>.
/// </returns> /// </returns>
bool IsMatch([NotNull] RequestMessage requestMessage); bool IsMatch([NotNull] RequestMessage requestMessage, [NotNull] RequestMatchResult requestMatchResult);
} }
} }

View File

@@ -1,9 +1,11 @@
namespace WireMock.Matchers.Request using System;
namespace WireMock.Matchers.Request
{ {
/// <summary> /// <summary>
/// RequestMatchResult /// RequestMatchResult
/// </summary> /// </summary>
public class RequestMatchResult public class RequestMatchResult : IComparable
{ {
/// <summary> /// <summary>
/// Gets or sets the number of matches. /// Gets or sets the number of matches.
@@ -27,6 +29,29 @@
/// <value> /// <value>
/// <c>true</c> if this instance is perfect match; otherwise, <c>false</c>. /// <c>true</c> if this instance is perfect match; otherwise, <c>false</c>.
/// </value> /// </value>
public bool IsPerfectMatch { get; set; } public bool IsPerfectMatch => Matched == Total;
/// <summary>
/// Gets the match percentage.
/// </summary>
/// <value>
/// The match percentage.
/// </value>
public double MatchPercentage => Total == 0 ? 100 : 100.0 * Matched / Total;
/// <summary>
/// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
/// </summary>
/// <param name="obj">An object to compare with this instance.</param>
/// <returns>
/// A value that indicates the relative order of the objects being compared. The return value has these meanings: Value Meaning Less than zero This instance precedes <paramref name="obj" /> in the sort order. Zero This instance occurs in the same position in the sort order as <paramref name="obj" />. Greater than zero This instance follows <paramref name="obj" /> in the sort order.
/// </returns>
/// <exception cref="System.NotImplementedException"></exception>
public int CompareTo(object obj)
{
var compareObj = (RequestMatchResult)obj;
return compareObj.MatchPercentage.CompareTo(MatchPercentage);
}
} }
} }

View File

@@ -98,10 +98,22 @@ namespace WireMock.Matchers.Request
/// Determines whether the specified RequestMessage is match. /// Determines whether the specified RequestMessage is match.
/// </summary> /// </summary>
/// <param name="requestMessage">The RequestMessage.</param> /// <param name="requestMessage">The RequestMessage.</param>
/// <param name="requestMatchResult">The RequestMatchResult.</param>
/// <returns> /// <returns>
/// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>. /// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>.
/// </returns> /// </returns>
public bool IsMatch(RequestMessage requestMessage) public bool IsMatch(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
{
bool isMatch = IsMatch(requestMessage);
if (isMatch)
requestMatchResult.Matched++;
requestMatchResult.Total++;
return isMatch;
}
private bool IsMatch(RequestMessage requestMessage)
{ {
if (Matcher != null) if (Matcher != null)
return Matcher.IsMatch(requestMessage.Body); return Matcher.IsMatch(requestMessage.Body);
@@ -113,10 +125,10 @@ namespace WireMock.Matchers.Request
return requestMessage.BodyAsBytes == _bodyData; return requestMessage.BodyAsBytes == _bodyData;
if (Func != null) if (Func != null)
return Func(requestMessage.Body); return requestMessage.Body != null && Func(requestMessage.Body);
if (DataFunc != null) if (DataFunc != null && requestMessage.BodyAsBytes != null)
return DataFunc(requestMessage.BodyAsBytes); return requestMessage.BodyAsBytes != null && DataFunc(requestMessage.BodyAsBytes);
return false; return false;
} }

View File

@@ -37,14 +37,32 @@ namespace WireMock.Matchers.Request
/// Determines whether the specified RequestMessage is match. /// Determines whether the specified RequestMessage is match.
/// </summary> /// </summary>
/// <param name="requestMessage">The RequestMessage.</param> /// <param name="requestMessage">The RequestMessage.</param>
/// <param name="requestMatchResult">The RequestMatchResult.</param>
/// <returns> /// <returns>
/// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>. /// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>.
/// </returns> /// </returns>
public virtual bool IsMatch(RequestMessage requestMessage) public bool IsMatch(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
{ {
return _type == CompositeMatcherType.And ? var list = new List<bool>();
RequestMatchers.All(matcher => matcher.IsMatch(requestMessage)) : if (_type == CompositeMatcherType.And)
RequestMatchers.Any(matcher => matcher.IsMatch(requestMessage)); {
foreach (var requestMatcher in RequestMatchers)
{
bool isMatch = requestMatcher.IsMatch(requestMessage, requestMatchResult);
list.Add(isMatch);
}
return list.All(match => match);
}
//var orRequestMatchResult = new RequestMatchResult();
foreach (var requestMatcher in RequestMatchers)
{
bool isMatch = requestMatcher.IsMatch(requestMessage, requestMatchResult);
list.Add(isMatch);
}
return list.Any(match => match);
} }
} }
} }

View File

@@ -25,7 +25,7 @@ namespace WireMock.Matchers.Request
/// The matchers. /// The matchers.
/// </value> /// </value>
public IMatcher[] Matchers { get; } public IMatcher[] Matchers { get; }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="RequestMessageCookieMatcher"/> class. /// Initializes a new instance of the <see cref="RequestMessageCookieMatcher"/> class.
/// </summary> /// </summary>
@@ -70,17 +70,32 @@ namespace WireMock.Matchers.Request
/// Determines whether the specified RequestMessage is match. /// Determines whether the specified RequestMessage is match.
/// </summary> /// </summary>
/// <param name="requestMessage">The RequestMessage.</param> /// <param name="requestMessage">The RequestMessage.</param>
/// <param name="requestMatchResult">The RequestMatchResult.</param>
/// <returns> /// <returns>
/// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>. /// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>.
/// </returns> /// </returns>
public bool IsMatch(RequestMessage requestMessage) public bool IsMatch(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
{
bool isMatch = IsMatch(requestMessage);
if (isMatch)
requestMatchResult.Matched++;
requestMatchResult.Total++;
return isMatch;
}
private bool IsMatch(RequestMessage requestMessage)
{ {
if (Funcs != null) if (Funcs != null)
return Funcs.Any(cf => cf(requestMessage.Cookies)); return requestMessage.Cookies != null && Funcs.Any(cf => cf(requestMessage.Cookies));
if (requestMessage.Cookies == null) if (requestMessage.Cookies == null)
return false; return false;
if (!requestMessage.Cookies.ContainsKey(Name))
return false;
string headerValue = requestMessage.Cookies[Name]; string headerValue = requestMessage.Cookies[Name];
return Matchers.Any(m => m.IsMatch(headerValue)); return Matchers.Any(m => m.IsMatch(headerValue));
} }

View File

@@ -70,17 +70,32 @@ namespace WireMock.Matchers.Request
/// Determines whether the specified RequestMessage is match. /// Determines whether the specified RequestMessage is match.
/// </summary> /// </summary>
/// <param name="requestMessage">The RequestMessage.</param> /// <param name="requestMessage">The RequestMessage.</param>
/// <param name="requestMatchResult">The RequestMatchResult.</param>
/// <returns> /// <returns>
/// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>. /// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>.
/// </returns> /// </returns>
public bool IsMatch(RequestMessage requestMessage) public bool IsMatch(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
{
bool isMatch = IsMatch(requestMessage);
if (isMatch)
requestMatchResult.Matched++;
requestMatchResult.Total++;
return isMatch;
}
private bool IsMatch(RequestMessage requestMessage)
{ {
if (Funcs != null) if (Funcs != null)
return Funcs.Any(hf => hf(requestMessage.Headers)); return requestMessage.Headers != null && Funcs.Any(hf => hf(requestMessage.Headers));
if (requestMessage.Headers == null) if (requestMessage.Headers == null)
return false; return false;
if (!requestMessage.Headers.ContainsKey(Name))
return false;
string headerValue = requestMessage.Headers[Name]; string headerValue = requestMessage.Headers[Name];
return Matchers.Any(m => m.IsMatch(headerValue)); return Matchers.Any(m => m.IsMatch(headerValue));
} }

View File

@@ -30,10 +30,22 @@ namespace WireMock.Matchers.Request
/// Determines whether the specified RequestMessage is match. /// Determines whether the specified RequestMessage is match.
/// </summary> /// </summary>
/// <param name="requestMessage">The RequestMessage.</param> /// <param name="requestMessage">The RequestMessage.</param>
/// <param name="requestMatchResult">The RequestMatchResult.</param>
/// <returns> /// <returns>
/// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>. /// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>.
/// </returns> /// </returns>
public bool IsMatch(RequestMessage requestMessage) public bool IsMatch(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
{
bool isMatch = IsMatch(requestMessage);
if (isMatch)
requestMatchResult.Matched++;
requestMatchResult.Total++;
return isMatch;
}
private bool IsMatch(RequestMessage requestMessage)
{ {
return Methods.Contains(requestMessage.Method); return Methods.Contains(requestMessage.Method);
} }

View File

@@ -59,13 +59,25 @@ namespace WireMock.Matchers.Request
/// Determines whether the specified RequestMessage is match. /// Determines whether the specified RequestMessage is match.
/// </summary> /// </summary>
/// <param name="requestMessage">The RequestMessage.</param> /// <param name="requestMessage">The RequestMessage.</param>
/// <param name="requestMatchResult">The RequestMatchResult.</param>
/// <returns> /// <returns>
/// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>. /// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>.
/// </returns> /// </returns>
public bool IsMatch(RequestMessage requestMessage) public bool IsMatch(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
{
bool isMatch = IsMatch(requestMessage);
if (isMatch)
requestMatchResult.Matched++;
requestMatchResult.Total++;
return isMatch;
}
private bool IsMatch(RequestMessage requestMessage)
{ {
if (Funcs != null) if (Funcs != null)
return Funcs.Any(f => f(requestMessage.Query)); return requestMessage.Query != null && Funcs.Any(f => f(requestMessage.Query));
var values = requestMessage.GetParameter(Key); var values = requestMessage.GetParameter(Key);
return values?.Intersect(Values).Count() == Values.Count(); return values?.Intersect(Values).Count() == Values.Count();

View File

@@ -53,16 +53,28 @@ namespace WireMock.Matchers.Request
/// Determines whether the specified RequestMessage is match. /// Determines whether the specified RequestMessage is match.
/// </summary> /// </summary>
/// <param name="requestMessage">The RequestMessage.</param> /// <param name="requestMessage">The RequestMessage.</param>
/// <param name="requestMatchResult">The RequestMatchResult.</param>
/// <returns> /// <returns>
/// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>. /// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>.
/// </returns> /// </returns>
public bool IsMatch(RequestMessage requestMessage) public bool IsMatch(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
{
bool isMatch = IsMatch(requestMessage);
if (isMatch)
requestMatchResult.Matched++;
requestMatchResult.Total++;
return isMatch;
}
private bool IsMatch(RequestMessage requestMessage)
{ {
if (Matchers != null) if (Matchers != null)
return Matchers.Any(matcher => matcher.IsMatch(requestMessage.Path)); return Matchers.Any(matcher => matcher.IsMatch(requestMessage.Path));
if (Funcs != null) if (Funcs != null)
return Funcs.Any(func => func(requestMessage.Path)); return requestMessage.Path != null && Funcs.Any(func => func(requestMessage.Path));
return false; return false;
} }

View File

@@ -53,16 +53,28 @@ namespace WireMock.Matchers.Request
/// Determines whether the specified RequestMessage is match. /// Determines whether the specified RequestMessage is match.
/// </summary> /// </summary>
/// <param name="requestMessage">The RequestMessage.</param> /// <param name="requestMessage">The RequestMessage.</param>
/// <param name="requestMatchResult">The RequestMatchResult.</param>
/// <returns> /// <returns>
/// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>. /// <c>true</c> if the specified RequestMessage is match; otherwise, <c>false</c>.
/// </returns> /// </returns>
public bool IsMatch(RequestMessage requestMessage) public bool IsMatch(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
{
bool isMatch = IsMatch(requestMessage);
if (isMatch)
requestMatchResult.Matched++;
requestMatchResult.Total++;
return isMatch;
}
private bool IsMatch(RequestMessage requestMessage)
{ {
if (Matchers != null) if (Matchers != null)
return Matchers.Any(matcher => matcher.IsMatch(requestMessage.Url)); return Matchers.Any(matcher => matcher.IsMatch(requestMessage.Url));
if (Funcs != null) if (Funcs != null)
return Funcs.Any(func => func(requestMessage.Url)); return requestMessage.Url != null && Funcs.Any(func => func(requestMessage.Url));
return false; return false;
} }

View File

@@ -202,6 +202,11 @@ namespace WireMock.Server
Body = logEntry.ResponseMessage.Body, Body = logEntry.ResponseMessage.Body,
BodyOriginal = logEntry.ResponseMessage.BodyOriginal, BodyOriginal = logEntry.ResponseMessage.BodyOriginal,
Headers = logEntry.ResponseMessage.Headers Headers = logEntry.ResponseMessage.Headers
},
RequestMatchResult = new LogRequestMatchModel
{
Matched = logEntry.RequestMatchResult.Matched,
Total = logEntry.RequestMatchResult.Total
} }
}; };
} }

View File

@@ -245,7 +245,8 @@ namespace WireMock.Server
{ {
lock (((ICollection)_logEntries).SyncRoot) lock (((ICollection)_logEntries).SyncRoot)
{ {
return _logEntries.Where(log => matcher.IsMatch(log.RequestMessage)); var requestMatchResult = new RequestMatchResult();
return _logEntries.Where(log => matcher.IsMatch(log.RequestMessage, requestMatchResult));
} }
} }
@@ -330,12 +331,34 @@ namespace WireMock.Server
var request = _requestMapper.Map(ctx.Request); var request = _requestMapper.Map(ctx.Request);
ResponseMessage response = null; ResponseMessage response = null;
RequestMatchResult requestMatchResult = null;
try try
{ {
var targetMapping = _mappings var possibleMatchingMappings = _mappings
.OrderBy(m => m.Priority) .Select(m => new { Mapping = m, MatchResult = m.IsRequestHandled(request) })
.FirstOrDefault(m => m.IsRequestHandled(request)); .ToList();
Mapping targetMapping;
if (_allowPartialMapping)
{
var orderedMappings = possibleMatchingMappings
.OrderBy(m => m.Mapping.Priority)
.ThenBy(m => m.MatchResult)
.ToList();
var bestPartialMatch = orderedMappings.FirstOrDefault();
targetMapping = bestPartialMatch?.Mapping;
requestMatchResult = bestPartialMatch?.MatchResult;
}
else
{
var perfectMatch = possibleMatchingMappings
.OrderBy(m => m.Mapping.Priority)
.FirstOrDefault(m => m.MatchResult.IsPerfectMatch);
targetMapping = perfectMatch?.Mapping;
requestMatchResult = perfectMatch?.MatchResult;
}
if (targetMapping == null) if (targetMapping == null)
{ {
@@ -364,7 +387,8 @@ namespace WireMock.Server
{ {
Guid = Guid.NewGuid(), Guid = Guid.NewGuid(),
RequestMessage = request, RequestMessage = request,
ResponseMessage = response ResponseMessage = response,
RequestMatchResult = requestMatchResult
}; };
LogRequest(log); LogRequest(log);

View File

@@ -183,14 +183,14 @@ namespace WireMock.Net.Tests
// when // when
await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + "/foo"); await new HttpClient().GetAsync("http://localhost:" + _server.Ports[0] + "/foo");
_server.Reset(); _server.ResetLogEntries();
// then // then
Check.That(_server.LogEntries).IsEmpty(); Check.That(_server.LogEntries).IsEmpty();
} }
[Test] [Test]
public void Should_reset_routes() public void Should_reset_mappings()
{ {
// given // given
_server = FluentMockServer.Start(); _server = FluentMockServer.Start();
@@ -203,9 +203,10 @@ namespace WireMock.Net.Tests
.WithBody(@"{ msg: ""Hello world!""}")); .WithBody(@"{ msg: ""Hello world!""}"));
// when // when
_server.Reset(); _server.ResetMappings();
// then // then
Check.That(_server.Mappings).IsEmpty();
Check.ThatAsyncCode(() => new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0] + "/foo")) Check.ThatAsyncCode(() => new HttpClient().GetStringAsync("http://localhost:" + _server.Ports[0] + "/foo"))
.ThrowsAny(); .ThrowsAny();
} }

View File

@@ -5,6 +5,7 @@ using NFluent;
using NUnit.Framework; using NUnit.Framework;
using WireMock.RequestBuilders; using WireMock.RequestBuilders;
using WireMock.Matchers; using WireMock.Matchers;
using WireMock.Matchers.Request;
namespace WireMock.Net.Tests namespace WireMock.Net.Tests
{ {
@@ -21,7 +22,8 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla"); var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla");
// then // then
Check.That(spec.IsMatch(request)).IsTrue(); var requestMatchResult = new RequestMatchResult();
Check.That(spec.IsMatch(request, requestMatchResult)).IsTrue();
} }
[Test] [Test]
@@ -32,8 +34,9 @@ namespace WireMock.Net.Tests
var request1 = new RequestMessage(new Uri("http://localhost/x1"), "blabla"); var request1 = new RequestMessage(new Uri("http://localhost/x1"), "blabla");
var request2 = new RequestMessage(new Uri("http://localhost/x2"), "blabla"); var request2 = new RequestMessage(new Uri("http://localhost/x2"), "blabla");
Check.That(requestBuilder.IsMatch(request1)).IsTrue(); var requestMatchResult = new RequestMatchResult();
Check.That(requestBuilder.IsMatch(request2)).IsTrue(); Check.That(requestBuilder.IsMatch(request1, requestMatchResult)).IsTrue();
Check.That(requestBuilder.IsMatch(request2, requestMatchResult)).IsTrue();
} }
[Test] [Test]
@@ -46,7 +49,8 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla"); var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla");
// then // then
Check.That(spec.IsMatch(request)).IsTrue(); var requestMatchResult = new RequestMatchResult();
Check.That(spec.IsMatch(request, requestMatchResult)).IsTrue();
} }
[Test] [Test]
@@ -59,7 +63,8 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo/bar"), "blabla"); var request = new RequestMessage(new Uri("http://localhost/foo/bar"), "blabla");
// then // then
Check.That(spec.IsMatch(request)).IsTrue(); var requestMatchResult = new RequestMatchResult();
Check.That(spec.IsMatch(request, requestMatchResult)).IsTrue();
} }
[Test] [Test]
@@ -72,7 +77,8 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/bar"), "blabla"); var request = new RequestMessage(new Uri("http://localhost/bar"), "blabla");
// then // then
Check.That(spec.IsMatch(request)).IsFalse(); var requestMatchResult = new RequestMatchResult();
Check.That(spec.IsMatch(request, requestMatchResult)).IsFalse();
} }
[Test] [Test]
@@ -85,7 +91,8 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla"); var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla");
// then // then
Check.That(spec.IsMatch(request)).IsTrue(); var requestMatchResult = new RequestMatchResult();
Check.That(spec.IsMatch(request, requestMatchResult)).IsTrue();
} }
[Test] [Test]
@@ -98,7 +105,8 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT"); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT");
// then // then
Check.That(spec.IsMatch(request)).IsTrue(); var requestMatchResult = new RequestMatchResult();
Check.That(spec.IsMatch(request, requestMatchResult)).IsTrue();
} }
[Test] [Test]
@@ -111,7 +119,8 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST"); var request = new RequestMessage(new Uri("http://localhost/foo"), "POST");
// then // then
Check.That(spec.IsMatch(request)).IsTrue(); var requestMatchResult = new RequestMatchResult();
Check.That(spec.IsMatch(request, requestMatchResult)).IsTrue();
} }
[Test] [Test]
@@ -124,7 +133,8 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "GET"); var request = new RequestMessage(new Uri("http://localhost/foo"), "GET");
// then // then
Check.That(spec.IsMatch(request)).IsTrue(); var requestMatchResult = new RequestMatchResult();
Check.That(spec.IsMatch(request, requestMatchResult)).IsTrue();
} }
[Test] [Test]
@@ -139,7 +149,8 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "Delete", body, bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "Delete", body, bodyAsString);
// then // then
Check.That(spec.IsMatch(request)).IsTrue(); var requestMatchResult = new RequestMatchResult();
Check.That(spec.IsMatch(request, requestMatchResult)).IsTrue();
} }
[Test] [Test]
@@ -152,7 +163,8 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "HEAD"); var request = new RequestMessage(new Uri("http://localhost/foo"), "HEAD");
// then // then
Check.That(spec.IsMatch(request)).IsTrue(); var requestMatchResult = new RequestMatchResult();
Check.That(spec.IsMatch(request, requestMatchResult)).IsTrue();
} }
[Test] [Test]
@@ -165,7 +177,8 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "HEAD"); var request = new RequestMessage(new Uri("http://localhost/foo"), "HEAD");
// then // then
Check.That(spec.IsMatch(request)).IsFalse(); var requestMatchResult = new RequestMatchResult();
Check.That(spec.IsMatch(request, requestMatchResult)).IsFalse();
} }
[Test] [Test]
@@ -178,7 +191,8 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT"); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT");
// then // then
Check.That(spec.IsMatch(request)).IsFalse(); var requestMatchResult = new RequestMatchResult();
Check.That(spec.IsMatch(request, requestMatchResult)).IsFalse();
} }
[Test] [Test]
@@ -193,7 +207,8 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "tata" } }); 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(); var requestMatchResult = new RequestMatchResult();
Check.That(spec.IsMatch(request, requestMatchResult)).IsTrue();
} }
[Test] [Test]
@@ -208,7 +223,8 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "tata" } }); 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)).IsFalse(); var requestMatchResult = new RequestMatchResult();
Check.That(spec.IsMatch(request, requestMatchResult)).IsFalse();
} }
[Test] [Test]
@@ -223,7 +239,8 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "ABC" } }); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "ABC" } });
// then // then
Check.That(spec.IsMatch(request)).IsFalse(); var requestMatchResult = new RequestMatchResult();
Check.That(spec.IsMatch(request, requestMatchResult)).IsFalse();
} }
[Test] [Test]
@@ -238,7 +255,8 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, new Dictionary<string, string> { { "X-toto", "TaTa" } }); 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(); var requestMatchResult = new RequestMatchResult();
Check.That(spec.IsMatch(request, requestMatchResult)).IsTrue();
} }
[Test] [Test]
@@ -251,7 +269,8 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", null, null, null, new Dictionary<string, string> { { "session", "abc" } }); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", null, null, null, new Dictionary<string, string> { { "session", "abc" } });
// then // then
Check.That(spec.IsMatch(request)).IsTrue(); var requestMatchResult = new RequestMatchResult();
Check.That(spec.IsMatch(request, requestMatchResult)).IsTrue();
} }
[Test] [Test]
@@ -266,7 +285,8 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString);
// then // then
Check.That(spec.IsMatch(request)).IsTrue(); var requestMatchResult = new RequestMatchResult();
Check.That(spec.IsMatch(request, requestMatchResult)).IsTrue();
} }
[Test] [Test]
@@ -281,7 +301,8 @@ namespace WireMock.Net.Tests
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", "tatata" } });
// then // then
Check.That(spec.IsMatch(request)).IsTrue(); var requestMatchResult = new RequestMatchResult();
Check.That(spec.IsMatch(request, requestMatchResult)).IsTrue();
} }
[Test] [Test]
@@ -296,7 +317,8 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString);
// then // then
Check.That(spec.IsMatch(request)).IsTrue(); var requestMatchResult = new RequestMatchResult();
Check.That(spec.IsMatch(request, requestMatchResult)).IsTrue();
} }
[Test] [Test]
@@ -316,7 +338,8 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, xmlBodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, xmlBodyAsString);
// then // then
Check.That(spec.IsMatch(request)).IsTrue(); var requestMatchResult = new RequestMatchResult();
Check.That(spec.IsMatch(request, requestMatchResult)).IsTrue();
} }
[Test] [Test]
@@ -336,7 +359,8 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, xmlBodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, xmlBodyAsString);
// then // then
Check.That(spec.IsMatch(request)).IsFalse(); var requestMatchResult = new RequestMatchResult();
Check.That(spec.IsMatch(request, requestMatchResult)).IsFalse();
} }
[Test] [Test]
@@ -351,7 +375,8 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString);
// then // then
Check.That(spec.IsMatch(request)).IsTrue(); var requestMatchResult = new RequestMatchResult();
Check.That(spec.IsMatch(request, requestMatchResult)).IsTrue();
} }
[Test] [Test]
@@ -366,7 +391,8 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString); var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString);
// then // then
Check.That(spec.IsMatch(request)).IsFalse(); var requestMatchResult = new RequestMatchResult();
Check.That(spec.IsMatch(request, requestMatchResult)).IsFalse();
} }
[Test] [Test]
@@ -381,7 +407,8 @@ namespace WireMock.Net.Tests
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", "tatata" } });
// then // then
Check.That(spec.IsMatch(request)).IsFalse(); var requestMatchResult = new RequestMatchResult();
Check.That(spec.IsMatch(request, requestMatchResult)).IsFalse();
} }
[Test] [Test]
@@ -394,7 +421,8 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo?bar=1&bar=2"), "PUT"); var request = new RequestMessage(new Uri("http://localhost/foo?bar=1&bar=2"), "PUT");
// then // then
Check.That(spec.IsMatch(request)).IsTrue(); var requestMatchResult = new RequestMatchResult();
Check.That(spec.IsMatch(request, requestMatchResult)).IsTrue();
} }
[Test] [Test]
@@ -407,7 +435,8 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo?bar"), "PUT"); var request = new RequestMessage(new Uri("http://localhost/foo?bar"), "PUT");
// then // then
Check.That(spec.IsMatch(request)).IsTrue(); var requestMatchResult = new RequestMatchResult();
Check.That(spec.IsMatch(request, requestMatchResult)).IsTrue();
} }
[Test] [Test]
@@ -420,7 +449,8 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/foo?bar=1&bar=2"), "PUT"); var request = new RequestMessage(new Uri("http://localhost/foo?bar=1&bar=2"), "PUT");
// then // then
Check.That(spec.IsMatch(request)).IsTrue(); var requestMatchResult = new RequestMatchResult();
Check.That(spec.IsMatch(request, requestMatchResult)).IsTrue();
} }
[Test] [Test]
@@ -433,7 +463,8 @@ namespace WireMock.Net.Tests
var request = new RequestMessage(new Uri("http://localhost/test=7"), "PUT"); var request = new RequestMessage(new Uri("http://localhost/test=7"), "PUT");
// then // then
Check.That(spec.IsMatch(request)).IsFalse(); var requestMatchResult = new RequestMatchResult();
Check.That(spec.IsMatch(request, requestMatchResult)).IsFalse();
} }
} }
} }