mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-07-08 22:05:13 +02:00
Implemented #82
This commit is contained in:
@@ -18,7 +18,8 @@ namespace WireMock.Net.Console.Record.NETCoreApp
|
|||||||
Url = "https://www.google.com",
|
Url = "https://www.google.com",
|
||||||
//X509Certificate2ThumbprintOrSubjectName = "www.yourclientcertname.com OR yourcertificatethumbprint (only if the service you're proxying to requires it)",
|
//X509Certificate2ThumbprintOrSubjectName = "www.yourclientcertname.com OR yourcertificatethumbprint (only if the service you're proxying to requires it)",
|
||||||
SaveMapping = true,
|
SaveMapping = true,
|
||||||
SaveMappingToFile = false
|
SaveMappingToFile = false,
|
||||||
|
BlackListedHeaders = new [] { "dnt", "Content-Length" }
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -67,7 +67,8 @@ namespace WireMock.Net.StandAlone
|
|||||||
Url = proxyURL,
|
Url = proxyURL,
|
||||||
SaveMapping = parser.GetBoolValue("SaveMapping"),
|
SaveMapping = parser.GetBoolValue("SaveMapping"),
|
||||||
SaveMappingToFile = parser.GetBoolValue("SaveMappingToFile"),
|
SaveMappingToFile = parser.GetBoolValue("SaveMappingToFile"),
|
||||||
X509Certificate2ThumbprintOrSubjectName = parser.GetStringValue("X509Certificate2ThumbprintOrSubjectName")
|
X509Certificate2ThumbprintOrSubjectName = parser.GetStringValue("X509Certificate2ThumbprintOrSubjectName"),
|
||||||
|
BlackListedHeaders = parser.GetValues("BlackListedHeaders")
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,7 +76,7 @@ namespace WireMock.Net.StandAlone
|
|||||||
|
|
||||||
FluentMockServer server = Start(settings);
|
FluentMockServer server = Start(settings);
|
||||||
|
|
||||||
Console.WriteLine("WireMock.Net server listening at {0}", string.Join(" and ", server.Urls));
|
Console.WriteLine("WireMock.Net server listening at {0}", string.Join(",", server.Urls));
|
||||||
|
|
||||||
return server;
|
return server;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -149,7 +149,7 @@ namespace WireMock.Server
|
|||||||
|
|
||||||
if (settings.SaveMapping)
|
if (settings.SaveMapping)
|
||||||
{
|
{
|
||||||
var mapping = ToMapping(requestMessage, responseMessage);
|
var mapping = ToMapping(requestMessage, responseMessage, settings.BlackListedHeaders ?? new string[] { });
|
||||||
_options.Mappings.Add(mapping);
|
_options.Mappings.Add(mapping);
|
||||||
|
|
||||||
if (settings.SaveMappingToFile)
|
if (settings.SaveMappingToFile)
|
||||||
@@ -161,16 +161,23 @@ namespace WireMock.Server
|
|||||||
return responseMessage;
|
return responseMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Mapping ToMapping(RequestMessage requestMessage, ResponseMessage responseMessage)
|
private Mapping ToMapping(RequestMessage requestMessage, ResponseMessage responseMessage, string[] blacklistedHeaders)
|
||||||
{
|
{
|
||||||
var request = Request.Create();
|
var request = Request.Create();
|
||||||
request.WithPath(requestMessage.Path);
|
request.WithPath(requestMessage.Path);
|
||||||
request.UsingVerb(requestMessage.Method);
|
request.UsingVerb(requestMessage.Method);
|
||||||
|
|
||||||
requestMessage.Query.Loop((key, value) => request.WithParam(key, value.ToArray()));
|
requestMessage.Query.Loop((key, value) => request.WithParam(key, value.ToArray()));
|
||||||
requestMessage.Headers.Loop((key, value) => request.WithHeader(key, value.ToArray()));
|
|
||||||
requestMessage.Cookies.Loop((key, value) => request.WithCookie(key, value));
|
requestMessage.Cookies.Loop((key, value) => request.WithCookie(key, value));
|
||||||
|
|
||||||
|
requestMessage.Headers.Loop((key, value) =>
|
||||||
|
{
|
||||||
|
if (!blacklistedHeaders.Any(b => string.Equals(key, b, StringComparison.OrdinalIgnoreCase)))
|
||||||
|
{
|
||||||
|
request.WithHeader(key, value.ToArray());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
if (requestMessage.Body != null)
|
if (requestMessage.Body != null)
|
||||||
{
|
{
|
||||||
request.WithBody(new ExactMatcher(requestMessage.Body));
|
request.WithBody(new ExactMatcher(requestMessage.Body));
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
namespace WireMock.Settings
|
namespace WireMock.Settings
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// IRecordAndSaveSettings
|
/// IProxyAndRecordSettings
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IProxyAndRecordSettings
|
public interface IProxyAndRecordSettings
|
||||||
{
|
{
|
||||||
@@ -24,5 +24,10 @@
|
|||||||
/// The clientCertificate thumbprint or subject name fragment to use. Example thumbprint : "D2DBF135A8D06ACCD0E1FAD9BFB28678DF7A9818". Example subject name: "www.google.com""
|
/// The clientCertificate thumbprint or subject name fragment to use. Example thumbprint : "D2DBF135A8D06ACCD0E1FAD9BFB28678DF7A9818". Example subject name: "www.google.com""
|
||||||
/// </summary>
|
/// </summary>
|
||||||
string X509Certificate2ThumbprintOrSubjectName { get; set; }
|
string X509Certificate2ThumbprintOrSubjectName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Defines a list from headers which will excluded from the saved mappings.
|
||||||
|
/// </summary>
|
||||||
|
string[] BlackListedHeaders { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
namespace WireMock.Settings
|
namespace WireMock.Settings
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// RecordAndSaveSettings
|
/// ProxyAndRecordSettings
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class ProxyAndRecordSettings : IProxyAndRecordSettings
|
public class ProxyAndRecordSettings : IProxyAndRecordSettings
|
||||||
{
|
{
|
||||||
@@ -22,5 +22,9 @@ namespace WireMock.Settings
|
|||||||
/// <inheritdoc cref="IProxyAndRecordSettings.X509Certificate2ThumbprintOrSubjectName"/>
|
/// <inheritdoc cref="IProxyAndRecordSettings.X509Certificate2ThumbprintOrSubjectName"/>
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public string X509Certificate2ThumbprintOrSubjectName { get; set; }
|
public string X509Certificate2ThumbprintOrSubjectName { get; set; }
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IProxyAndRecordSettings.BlackListedHeaders"/>
|
||||||
|
[PublicAPI]
|
||||||
|
public string[] BlackListedHeaders { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5,9 +5,11 @@ using System.Net.Http;
|
|||||||
using System.Net.Http.Headers;
|
using System.Net.Http.Headers;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using NFluent;
|
using NFluent;
|
||||||
|
using WireMock.Matchers.Request;
|
||||||
using WireMock.RequestBuilders;
|
using WireMock.RequestBuilders;
|
||||||
using WireMock.ResponseBuilders;
|
using WireMock.ResponseBuilders;
|
||||||
using WireMock.Server;
|
using WireMock.Server;
|
||||||
|
using WireMock.Settings;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace WireMock.Net.Tests
|
namespace WireMock.Net.Tests
|
||||||
@@ -41,7 +43,16 @@ namespace WireMock.Net.Tests
|
|||||||
.Given(Request.Create().WithPath("/*"))
|
.Given(Request.Create().WithPath("/*"))
|
||||||
.RespondWith(Response.Create());
|
.RespondWith(Response.Create());
|
||||||
|
|
||||||
_server = FluentMockServer.Start();
|
var settings = new FluentMockServerSettings
|
||||||
|
{
|
||||||
|
ProxyAndRecordSettings = new ProxyAndRecordSettings
|
||||||
|
{
|
||||||
|
Url = _serverForProxyForwarding.Urls[0],
|
||||||
|
SaveMapping = true,
|
||||||
|
SaveMappingToFile = false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
_server = FluentMockServer.Start(settings);
|
||||||
_server
|
_server
|
||||||
.Given(Request.Create().WithPath("/*"))
|
.Given(Request.Create().WithPath("/*"))
|
||||||
.RespondWith(Response.Create().WithProxy(_serverForProxyForwarding.Urls[0]));
|
.RespondWith(Response.Create().WithProxy(_serverForProxyForwarding.Urls[0]));
|
||||||
@@ -54,6 +65,7 @@ namespace WireMock.Net.Tests
|
|||||||
Content = new StringContent("stringContent")
|
Content = new StringContent("stringContent")
|
||||||
};
|
};
|
||||||
requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
|
requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain");
|
||||||
|
requestMessage.Content.Headers.Add("bbb", "test");
|
||||||
await new HttpClient().SendAsync(requestMessage);
|
await new HttpClient().SendAsync(requestMessage);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
@@ -61,6 +73,54 @@ namespace WireMock.Net.Tests
|
|||||||
Check.That(receivedRequest.Body).IsEqualTo("stringContent");
|
Check.That(receivedRequest.Body).IsEqualTo("stringContent");
|
||||||
Check.That(receivedRequest.Headers).ContainsKey("Content-Type");
|
Check.That(receivedRequest.Headers).ContainsKey("Content-Type");
|
||||||
Check.That(receivedRequest.Headers["Content-Type"]).ContainsExactly("text/plain");
|
Check.That(receivedRequest.Headers["Content-Type"]).ContainsExactly("text/plain");
|
||||||
|
Check.That(receivedRequest.Headers).ContainsKey("bbb");
|
||||||
|
|
||||||
|
var mapping = _server.Mappings.Last();
|
||||||
|
var matcher = ((Request) mapping.RequestMatcher).GetRequestMessageMatchers<RequestMessageHeaderMatcher>().FirstOrDefault(m => m.Name == "bbb");
|
||||||
|
Check.That(matcher).IsNotNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task FluentMockServer_Proxy_Should_exclude_blacklisted_content_header_in_mapping()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
_serverForProxyForwarding = FluentMockServer.Start();
|
||||||
|
_serverForProxyForwarding
|
||||||
|
.Given(Request.Create().WithPath("/*"))
|
||||||
|
.RespondWith(Response.Create());
|
||||||
|
|
||||||
|
var settings = new FluentMockServerSettings
|
||||||
|
{
|
||||||
|
ProxyAndRecordSettings = new ProxyAndRecordSettings
|
||||||
|
{
|
||||||
|
Url = _serverForProxyForwarding.Urls[0],
|
||||||
|
SaveMapping = true,
|
||||||
|
SaveMappingToFile = false,
|
||||||
|
BlackListedHeaders = new[] { "bbb" }
|
||||||
|
}
|
||||||
|
};
|
||||||
|
_server = FluentMockServer.Start(settings);
|
||||||
|
_server
|
||||||
|
.Given(Request.Create().WithPath("/*"))
|
||||||
|
.RespondWith(Response.Create());
|
||||||
|
|
||||||
|
// when
|
||||||
|
var requestMessage = new HttpRequestMessage
|
||||||
|
{
|
||||||
|
Method = HttpMethod.Post,
|
||||||
|
RequestUri = new Uri(_server.Urls[0]),
|
||||||
|
Content = new StringContent("stringContent")
|
||||||
|
};
|
||||||
|
requestMessage.Content.Headers.Add("bbb", "test");
|
||||||
|
await new HttpClient().SendAsync(requestMessage);
|
||||||
|
|
||||||
|
// then
|
||||||
|
var receivedRequest = _serverForProxyForwarding.LogEntries.First().RequestMessage;
|
||||||
|
Check.That(receivedRequest.Headers).ContainsKey("bbb");
|
||||||
|
|
||||||
|
var mapping = _server.Mappings.Last();
|
||||||
|
var matcher = ((Request)mapping.RequestMatcher).GetRequestMessageMatchers<RequestMessageHeaderMatcher>().FirstOrDefault(m => m.Name == "bbb");
|
||||||
|
Check.That(matcher).IsNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ namespace WireMock.Net.Tests
|
|||||||
private FluentMockServer _server;
|
private FluentMockServer _server;
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async void Test()
|
public async void FluentMockServer_LogEntriesChanged()
|
||||||
{
|
{
|
||||||
// Assign
|
// Assign
|
||||||
_server = FluentMockServer.Start();
|
_server = FluentMockServer.Start();
|
||||||
@@ -41,9 +41,9 @@ namespace WireMock.Net.Tests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task ParallelTest()
|
public async Task FluentMockServer_LogEntriesChanged_Parallel()
|
||||||
{
|
{
|
||||||
var expectedCount = 100;
|
int expectedCount = 10;
|
||||||
|
|
||||||
// Assign
|
// Assign
|
||||||
_server = FluentMockServer.Start();
|
_server = FluentMockServer.Start();
|
||||||
@@ -65,11 +65,11 @@ namespace WireMock.Net.Tests
|
|||||||
var listOfTasks = new List<Task<HttpResponseMessage>>();
|
var listOfTasks = new List<Task<HttpResponseMessage>>();
|
||||||
for (var i = 0; i < expectedCount; i++)
|
for (var i = 0; i < expectedCount; i++)
|
||||||
{
|
{
|
||||||
Thread.Sleep(3);
|
Thread.Sleep(100);
|
||||||
listOfTasks.Add(http.GetAsync(_server.Urls[0] + $"/foo"));
|
listOfTasks.Add(http.GetAsync($"{_server.Urls[0]}/foo"));
|
||||||
}
|
}
|
||||||
var responses = await Task.WhenAll(listOfTasks);
|
var responses = await Task.WhenAll(listOfTasks);
|
||||||
var countResponsesWithStatusNotOk = responses.Where(r => r.StatusCode != HttpStatusCode.OK).Count();
|
var countResponsesWithStatusNotOk = responses.Count(r => r.StatusCode != HttpStatusCode.OK);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Check.That(countResponsesWithStatusNotOk).Equals(0);
|
Check.That(countResponsesWithStatusNotOk).Equals(0);
|
||||||
@@ -81,4 +81,4 @@ namespace WireMock.Net.Tests
|
|||||||
_server?.Dispose();
|
_server?.Dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using NFluent;
|
||||||
|
using WireMock.Matchers.Request;
|
||||||
|
using WireMock.RequestBuilders;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace WireMock.Net.Tests
|
||||||
|
{
|
||||||
|
public class RequestCookieTests
|
||||||
|
{
|
||||||
|
private const string ClientIp = "::1";
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Request_WithCookie_OK()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
var spec = Request.Create().UsingAnyVerb().WithCookie("session", "a*");
|
||||||
|
|
||||||
|
// when
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, null, null, null, null, new Dictionary<string, string> { { "session", "abc" } });
|
||||||
|
|
||||||
|
// then
|
||||||
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,85 +4,14 @@ using System.Text;
|
|||||||
using NFluent;
|
using NFluent;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
using WireMock.RequestBuilders;
|
using WireMock.RequestBuilders;
|
||||||
using WireMock.Matchers;
|
|
||||||
using WireMock.Matchers.Request;
|
using WireMock.Matchers.Request;
|
||||||
|
|
||||||
namespace WireMock.Net.Tests
|
namespace WireMock.Net.Tests
|
||||||
{
|
{
|
||||||
//[TestFixture]
|
public class RequestTests
|
||||||
public partial class RequestTests
|
|
||||||
{
|
{
|
||||||
private const string ClientIp = "::1";
|
private const string ClientIp = "::1";
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Should_specify_requests_matching_given_path()
|
|
||||||
{
|
|
||||||
// given
|
|
||||||
var spec = Request.Create().WithPath("/foo");
|
|
||||||
|
|
||||||
// when
|
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla", ClientIp);
|
|
||||||
|
|
||||||
// then
|
|
||||||
var requestMatchResult = new RequestMatchResult();
|
|
||||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Should_specify_requests_matching_given_paths()
|
|
||||||
{
|
|
||||||
var requestBuilder = Request.Create().WithPath("/x1", "/x2");
|
|
||||||
|
|
||||||
var request1 = new RequestMessage(new Uri("http://localhost/x1"), "blabla", ClientIp);
|
|
||||||
var request2 = new RequestMessage(new Uri("http://localhost/x2"), "blabla", ClientIp);
|
|
||||||
|
|
||||||
var requestMatchResult = new RequestMatchResult();
|
|
||||||
Check.That(requestBuilder.GetMatchingScore(request1, requestMatchResult)).IsEqualTo(1.0);
|
|
||||||
Check.That(requestBuilder.GetMatchingScore(request2, requestMatchResult)).IsEqualTo(1.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Should_specify_requests_matching_given_pathFuncs()
|
|
||||||
{
|
|
||||||
// given
|
|
||||||
var spec = Request.Create().WithPath(url => url.EndsWith("/foo"));
|
|
||||||
|
|
||||||
// when
|
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla", ClientIp);
|
|
||||||
|
|
||||||
// then
|
|
||||||
var requestMatchResult = new RequestMatchResult();
|
|
||||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Should_specify_requests_matching_given_path_prefix()
|
|
||||||
{
|
|
||||||
// given
|
|
||||||
var spec = Request.Create().WithPath(new RegexMatcher("^/foo"));
|
|
||||||
|
|
||||||
// when
|
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo/bar"), "blabla", ClientIp);
|
|
||||||
|
|
||||||
// then
|
|
||||||
var requestMatchResult = new RequestMatchResult();
|
|
||||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Should_exclude_requests_not_matching_given_path()
|
|
||||||
{
|
|
||||||
// given
|
|
||||||
var spec = Request.Create().WithPath("/foo");
|
|
||||||
|
|
||||||
// when
|
|
||||||
var request = new RequestMessage(new Uri("http://localhost/bar"), "blabla", ClientIp);
|
|
||||||
|
|
||||||
// then
|
|
||||||
var requestMatchResult = new RequestMatchResult();
|
|
||||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsNotEqualTo(1.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Should_exclude_requests_matching_given_http_method_but_not_url()
|
public void Should_exclude_requests_matching_given_http_method_but_not_url()
|
||||||
{
|
{
|
||||||
@@ -97,22 +26,6 @@ namespace WireMock.Net.Tests
|
|||||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsNotEqualTo(1.0);
|
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsNotEqualTo(1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Should_specify_requests_matching_given_path_and_headers()
|
|
||||||
{
|
|
||||||
// given
|
|
||||||
var spec = Request.Create().WithPath("/foo").UsingAnyVerb().WithHeader("X-toto", "tata");
|
|
||||||
|
|
||||||
// when
|
|
||||||
string bodyAsString = "whatever";
|
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8, new Dictionary<string, string[]> { { "X-toto", new[] { "tata" } } });
|
|
||||||
|
|
||||||
// then
|
|
||||||
var requestMatchResult = new RequestMatchResult();
|
|
||||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Should_exclude_requests_not_matching_given_headers()
|
public void Should_exclude_requests_not_matching_given_headers()
|
||||||
{
|
{
|
||||||
@@ -161,19 +74,7 @@ namespace WireMock.Net.Tests
|
|||||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Should_specify_requests_matching_given_cookies()
|
|
||||||
{
|
|
||||||
// given
|
|
||||||
var spec = Request.Create().UsingAnyVerb().WithCookie("session", "a*");
|
|
||||||
|
|
||||||
// when
|
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, null, null, null, null, new Dictionary<string, string> { { "session", "abc" } });
|
|
||||||
|
|
||||||
// then
|
|
||||||
var requestMatchResult = new RequestMatchResult();
|
|
||||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Should_specify_requests_matching_given_body()
|
public void Should_specify_requests_matching_given_body()
|
||||||
@@ -191,192 +92,7 @@ namespace WireMock.Net.Tests
|
|||||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Should_specify_requests_matching_given_body_using_ExactMatcher_true()
|
|
||||||
{
|
|
||||||
// given
|
|
||||||
var requestBuilder = Request.Create().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", ClientIp, body, bodyAsString, Encoding.UTF8);
|
|
||||||
|
|
||||||
// then
|
|
||||||
var requestMatchResult = new RequestMatchResult();
|
|
||||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Should_specify_requests_matching_given_body_using_ExactMatcher_multiplePatterns()
|
|
||||||
{
|
|
||||||
// given
|
|
||||||
var requestBuilder = Request.Create().UsingAnyVerb().WithBody(new ExactMatcher("cat", "dog"));
|
|
||||||
|
|
||||||
// when
|
|
||||||
string bodyAsString = "cat";
|
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8);
|
|
||||||
|
|
||||||
// then
|
|
||||||
var requestMatchResult = new RequestMatchResult();
|
|
||||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(0.5);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Should_specify_requests_matching_given_body_using_ExactMatcher_false()
|
|
||||||
{
|
|
||||||
// given
|
|
||||||
var requestBuilder = Request.Create().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", ClientIp, body, bodyAsString, Encoding.UTF8);
|
|
||||||
|
|
||||||
// then
|
|
||||||
var requestMatchResult = new RequestMatchResult();
|
|
||||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsStrictlyLessThan(1.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Should_specify_requests_matching_given_body_using_SimMetricsMatcher1()
|
|
||||||
{
|
|
||||||
// given
|
|
||||||
var requestBuilder = Request.Create().UsingAnyVerb().WithBody(new SimMetricsMatcher("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", ClientIp, body, bodyAsString, Encoding.UTF8);
|
|
||||||
|
|
||||||
// then
|
|
||||||
var requestMatchResult = new RequestMatchResult();
|
|
||||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsStrictlyLessThan(1.0).And.IsStrictlyGreaterThan(0.5);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Should_specify_requests_matching_given_body_using_SimMetricsMatcher2()
|
|
||||||
{
|
|
||||||
// given
|
|
||||||
var requestBuilder = Request.Create().UsingAnyVerb().WithBody(new SimMetricsMatcher("The cat walks in the street."));
|
|
||||||
|
|
||||||
// when
|
|
||||||
string bodyAsString = "Hello";
|
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8);
|
|
||||||
|
|
||||||
// then
|
|
||||||
var requestMatchResult = new RequestMatchResult();
|
|
||||||
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsStrictlyLessThan(0.1).And.IsStrictlyGreaterThan(0.05);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Should_specify_requests_matching_given_body_using_WildcardMatcher()
|
|
||||||
{
|
|
||||||
// given
|
|
||||||
var spec = Request.Create().WithPath("/foo").UsingAnyVerb().WithBody(new WildcardMatcher("H*o*"));
|
|
||||||
|
|
||||||
// when
|
|
||||||
string bodyAsString = "Hello world!";
|
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8, new Dictionary<string, string[]> { { "X-toto", new[] { "tatata" } } });
|
|
||||||
|
|
||||||
// then
|
|
||||||
var requestMatchResult = new RequestMatchResult();
|
|
||||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Should_specify_requests_matching_given_body_using_RegexMatcher()
|
|
||||||
{
|
|
||||||
// given
|
|
||||||
var spec = Request.Create().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", ClientIp, body, bodyAsString, Encoding.UTF8);
|
|
||||||
|
|
||||||
// then
|
|
||||||
var requestMatchResult = new RequestMatchResult();
|
|
||||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Should_specify_requests_matching_given_body_using_XPathMatcher_true()
|
|
||||||
{
|
|
||||||
// given
|
|
||||||
var spec = Request.Create().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", ClientIp, body, xmlBodyAsString, Encoding.UTF8);
|
|
||||||
|
|
||||||
// then
|
|
||||||
var requestMatchResult = new RequestMatchResult();
|
|
||||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Should_specify_requests_matching_given_body_using_XPathMatcher_false()
|
|
||||||
{
|
|
||||||
// given
|
|
||||||
var spec = Request.Create().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", ClientIp, body, xmlBodyAsString, Encoding.UTF8);
|
|
||||||
|
|
||||||
// then
|
|
||||||
var requestMatchResult = new RequestMatchResult();
|
|
||||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsNotEqualTo(1.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Should_specify_requests_matching_given_body_using_JsonPathMatcher_true()
|
|
||||||
{
|
|
||||||
// given
|
|
||||||
var spec = Request.Create().UsingAnyVerb().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]"));
|
|
||||||
|
|
||||||
// when
|
|
||||||
string bodyAsString = "{ \"things\": [ { \"name\": \"RequiredThing\" }, { \"name\": \"Wiremock\" } ] }";
|
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8);
|
|
||||||
|
|
||||||
// then
|
|
||||||
var requestMatchResult = new RequestMatchResult();
|
|
||||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void Should_specify_requests_matching_given_body_using_JsonPathMatcher_false()
|
|
||||||
{
|
|
||||||
// given
|
|
||||||
var spec = Request.Create().UsingAnyVerb().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]"));
|
|
||||||
|
|
||||||
// when
|
|
||||||
string bodyAsString = "{ \"things\": { \"name\": \"Wiremock\" } }";
|
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8);
|
|
||||||
|
|
||||||
// then
|
|
||||||
var requestMatchResult = new RequestMatchResult();
|
|
||||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsNotEqualTo(1.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Should_exclude_requests_not_matching_given_body()
|
public void Should_exclude_requests_not_matching_given_body()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,202 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
using NFluent;
|
||||||
|
using WireMock.Matchers;
|
||||||
|
using WireMock.Matchers.Request;
|
||||||
|
using WireMock.RequestBuilders;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace WireMock.Net.Tests
|
||||||
|
{
|
||||||
|
public class RequestWithBodyTests
|
||||||
|
{
|
||||||
|
private const string ClientIp = "::1";
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Request_WithBodyExactMatcher()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
var requestBuilder = Request.Create().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", ClientIp, body, bodyAsString, Encoding.UTF8);
|
||||||
|
|
||||||
|
// then
|
||||||
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Request_WithBodyExactMatcher_multiplePatterns()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
var requestBuilder = Request.Create().UsingAnyVerb().WithBody(new ExactMatcher("cat", "dog"));
|
||||||
|
|
||||||
|
// when
|
||||||
|
string bodyAsString = "cat";
|
||||||
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8);
|
||||||
|
|
||||||
|
// then
|
||||||
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsEqualTo(0.5);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Request_WithBodyExactMatcher_false()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
var requestBuilder = Request.Create().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", ClientIp, body, bodyAsString, Encoding.UTF8);
|
||||||
|
|
||||||
|
// then
|
||||||
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsStrictlyLessThan(1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Request_WithBodySimMetricsMatcher1()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
var requestBuilder = Request.Create().UsingAnyVerb().WithBody(new SimMetricsMatcher("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", ClientIp, body, bodyAsString, Encoding.UTF8);
|
||||||
|
|
||||||
|
// then
|
||||||
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsStrictlyLessThan(1.0).And.IsStrictlyGreaterThan(0.5);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Request_WithBodySimMetricsMatcher2()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
var requestBuilder = Request.Create().UsingAnyVerb().WithBody(new SimMetricsMatcher("The cat walks in the street."));
|
||||||
|
|
||||||
|
// when
|
||||||
|
string bodyAsString = "Hello";
|
||||||
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", ClientIp, body, bodyAsString, Encoding.UTF8);
|
||||||
|
|
||||||
|
// then
|
||||||
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
Check.That(requestBuilder.GetMatchingScore(request, requestMatchResult)).IsStrictlyLessThan(0.1).And.IsStrictlyGreaterThan(0.05);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Request_WithBodyWildcardMatcher()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
var spec = Request.Create().WithPath("/foo").UsingAnyVerb().WithBody(new WildcardMatcher("H*o*"));
|
||||||
|
|
||||||
|
// when
|
||||||
|
string bodyAsString = "Hello world!";
|
||||||
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8, new Dictionary<string, string[]> { { "X-toto", new[] { "tatata" } } });
|
||||||
|
|
||||||
|
// then
|
||||||
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Request_WithBodyRegexMatcher()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
var spec = Request.Create().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", ClientIp, body, bodyAsString, Encoding.UTF8);
|
||||||
|
|
||||||
|
// then
|
||||||
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Request_WithBodyXPathMatcher_true()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
var spec = Request.Create().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", ClientIp, body, xmlBodyAsString, Encoding.UTF8);
|
||||||
|
|
||||||
|
// then
|
||||||
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Request_WithBodyXPathMatcher_false()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
var spec = Request.Create().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", ClientIp, body, xmlBodyAsString, Encoding.UTF8);
|
||||||
|
|
||||||
|
// then
|
||||||
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsNotEqualTo(1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Request_WithBodyJsonPathMatcher_true()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
var spec = Request.Create().UsingAnyVerb().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]"));
|
||||||
|
|
||||||
|
// when
|
||||||
|
string bodyAsString = "{ \"things\": [ { \"name\": \"RequiredThing\" }, { \"name\": \"Wiremock\" } ] }";
|
||||||
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8);
|
||||||
|
|
||||||
|
// then
|
||||||
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Request_WithBodyJsonPathMatcher_false()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
var spec = Request.Create().UsingAnyVerb().WithBody(new JsonPathMatcher("$.things[?(@.name == 'RequiredThing')]"));
|
||||||
|
|
||||||
|
// when
|
||||||
|
string bodyAsString = "{ \"things\": { \"name\": \"Wiremock\" } }";
|
||||||
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8);
|
||||||
|
|
||||||
|
// then
|
||||||
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsNotEqualTo(1.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+38
-39
@@ -1,40 +1,39 @@
|
|||||||
using System;
|
using System;
|
||||||
using NFluent;
|
using NFluent;
|
||||||
using WireMock.Matchers.Request;
|
using WireMock.Matchers.Request;
|
||||||
using WireMock.RequestBuilders;
|
using WireMock.RequestBuilders;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace WireMock.Net.Tests
|
namespace WireMock.Net.Tests
|
||||||
{
|
{
|
||||||
//[TestFixture]
|
public class RequestWithClientIPTests
|
||||||
public partial class RequestTests
|
{
|
||||||
{
|
[Fact]
|
||||||
[Fact]
|
public void Request_WithClientIP_Match_Ok()
|
||||||
public void Request_WithClientIP_Match_Ok()
|
{
|
||||||
{
|
// given
|
||||||
// given
|
var spec = Request.Create().WithClientIP("127.0.0.2", "1.1.1.1");
|
||||||
var spec = Request.Create().WithClientIP("127.0.0.2", "1.1.1.1");
|
|
||||||
|
// when
|
||||||
// when
|
var request = new RequestMessage(new Uri("http://localhost"), "GET", "127.0.0.2");
|
||||||
var request = new RequestMessage(new Uri("http://localhost"), "GET", "127.0.0.2");
|
|
||||||
|
// then
|
||||||
// then
|
var requestMatchResult = new RequestMatchResult();
|
||||||
var requestMatchResult = new RequestMatchResult();
|
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
}
|
||||||
}
|
|
||||||
|
[Fact]
|
||||||
[Fact]
|
public void Request_WithClientIP_Match_Fail()
|
||||||
public void Request_WithClientIP_Match_Fail()
|
{
|
||||||
{
|
// given
|
||||||
// given
|
var spec = Request.Create().WithClientIP("127.0.0.2");
|
||||||
var spec = Request.Create().WithClientIP("127.0.0.2");
|
|
||||||
|
// when
|
||||||
// when
|
var request = new RequestMessage(new Uri("http://localhost"), "GET", "192.1.1.1");
|
||||||
var request = new RequestMessage(new Uri("http://localhost"), "GET", "192.1.1.1");
|
|
||||||
|
// then
|
||||||
// then
|
var requestMatchResult = new RequestMatchResult();
|
||||||
var requestMatchResult = new RequestMatchResult();
|
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(0.0);
|
||||||
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(0.0);
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
+90
-2
@@ -1,15 +1,103 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using NFluent;
|
using NFluent;
|
||||||
|
using WireMock.Matchers;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
using WireMock.RequestBuilders;
|
using WireMock.RequestBuilders;
|
||||||
using WireMock.Matchers.Request;
|
using WireMock.Matchers.Request;
|
||||||
|
|
||||||
namespace WireMock.Net.Tests
|
namespace WireMock.Net.Tests
|
||||||
{
|
{
|
||||||
//[TestFixture]
|
public class RequestWithPathTests
|
||||||
public partial class RequestTests
|
|
||||||
{
|
{
|
||||||
|
private const string ClientIp = "::1";
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Request_WithPath_WithHeader_Match()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
var spec = Request.Create().WithPath("/foo").UsingAnyVerb().WithHeader("X-toto", "tata");
|
||||||
|
|
||||||
|
// when
|
||||||
|
string bodyAsString = "whatever";
|
||||||
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", ClientIp, body, bodyAsString, Encoding.UTF8, new Dictionary<string, string[]> { { "X-toto", new[] { "tata" } } });
|
||||||
|
|
||||||
|
// then
|
||||||
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Request_WithPath()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
var spec = Request.Create().WithPath("/foo");
|
||||||
|
|
||||||
|
// when
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla", ClientIp);
|
||||||
|
|
||||||
|
// then
|
||||||
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Request_WithPaths()
|
||||||
|
{
|
||||||
|
var requestBuilder = Request.Create().WithPath("/x1", "/x2");
|
||||||
|
|
||||||
|
var request1 = new RequestMessage(new Uri("http://localhost/x1"), "blabla", ClientIp);
|
||||||
|
var request2 = new RequestMessage(new Uri("http://localhost/x2"), "blabla", ClientIp);
|
||||||
|
|
||||||
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
Check.That(requestBuilder.GetMatchingScore(request1, requestMatchResult)).IsEqualTo(1.0);
|
||||||
|
Check.That(requestBuilder.GetMatchingScore(request2, requestMatchResult)).IsEqualTo(1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Request_WithPathFunc()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
var spec = Request.Create().WithPath(url => url.EndsWith("/foo"));
|
||||||
|
|
||||||
|
// when
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla", ClientIp);
|
||||||
|
|
||||||
|
// then
|
||||||
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Request_WithPathRegexMatcher()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
var spec = Request.Create().WithPath(new RegexMatcher("^/foo"));
|
||||||
|
|
||||||
|
// when
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/foo/bar"), "blabla", ClientIp);
|
||||||
|
|
||||||
|
// then
|
||||||
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Request_WithPathRegex_false()
|
||||||
|
{
|
||||||
|
// given
|
||||||
|
var spec = Request.Create().WithPath("/foo");
|
||||||
|
|
||||||
|
// when
|
||||||
|
var request = new RequestMessage(new Uri("http://localhost/bar"), "blabla", ClientIp);
|
||||||
|
|
||||||
|
// then
|
||||||
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsNotEqualTo(1.0);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Should_specify_requests_matching_given_path_and_method_delete()
|
public void Should_specify_requests_matching_given_path_and_method_delete()
|
||||||
{
|
{
|
||||||
+5
-3
@@ -6,10 +6,12 @@ using Xunit;
|
|||||||
|
|
||||||
namespace WireMock.Net.Tests
|
namespace WireMock.Net.Tests
|
||||||
{
|
{
|
||||||
public partial class RequestTests
|
public class RequestWithUrlTests
|
||||||
{
|
{
|
||||||
|
private const string ClientIp = "::1";
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Should_specify_requests_matching_given_url_wildcard()
|
public void Request_WithUrl()
|
||||||
{
|
{
|
||||||
// given
|
// given
|
||||||
var spec = Request.Create().WithUrl("*/foo");
|
var spec = Request.Create().WithUrl("*/foo");
|
||||||
@@ -23,7 +25,7 @@ namespace WireMock.Net.Tests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Should_specify_requests_matching_given_url_exact()
|
public void Request_WithUrlExact()
|
||||||
{
|
{
|
||||||
// given
|
// given
|
||||||
var spec = Request.Create().WithUrl("http://localhost/foo");
|
var spec = Request.Create().WithUrl("http://localhost/foo");
|
||||||
+3
-1
@@ -8,8 +8,10 @@ using Xunit;
|
|||||||
|
|
||||||
namespace WireMock.Net.Tests
|
namespace WireMock.Net.Tests
|
||||||
{
|
{
|
||||||
public partial class ResponseTests
|
public class ResponseWithBodyHandlebarsTests
|
||||||
{
|
{
|
||||||
|
private const string ClientIp = "::1";
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Response_ProvideResponse_Handlebars_UrlPathVerb()
|
public async Task Response_ProvideResponse_Handlebars_UrlPathVerb()
|
||||||
{
|
{
|
||||||
+1
-1
@@ -7,7 +7,7 @@ using Xunit;
|
|||||||
|
|
||||||
namespace WireMock.Net.Tests
|
namespace WireMock.Net.Tests
|
||||||
{
|
{
|
||||||
public partial class ResponseTests
|
public class ResponseWithBodyTests
|
||||||
{
|
{
|
||||||
private const string ClientIp = "::1";
|
private const string ClientIp = "::1";
|
||||||
|
|
||||||
Reference in New Issue
Block a user