mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-03-25 10:31:01 +01:00
Add WireMock.Net.WebApplication example (#75)
This commit is contained in:
@@ -18,7 +18,7 @@ namespace WireMock.Net.StandAlone
|
||||
/// </summary>
|
||||
/// <param name="settings">The FluentMockServerSettings</param>
|
||||
[PublicAPI]
|
||||
public static FluentMockServer Start([NotNull] FluentMockServerSettings settings)
|
||||
public static FluentMockServer Start([NotNull] IFluentMockServerSettings settings)
|
||||
{
|
||||
Check.NotNull(settings, nameof(settings));
|
||||
|
||||
|
||||
@@ -42,10 +42,10 @@ namespace WireMock
|
||||
|
||||
internal class ProxyAsyncResponseProvider : IResponseProvider
|
||||
{
|
||||
private readonly Func<RequestMessage, ProxyAndRecordSettings, Task<ResponseMessage>> _responseMessageFunc;
|
||||
private readonly ProxyAndRecordSettings _settings;
|
||||
private readonly Func<RequestMessage, IProxyAndRecordSettings, Task<ResponseMessage>> _responseMessageFunc;
|
||||
private readonly IProxyAndRecordSettings _settings;
|
||||
|
||||
public ProxyAsyncResponseProvider([NotNull] Func<RequestMessage, ProxyAndRecordSettings, Task<ResponseMessage>> responseMessageFunc, [NotNull] ProxyAndRecordSettings settings)
|
||||
public ProxyAsyncResponseProvider([NotNull] Func<RequestMessage, IProxyAndRecordSettings, Task<ResponseMessage>> responseMessageFunc, [NotNull] IProxyAndRecordSettings settings)
|
||||
{
|
||||
Check.NotNull(responseMessageFunc, nameof(responseMessageFunc));
|
||||
Check.NotNull(settings, nameof(settings));
|
||||
|
||||
@@ -132,13 +132,13 @@ namespace WireMock.Server
|
||||
#region Proxy and Record
|
||||
private HttpClient _httpClientForProxy;
|
||||
|
||||
private void InitProxyAndRecord(ProxyAndRecordSettings settings)
|
||||
private void InitProxyAndRecord(IProxyAndRecordSettings settings)
|
||||
{
|
||||
_httpClientForProxy = HttpClientHelper.CreateHttpClient(settings.X509Certificate2ThumbprintOrSubjectName);
|
||||
Given(Request.Create().WithPath("/*").UsingAnyVerb()).RespondWith(new ProxyAsyncResponseProvider(ProxyAndRecordAsync, settings));
|
||||
}
|
||||
|
||||
private async Task<ResponseMessage> ProxyAndRecordAsync(RequestMessage requestMessage, ProxyAndRecordSettings settings)
|
||||
private async Task<ResponseMessage> ProxyAndRecordAsync(RequestMessage requestMessage, IProxyAndRecordSettings settings)
|
||||
{
|
||||
var requestUri = new Uri(requestMessage.Url);
|
||||
var proxyUri = new Uri(settings.Url);
|
||||
|
||||
@@ -25,12 +25,15 @@ namespace WireMock.Server
|
||||
private readonly IOwinSelfHost _httpServer;
|
||||
private readonly WireMockMiddlewareOptions _options = new WireMockMiddlewareOptions();
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether this server is started.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public bool IsStarted { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the ports.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The ports.
|
||||
/// </value>
|
||||
[PublicAPI]
|
||||
public List<int> Ports { get; }
|
||||
|
||||
@@ -59,7 +62,7 @@ namespace WireMock.Server
|
||||
/// <param name="settings">The FluentMockServerSettings.</param>
|
||||
/// <returns>The <see cref="FluentMockServer"/>.</returns>
|
||||
[PublicAPI]
|
||||
public static FluentMockServer Start(FluentMockServerSettings settings)
|
||||
public static FluentMockServer Start(IFluentMockServerSettings settings)
|
||||
{
|
||||
Check.NotNull(settings, nameof(settings));
|
||||
|
||||
@@ -150,7 +153,7 @@ namespace WireMock.Server
|
||||
});
|
||||
}
|
||||
|
||||
private FluentMockServer(FluentMockServerSettings settings)
|
||||
private FluentMockServer(IFluentMockServerSettings settings)
|
||||
{
|
||||
if (settings.Urls != null)
|
||||
{
|
||||
@@ -170,6 +173,8 @@ namespace WireMock.Server
|
||||
#else
|
||||
_httpServer = new OwinSelfHost(_options, Urls);
|
||||
#endif
|
||||
IsStarted = _httpServer.IsStarted;
|
||||
|
||||
Ports = _httpServer.Ports;
|
||||
|
||||
_httpServer.StartAsync();
|
||||
|
||||
@@ -6,91 +6,62 @@ namespace WireMock.Settings
|
||||
/// <summary>
|
||||
/// FluentMockServerSettings
|
||||
/// </summary>
|
||||
public class FluentMockServerSettings
|
||||
public class FluentMockServerSettings : IFluentMockServerSettings
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the port.
|
||||
/// </summary>
|
||||
/// <inheritdoc cref="IFluentMockServerSettings.Port"/>
|
||||
[PublicAPI]
|
||||
public int? Port { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the use SSL.
|
||||
/// </summary>
|
||||
// ReSharper disable once InconsistentNaming
|
||||
/// <inheritdoc cref="IFluentMockServerSettings.UseSSL"/>
|
||||
[PublicAPI]
|
||||
// ReSharper disable once InconsistentNaming
|
||||
public bool? UseSSL { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets wether to start admin interface.
|
||||
/// </summary>
|
||||
/// <inheritdoc cref="IFluentMockServerSettings.StartAdminInterface"/>
|
||||
[PublicAPI]
|
||||
public bool? StartAdminInterface { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets if the static mappings should be read at startup.
|
||||
/// </summary>
|
||||
/// <inheritdoc cref="IFluentMockServerSettings.ReadStaticMappings"/>
|
||||
[PublicAPI]
|
||||
public bool? ReadStaticMappings { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets if the server should record and save requests and responses.
|
||||
/// </summary>
|
||||
/// <value>true/false</value>
|
||||
/// <inheritdoc cref="IFluentMockServerSettings.ProxyAndRecordSettings"/>
|
||||
[PublicAPI]
|
||||
public ProxyAndRecordSettings ProxyAndRecordSettings { get; set; }
|
||||
public IProxyAndRecordSettings ProxyAndRecordSettings { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the urls.
|
||||
/// </summary>
|
||||
/// <inheritdoc cref="IFluentMockServerSettings.Urls"/>
|
||||
[PublicAPI]
|
||||
public string[] Urls { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// StartTimeout
|
||||
/// </summary>
|
||||
/// <inheritdoc cref="IFluentMockServerSettings.StartTimeout"/>
|
||||
[PublicAPI]
|
||||
public int StartTimeout { get; set; } = 10000;
|
||||
|
||||
/// <summary>
|
||||
/// Allow Partial Mapping (default set to false).
|
||||
/// </summary>
|
||||
/// <inheritdoc cref="IFluentMockServerSettings.AllowPartialMapping"/>
|
||||
[PublicAPI]
|
||||
public bool? AllowPartialMapping { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The username needed for __admin access.
|
||||
/// </summary>
|
||||
/// <inheritdoc cref="IFluentMockServerSettings.AdminUsername"/>
|
||||
[PublicAPI]
|
||||
public string AdminUsername { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The password needed for __admin access.
|
||||
/// </summary>
|
||||
/// <inheritdoc cref="IFluentMockServerSettings.AdminPassword"/>
|
||||
[PublicAPI]
|
||||
public string AdminPassword { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The RequestLog expiration in hours (optional).
|
||||
/// </summary>
|
||||
/// <inheritdoc cref="IFluentMockServerSettings.RequestLogExpirationDuration"/>
|
||||
[PublicAPI]
|
||||
public int? RequestLogExpirationDuration { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The MaxRequestLog count (optional).
|
||||
/// </summary>
|
||||
/// <inheritdoc cref="IFluentMockServerSettings.MaxRequestLogCount"/>
|
||||
[PublicAPI]
|
||||
public int? MaxRequestLogCount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Action which is called (with the IAppBuilder or IApplicationBuilder) before the internal WireMockMiddleware is initialized. [Optional]
|
||||
/// </summary>
|
||||
/// <inheritdoc cref="IFluentMockServerSettings.PreWireMockMiddlewareInit"/>
|
||||
[PublicAPI]
|
||||
public Action<object> PreWireMockMiddlewareInit { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Action which is called (with the IAppBuilder or IApplicationBuilder) after the internal WireMockMiddleware is initialized. [Optional]
|
||||
/// </summary>
|
||||
/// <inheritdoc cref="IFluentMockServerSettings.PostWireMockMiddlewareInit"/>
|
||||
[PublicAPI]
|
||||
public Action<object> PostWireMockMiddlewareInit { get; set; }
|
||||
}
|
||||
|
||||
81
src/WireMock.Net/Settings/IFluentMockServerSettings.cs
Normal file
81
src/WireMock.Net/Settings/IFluentMockServerSettings.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
using System;
|
||||
|
||||
namespace WireMock.Settings
|
||||
{
|
||||
/// <summary>
|
||||
/// IFluentMockServerSettings
|
||||
/// </summary>
|
||||
public interface IFluentMockServerSettings
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the port.
|
||||
/// </summary>
|
||||
int? Port { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the use SSL.
|
||||
/// </summary>
|
||||
// ReSharper disable once InconsistentNaming
|
||||
bool? UseSSL { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets wether to start admin interface.
|
||||
/// </summary>
|
||||
bool? StartAdminInterface { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets if the static mappings should be read at startup.
|
||||
/// </summary>
|
||||
bool? ReadStaticMappings { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets if the proxy and record settings.
|
||||
/// </summary>
|
||||
IProxyAndRecordSettings ProxyAndRecordSettings { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the urls.
|
||||
/// </summary>
|
||||
string[] Urls { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// StartTimeout
|
||||
/// </summary>
|
||||
int StartTimeout { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Allow Partial Mapping (default set to false).
|
||||
/// </summary>
|
||||
bool? AllowPartialMapping { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The username needed for __admin access.
|
||||
/// </summary>
|
||||
string AdminUsername { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The password needed for __admin access.
|
||||
/// </summary>
|
||||
string AdminPassword { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The RequestLog expiration in hours (optional).
|
||||
/// </summary>
|
||||
int? RequestLogExpirationDuration { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The MaxRequestLog count (optional).
|
||||
/// </summary>
|
||||
int? MaxRequestLogCount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Action which is called (with the IAppBuilder or IApplicationBuilder) before the internal WireMockMiddleware is initialized. [Optional]
|
||||
/// </summary>
|
||||
Action<object> PreWireMockMiddlewareInit { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Action which is called (with the IAppBuilder or IApplicationBuilder) after the internal WireMockMiddleware is initialized. [Optional]
|
||||
/// </summary>
|
||||
Action<object> PostWireMockMiddlewareInit { get; set; }
|
||||
}
|
||||
}
|
||||
28
src/WireMock.Net/Settings/IProxyAndRecordSettings.cs
Normal file
28
src/WireMock.Net/Settings/IProxyAndRecordSettings.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
namespace WireMock.Settings
|
||||
{
|
||||
/// <summary>
|
||||
/// IRecordAndSaveSettings
|
||||
/// </summary>
|
||||
public interface IProxyAndRecordSettings
|
||||
{
|
||||
/// <summary>
|
||||
/// The URL to proxy.
|
||||
/// </summary>
|
||||
string Url { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Save the mapping for each request/response to the internal Mappings.
|
||||
/// </summary>
|
||||
bool SaveMapping { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Save the mapping for each request/response to also file. (Note that SaveMapping must also be set to true.)
|
||||
/// </summary>
|
||||
bool SaveMappingToFile { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The clientCertificate thumbprint or subject name fragment to use. Example thumbprint : "D2DBF135A8D06ACCD0E1FAD9BFB28678DF7A9818". Example subject name: "www.google.com""
|
||||
/// </summary>
|
||||
string X509Certificate2ThumbprintOrSubjectName { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,28 +1,26 @@
|
||||
namespace WireMock.Settings
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace WireMock.Settings
|
||||
{
|
||||
/// <summary>
|
||||
/// RecordAndSaveSettings
|
||||
/// </summary>
|
||||
public class ProxyAndRecordSettings
|
||||
public class ProxyAndRecordSettings : IProxyAndRecordSettings
|
||||
{
|
||||
/// <summary>
|
||||
/// The URL to proxy.
|
||||
/// </summary>
|
||||
/// <inheritdoc cref="IProxyAndRecordSettings.Url"/>
|
||||
[PublicAPI]
|
||||
public string Url { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Save the mapping for each request/response to the internal Mappings.
|
||||
/// </summary>
|
||||
/// <inheritdoc cref="IProxyAndRecordSettings.SaveMapping"/>
|
||||
[PublicAPI]
|
||||
public bool SaveMapping { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Save the mapping for each request/response to also file. (Note that SaveMapping must also be set to true.)
|
||||
/// </summary>
|
||||
/// <inheritdoc cref="IProxyAndRecordSettings.SaveMappingToFile"/>
|
||||
[PublicAPI]
|
||||
public bool SaveMappingToFile { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// The clientCertificate thumbprint or subject name fragment to use. Example thumbprint : "D2DBF135A8D06ACCD0E1FAD9BFB28678DF7A9818". Example subject name: "www.google.com""
|
||||
/// </summary>
|
||||
/// <inheritdoc cref="IProxyAndRecordSettings.X509Certificate2ThumbprintOrSubjectName"/>
|
||||
[PublicAPI]
|
||||
public string X509Certificate2ThumbprintOrSubjectName { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -34,9 +34,9 @@
|
||||
</PackageReference>
|
||||
<PackageReference Include="Handlebars.Net" Version="1.9.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="10.0.1" />
|
||||
<PackageReference Include="SimMetrics.Net" Version="1.0.3" />
|
||||
<PackageReference Include="System.Net.Http" Version="4.3.2" />
|
||||
<PackageReference Include="RestEase" Version="1.4.2" />
|
||||
<PackageReference Include="SimMetrics.Net" Version="1.0.4" />
|
||||
<PackageReference Include="System.Net.Http" Version="4.3.3" />
|
||||
<PackageReference Include="RestEase" Version="1.4.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition=" '$(TargetFramework)' == 'net452' ">
|
||||
@@ -51,7 +51,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
|
||||
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
|
||||
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.5" />
|
||||
<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