Add ClientIP to RequestMessage / RequestLog (#46)

This commit is contained in:
Stef Heyenrath
2017-09-18 20:01:38 +02:00
parent f776911ef8
commit 139ea77888
22 changed files with 414 additions and 134 deletions

View File

@@ -2,8 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp1.0;netcoreapp2.0</TargetFrameworks>
<!-- <RuntimeFrameworkVersion>1.0.1</RuntimeFrameworkVersion> -->
<TargetFrameworks>netcoreapp1.1;netcoreapp2.0</TargetFrameworks>
<ApplicationIcon>../../WireMock.Net-Logo.ico</ApplicationIcon>
</PropertyGroup>

View File

@@ -0,0 +1 @@
dotnet run --framework netcoreapp1.1

View File

@@ -0,0 +1 @@
dotnet run --framework netcoreapp2.0

View File

@@ -0,0 +1,24 @@
namespace WireMock.Admin.Mappings
{
/// <summary>
/// ClientIPModel
/// </summary>
public class ClientIPModel
{
/// <summary>
/// Gets or sets the matchers.
/// </summary>
/// <value>
/// The matchers.
/// </value>
public MatcherModel[] Matchers { get; set; }
/// <summary>
/// Gets or sets the functions.
/// </summary>
/// <value>
/// The functions.
/// </value>
public string[] Funcs { get; set; }
}
}

View File

@@ -7,11 +7,19 @@ namespace WireMock.Admin.Mappings
/// </summary>
public class RequestModel
{
/// <summary>
/// Gets or sets the ClientIP. (Can be a string or a ClientIPModel)
/// </summary>
/// <value>
/// The ClientIP.
/// </value>
public object ClientIP { get; set; }
/// <summary>
/// Gets or sets the Path. (Can be a string or a PathModel)
/// </summary>
/// <value>
/// The URL.
/// The Path.
/// </value>
public object Path { get; set; }

View File

@@ -10,6 +10,11 @@ namespace WireMock.Admin.Requests
/// </summary>
public class LogRequestModel
{
/// <summary>
/// Gets the Client IP Address.
/// </summary>
public string ClientIP { get; set; }
/// <summary>
/// Gets the DateTime.
/// </summary>

View File

@@ -37,22 +37,20 @@ namespace WireMock.Matchers
/// Calculates the score from multiple funcs.
/// </summary>
/// <param name="values">The values.</param>
/// <returns>score</returns>
/// <returns>average score</returns>
public static double ToScore(IEnumerable<bool> values)
{
var list = values.Select(ToScore).ToList();
return list.Sum() / list.Count;
return values.Any() ? values.Select(ToScore).Average() : Mismatch;
}
/// <summary>
/// Calculates the score from multiple funcs.
/// </summary>
/// <param name="values">The values.</param>
/// <returns>score</returns>
/// <returns>average score</returns>
public static double ToScore(IEnumerable<double> values)
{
var list = values.ToList();
return list.Sum() / list.Count;
return values.Any() ? values.Average() : Mismatch;
}
}
}

View File

@@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using WireMock.Validation;
namespace WireMock.Matchers.Request
{
/// <summary>
/// The request ClientIP matcher.
/// </summary>
public class RequestMessageClientIPMatcher : IRequestMatcher
{
/// <summary>
/// The matchers.
/// </summary>
public IReadOnlyList<IMatcher> Matchers { get; }
/// <summary>
/// The ClientIP functions.
/// </summary>
public Func<string, bool>[] Funcs { get; }
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageClientIPMatcher"/> class.
/// </summary>
/// <param name="clientIPs">The clientIPs.</param>
public RequestMessageClientIPMatcher([NotNull] params string[] clientIPs) : this(clientIPs.Select(ip => new WildcardMatcher(ip)).ToArray())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageClientIPMatcher"/> class.
/// </summary>
/// <param name="matchers">The matchers.</param>
public RequestMessageClientIPMatcher([NotNull] params IMatcher[] matchers)
{
Check.NotNull(matchers, nameof(matchers));
Matchers = matchers;
}
/// <summary>
/// Initializes a new instance of the <see cref="RequestMessageClientIPMatcher"/> class.
/// </summary>
/// <param name="funcs">The clientIP functions.</param>
public RequestMessageClientIPMatcher([NotNull] params Func<string, bool>[] funcs)
{
Check.NotNull(funcs, nameof(funcs));
Funcs = funcs;
}
/// <summary>
/// Determines whether the specified RequestMessage is match.
/// </summary>
/// <param name="requestMessage">The RequestMessage.</param>
/// <param name="requestMatchResult">The RequestMatchResult.</param>
/// <returns>
/// A value between 0.0 - 1.0 of the similarity.
/// </returns>
public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
{
double score = IsMatch(requestMessage);
requestMatchResult.TotalScore += score;
requestMatchResult.TotalNumber++;
return score;
}
private double IsMatch(RequestMessage requestMessage)
{
if (Matchers != null)
return Matchers.Max(matcher => matcher.IsMatch(requestMessage.ClientIP));
if (Funcs != null)
return MatchScores.ToScore(requestMessage.ClientIP != null && Funcs.Any(func => func(requestMessage.ClientIP)));
return MatchScores.Mismatch;
}
}
}

View File

@@ -43,6 +43,11 @@ namespace WireMock.Matchers.Request
/// </returns>
public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
{
if (!RequestMatchers.Any())
{
return MatchScores.Mismatch;
}
if (_type == CompositeMatcherType.And)
{
return RequestMatchers.Average(requestMatcher => requestMatcher.GetMatchingScore(requestMessage, requestMatchResult));

View File

@@ -12,12 +12,12 @@ namespace WireMock.Matchers.Request
public class RequestMessageUrlMatcher : IRequestMatcher
{
/// <summary>
/// The matcher.
/// The matchers.
/// </summary>
public IReadOnlyList<IMatcher> Matchers { get; }
/// <summary>
/// The url functions
/// The url functions.
/// </summary>
public Func<string, bool>[] Funcs { get; }

View File

@@ -1,69 +1,74 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#if !NETSTANDARD
using Microsoft.Owin;
#else
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
#endif
namespace WireMock.Owin
{
/// <summary>
/// OwinRequestMapper
/// </summary>
public class OwinRequestMapper
{
/// <summary>
/// MapAsync IOwinRequest to RequestMessage
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public async Task<RequestMessage> MapAsync(
#if !NETSTANDARD
IOwinRequest request
#else
HttpRequest request
#endif
)
{
#if !NETSTANDARD
Uri url = request.Uri;
#else
Uri url = new Uri(request.GetEncodedUrl());
#endif
string verb = request.Method;
string bodyAsString = null;
byte[] body = null;
Encoding bodyEncoding = null;
if (request.Body != null)
{
using (var streamReader = new StreamReader(request.Body))
{
bodyAsString = await streamReader.ReadToEndAsync();
bodyEncoding = streamReader.CurrentEncoding;
}
body = bodyEncoding.GetBytes(bodyAsString);
}
var listenerHeaders = request.Headers;
var headers = new Dictionary<string, string>();
foreach (var header in listenerHeaders)
headers.Add(header.Key, header.Value.FirstOrDefault());
var cookies = new Dictionary<string, string>();
foreach (var cookie in request.Cookies)
cookies.Add(cookie.Key, cookie.Value);
return new RequestMessage(url, verb, body, bodyAsString, bodyEncoding, headers, cookies) { DateTime = DateTime.Now };
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
#if !NETSTANDARD
using Microsoft.Owin;
#else
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
#endif
namespace WireMock.Owin
{
/// <summary>
/// OwinRequestMapper
/// </summary>
public class OwinRequestMapper
{
/// <summary>
/// MapAsync IOwinRequest to RequestMessage
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public async Task<RequestMessage> MapAsync(
#if !NETSTANDARD
IOwinRequest request
#else
HttpRequest request
#endif
)
{
#if !NETSTANDARD
Uri url = request.Uri;
string clientIP = request.RemoteIpAddress;
#else
Uri url = new Uri(request.GetEncodedUrl());
var connection = request.HttpContext.Connection;
string clientIP = connection.RemoteIpAddress.IsIPv4MappedToIPv6
? connection.RemoteIpAddress.MapToIPv4().ToString()
: connection.RemoteIpAddress.ToString();
#endif
string method = request.Method;
string bodyAsString = null;
byte[] body = null;
Encoding bodyEncoding = null;
if (request.Body != null)
{
using (var streamReader = new StreamReader(request.Body))
{
bodyAsString = await streamReader.ReadToEndAsync();
bodyEncoding = streamReader.CurrentEncoding;
}
body = bodyEncoding.GetBytes(bodyAsString);
}
var listenerHeaders = request.Headers;
var headers = new Dictionary<string, string>();
foreach (var header in listenerHeaders)
headers.Add(header.Key, header.Value.FirstOrDefault());
var cookies = new Dictionary<string, string>();
foreach (var cookie in request.Cookies)
cookies.Add(cookie.Key, cookie.Value);
return new RequestMessage(url, method, clientIP, body, bodyAsString, bodyEncoding, headers, cookies) { DateTime = DateTime.Now };
}
}
}

View File

@@ -0,0 +1,33 @@
using System;
using JetBrains.Annotations;
using WireMock.Matchers;
namespace WireMock.RequestBuilders
{
/// <summary>
/// The IClientIPRequestBuilder interface.
/// </summary>
public interface IClientIPRequestBuilder : IUrlAndPathRequestBuilder
{
/// <summary>
/// The with ClientIP.
/// </summary>
/// <param name="matchers">The matchers.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
IRequestBuilder WithClientIP([NotNull] params IMatcher[] matchers);
/// <summary>
/// The with ClientIP.
/// </summary>
/// <param name="clientIPs">The clientIPs.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
IRequestBuilder WithClientIP([NotNull] params string[] clientIPs);
/// <summary>
/// The with ClientIP.
/// </summary>
/// <param name="funcs">The path funcs.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
IRequestBuilder WithClientIP([NotNull] params Func<string, bool>[] funcs);
}
}

View File

@@ -3,7 +3,7 @@
/// <summary>
/// IRequestBuilder
/// </summary>
public interface IRequestBuilder : IUrlAndPathRequestBuilder
public interface IRequestBuilder : IClientIPRequestBuilder
{
}
}

View File

@@ -54,6 +54,45 @@ namespace WireMock.RequestBuilders
return _requestMatchers.Where(rm => rm is T).Cast<T>().FirstOrDefault();
}
/// <summary>
/// The with clientIP.
/// </summary>
/// <param name="matchers">The matchers.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
public IRequestBuilder WithClientIP(params IMatcher[] matchers)
{
Check.NotEmpty(matchers, nameof(matchers));
_requestMatchers.Add(new RequestMessageClientIPMatcher(matchers));
return this;
}
/// <summary>
/// The with clientIP.
/// </summary>
/// <param name="clientIPs">The ClientIPs.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
public IRequestBuilder WithClientIP(params string[] clientIPs)
{
Check.NotEmpty(clientIPs, nameof(clientIPs));
_requestMatchers.Add(new RequestMessageClientIPMatcher(clientIPs));
return this;
}
/// <summary>
/// The with clientIP.
/// </summary>
/// <param name="funcs">The clientIP funcs.</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
public IRequestBuilder WithClientIP(params Func<string, bool>[] funcs)
{
Check.NotEmpty(funcs, nameof(funcs));
_requestMatchers.Add(new RequestMessageClientIPMatcher(funcs));
return this;
}
/// <summary>
/// The with path.
/// </summary>

View File

@@ -13,6 +13,11 @@ namespace WireMock
/// </summary>
public class RequestMessage
{
/// <summary>
/// Gets the Client IP Address.
/// </summary>
public string ClientIP { get; }
/// <summary>
/// Gets the url.
/// </summary>
@@ -67,20 +72,23 @@ namespace WireMock
/// Initializes a new instance of the <see cref="RequestMessage"/> class.
/// </summary>
/// <param name="url">The original url.</param>
/// <param name="verb">The verb.</param>
/// <param name="method">The HTTP method.</param>
/// <param name="clientIP">The client IP Address.</param>
/// <param name="bodyAsBytes">The bodyAsBytes byte[].</param>
/// <param name="body">The body string.</param>
/// <param name="bodyEncoding">The body encoding</param>
/// <param name="headers">The headers.</param>
/// <param name="cookies">The cookies.</param>
public RequestMessage([NotNull] Uri url, [NotNull] string verb, [CanBeNull] byte[] bodyAsBytes = null, [CanBeNull] string body = null, [CanBeNull] Encoding bodyEncoding = null, [CanBeNull] IDictionary<string, string> headers = null, [CanBeNull] IDictionary<string, string> cookies = null)
public RequestMessage([NotNull] Uri url, [NotNull] string method, [NotNull] string clientIP, [CanBeNull] byte[] bodyAsBytes = null, [CanBeNull] string body = null, [CanBeNull] Encoding bodyEncoding = null, [CanBeNull] IDictionary<string, string> headers = null, [CanBeNull] IDictionary<string, string> cookies = null)
{
Check.NotNull(url, nameof(url));
Check.NotNull(verb, nameof(verb));
Check.NotNull(method, nameof(method));
Check.NotNull(clientIP, nameof(clientIP));
Url = url.ToString();
Path = url.AbsolutePath;
Method = verb.ToLower();
Method = method.ToLower();
ClientIP = clientIP;
BodyAsBytes = bodyAsBytes;
Body = body;
BodyEncoding = bodyEncoding;
@@ -100,14 +108,16 @@ namespace WireMock
(dict, term) =>
{
var parts = term.Split('=');
var key = parts[0];
string key = parts[0];
if (!dict.ContainsKey(key))
{
dict.Add(key, new WireMockList<string>());
}
if (parts.Length == 2)
{
dict[key].Add(parts[1]);
}
return dict;
});

View File

@@ -18,6 +18,7 @@ namespace WireMock.Serialization
var request = (Request)mapping.RequestMatcher;
var response = (Response)mapping.Provider;
var clientIPMatchers = request.GetRequestMessageMatchers<RequestMessageClientIPMatcher>();
var pathMatchers = request.GetRequestMessageMatchers<RequestMessagePathMatcher>();
var urlMatchers = request.GetRequestMessageMatchers<RequestMessageUrlMatcher>();
var headerMatchers = request.GetRequestMessageMatchers<RequestMessageHeaderMatcher>();
@@ -33,6 +34,12 @@ namespace WireMock.Serialization
Priority = mapping.Priority,
Request = new RequestModel
{
ClientIP = clientIPMatchers != null && clientIPMatchers.Any() ? new ClientIPModel
{
Matchers = Map(clientIPMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers)),
Funcs = Map(clientIPMatchers.Where(m => m.Funcs != null).SelectMany(m => m.Funcs))
} : null,
Path = pathMatchers != null && pathMatchers.Any() ? new PathModel
{
Matchers = Map(pathMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers)),
@@ -184,6 +191,5 @@ namespace WireMock.Serialization
throw new NotSupportedException($"Matcher '{matcherName}' is not supported.");
}
}
}
}

View File

@@ -377,6 +377,7 @@ namespace WireMock.Server
Request = new LogRequestModel
{
DateTime = logEntry.RequestMessage.DateTime,
ClientIP = logEntry.RequestMessage.ClientIP,
Path = logEntry.RequestMessage.Path,
AbsoluteUrl = logEntry.RequestMessage.Url,
Query = logEntry.RequestMessage.Query,
@@ -451,6 +452,23 @@ namespace WireMock.Server
{
IRequestBuilder requestBuilder = Request.Create();
if (requestModel.ClientIP != null)
{
string clientIP = requestModel.ClientIP as string;
if (clientIP != null)
{
requestBuilder = requestBuilder.WithClientIP(clientIP);
}
else
{
var clientIPModel = JsonUtils.ParseJTokenToObject<ClientIPModel>(requestModel.ClientIP);
if (clientIPModel?.Matchers != null)
{
requestBuilder = requestBuilder.WithPath(clientIPModel.Matchers.Select(MappingConverter.Map).ToArray());
}
}
}
if (requestModel.Path != null)
{
string path = requestModel.Path as string;

View File

@@ -8,11 +8,13 @@ namespace WireMock.Net.Tests
//[TestFixture]
public class RequestMessageTests
{
private const string clientIP = "::1";
[Fact]
public void Should_handle_empty_query()
{
// given
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST");
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", clientIP);
// then
Check.That(request.GetParameter("not_there")).IsNull();
@@ -24,7 +26,7 @@ namespace WireMock.Net.Tests
// given
string bodyAsString = "whatever";
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
var request = new RequestMessage(new Uri("http://localhost?foo=bar&multi=1&multi=2"), "POST", body, bodyAsString, Encoding.UTF8);
var request = new RequestMessage(new Uri("http://localhost?foo=bar&multi=1&multi=2"), "POST", clientIP, body, bodyAsString, Encoding.UTF8);
// then
Check.That(request.GetParameter("foo")).Contains("bar");
@@ -32,4 +34,4 @@ namespace WireMock.Net.Tests
Check.That(request.GetParameter("multi")).Contains("2");
}
}
}
}

View File

@@ -0,0 +1,41 @@
using System;
using NFluent;
using WireMock.Matchers.Request;
using WireMock.RequestBuilders;
using Xunit;
namespace WireMock.Net.Tests
{
//[TestFixture]
public partial class RequestTests
{
[Fact]
public void Request_WithClientIP_Match_Ok()
{
// given
var spec = Request.Create().WithClientIP("127.0.0.2", "1.1.1.1");
// when
var request = new RequestMessage(new Uri("http://localhost"), "GET", "127.0.0.2");
// then
var requestMatchResult = new RequestMatchResult();
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(1.0);
}
[Fact]
public void Request_WithClientIP_Match_Fail()
{
// given
var spec = Request.Create().WithClientIP("127.0.0.2");
// when
var request = new RequestMessage(new Uri("http://localhost"), "GET", "192.1.1.1");
// then
var requestMatchResult = new RequestMatchResult();
Check.That(spec.GetMatchingScore(request, requestMatchResult)).IsEqualTo(0.0);
}
}
}

View File

@@ -10,8 +10,10 @@ using WireMock.Matchers.Request;
namespace WireMock.Net.Tests
{
//[TestFixture]
public class RequestTests
public partial class RequestTests
{
private const string clientIP = "::1";
[Fact]
public void Should_specify_requests_matching_given_path()
{
@@ -19,7 +21,7 @@ namespace WireMock.Net.Tests
var spec = Request.Create().WithPath("/foo");
// when
var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla");
var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla", clientIP);
// then
var requestMatchResult = new RequestMatchResult();
@@ -31,8 +33,8 @@ namespace WireMock.Net.Tests
{
var requestBuilder = Request.Create().WithPath("/x1", "/x2");
var request1 = new RequestMessage(new Uri("http://localhost/x1"), "blabla");
var request2 = new RequestMessage(new Uri("http://localhost/x2"), "blabla");
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);
@@ -46,7 +48,7 @@ namespace WireMock.Net.Tests
var spec = Request.Create().WithPath(url => url.EndsWith("/foo"));
// when
var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla");
var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla", clientIP);
// then
var requestMatchResult = new RequestMatchResult();
@@ -60,7 +62,7 @@ namespace WireMock.Net.Tests
var spec = Request.Create().WithPath(new RegexMatcher("^/foo"));
// when
var request = new RequestMessage(new Uri("http://localhost/foo/bar"), "blabla");
var request = new RequestMessage(new Uri("http://localhost/foo/bar"), "blabla", clientIP);
// then
var requestMatchResult = new RequestMatchResult();
@@ -74,7 +76,7 @@ namespace WireMock.Net.Tests
var spec = Request.Create().WithPath("/foo");
// when
var request = new RequestMessage(new Uri("http://localhost/bar"), "blabla");
var request = new RequestMessage(new Uri("http://localhost/bar"), "blabla", clientIP);
// then
var requestMatchResult = new RequestMatchResult();
@@ -88,7 +90,7 @@ namespace WireMock.Net.Tests
var spec = Request.Create().WithUrl("*/foo");
// when
var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla");
var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla", clientIP);
// then
var requestMatchResult = new RequestMatchResult();
@@ -102,7 +104,7 @@ namespace WireMock.Net.Tests
var spec = Request.Create().WithPath("/foo").UsingPut();
// when
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT");
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", clientIP);
// then
var requestMatchResult = new RequestMatchResult();
@@ -116,7 +118,7 @@ namespace WireMock.Net.Tests
var spec = Request.Create().WithPath("/foo").UsingPost();
// when
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST");
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", clientIP);
// then
var requestMatchResult = new RequestMatchResult();
@@ -130,7 +132,7 @@ namespace WireMock.Net.Tests
var spec = Request.Create().WithPath("/foo").UsingGet();
// when
var request = new RequestMessage(new Uri("http://localhost/foo"), "GET");
var request = new RequestMessage(new Uri("http://localhost/foo"), "GET", clientIP);
// then
var requestMatchResult = new RequestMatchResult();
@@ -146,7 +148,7 @@ namespace WireMock.Net.Tests
// when
string bodyAsString = "whatever";
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
var request = new RequestMessage(new Uri("http://localhost/foo"), "Delete", body, bodyAsString, Encoding.UTF8);
var request = new RequestMessage(new Uri("http://localhost/foo"), "Delete", clientIP, body, bodyAsString, Encoding.UTF8);
// then
var requestMatchResult = new RequestMatchResult();
@@ -160,7 +162,7 @@ namespace WireMock.Net.Tests
var spec = Request.Create().WithPath("/foo").UsingHead();
// when
var request = new RequestMessage(new Uri("http://localhost/foo"), "HEAD");
var request = new RequestMessage(new Uri("http://localhost/foo"), "HEAD", clientIP);
// then
var requestMatchResult = new RequestMatchResult();
@@ -174,7 +176,7 @@ namespace WireMock.Net.Tests
var spec = Request.Create().WithPath("/foo").UsingPut();
// when
var request = new RequestMessage(new Uri("http://localhost/foo"), "HEAD");
var request = new RequestMessage(new Uri("http://localhost/foo"), "HEAD", clientIP);
// then
var requestMatchResult = new RequestMatchResult();
@@ -188,7 +190,7 @@ namespace WireMock.Net.Tests
var spec = Request.Create().WithPath("/bar").UsingPut();
// when
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT");
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", clientIP);
// then
var requestMatchResult = new RequestMatchResult();
@@ -204,7 +206,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, Encoding.UTF8, new Dictionary <string, string> { { "X-toto", "tata" } });
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", clientIP, body, bodyAsString, Encoding.UTF8, new Dictionary<string, string> { { "X-toto", "tata" } });
// then
var requestMatchResult = new RequestMatchResult();
@@ -220,7 +222,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, Encoding.UTF8, new Dictionary <string, string> { { "X-toto", "tata" } });
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", clientIP, body, bodyAsString, Encoding.UTF8, new Dictionary<string, string> { { "X-toto", "tata" } });
// then
var requestMatchResult = new RequestMatchResult();
@@ -236,7 +238,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, Encoding.UTF8, new Dictionary <string, string> { { "X-toto", "ABC" } });
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", clientIP, body, bodyAsString, Encoding.UTF8, new Dictionary<string, string> { { "X-toto", "ABC" } });
// then
var requestMatchResult = new RequestMatchResult();
@@ -252,7 +254,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, Encoding.UTF8, new Dictionary<string, string> { { "X-toto", "TaTa" } });
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", clientIP, body, bodyAsString, Encoding.UTF8, new Dictionary<string, string> { { "X-toto", "TaTa" } });
// then
var requestMatchResult = new RequestMatchResult();
@@ -266,7 +268,7 @@ namespace WireMock.Net.Tests
var spec = Request.Create().UsingAnyVerb().WithCookie("session", "a*");
// when
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", null, null, null, null, new Dictionary<string, string> { { "session", "abc" } });
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();
@@ -282,7 +284,7 @@ namespace WireMock.Net.Tests
// when
string bodyAsString = "Hello world!";
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8);
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", clientIP, body, bodyAsString, Encoding.UTF8);
// then
var requestMatchResult = new RequestMatchResult();
@@ -298,7 +300,7 @@ namespace WireMock.Net.Tests
// when
string bodyAsString = "cat";
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8);
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", clientIP, body, bodyAsString, Encoding.UTF8);
// then
var requestMatchResult = new RequestMatchResult();
@@ -314,7 +316,7 @@ namespace WireMock.Net.Tests
// when
string bodyAsString = "cat";
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8);
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", clientIP, body, bodyAsString, Encoding.UTF8);
// then
var requestMatchResult = new RequestMatchResult();
@@ -330,7 +332,7 @@ namespace WireMock.Net.Tests
// when
string bodyAsString = "caR";
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8);
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", clientIP, body, bodyAsString, Encoding.UTF8);
// then
var requestMatchResult = new RequestMatchResult();
@@ -346,7 +348,7 @@ namespace WireMock.Net.Tests
// 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", body, bodyAsString, Encoding.UTF8);
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", clientIP, body, bodyAsString, Encoding.UTF8);
// then
var requestMatchResult = new RequestMatchResult();
@@ -362,7 +364,7 @@ namespace WireMock.Net.Tests
// when
string bodyAsString = "Hello";
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8);
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", clientIP, body, bodyAsString, Encoding.UTF8);
// then
var requestMatchResult = new RequestMatchResult();
@@ -378,7 +380,7 @@ namespace WireMock.Net.Tests
// when
string bodyAsString = "Hello world!";
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8, new Dictionary<string, string> { { "X-toto", "tatata" } });
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", clientIP, body, bodyAsString, Encoding.UTF8, new Dictionary<string, string> { { "X-toto", "tatata" } });
// then
var requestMatchResult = new RequestMatchResult();
@@ -394,7 +396,7 @@ namespace WireMock.Net.Tests
// when
string bodyAsString = "Hello world!";
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8);
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", clientIP, body, bodyAsString, Encoding.UTF8);
// then
var requestMatchResult = new RequestMatchResult();
@@ -415,7 +417,7 @@ namespace WireMock.Net.Tests
<todo-item id='a3'>xyz</todo-item>
</todo-list>";
byte[] body = Encoding.UTF8.GetBytes(xmlBodyAsString);
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, xmlBodyAsString, Encoding.UTF8);
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", clientIP, body, xmlBodyAsString, Encoding.UTF8);
// then
var requestMatchResult = new RequestMatchResult();
@@ -436,7 +438,7 @@ namespace WireMock.Net.Tests
<todo-item id='a3'>xyz</todo-item>
</todo-list>";
byte[] body = Encoding.UTF8.GetBytes(xmlBodyAsString);
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, xmlBodyAsString, Encoding.UTF8);
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", clientIP, body, xmlBodyAsString, Encoding.UTF8);
// then
var requestMatchResult = new RequestMatchResult();
@@ -452,7 +454,7 @@ namespace WireMock.Net.Tests
// when
string bodyAsString = "{ \"things\": [ { \"name\": \"RequiredThing\" }, { \"name\": \"Wiremock\" } ] }";
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8);
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", clientIP, body, bodyAsString, Encoding.UTF8);
// then
var requestMatchResult = new RequestMatchResult();
@@ -468,7 +470,7 @@ namespace WireMock.Net.Tests
// when
string bodyAsString = "{ \"things\": { \"name\": \"Wiremock\" } }";
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8);
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", clientIP, body, bodyAsString, Encoding.UTF8);
// then
var requestMatchResult = new RequestMatchResult();
@@ -484,7 +486,7 @@ namespace WireMock.Net.Tests
// when
string bodyAsString = "xxx";
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, Encoding.UTF8, new Dictionary<string, string> { { "X-toto", "tatata" } });
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", clientIP, body, bodyAsString, Encoding.UTF8, new Dictionary<string, string> { { "X-toto", "tatata" } });
// then
var requestMatchResult = new RequestMatchResult();
@@ -498,7 +500,7 @@ namespace WireMock.Net.Tests
var spec = Request.Create().WithParam("bar", "1", "2");
// when
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", clientIP);
// then
var requestMatchResult = new RequestMatchResult();
@@ -512,7 +514,7 @@ namespace WireMock.Net.Tests
var spec = Request.Create().WithParam("bar");
// when
var request = new RequestMessage(new Uri("http://localhost/foo?bar"), "PUT");
var request = new RequestMessage(new Uri("http://localhost/foo?bar"), "PUT", clientIP);
// then
var requestMatchResult = new RequestMatchResult();
@@ -526,7 +528,7 @@ namespace WireMock.Net.Tests
var spec = Request.Create().UsingAnyVerb().WithParam(p => p.ContainsKey("bar"));
// when
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", clientIP);
// then
var requestMatchResult = new RequestMatchResult();
@@ -540,7 +542,7 @@ namespace WireMock.Net.Tests
var spec = Request.Create().WithParam("bar", "1");
// when
var request = new RequestMessage(new Uri("http://localhost/test=7"), "PUT");
var request = new RequestMessage(new Uri("http://localhost/test=7"), "PUT", clientIP);
// then
var requestMatchResult = new RequestMatchResult();

