mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-20 00:03:48 +01:00
Added X509Certificate2Filename (#27)
This commit is contained in:
@@ -8,7 +8,8 @@ A C# .NET version based on [mock4net](https://github.com/alexvictoor/mock4net) w
|
||||
|
||||
### Frameworks
|
||||
The following frameworks are supported:
|
||||
- net45 and up
|
||||
- net 4.5
|
||||
- net 4.5.2 and up
|
||||
- netstandard 1.3
|
||||
|
||||
## Stubbing
|
||||
|
||||
@@ -2,21 +2,19 @@
|
||||
using WireMock.Server;
|
||||
using WireMock.Settings;
|
||||
|
||||
namespace WireMock.Net.Console.NETCoreApp
|
||||
namespace WireMock.Net.Console.Record.NETCoreApp
|
||||
{
|
||||
static class Program
|
||||
{
|
||||
static void Main(params string[] args)
|
||||
{
|
||||
string url = "http://localhost:9095/";
|
||||
|
||||
var server = FluentMockServer.Start(new FluentMockServerSettings
|
||||
{
|
||||
Urls = new[] { url },
|
||||
Urls = new[] { "http://localhost:9095/", "https://localhost:9096/" },
|
||||
StartAdminInterface = true,
|
||||
ProxyAndRecordSettings = new ProxyAndRecordSettings
|
||||
{
|
||||
Url = "http://www.bbc.com",
|
||||
Url = "https://www.msn.com",
|
||||
SaveMapping = true
|
||||
}
|
||||
});
|
||||
|
||||
@@ -27,8 +27,11 @@ namespace WireMock.Net.StandAlone.NETCoreApp
|
||||
[ValueArgument(typeof(string), "ProxyURL", Description = "The ProxyURL to use.", Optional = true)]
|
||||
public string ProxyURL { get; set; }
|
||||
|
||||
[SwitchArgument("SaveProxyMapping", false, Description = "Save the proxied request and response mapping files in ./__admin/mappings. (default set to false).", Optional = true)]
|
||||
[SwitchArgument("SaveProxyMapping", true, Description = "Save the proxied request and response mapping files in ./__admin/mappings. (default set to true).", Optional = true)]
|
||||
public bool SaveMapping { get; set; }
|
||||
|
||||
[ValueArgument(typeof(string), "X509Certificate2", Description = "The X509Certificate2 Filename to use.", Optional = true)]
|
||||
public string X509Certificate2Filename { get; set; }
|
||||
}
|
||||
|
||||
static void Main(string[] args)
|
||||
@@ -58,7 +61,8 @@ namespace WireMock.Net.StandAlone.NETCoreApp
|
||||
settings.ProxyAndRecordSettings = new ProxyAndRecordSettings
|
||||
{
|
||||
Url = options.ProxyURL,
|
||||
SaveMapping = options.SaveMapping
|
||||
SaveMapping = options.SaveMapping,
|
||||
X509Certificate2Filename = options.X509Certificate2Filename
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -27,8 +27,11 @@ namespace WireMock.Net.StandAlone
|
||||
[ValueArgument(typeof(string), "ProxyURL", Description = "The ProxyURL to use.", Optional = true)]
|
||||
public string ProxyURL { get; set; }
|
||||
|
||||
[SwitchArgument("SaveProxyMapping", false, Description = "Save the proxied request and response mapping files in ./__admin/mappings. (default set to false).", Optional = true)]
|
||||
[SwitchArgument("SaveProxyMapping", true, Description = "Save the proxied request and response mapping files in ./__admin/mappings. (default set to true).", Optional = true)]
|
||||
public bool SaveMapping { get; set; }
|
||||
|
||||
[ValueArgument(typeof(string), "X509Certificate2", Description = "The X509Certificate2 Filename to use.", Optional = true)]
|
||||
public string X509Certificate2Filename { get; set; }
|
||||
}
|
||||
|
||||
static void Main(params string[] args)
|
||||
@@ -58,7 +61,8 @@ namespace WireMock.Net.StandAlone
|
||||
settings.ProxyAndRecordSettings = new ProxyAndRecordSettings
|
||||
{
|
||||
Url = options.ProxyURL,
|
||||
SaveMapping = options.SaveMapping
|
||||
SaveMapping = options.SaveMapping,
|
||||
X509Certificate2Filename = options.X509Certificate2Filename
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -7,10 +7,39 @@ namespace WireMock.Http
|
||||
{
|
||||
internal static class HttpClientHelper
|
||||
{
|
||||
private static HttpClient client = new HttpClient();
|
||||
|
||||
public static async Task<ResponseMessage> SendAsync(RequestMessage requestMessage, string url)
|
||||
private static HttpClient CreateHttpClient(string clientX509Certificate2Filename = null)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(clientX509Certificate2Filename))
|
||||
{
|
||||
#if NETSTANDARD || NET46
|
||||
var handler = new HttpClientHandler
|
||||
{
|
||||
ClientCertificateOptions = ClientCertificateOption.Manual,
|
||||
SslProtocols = System.Security.Authentication.SslProtocols.Tls12 | System.Security.Authentication.SslProtocols.Tls11 | System.Security.Authentication.SslProtocols.Tls,
|
||||
ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true
|
||||
};
|
||||
|
||||
handler.ClientCertificates.Add(new System.Security.Cryptography.X509Certificates.X509Certificate2(clientX509Certificate2Filename));
|
||||
return new HttpClient(handler);
|
||||
#else
|
||||
var handler = new WebRequestHandler
|
||||
{
|
||||
ClientCertificateOptions = ClientCertificateOption.Manual,
|
||||
ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true
|
||||
};
|
||||
|
||||
handler.ClientCertificates.Add(new System.Security.Cryptography.X509Certificates.X509Certificate2(clientX509Certificate2Filename));
|
||||
return new HttpClient(handler);
|
||||
#endif
|
||||
}
|
||||
|
||||
return new HttpClient();
|
||||
}
|
||||
|
||||
public static async Task<ResponseMessage> SendAsync(RequestMessage requestMessage, string url, string clientX509Certificate2Filename = null)
|
||||
{
|
||||
var client = CreateHttpClient(clientX509Certificate2Filename);
|
||||
|
||||
var httpRequestMessage = new HttpRequestMessage(new HttpMethod(requestMessage.Method), url);
|
||||
|
||||
// Overwrite the host header
|
||||
|
||||
@@ -43,25 +43,12 @@ namespace WireMock.Matchers.Request
|
||||
/// </returns>
|
||||
public double GetMatchingScore(RequestMessage requestMessage, RequestMatchResult requestMatchResult)
|
||||
{
|
||||
var list = new List<double>();
|
||||
if (_type == CompositeMatcherType.And)
|
||||
{
|
||||
foreach (var requestMatcher in RequestMatchers)
|
||||
{
|
||||
double score = requestMatcher.GetMatchingScore(requestMessage, requestMatchResult);
|
||||
list.Add(score);
|
||||
}
|
||||
|
||||
return list.Sum() / list.Count;
|
||||
}
|
||||
|
||||
foreach (var requestMatcher in RequestMatchers)
|
||||
{
|
||||
double score = requestMatcher.GetMatchingScore(requestMessage, requestMatchResult);
|
||||
list.Add(score);
|
||||
return RequestMatchers.Average(requestMatcher => requestMatcher.GetMatchingScore(requestMessage, requestMatchResult));
|
||||
}
|
||||
|
||||
return list.Max();
|
||||
return RequestMatchers.Max(requestMatcher => requestMatcher.GetMatchingScore(requestMessage, requestMatchResult));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ using System.Linq;
|
||||
using System.Xml;
|
||||
using JetBrains.Annotations;
|
||||
using WireMock.Validation;
|
||||
#if NET45
|
||||
#if !NETSTANDARD
|
||||
using Wmhelp.XPath2;
|
||||
#endif
|
||||
|
||||
@@ -41,10 +41,10 @@ namespace WireMock.Matchers
|
||||
try
|
||||
{
|
||||
var nav = new XmlDocument { InnerXml = input }.CreateNavigator();
|
||||
#if NET45
|
||||
return MatchScores.ToScore(_patterns.Select(p => true.Equals(nav.XPath2Evaluate($"boolean({p})"))));
|
||||
#else
|
||||
#if NETSTANDARD
|
||||
return MatchScores.ToScore(_patterns.Select(p => true.Equals(nav.Evaluate($"boolean({p})"))));
|
||||
#else
|
||||
return MatchScores.ToScore(_patterns.Select(p => true.Equals(nav.XPath2Evaluate($"boolean({p})"))));
|
||||
#endif
|
||||
}
|
||||
catch (Exception)
|
||||
@@ -65,9 +65,7 @@ namespace WireMock.Matchers
|
||||
/// <summary>
|
||||
/// Gets the name.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// Name
|
||||
/// </returns>
|
||||
/// <returns>Name</returns>
|
||||
public string GetName()
|
||||
{
|
||||
return "XPathMatcher";
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#if !NET45
|
||||
#if NETSTANDARD
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
|
||||
@@ -4,12 +4,11 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
#if NET45
|
||||
#if !NETSTANDARD
|
||||
using Microsoft.Owin;
|
||||
#else
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.Extensions;
|
||||
using Microsoft.AspNetCore.Http.Features;
|
||||
#endif
|
||||
|
||||
namespace WireMock.Owin
|
||||
@@ -25,14 +24,14 @@ namespace WireMock.Owin
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<RequestMessage> MapAsync(
|
||||
#if NET45
|
||||
#if !NETSTANDARD
|
||||
IOwinRequest request
|
||||
#else
|
||||
HttpRequest request
|
||||
#endif
|
||||
)
|
||||
{
|
||||
#if NET45
|
||||
#if !NETSTANDARD
|
||||
Uri url = request.Uri;
|
||||
#else
|
||||
Uri url = new Uri(request.GetEncodedUrl());
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
#if NET45
|
||||
#if !NETSTANDARD
|
||||
using Microsoft.Owin;
|
||||
#else
|
||||
using Microsoft.AspNetCore.Http;
|
||||
@@ -23,7 +23,7 @@ namespace WireMock.Owin
|
||||
/// <param name="responseMessage"></param>
|
||||
/// <param name="response"></param>
|
||||
public async Task MapAsync(ResponseMessage responseMessage
|
||||
#if NET45
|
||||
#if !NETSTANDARD
|
||||
, IOwinResponse response
|
||||
#else
|
||||
, HttpResponse response
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#if NET45
|
||||
#if !NETSTANDARD
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
|
||||
@@ -4,7 +4,7 @@ using System.Threading.Tasks;
|
||||
using WireMock.Logging;
|
||||
using WireMock.Matchers.Request;
|
||||
using System.Linq;
|
||||
#if NET45
|
||||
#if !NETSTANDARD
|
||||
using Microsoft.Owin;
|
||||
#else
|
||||
using Microsoft.AspNetCore.Http;
|
||||
@@ -12,7 +12,7 @@ using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace WireMock.Owin
|
||||
{
|
||||
#if NET45
|
||||
#if !NETSTANDARD
|
||||
internal class WireMockMiddleware : OwinMiddleware
|
||||
#else
|
||||
internal class WireMockMiddleware
|
||||
@@ -24,7 +24,7 @@ namespace WireMock.Owin
|
||||
private readonly OwinRequestMapper _requestMapper = new OwinRequestMapper();
|
||||
private readonly OwinResponseMapper _responseMapper = new OwinResponseMapper();
|
||||
|
||||
#if NET45
|
||||
#if !NETSTANDARD
|
||||
public WireMockMiddleware(OwinMiddleware next, WireMockMiddlewareOptions options) : base(next)
|
||||
{
|
||||
_options = options;
|
||||
@@ -36,7 +36,7 @@ namespace WireMock.Owin
|
||||
}
|
||||
#endif
|
||||
|
||||
#if NET45
|
||||
#if !NETSTANDARD
|
||||
public override async Task Invoke(IOwinContext ctx)
|
||||
#else
|
||||
public async Task Invoke(HttpContext ctx)
|
||||
|
||||
@@ -8,10 +8,18 @@ namespace WireMock.ResponseBuilders
|
||||
public interface IProxyResponseBuilder : IStatusCodeResponseBuilder
|
||||
{
|
||||
/// <summary>
|
||||
/// From Proxy URL.
|
||||
/// With Proxy URL.
|
||||
/// </summary>
|
||||
/// <param name="proxyUrl">The proxy url.</param>
|
||||
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
||||
IResponseBuilder WithProxy([NotNull] string proxyUrl);
|
||||
|
||||
/// <summary>
|
||||
/// With Proxy URL using X509Certificate2.
|
||||
/// </summary>
|
||||
/// <param name="proxyUrl">The proxy url.</param>
|
||||
/// <param name="clientX509Certificate2Filename">The X509Certificate2 file to use for client authentication.</param>
|
||||
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
||||
IResponseBuilder WithProxy([NotNull] string proxyUrl, [CanBeNull] string clientX509Certificate2Filename);
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,11 @@ namespace WireMock.ResponseBuilders
|
||||
/// </summary>
|
||||
public string ProxyUrl { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// The client X509Certificate2Filename to use.
|
||||
/// </summary>
|
||||
public string X509Certificate2Filename { get; private set; } = null;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the response message.
|
||||
/// </summary>
|
||||
@@ -190,7 +195,7 @@ namespace WireMock.ResponseBuilders
|
||||
/// <param name="bodyAsbase64">The body asbase64.</param>
|
||||
/// <param name="encoding">The Encoding.</param>
|
||||
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
||||
public IResponseBuilder WithBodyAsBase64([NotNull] string bodyAsbase64, Encoding encoding = null)
|
||||
public IResponseBuilder WithBodyAsBase64(string bodyAsbase64, Encoding encoding = null)
|
||||
{
|
||||
Check.NotNull(bodyAsbase64, nameof(bodyAsbase64));
|
||||
|
||||
@@ -237,7 +242,7 @@ namespace WireMock.ResponseBuilders
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// From Proxy URL.
|
||||
/// With Proxy URL.
|
||||
/// </summary>
|
||||
/// <param name="proxyUrl">The proxy url.</param>
|
||||
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
||||
@@ -250,6 +255,22 @@ namespace WireMock.ResponseBuilders
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// With Proxy URL.
|
||||
/// </summary>
|
||||
/// <param name="proxyUrl">The proxy url.</param>
|
||||
/// <param name="clientX509Certificate2Filename">The X509Certificate2 file to use for client authentication.</param>
|
||||
/// <returns>A <see cref="IResponseBuilder"/>.</returns>
|
||||
public IResponseBuilder WithProxy(string proxyUrl, string clientX509Certificate2Filename)
|
||||
{
|
||||
Check.NotEmpty(proxyUrl, nameof(proxyUrl));
|
||||
Check.NotEmpty(clientX509Certificate2Filename, nameof(clientX509Certificate2Filename));
|
||||
|
||||
ProxyUrl = proxyUrl;
|
||||
X509Certificate2Filename = clientX509Certificate2Filename;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The provide response.
|
||||
/// </summary>
|
||||
@@ -264,9 +285,10 @@ namespace WireMock.ResponseBuilders
|
||||
|
||||
if (ProxyUrl != null)
|
||||
{
|
||||
return await HttpClientHelper.SendAsync(requestMessage, ProxyUrl);
|
||||
return await HttpClientHelper.SendAsync(requestMessage, ProxyUrl, X509Certificate2Filename);
|
||||
}
|
||||
else if (UseTransformer)
|
||||
|
||||
if (UseTransformer)
|
||||
{
|
||||
return ResponseMessageTransformer.Transform(requestMessage, ResponseMessage);
|
||||
}
|
||||
|
||||
@@ -164,10 +164,10 @@ namespace WireMock.Server
|
||||
Urls = new[] { (settings.UseSSL == true ? "https" : "http") + "://localhost:" + port + "/" };
|
||||
}
|
||||
|
||||
#if NET45
|
||||
_httpServer = new OwinSelfHost(_options, Urls);
|
||||
#else
|
||||
#if NETSTANDARD
|
||||
_httpServer = new AspNetCoreSelfHost(_options, Urls);
|
||||
#else
|
||||
_httpServer = new OwinSelfHost(_options, Urls);
|
||||
#endif
|
||||
Ports = _httpServer.Ports;
|
||||
|
||||
|
||||
@@ -14,5 +14,10 @@
|
||||
/// Save the mapping for each request/response.
|
||||
/// </summary>
|
||||
public bool SaveMapping { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// The clientCertificateFilename to use. Example : "C:\certificates\cert.pfx"
|
||||
/// </summary>
|
||||
public string X509Certificate2Filename { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@
|
||||
<AssemblyTitle>WireMock.Net</AssemblyTitle>
|
||||
<Version>1.0.2.0</Version>
|
||||
<Authors>Alexandre Victoor;Stef Heyenrath</Authors>
|
||||
<TargetFrameworks>net45;netstandard1.3</TargetFrameworks>
|
||||
<TargetFrameworks>net45;net452;net46;netstandard1.3</TargetFrameworks>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<AssemblyName>WireMock.Net</AssemblyName>
|
||||
<PackageId>WireMock.Net</PackageId>
|
||||
@@ -26,28 +26,31 @@
|
||||
<DefineConstants>NETSTANDARD</DefineConstants>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="Server\FluentMockServer.LogEntries.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="JetBrains.Annotations" Version="10.4.0">
|
||||
<PrivateAssets>All</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
|
||||
<PackageReference Include="SimMetrics.Net" Version="1.0.3" />
|
||||
<PackageReference Include="System.Net.Http" Version="4.3.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
|
||||
<PackageReference Include="Microsoft.AspNet.WebApi.OwinSelfHost" Version="5.2.3" />
|
||||
<PackageReference Include="Handlebars.Net" Version="1.8.0" />
|
||||
<PackageReference Include="XPath2" Version="1.0.3.1" />
|
||||
<Reference Include="System.Net.Http.WebRequest" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition=" '$(TargetFramework)' == 'net451' ">
|
||||
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
|
||||
<PackageReference Include="System.Net.Http" Version="4.3.0" />
|
||||
<PackageReference Include="System.Threading.Tasks" Version="4.3.0" />
|
||||
<ItemGroup Condition=" '$(TargetFramework)' == 'net452' ">
|
||||
<PackageReference Include="Microsoft.AspNet.WebApi.OwinSelfHost" Version="5.2.3" />
|
||||
<PackageReference Include="Handlebars.Net" Version="1.8.0" />
|
||||
<PackageReference Include="XPath2" Version="1.0.3.1" />
|
||||
<Reference Include="System.Net.Http.WebRequest" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition=" '$(TargetFramework)' == 'net46' ">
|
||||
<PackageReference Include="Microsoft.AspNet.WebApi.OwinSelfHost" Version="5.2.3" />
|
||||
<PackageReference Include="Handlebars.Net" Version="1.8.0" />
|
||||
<PackageReference Include="XPath2" Version="1.0.3.1" />
|
||||
</ItemGroup>
|
||||
@@ -55,7 +58,6 @@
|
||||
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
|
||||
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
|
||||
<PackageReference Include="Handlebars.NetStandard" Version="1.8.1" />
|
||||
<PackageReference Include="System.Net.Http" Version="4.3.0" />
|
||||
<PackageReference Include="System.Xml.XmlDocument" Version="4.3.0" />
|
||||
<PackageReference Include="System.Xml.XPath.XmlDocument" Version="4.3.0" />
|
||||
</ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user