mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-21 00:11:44 +02:00
Initial code for proxy and record #27
This commit is contained in:
@@ -35,6 +35,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WireMock.Net.StandAlone.NET
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WireMock.Net.Console.NETCoreApp", "examples\WireMock.Net.Console.NETCoreApp\WireMock.Net.Console.NETCoreApp.csproj", "{FE281639-B014-4C8A-96FA-141164A74713}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WireMock.Net.Console.NETCoreApp", "examples\WireMock.Net.Console.NETCoreApp\WireMock.Net.Console.NETCoreApp.csproj", "{FE281639-B014-4C8A-96FA-141164A74713}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net.Console.Record.NETCoreApp", "examples\WireMock.Net.Console.Record.NETCoreApp\WireMock.Net.Console.Record.NETCoreApp.csproj", "{1995E414-F197-4AB4-90C2-68D806B5AF59}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@@ -65,6 +67,10 @@ Global
|
|||||||
{FE281639-B014-4C8A-96FA-141164A74713}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{FE281639-B014-4C8A-96FA-141164A74713}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{FE281639-B014-4C8A-96FA-141164A74713}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{FE281639-B014-4C8A-96FA-141164A74713}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{FE281639-B014-4C8A-96FA-141164A74713}.Release|Any CPU.Build.0 = Release|Any CPU
|
{FE281639-B014-4C8A-96FA-141164A74713}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{1995E414-F197-4AB4-90C2-68D806B5AF59}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{1995E414-F197-4AB4-90C2-68D806B5AF59}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{1995E414-F197-4AB4-90C2-68D806B5AF59}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{1995E414-F197-4AB4-90C2-68D806B5AF59}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
@@ -76,5 +82,6 @@ Global
|
|||||||
{31DC2EF8-C3FE-467D-84BE-FB5D956E612E} = {890A1DED-C229-4FA1-969E-AAC3BBFC05E5}
|
{31DC2EF8-C3FE-467D-84BE-FB5D956E612E} = {890A1DED-C229-4FA1-969E-AAC3BBFC05E5}
|
||||||
{14D7298C-2BE5-42C3-A3D5-9433E77218F9} = {EF242EDF-7133-4277-9A0C-18744DE08707}
|
{14D7298C-2BE5-42C3-A3D5-9433E77218F9} = {EF242EDF-7133-4277-9A0C-18744DE08707}
|
||||||
{FE281639-B014-4C8A-96FA-141164A74713} = {F0C22C47-DF71-463C-9B04-B4E0F3B8708A}
|
{FE281639-B014-4C8A-96FA-141164A74713} = {F0C22C47-DF71-463C-9B04-B4E0F3B8708A}
|
||||||
|
{1995E414-F197-4AB4-90C2-68D806B5AF59} = {F0C22C47-DF71-463C-9B04-B4E0F3B8708A}
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
EndGlobal
|
EndGlobal
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ using WireMock.Matchers;
|
|||||||
using WireMock.RequestBuilders;
|
using WireMock.RequestBuilders;
|
||||||
using WireMock.ResponseBuilders;
|
using WireMock.ResponseBuilders;
|
||||||
using WireMock.Server;
|
using WireMock.Server;
|
||||||
|
using WireMock.Settings;
|
||||||
|
|
||||||
namespace WireMock.Net.Console.NETCoreApp
|
namespace WireMock.Net.Console.NETCoreApp
|
||||||
{
|
{
|
||||||
|
|||||||
32
examples/WireMock.Net.Console.Record.NETCoreApp/Program.cs
Normal file
32
examples/WireMock.Net.Console.Record.NETCoreApp/Program.cs
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
using Newtonsoft.Json;
|
||||||
|
using WireMock.Server;
|
||||||
|
using WireMock.Settings;
|
||||||
|
|
||||||
|
namespace WireMock.Net.Console.NETCoreApp
|
||||||
|
{
|
||||||
|
static class Program
|
||||||
|
{
|
||||||
|
static void Main(params string[] args)
|
||||||
|
{
|
||||||
|
string url1 = "http://localhost:9095/";
|
||||||
|
|
||||||
|
var server = FluentMockServer.Start(new FluentMockServerSettings
|
||||||
|
{
|
||||||
|
Urls = new[] { url1 },
|
||||||
|
StartAdminInterface = true,
|
||||||
|
ProxyAndRecordSettings = new ProxyAndRecordSettings { Url = "http://www.bbc.com" }
|
||||||
|
});
|
||||||
|
|
||||||
|
System.Console.WriteLine("Press any key to stop the server");
|
||||||
|
System.Console.ReadKey();
|
||||||
|
server.Stop();
|
||||||
|
|
||||||
|
System.Console.WriteLine("Displaying all requests");
|
||||||
|
var allRequests = server.LogEntries;
|
||||||
|
System.Console.WriteLine(JsonConvert.SerializeObject(allRequests, Formatting.Indented));
|
||||||
|
|
||||||
|
System.Console.WriteLine("Press any key to quit");
|
||||||
|
System.Console.ReadKey();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>netcoreapp1.1</TargetFramework>
|
||||||
|
<ApplicationIcon>../../WireMock.Net-Logo.ico</ApplicationIcon>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="CommandLineArgumentsParser" Version="3.0.9" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\..\src\WireMock.Net\WireMock.Net.csproj" />
|
||||||
|
<!--<ProjectReference Include="..\WireMock.Net\WireMock.Net.csproj" />-->
|
||||||
|
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -5,6 +5,7 @@ using WireMock.Matchers;
|
|||||||
using WireMock.RequestBuilders;
|
using WireMock.RequestBuilders;
|
||||||
using WireMock.ResponseBuilders;
|
using WireMock.ResponseBuilders;
|
||||||
using WireMock.Server;
|
using WireMock.Server;
|
||||||
|
using WireMock.Settings;
|
||||||
|
|
||||||
namespace WireMock.Net.ConsoleApplication
|
namespace WireMock.Net.ConsoleApplication
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ using System.Linq;
|
|||||||
using CommandLineParser.Arguments;
|
using CommandLineParser.Arguments;
|
||||||
using CommandLineParser.Exceptions;
|
using CommandLineParser.Exceptions;
|
||||||
using WireMock.Server;
|
using WireMock.Server;
|
||||||
|
using WireMock.Settings;
|
||||||
|
|
||||||
namespace WireMock.Net.StandAlone.NETCoreApp
|
namespace WireMock.Net.StandAlone.NETCoreApp
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ using System.Linq;
|
|||||||
using CommandLineParser.Arguments;
|
using CommandLineParser.Arguments;
|
||||||
using CommandLineParser.Exceptions;
|
using CommandLineParser.Exceptions;
|
||||||
using WireMock.Server;
|
using WireMock.Server;
|
||||||
|
using WireMock.Settings;
|
||||||
|
|
||||||
namespace WireMock.Net.StandAlone
|
namespace WireMock.Net.StandAlone
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
using WireMock.Validation;
|
using WireMock.Validation;
|
||||||
|
using WireMock.Settings;
|
||||||
|
|
||||||
namespace WireMock
|
namespace WireMock
|
||||||
{
|
{
|
||||||
@@ -21,4 +22,41 @@ namespace WireMock
|
|||||||
return Task.FromResult(_responseMessageFunc(requestMessage));
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -91,6 +91,6 @@ namespace WireMock
|
|||||||
/// <value>
|
/// <value>
|
||||||
/// <c>true</c> if this mapping is an Admin Interface; otherwise, <c>false</c>.
|
/// <c>true</c> if this mapping is an Admin Interface; otherwise, <c>false</c>.
|
||||||
/// </value>
|
/// </value>
|
||||||
public bool IsAdminInterface => Provider is DynamicResponseProvider;
|
public bool IsAdminInterface => Provider is DynamicResponseProvider || Provider is DynamicAsyncResponseProvider || Provider is ProxyAsyncResponseProvider;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -16,6 +16,9 @@ using WireMock.RequestBuilders;
|
|||||||
using WireMock.ResponseBuilders;
|
using WireMock.ResponseBuilders;
|
||||||
using WireMock.Util;
|
using WireMock.Util;
|
||||||
using WireMock.Validation;
|
using WireMock.Validation;
|
||||||
|
using WireMock.Http;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using WireMock.Settings;
|
||||||
|
|
||||||
namespace WireMock.Server
|
namespace WireMock.Server
|
||||||
{
|
{
|
||||||
@@ -117,6 +120,20 @@ namespace WireMock.Server
|
|||||||
Given(Request.Create().WithPath(AdminRequests + "/find").UsingPost()).RespondWith(new DynamicResponseProvider(RequestsFind));
|
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
|
#region Settings
|
||||||
private ResponseMessage SettingsGet(RequestMessage requestMessage)
|
private ResponseMessage SettingsGet(RequestMessage requestMessage)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ using WireMock.Http;
|
|||||||
using WireMock.Matchers;
|
using WireMock.Matchers;
|
||||||
using WireMock.Matchers.Request;
|
using WireMock.Matchers.Request;
|
||||||
using WireMock.RequestBuilders;
|
using WireMock.RequestBuilders;
|
||||||
|
using WireMock.Settings;
|
||||||
using WireMock.Validation;
|
using WireMock.Validation;
|
||||||
using WireMock.Owin;
|
using WireMock.Owin;
|
||||||
|
|
||||||
@@ -181,6 +182,11 @@ namespace WireMock.Server
|
|||||||
{
|
{
|
||||||
ReadStaticMappings();
|
ReadStaticMappings();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (settings.ProxyAndRecordSettings != null)
|
||||||
|
{
|
||||||
|
InitProxyAndRecord(settings.ProxyAndRecordSettings);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -235,7 +241,7 @@ namespace WireMock.Server
|
|||||||
{
|
{
|
||||||
lock (((ICollection)_options.Mappings).SyncRoot)
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace WireMock.Server
|
namespace WireMock.Server
|
||||||
{
|
{
|
||||||
@@ -38,9 +39,13 @@ namespace WireMock.Server
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The respond with.
|
/// The respond with.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="provider">
|
/// <param name="provider">The provider.</param>
|
||||||
/// The provider.
|
|
||||||
/// </param>
|
|
||||||
void RespondWith(IResponseProvider provider);
|
void RespondWith(IResponseProvider provider);
|
||||||
|
|
||||||
|
///// <summary>
|
||||||
|
///// The respond with.
|
||||||
|
///// </summary>
|
||||||
|
///// <param name="provider">The provider.</param>
|
||||||
|
//Task RespondWithAsync(IResponseProvider provider);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
namespace WireMock.Server
|
namespace WireMock.Settings
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// FluentMockServerSettings
|
/// FluentMockServerSettings
|
||||||
@@ -31,13 +31,17 @@
|
|||||||
public bool? StartAdminInterface { get; set; }
|
public bool? StartAdminInterface { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the read static mappings.
|
/// Gets or sets if the static mappings should be read at startup.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>
|
/// <value>true/false</value>
|
||||||
/// The read static mappings.
|
|
||||||
/// </value>
|
|
||||||
public bool? ReadStaticMappings { get; set; }
|
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>
|
/// <summary>
|
||||||
/// Gets or sets the urls.
|
/// Gets or sets the urls.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
18
src/WireMock.Net/Settings/ProxyAndRecordSettings.cs
Normal file
18
src/WireMock.Net/Settings/ProxyAndRecordSettings.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user