diff --git a/src/WireMock.Net.Abstractions/Admin/Settings/ProxyAndRecordSettingsModel.cs b/src/WireMock.Net.Abstractions/Admin/Settings/ProxyAndRecordSettingsModel.cs index 5bf9e95c9..0eb31dcd3 100644 --- a/src/WireMock.Net.Abstractions/Admin/Settings/ProxyAndRecordSettingsModel.cs +++ b/src/WireMock.Net.Abstractions/Admin/Settings/ProxyAndRecordSettingsModel.cs @@ -73,4 +73,9 @@ public class ProxyAndRecordSettingsModel /// Defines the Replace Settings /// public ProxyUrlReplaceSettingsModel? ReplaceSettings { get; set; } + + /// + /// Proxy all Api calls, irrespective of any condition + /// + public bool ProxyAll { get; set; } = false; } \ No newline at end of file diff --git a/src/WireMock.Net/Owin/IWireMockMiddlewareOptions.cs b/src/WireMock.Net/Owin/IWireMockMiddlewareOptions.cs index 69f1e1313..342bfe1ab 100644 --- a/src/WireMock.Net/Owin/IWireMockMiddlewareOptions.cs +++ b/src/WireMock.Net/Owin/IWireMockMiddlewareOptions.cs @@ -77,4 +77,6 @@ internal interface IWireMockMiddlewareOptions bool? DoNotSaveDynamicResponseInLogEntry { get; set; } QueryParameterMultipleValueSupport? QueryParameterMultipleValueSupport { get; set; } + + public bool ProxyAll { get; set; } } \ No newline at end of file diff --git a/src/WireMock.Net/Owin/WireMockMiddlewareOptions.cs b/src/WireMock.Net/Owin/WireMockMiddlewareOptions.cs index bfcbcbcff..2e52f7f2b 100644 --- a/src/WireMock.Net/Owin/WireMockMiddlewareOptions.cs +++ b/src/WireMock.Net/Owin/WireMockMiddlewareOptions.cs @@ -95,4 +95,7 @@ internal class WireMockMiddlewareOptions : IWireMockMiddlewareOptions /// public QueryParameterMultipleValueSupport? QueryParameterMultipleValueSupport { get; set; } + + /// + public bool ProxyAll { get; set; } } \ No newline at end of file diff --git a/src/WireMock.Net/Server/WireMockServer.Proxy.cs b/src/WireMock.Net/Server/WireMockServer.Proxy.cs index b3d055991..1cfaa2465 100644 --- a/src/WireMock.Net/Server/WireMockServer.Proxy.cs +++ b/src/WireMock.Net/Server/WireMockServer.Proxy.cs @@ -31,6 +31,11 @@ private void InitProxyAndRecord(WireMockServerSettings settings) proxyRespondProvider.AtPriority(WireMockConstants.ProxyPriority); } + if(settings.ProxyAndRecordSettings.ProxyAll) + { + proxyRespondProvider.AtPriority(int.MinValue); + } + proxyRespondProvider.RespondWith(new ProxyAsyncResponseProvider(ProxyAndRecordAsync, settings)); } diff --git a/src/WireMock.Net/Settings/ProxyAndRecordSettings.cs b/src/WireMock.Net/Settings/ProxyAndRecordSettings.cs index 2ca3fda5c..eb8631636 100644 --- a/src/WireMock.Net/Settings/ProxyAndRecordSettings.cs +++ b/src/WireMock.Net/Settings/ProxyAndRecordSettings.cs @@ -94,4 +94,10 @@ public string SaveMappingForStatusCodePattern /// Append an unique GUID to the filename from the saved mapping file. /// public bool AppendGuidToSavedMappingFile { get; set; } + + /// + /// Proxy all Api calls, irrespective of any condition + /// + [PublicAPI] + public bool ProxyAll { get; set; } = false; } \ No newline at end of file diff --git a/src/WireMock.Net/Settings/WireMockServerSettingsParser.cs b/src/WireMock.Net/Settings/WireMockServerSettingsParser.cs index 08e18c26d..962b94812 100644 --- a/src/WireMock.Net/Settings/WireMockServerSettingsParser.cs +++ b/src/WireMock.Net/Settings/WireMockServerSettingsParser.cs @@ -125,7 +125,8 @@ private static void ParseProxyAndRecordSettings(WireMockServerSettings settings, { StatusCodePattern = parser.GetStringValue("SaveMappingForStatusCodePattern", "*"), // HttpMethods = new ProxySaveMappingSetting(parser.GetValues("DoNotSaveMappingForHttpMethods", new string[0]), MatchBehaviour.RejectOnMatch) - } + }, + ProxyAll = parser.GetBoolValue(nameof(ProxyAndRecordSettings.ProxyAll)) }; ParseWebProxyAddressSettings(proxyAndRecordSettings, parser); diff --git a/test/WireMock.Net.Tests/WireMockServer.Proxy.cs b/test/WireMock.Net.Tests/WireMockServer.Proxy.cs index 8d4d7273b..95f85e0cd 100644 --- a/test/WireMock.Net.Tests/WireMockServer.Proxy.cs +++ b/test/WireMock.Net.Tests/WireMockServer.Proxy.cs @@ -922,4 +922,49 @@ public async Task WireMockServer_Proxy_WhenTargetIsNotAvailable_Should_Return_Co server.LogEntries.Should().HaveCount(1); server.Stop(); } + + [Fact] + public async Task WireMockServer_ProxyAndRecordSettings_SameRequest_ShouldProxyAll() + { + //Arrange + var wireMockServerSettings = new WireMockServerSettings + { + Urls = new[] { "http://localhost:9091" }, + ProxyAndRecordSettings = new ProxyAndRecordSettings + { + Url = "http://postman-echo.com", + SaveMapping = true, + ProxyAll = true, + SaveMappingToFile = false, + ExcludedHeaders = new[] { "Postman-Token" }, + ExcludedCookies = new[] { "sails.sid" } + } + }; + + var server = WireMockServer.Start(wireMockServerSettings); + + var requestBody = "{\"key1\": \"value1\", \"key2\": \"value2\"}"; + var request = new HttpRequestMessage + { + Method = HttpMethod.Post, + RequestUri = new Uri("http://localhost:9091/post"), + Content = new StringContent(requestBody) + }; + var request2 = new HttpRequestMessage + { + Method = HttpMethod.Post, + RequestUri = new Uri("http://localhost:9091/post"), + Content = new StringContent(requestBody) + }; + server.ResetMappings(); + + //Act + await new HttpClient().SendAsync(request); + await new HttpClient().SendAsync(request2); + + //Assert + Check.That(server.Mappings.Count()).IsEqualTo(3); + + server.Dispose(); + } } \ No newline at end of file