Initial code for proxy and record #27

This commit is contained in:
Stef Heyenrath
2017-05-09 21:43:10 +02:00
parent b25371cf15
commit 31261ec45d
14 changed files with 160 additions and 10 deletions

View File

@@ -4,6 +4,7 @@ using System.Linq;
using CommandLineParser.Arguments;
using CommandLineParser.Exceptions;
using WireMock.Server;
using WireMock.Settings;
namespace WireMock.Net.StandAlone.NETCoreApp
{

View File

@@ -4,6 +4,7 @@ using System.Linq;
using CommandLineParser.Arguments;
using CommandLineParser.Exceptions;
using WireMock.Server;
using WireMock.Settings;
namespace WireMock.Net.StandAlone
{

View File

@@ -2,6 +2,7 @@
using System.Threading.Tasks;
using JetBrains.Annotations;
using WireMock.Validation;
using WireMock.Settings;
namespace WireMock
{
@@ -21,4 +22,41 @@ namespace WireMock
return Task.FromResult(_responseMessageFunc(requestMessage));
}
}
internal class DynamicAsyncResponseProvider : IResponseProvider
{
private readonly Func<RequestMessage, Task<ResponseMessage>> _responseMessageFunc;
public DynamicAsyncResponseProvider([NotNull] Func<RequestMessage, Task<ResponseMessage>> responseMessageFunc)
{
Check.NotNull(responseMessageFunc, nameof(responseMessageFunc));
_responseMessageFunc = responseMessageFunc;
}
public Task<ResponseMessage> ProvideResponseAsync(RequestMessage requestMessage)
{
return _responseMessageFunc(requestMessage);
}
}
internal class ProxyAsyncResponseProvider : IResponseProvider
{
private readonly Func<RequestMessage, ProxyAndRecordSettings, Task<ResponseMessage>> _responseMessageFunc;
private readonly ProxyAndRecordSettings _settings;
public ProxyAsyncResponseProvider([NotNull] Func<RequestMessage, ProxyAndRecordSettings, Task<ResponseMessage>> responseMessageFunc, [NotNull] ProxyAndRecordSettings settings)
{
Check.NotNull(responseMessageFunc, nameof(responseMessageFunc));
Check.NotNull(settings, nameof(settings));
_responseMessageFunc = responseMessageFunc;
_settings = settings;
}
public Task<ResponseMessage> ProvideResponseAsync(RequestMessage requestMessage)
{
return _responseMessageFunc(requestMessage, _settings);
}
}
}

View File

@@ -91,6 +91,6 @@ namespace WireMock
/// <value>
/// <c>true</c> if this mapping is an Admin Interface; otherwise, <c>false</c>.
/// </value>
public bool IsAdminInterface => Provider is DynamicResponseProvider;
public bool IsAdminInterface => Provider is DynamicResponseProvider || Provider is DynamicAsyncResponseProvider || Provider is ProxyAsyncResponseProvider;
}
}

View File

@@ -16,6 +16,9 @@ using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Util;
using WireMock.Validation;
using WireMock.Http;
using System.Threading.Tasks;
using WireMock.Settings;
namespace WireMock.Server
{
@@ -117,6 +120,20 @@ namespace WireMock.Server
Given(Request.Create().WithPath(AdminRequests + "/find").UsingPost()).RespondWith(new DynamicResponseProvider(RequestsFind));
}
private void InitProxyAndRecord(ProxyAndRecordSettings settings)
{
Given(Request.Create().WithPath("/*").UsingAnyVerb()).RespondWith(new ProxyAsyncResponseProvider(ProxyAndRecordAsync, settings));
}
#region Proxy and Record
private async Task<ResponseMessage> ProxyAndRecordAsync(RequestMessage requestMessage, ProxyAndRecordSettings settings)
{
var responseMessage = await HttpClientHelper.SendAsync(requestMessage, settings.Url);
return responseMessage;
}
#endregion
#region Settings
private ResponseMessage SettingsGet(RequestMessage requestMessage)
{

View File

@@ -9,6 +9,7 @@ using WireMock.Http;
using WireMock.Matchers;
using WireMock.Matchers.Request;
using WireMock.RequestBuilders;
using WireMock.Settings;
using WireMock.Validation;
using WireMock.Owin;
@@ -181,6 +182,11 @@ namespace WireMock.Server
{
ReadStaticMappings();
}
if (settings.ProxyAndRecordSettings != null)
{
InitProxyAndRecord(settings.ProxyAndRecordSettings);
}
}
/// <summary>
@@ -235,7 +241,7 @@ namespace WireMock.Server
{
lock (((ICollection)_options.Mappings).SyncRoot)
{
_options.Mappings = _options.Mappings.Where(m => m.Provider is DynamicResponseProvider).ToList();
_options.Mappings = _options.Mappings.Where(m => m.IsAdminInterface).ToList();
}
}

View File

@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
namespace WireMock.Server
{
@@ -38,9 +39,13 @@ namespace WireMock.Server
/// <summary>
/// The respond with.
/// </summary>
/// <param name="provider">
/// The provider.
/// </param>
/// <param name="provider">The provider.</param>
void RespondWith(IResponseProvider provider);
///// <summary>
///// The respond with.
///// </summary>
///// <param name="provider">The provider.</param>
//Task RespondWithAsync(IResponseProvider provider);
}
}

View File

@@ -1,4 +1,4 @@
namespace WireMock.Server
namespace WireMock.Settings
{
/// <summary>
/// FluentMockServerSettings
@@ -31,13 +31,17 @@
public bool? StartAdminInterface { get; set; }
/// <summary>
/// Gets or sets the read static mappings.
/// Gets or sets if the static mappings should be read at startup.
/// </summary>
/// <value>
/// The read static mappings.
/// </value>
/// <value>true/false</value>
public bool? ReadStaticMappings { get; set; }
/// <summary>
/// Gets or sets if the server should record and save requests and responses.
/// </summary>
/// <value>true/false</value>
public ProxyAndRecordSettings ProxyAndRecordSettings { get; set; }
/// <summary>
/// Gets or sets the urls.
/// </summary>

View File

@@ -0,0 +1,18 @@
namespace WireMock.Settings
{
/// <summary>
/// RecordAndSaveSettings
/// </summary>
public class ProxyAndRecordSettings
{
/// <summary>
/// The URL to proxy.
/// </summary>
public string Url { get; set; }
/// <summary>
/// Save the mapping for each request/response.
/// </summary>
public bool SaveMapping { get; set; } = true;
}
}