View File

@@ -11,18 +11,20 @@ namespace WireMock.Net.Tests
//[TestFixture]
public class ResponseTests
{
private const string clientIP = "::1";
[Fact]
public async Task Response_ProvideResponse_Handlebars_UrlPathVerb()
{
// given
string bodyAsString = "abc";
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8);
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", clientIP, body, bodyAsString, Encoding.UTF8);
var response = Response.Create()
.WithBody("test {{request.url}} {{request.path}} {{request.method}}")
.WithTransformer();
// act
var responseMessage = await response.ProvideResponseAsync(request);
@@ -36,7 +38,7 @@ namespace WireMock.Net.Tests
// given
string bodyAsString = "abc";
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
var request = new RequestMessage(new Uri("http://localhost/foo?a=1&a=2&b=5"), "POST", body, bodyAsString, Encoding.UTF8);
var request = new RequestMessage(new Uri("http://localhost/foo?a=1&a=2&b=5"), "POST", clientIP, body, bodyAsString, Encoding.UTF8);
var response = Response.Create()
.WithBody("test keya={{request.query.a}} idx={{request.query.a.[0]}} idx={{request.query.a.[1]}} keyb={{request.query.b}}")
@@ -55,7 +57,7 @@ namespace WireMock.Net.Tests
// given
string bodyAsString = "abc";
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8, new Dictionary<string, string> { { "Content-Type", "text/plain" } });
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", clientIP, body, bodyAsString, Encoding.UTF8, new Dictionary<string, string> { { "Content-Type", "text/plain" } });
var response = Response.Create().WithHeader("x", "{{request.headers.Content-Type}}").WithBody("test").WithTransformer();
@@ -64,7 +66,7 @@ namespace WireMock.Net.Tests
// then
Check.That(responseMessage.Body).Equals("test");
Check.That(responseMessage.Headers).Contains(new KeyValuePair<string,string>("x", "text/plain"));
Check.That(responseMessage.Headers).Contains(new KeyValuePair<string, string>("x", "text/plain"));
}
[Fact]
@@ -73,7 +75,7 @@ namespace WireMock.Net.Tests
// given
string bodyAsString = "abc";
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8);
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", clientIP, body, bodyAsString, Encoding.UTF8);
var response = Response.Create().WithBody("test", Encoding.ASCII);
@@ -91,7 +93,7 @@ namespace WireMock.Net.Tests
// given
string bodyAsString = "abc";
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", body, bodyAsString, Encoding.UTF8);
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", clientIP, body, bodyAsString, Encoding.UTF8);
var response = Response.Create().WithBodyAsJson(new { value = "test" }, Encoding.ASCII);

View File

@@ -14,7 +14,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="Microsoft.Owin.Host.HttpListener" Version="3.1.0" />
<PackageReference Include="Moq" Version="4.7.99" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />