mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-19 07:54:53 +01:00
Add ClientIP to RequestMessage / RequestLog (#46)
This commit is contained in:
@@ -2,8 +2,7 @@
|
|||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFrameworks>netcoreapp1.0;netcoreapp2.0</TargetFrameworks>
|
<TargetFrameworks>netcoreapp1.1;netcoreapp2.0</TargetFrameworks>
|
||||||
<!-- <RuntimeFrameworkVersion>1.0.1</RuntimeFrameworkVersion> -->
|
|
||||||
<ApplicationIcon>../../WireMock.Net-Logo.ico</ApplicationIcon>
|
<ApplicationIcon>../../WireMock.Net-Logo.ico</ApplicationIcon>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
|||||||
1
examples/WireMock.Net.StandAlone.NETCoreApp/run1.cmd
Normal file
1
examples/WireMock.Net.StandAlone.NETCoreApp/run1.cmd
Normal file
@@ -0,0 +1 @@
|
|||||||
|
dotnet run --framework netcoreapp1.1
|
||||||
1
examples/WireMock.Net.StandAlone.NETCoreApp/run2.cmd
Normal file
1
examples/WireMock.Net.StandAlone.NETCoreApp/run2.cmd
Normal file
@@ -0,0 +1 @@
|
|||||||
|
dotnet run --framework netcoreapp2.0
|
||||||
24
src/WireMock.Net/Admin/Mappings/ClientIPModel.cs
Normal file
24
src/WireMock.Net/Admin/Mappings/ClientIPModel.cs
Normal 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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,11 +7,19 @@ namespace WireMock.Admin.Mappings
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class RequestModel
|
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>
|
/// <summary>
|
||||||
/// Gets or sets the Path. (Can be a string or a PathModel)
|
/// Gets or sets the Path. (Can be a string or a PathModel)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>
|
/// <value>
|
||||||
/// The URL.
|
/// The Path.
|
||||||
/// </value>
|
/// </value>
|
||||||
public object Path { get; set; }
|
public object Path { get; set; }
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,11 @@ namespace WireMock.Admin.Requests
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class LogRequestModel
|
public class LogRequestModel
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the Client IP Address.
|
||||||
|
/// </summary>
|
||||||
|
public string ClientIP { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the DateTime.
|
/// Gets the DateTime.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -37,22 +37,20 @@ namespace WireMock.Matchers
|
|||||||
/// Calculates the score from multiple funcs.
|
/// Calculates the score from multiple funcs.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="values">The values.</param>
|
/// <param name="values">The values.</param>
|
||||||
/// <returns>score</returns>
|
/// <returns>average score</returns>
|
||||||
public static double ToScore(IEnumerable<bool> values)
|
public static double ToScore(IEnumerable<bool> values)
|
||||||
{
|
{
|
||||||
var list = values.Select(ToScore).ToList();
|
return values.Any() ? values.Select(ToScore).Average() : Mismatch;
|
||||||
return list.Sum() / list.Count;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Calculates the score from multiple funcs.
|
/// Calculates the score from multiple funcs.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="values">The values.</param>
|
/// <param name="values">The values.</param>
|
||||||
/// <returns>score</returns>
|
/// <returns>average score</returns>
|
||||||
public static double ToScore(IEnumerable<double> values)
|
public static double ToScore(IEnumerable<double> values)
|
||||||
{
|
{
|
||||||
var list = values.ToList();
|
return values.Any() ? values.Average() : Mismatch;
|
||||||
return list.Sum() / list.Count;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -43,6 +43,11 @@ namespace WireMock.Matchers.Request
|
|||||||
/// </returns>
|
/// </returns>
|
||||||
public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
|
public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
|
||||||
{
|
{
|
||||||
|
if (!RequestMatchers.Any())
|
||||||
|
{
|
||||||
|
return MatchScores.Mismatch;
|
||||||
|
}
|
||||||
|
|
||||||
if (_type == CompositeMatcherType.And)
|
if (_type == CompositeMatcherType.And)
|
||||||
{
|
{
|
||||||
return RequestMatchers.Average(requestMatcher => requestMatcher.GetMatchingScore(requestMessage, requestMatchResult));
|
return RequestMatchers.Average(requestMatcher => requestMatcher.GetMatchingScore(requestMessage, requestMatchResult));
|
||||||
|
|||||||
@@ -12,12 +12,12 @@ namespace WireMock.Matchers.Request
|
|||||||
public class RequestMessageUrlMatcher : IRequestMatcher
|
public class RequestMessageUrlMatcher : IRequestMatcher
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The matcher.
|
/// The matchers.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IReadOnlyList<IMatcher> Matchers { get; }
|
public IReadOnlyList<IMatcher> Matchers { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The url functions
|
/// The url functions.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Func<string, bool>[] Funcs { get; }
|
public Func<string, bool>[] Funcs { get; }
|
||||||
|
|
||||||
|
|||||||
@@ -1,69 +1,74 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
#if !NETSTANDARD
|
#if !NETSTANDARD
|
||||||
using Microsoft.Owin;
|
using Microsoft.Owin;
|
||||||
#else
|
#else
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Http.Extensions;
|
using Microsoft.AspNetCore.Http.Extensions;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace WireMock.Owin
|
namespace WireMock.Owin
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// OwinRequestMapper
|
/// OwinRequestMapper
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class OwinRequestMapper
|
public class OwinRequestMapper
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// MapAsync IOwinRequest to RequestMessage
|
/// MapAsync IOwinRequest to RequestMessage
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="request"></param>
|
/// <param name="request"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<RequestMessage> MapAsync(
|
public async Task<RequestMessage> MapAsync(
|
||||||
#if !NETSTANDARD
|
#if !NETSTANDARD
|
||||||
IOwinRequest request
|
IOwinRequest request
|
||||||
#else
|
#else
|
||||||
HttpRequest request
|
HttpRequest request
|
||||||
#endif
|
#endif
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
#if !NETSTANDARD
|
#if !NETSTANDARD
|
||||||
Uri url = request.Uri;
|
Uri url = request.Uri;
|
||||||
#else
|
string clientIP = request.RemoteIpAddress;
|
||||||
Uri url = new Uri(request.GetEncodedUrl());
|
#else
|
||||||
#endif
|
Uri url = new Uri(request.GetEncodedUrl());
|
||||||
string verb = request.Method;
|
var connection = request.HttpContext.Connection;
|
||||||
|
string clientIP = connection.RemoteIpAddress.IsIPv4MappedToIPv6
|
||||||
string bodyAsString = null;
|
? connection.RemoteIpAddress.MapToIPv4().ToString()
|
||||||
byte[] body = null;
|
: connection.RemoteIpAddress.ToString();
|
||||||
Encoding bodyEncoding = null;
|
#endif
|
||||||
if (request.Body != null)
|
string method = request.Method;
|
||||||
{
|
|
||||||
using (var streamReader = new StreamReader(request.Body))
|
string bodyAsString = null;
|
||||||
{
|
byte[] body = null;
|
||||||
bodyAsString = await streamReader.ReadToEndAsync();
|
Encoding bodyEncoding = null;
|
||||||
bodyEncoding = streamReader.CurrentEncoding;
|
if (request.Body != null)
|
||||||
}
|
{
|
||||||
|
using (var streamReader = new StreamReader(request.Body))
|
||||||
body = bodyEncoding.GetBytes(bodyAsString);
|
{
|
||||||
}
|
bodyAsString = await streamReader.ReadToEndAsync();
|
||||||
|
bodyEncoding = streamReader.CurrentEncoding;
|
||||||
var listenerHeaders = request.Headers;
|
}
|
||||||
|
|
||||||
var headers = new Dictionary<string, string>();
|
body = bodyEncoding.GetBytes(bodyAsString);
|
||||||
foreach (var header in listenerHeaders)
|
}
|
||||||
headers.Add(header.Key, header.Value.FirstOrDefault());
|
|
||||||
|
var listenerHeaders = request.Headers;
|
||||||
var cookies = new Dictionary<string, string>();
|
|
||||||
|
var headers = new Dictionary<string, string>();
|
||||||
foreach (var cookie in request.Cookies)
|
foreach (var header in listenerHeaders)
|
||||||
cookies.Add(cookie.Key, cookie.Value);
|
headers.Add(header.Key, header.Value.FirstOrDefault());
|
||||||
|
|
||||||
return new RequestMessage(url, verb, body, bodyAsString, bodyEncoding, headers, cookies) { DateTime = DateTime.Now };
|
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 };
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
33
src/WireMock.Net/RequestBuilders/IClientIPRequestBuilder.cs
Normal file
33
src/WireMock.Net/RequestBuilders/IClientIPRequestBuilder.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// IRequestBuilder
|
/// IRequestBuilder
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IRequestBuilder : IUrlAndPathRequestBuilder
|
public interface IRequestBuilder : IClientIPRequestBuilder
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -54,6 +54,45 @@ namespace WireMock.RequestBuilders
|
|||||||
return _requestMatchers.Where(rm => rm is T).Cast<T>().FirstOrDefault();
|
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>
|
/// <summary>
|
||||||
/// The with path.
|
/// The with path.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -13,6 +13,11 @@ namespace WireMock
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class RequestMessage
|
public class RequestMessage
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the Client IP Address.
|
||||||
|
/// </summary>
|
||||||
|
public string ClientIP { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the url.
|
/// Gets the url.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -67,20 +72,23 @@ namespace WireMock
|
|||||||
/// Initializes a new instance of the <see cref="RequestMessage"/> class.
|
/// Initializes a new instance of the <see cref="RequestMessage"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="url">The original url.</param>
|
/// <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="bodyAsBytes">The bodyAsBytes byte[].</param>
|
||||||
/// <param name="body">The body string.</param>
|
/// <param name="body">The body string.</param>
|
||||||
/// <param name="bodyEncoding">The body encoding</param>
|
/// <param name="bodyEncoding">The body encoding</param>
|
||||||
/// <param name="headers">The headers.</param>
|
/// <param name="headers">The headers.</param>
|
||||||
/// <param name="cookies">The cookies.</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(url, nameof(url));
|
||||||
Check.NotNull(verb, nameof(verb));
|
Check.NotNull(method, nameof(method));
|
||||||
|
Check.NotNull(clientIP, nameof(clientIP));
|
||||||
|
|
||||||
Url = url.ToString();
|
Url = url.ToString();
|
||||||
Path = url.AbsolutePath;
|
Path = url.AbsolutePath;
|
||||||
Method = verb.ToLower();
|
Method = method.ToLower();
|
||||||
|
ClientIP = clientIP;
|
||||||
BodyAsBytes = bodyAsBytes;
|
BodyAsBytes = bodyAsBytes;
|
||||||
Body = body;
|
Body = body;
|
||||||
BodyEncoding = bodyEncoding;
|
BodyEncoding = bodyEncoding;
|
||||||
@@ -100,14 +108,16 @@ namespace WireMock
|
|||||||
(dict, term) =>
|
(dict, term) =>
|
||||||
{
|
{
|
||||||
var parts = term.Split('=');
|
var parts = term.Split('=');
|
||||||
var key = parts[0];
|
string key = parts[0];
|
||||||
if (!dict.ContainsKey(key))
|
if (!dict.ContainsKey(key))
|
||||||
{
|
{
|
||||||
dict.Add(key, new WireMockList<string>());
|
dict.Add(key, new WireMockList<string>());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parts.Length == 2)
|
if (parts.Length == 2)
|
||||||
|
{
|
||||||
dict[key].Add(parts[1]);
|
dict[key].Add(parts[1]);
|
||||||
|
}
|
||||||
|
|
||||||
return dict;
|
return dict;
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ namespace WireMock.Serialization
|
|||||||
var request = (Request)mapping.RequestMatcher;
|
var request = (Request)mapping.RequestMatcher;
|
||||||
var response = (Response)mapping.Provider;
|
var response = (Response)mapping.Provider;
|
||||||
|
|
||||||
|
var clientIPMatchers = request.GetRequestMessageMatchers<RequestMessageClientIPMatcher>();
|
||||||
var pathMatchers = request.GetRequestMessageMatchers<RequestMessagePathMatcher>();
|
var pathMatchers = request.GetRequestMessageMatchers<RequestMessagePathMatcher>();
|
||||||
var urlMatchers = request.GetRequestMessageMatchers<RequestMessageUrlMatcher>();
|
var urlMatchers = request.GetRequestMessageMatchers<RequestMessageUrlMatcher>();
|
||||||
var headerMatchers = request.GetRequestMessageMatchers<RequestMessageHeaderMatcher>();
|
var headerMatchers = request.GetRequestMessageMatchers<RequestMessageHeaderMatcher>();
|
||||||
@@ -33,6 +34,12 @@ namespace WireMock.Serialization
|
|||||||
Priority = mapping.Priority,
|
Priority = mapping.Priority,
|
||||||
Request = new RequestModel
|
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
|
Path = pathMatchers != null && pathMatchers.Any() ? new PathModel
|
||||||
{
|
{
|
||||||
Matchers = Map(pathMatchers.Where(m => m.Matchers != null).SelectMany(m => m.Matchers)),
|
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.");
|
throw new NotSupportedException($"Matcher '{matcherName}' is not supported.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -377,6 +377,7 @@ namespace WireMock.Server
|
|||||||
Request = new LogRequestModel
|
Request = new LogRequestModel
|
||||||
{
|
{
|
||||||
DateTime = logEntry.RequestMessage.DateTime,
|
DateTime = logEntry.RequestMessage.DateTime,
|
||||||
|
ClientIP = logEntry.RequestMessage.ClientIP,
|
||||||
Path = logEntry.RequestMessage.Path,
|
Path = logEntry.RequestMessage.Path,
|
||||||
AbsoluteUrl = logEntry.RequestMessage.Url,
|
AbsoluteUrl = logEntry.RequestMessage.Url,
|
||||||
Query = logEntry.RequestMessage.Query,
|
Query = logEntry.RequestMessage.Query,
|
||||||
@@ -451,6 +452,23 @@ namespace WireMock.Server
|
|||||||
{
|
{
|
||||||
IRequestBuilder requestBuilder = Request.Create();
|
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)
|
if (requestModel.Path != null)
|
||||||
{
|
{
|
||||||
string path = requestModel.Path as string;
|
string path = requestModel.Path as string;
|
||||||
|
|||||||
@@ -8,11 +8,13 @@ namespace WireMock.Net.Tests
|
|||||||
//[TestFixture]
|
//[TestFixture]
|
||||||
public class RequestMessageTests
|
public class RequestMessageTests
|
||||||
{
|
{
|
||||||
|
private const string clientIP = "::1";
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Should_handle_empty_query()
|
public void Should_handle_empty_query()
|
||||||
{
|
{
|
||||||
// given
|
// given
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST");
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", clientIP);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
Check.That(request.GetParameter("not_there")).IsNull();
|
Check.That(request.GetParameter("not_there")).IsNull();
|
||||||
@@ -24,7 +26,7 @@ namespace WireMock.Net.Tests
|
|||||||
// given
|
// given
|
||||||
string bodyAsString = "whatever";
|
string bodyAsString = "whatever";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
var request = new RequestMessage(new Uri("http://localhost?foo=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
|
// then
|
||||||
Check.That(request.GetParameter("foo")).Contains("bar");
|
Check.That(request.GetParameter("foo")).Contains("bar");
|
||||||
@@ -32,4 +34,4 @@ namespace WireMock.Net.Tests
|
|||||||
Check.That(request.GetParameter("multi")).Contains("2");
|
Check.That(request.GetParameter("multi")).Contains("2");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
41
test/WireMock.Net.Tests/RequestTests.ClientIP.cs
Normal file
41
test/WireMock.Net.Tests/RequestTests.ClientIP.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,8 +10,10 @@ using WireMock.Matchers.Request;
|
|||||||
namespace WireMock.Net.Tests
|
namespace WireMock.Net.Tests
|
||||||
{
|
{
|
||||||
//[TestFixture]
|
//[TestFixture]
|
||||||
public class RequestTests
|
public partial class RequestTests
|
||||||
{
|
{
|
||||||
|
private const string clientIP = "::1";
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Should_specify_requests_matching_given_path()
|
public void Should_specify_requests_matching_given_path()
|
||||||
{
|
{
|
||||||
@@ -19,7 +21,7 @@ namespace WireMock.Net.Tests
|
|||||||
var spec = Request.Create().WithPath("/foo");
|
var spec = Request.Create().WithPath("/foo");
|
||||||
|
|
||||||
// when
|
// when
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla");
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla", clientIP);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -31,8 +33,8 @@ namespace WireMock.Net.Tests
|
|||||||
{
|
{
|
||||||
var requestBuilder = Request.Create().WithPath("/x1", "/x2");
|
var requestBuilder = Request.Create().WithPath("/x1", "/x2");
|
||||||
|
|
||||||
var request1 = new RequestMessage(new Uri("http://localhost/x1"), "blabla");
|
var request1 = new RequestMessage(new Uri("http://localhost/x1"), "blabla", clientIP);
|
||||||
var request2 = new RequestMessage(new Uri("http://localhost/x2"), "blabla");
|
var request2 = new RequestMessage(new Uri("http://localhost/x2"), "blabla", clientIP);
|
||||||
|
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
Check.That(requestBuilder.GetMatchingScore(request1, requestMatchResult)).IsEqualTo(1.0);
|
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"));
|
var spec = Request.Create().WithPath(url => url.EndsWith("/foo"));
|
||||||
|
|
||||||
// when
|
// when
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla");
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla", clientIP);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -60,7 +62,7 @@ namespace WireMock.Net.Tests
|
|||||||
var spec = Request.Create().WithPath(new RegexMatcher("^/foo"));
|
var spec = Request.Create().WithPath(new RegexMatcher("^/foo"));
|
||||||
|
|
||||||
// when
|
// 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
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -74,7 +76,7 @@ namespace WireMock.Net.Tests
|
|||||||
var spec = Request.Create().WithPath("/foo");
|
var spec = Request.Create().WithPath("/foo");
|
||||||
|
|
||||||
// when
|
// when
|
||||||
var request = new RequestMessage(new Uri("http://localhost/bar"), "blabla");
|
var request = new RequestMessage(new Uri("http://localhost/bar"), "blabla", clientIP);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -88,7 +90,7 @@ namespace WireMock.Net.Tests
|
|||||||
var spec = Request.Create().WithUrl("*/foo");
|
var spec = Request.Create().WithUrl("*/foo");
|
||||||
|
|
||||||
// when
|
// when
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla");
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "blabla", clientIP);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -102,7 +104,7 @@ namespace WireMock.Net.Tests
|
|||||||
var spec = Request.Create().WithPath("/foo").UsingPut();
|
var spec = Request.Create().WithPath("/foo").UsingPut();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT");
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", clientIP);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -116,7 +118,7 @@ namespace WireMock.Net.Tests
|
|||||||
var spec = Request.Create().WithPath("/foo").UsingPost();
|
var spec = Request.Create().WithPath("/foo").UsingPost();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST");
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "POST", clientIP);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -130,7 +132,7 @@ namespace WireMock.Net.Tests
|
|||||||
var spec = Request.Create().WithPath("/foo").UsingGet();
|
var spec = Request.Create().WithPath("/foo").UsingGet();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "GET");
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "GET", clientIP);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -146,7 +148,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "whatever";
|
string bodyAsString = "whatever";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "Delete", body, bodyAsString, Encoding.UTF8);
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "Delete", clientIP, body, bodyAsString, Encoding.UTF8);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -160,7 +162,7 @@ namespace WireMock.Net.Tests
|
|||||||
var spec = Request.Create().WithPath("/foo").UsingHead();
|
var spec = Request.Create().WithPath("/foo").UsingHead();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "HEAD");
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "HEAD", clientIP);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -174,7 +176,7 @@ namespace WireMock.Net.Tests
|
|||||||
var spec = Request.Create().WithPath("/foo").UsingPut();
|
var spec = Request.Create().WithPath("/foo").UsingPut();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "HEAD");
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "HEAD", clientIP);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -188,7 +190,7 @@ namespace WireMock.Net.Tests
|
|||||||
var spec = Request.Create().WithPath("/bar").UsingPut();
|
var spec = Request.Create().WithPath("/bar").UsingPut();
|
||||||
|
|
||||||
// when
|
// when
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT");
|
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", clientIP);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -204,7 +206,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "whatever";
|
string bodyAsString = "whatever";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, 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
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -220,7 +222,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "whatever";
|
string bodyAsString = "whatever";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, 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
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -236,7 +238,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "whatever";
|
string bodyAsString = "whatever";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, 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
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -252,7 +254,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "whatever";
|
string bodyAsString = "whatever";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
||||||
var request = new RequestMessage(new Uri("http://localhost/foo"), "PUT", body, bodyAsString, 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
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -266,7 +268,7 @@ namespace WireMock.Net.Tests
|
|||||||
var spec = Request.Create().UsingAnyVerb().WithCookie("session", "a*");
|
var spec = Request.Create().UsingAnyVerb().WithCookie("session", "a*");
|
||||||
|
|
||||||
// when
|
// 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
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -282,7 +284,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "Hello world!";
|
string bodyAsString = "Hello world!";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
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
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -298,7 +300,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "cat";
|
string bodyAsString = "cat";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
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
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -314,7 +316,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "cat";
|
string bodyAsString = "cat";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
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
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -330,7 +332,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "caR";
|
string bodyAsString = "caR";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
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
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -346,7 +348,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "The car drives in the street.";
|
string bodyAsString = "The car drives in the street.";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
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
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -362,7 +364,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "Hello";
|
string bodyAsString = "Hello";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
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
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -378,7 +380,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "Hello world!";
|
string bodyAsString = "Hello world!";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
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
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -394,7 +396,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "Hello world!";
|
string bodyAsString = "Hello world!";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
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
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -415,7 +417,7 @@ namespace WireMock.Net.Tests
|
|||||||
<todo-item id='a3'>xyz</todo-item>
|
<todo-item id='a3'>xyz</todo-item>
|
||||||
</todo-list>";
|
</todo-list>";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(xmlBodyAsString);
|
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
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -436,7 +438,7 @@ namespace WireMock.Net.Tests
|
|||||||
<todo-item id='a3'>xyz</todo-item>
|
<todo-item id='a3'>xyz</todo-item>
|
||||||
</todo-list>";
|
</todo-list>";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(xmlBodyAsString);
|
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
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -452,7 +454,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "{ \"things\": [ { \"name\": \"RequiredThing\" }, { \"name\": \"Wiremock\" } ] }";
|
string bodyAsString = "{ \"things\": [ { \"name\": \"RequiredThing\" }, { \"name\": \"Wiremock\" } ] }";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
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
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -468,7 +470,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "{ \"things\": { \"name\": \"Wiremock\" } }";
|
string bodyAsString = "{ \"things\": { \"name\": \"Wiremock\" } }";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
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
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -484,7 +486,7 @@ namespace WireMock.Net.Tests
|
|||||||
// when
|
// when
|
||||||
string bodyAsString = "xxx";
|
string bodyAsString = "xxx";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
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
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -498,7 +500,7 @@ namespace WireMock.Net.Tests
|
|||||||
var spec = Request.Create().WithParam("bar", "1", "2");
|
var spec = Request.Create().WithParam("bar", "1", "2");
|
||||||
|
|
||||||
// when
|
// 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
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -512,7 +514,7 @@ namespace WireMock.Net.Tests
|
|||||||
var spec = Request.Create().WithParam("bar");
|
var spec = Request.Create().WithParam("bar");
|
||||||
|
|
||||||
// when
|
// 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
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -526,7 +528,7 @@ namespace WireMock.Net.Tests
|
|||||||
var spec = Request.Create().UsingAnyVerb().WithParam(p => p.ContainsKey("bar"));
|
var spec = Request.Create().UsingAnyVerb().WithParam(p => p.ContainsKey("bar"));
|
||||||
|
|
||||||
// when
|
// 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
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
@@ -540,7 +542,7 @@ namespace WireMock.Net.Tests
|
|||||||
var spec = Request.Create().WithParam("bar", "1");
|
var spec = Request.Create().WithParam("bar", "1");
|
||||||
|
|
||||||
// when
|
// 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
|
// then
|
||||||
var requestMatchResult = new RequestMatchResult();
|
var requestMatchResult = new RequestMatchResult();
|
||||||
|
|||||||
@@ -11,18 +11,20 @@ namespace WireMock.Net.Tests
|
|||||||
//[TestFixture]
|
//[TestFixture]
|
||||||
public class ResponseTests
|
public class ResponseTests
|
||||||
{
|
{
|
||||||
|
private const string clientIP = "::1";
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Response_ProvideResponse_Handlebars_UrlPathVerb()
|
public async Task Response_ProvideResponse_Handlebars_UrlPathVerb()
|
||||||
{
|
{
|
||||||
// given
|
// given
|
||||||
string bodyAsString = "abc";
|
string bodyAsString = "abc";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
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()
|
var response = Response.Create()
|
||||||
.WithBody("test {{request.url}} {{request.path}} {{request.method}}")
|
.WithBody("test {{request.url}} {{request.path}} {{request.method}}")
|
||||||
.WithTransformer();
|
.WithTransformer();
|
||||||
|
|
||||||
// act
|
// act
|
||||||
var responseMessage = await response.ProvideResponseAsync(request);
|
var responseMessage = await response.ProvideResponseAsync(request);
|
||||||
|
|
||||||
@@ -36,7 +38,7 @@ namespace WireMock.Net.Tests
|
|||||||
// given
|
// given
|
||||||
string bodyAsString = "abc";
|
string bodyAsString = "abc";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
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()
|
var response = Response.Create()
|
||||||
.WithBody("test keya={{request.query.a}} idx={{request.query.a.[0]}} idx={{request.query.a.[1]}} keyb={{request.query.b}}")
|
.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
|
// given
|
||||||
string bodyAsString = "abc";
|
string bodyAsString = "abc";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
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();
|
var response = Response.Create().WithHeader("x", "{{request.headers.Content-Type}}").WithBody("test").WithTransformer();
|
||||||
|
|
||||||
@@ -64,7 +66,7 @@ namespace WireMock.Net.Tests
|
|||||||
|
|
||||||
// then
|
// then
|
||||||
Check.That(responseMessage.Body).Equals("test");
|
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]
|
[Fact]
|
||||||
@@ -73,7 +75,7 @@ namespace WireMock.Net.Tests
|
|||||||
// given
|
// given
|
||||||
string bodyAsString = "abc";
|
string bodyAsString = "abc";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
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);
|
var response = Response.Create().WithBody("test", Encoding.ASCII);
|
||||||
|
|
||||||
@@ -91,7 +93,7 @@ namespace WireMock.Net.Tests
|
|||||||
// given
|
// given
|
||||||
string bodyAsString = "abc";
|
string bodyAsString = "abc";
|
||||||
byte[] body = Encoding.UTF8.GetBytes(bodyAsString);
|
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);
|
var response = Response.Create().WithBodyAsJson(new { value = "test" }, Encoding.ASCII);
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<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="Microsoft.Owin.Host.HttpListener" Version="3.1.0" />
|
||||||
<PackageReference Include="Moq" Version="4.7.99" />
|
<PackageReference Include="Moq" Version="4.7.99" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
|
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
|
||||||
|
|||||||
Reference in New Issue
Block a user