From 77c9f372d315bbecef26a9583091d3f89626bd73 Mon Sep 17 00:00:00 2001 From: Sameena Syed Date: Fri, 1 Dec 2023 15:09:28 +0100 Subject: [PATCH 1/7] Adding proxyall --- .../Owin/IWireMockMiddlewareOptions.cs | 2 + .../Owin/WireMockMiddlewareOptions.cs | 2 + .../Server/WireMockServer.Proxy.cs | 4 ++ .../Settings/ProxyAndRecordSettings.cs | 6 +++ .../WireMockServer.Proxy.cs | 52 +++++++++++++++++++ .../WireMockServer.Proxy2.cs | 1 + 6 files changed, 67 insertions(+) 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..afecd8ac8 100644 --- a/src/WireMock.Net/Owin/WireMockMiddlewareOptions.cs +++ b/src/WireMock.Net/Owin/WireMockMiddlewareOptions.cs @@ -95,4 +95,6 @@ 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..0a98c175f 100644 --- a/src/WireMock.Net/Server/WireMockServer.Proxy.cs +++ b/src/WireMock.Net/Server/WireMockServer.Proxy.cs @@ -31,6 +31,10 @@ 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/test/WireMock.Net.Tests/WireMockServer.Proxy.cs b/test/WireMock.Net.Tests/WireMockServer.Proxy.cs index 8d4d7273b..8a1a653eb 100644 --- a/test/WireMock.Net.Tests/WireMockServer.Proxy.cs +++ b/test/WireMock.Net.Tests/WireMockServer.Proxy.cs @@ -922,4 +922,56 @@ public async Task WireMockServer_Proxy_WhenTargetIsNotAvailable_Should_Return_Co server.LogEntries.Should().HaveCount(1); server.Stop(); } + + [Fact] + public async Task WireMockServer_ProxyAndRecordSettings_ShouldProxyAll() + { + WireMockServerSettings wireMockServerSettings = new WireMockServerSettings + { + Urls = new[] { "http://localhost:9091/" }, + StartAdminInterface = false, + ReadStaticMappings = true, + WatchStaticMappings = true, + WatchStaticMappingsInSubdirectories = true, + ProxyAndRecordSettings = new ProxyAndRecordSettings + { + Url = "http://postman-echo.com/post", + SaveMapping = true, + ProxyAll = false, + SaveMappingToFile = true, + ExcludedHeaders = new[] { "Postman-Token" }, + ExcludedCookies = new[] { "sails.sid" } + } + }; + + WireMockServer server = WireMockServer.Start(wireMockServerSettings); + /*server + .Given(Request.Create().UsingPost().WithPath("/post")) + .RespondWith(Response.Create().WithProxy("ok")); + */ + + 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(); + + await new HttpClient().SendAsync(request); + Check.That(server.Mappings.Count()).IsEqualTo(2); + + await new HttpClient().SendAsync(request2); + Check.That(server.Mappings.Count()).IsEqualTo(3); + + server.Dispose(); + } } \ No newline at end of file diff --git a/test/WireMock.Net.Tests/WireMockServer.Proxy2.cs b/test/WireMock.Net.Tests/WireMockServer.Proxy2.cs index bace30b65..d637846e0 100644 --- a/test/WireMock.Net.Tests/WireMockServer.Proxy2.cs +++ b/test/WireMock.Net.Tests/WireMockServer.Proxy2.cs @@ -45,5 +45,6 @@ public async Task WireMockServer_ProxyAndRecordSettings_ShouldProxy() server.Dispose(); serverAsProxy.Dispose(); } + } } \ No newline at end of file From 7bdde41c887c4849ca9b087adb2be4eaf1942cf5 Mon Sep 17 00:00:00 2001 From: Sameena Syed Date: Mon, 11 Dec 2023 10:42:10 +0100 Subject: [PATCH 2/7] Adding proxyAll flag --- .../WireMockServer.Proxy.cs | 27 +++++++------------ 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/test/WireMock.Net.Tests/WireMockServer.Proxy.cs b/test/WireMock.Net.Tests/WireMockServer.Proxy.cs index 8a1a653eb..bf5f0fbef 100644 --- a/test/WireMock.Net.Tests/WireMockServer.Proxy.cs +++ b/test/WireMock.Net.Tests/WireMockServer.Proxy.cs @@ -924,34 +924,26 @@ public async Task WireMockServer_Proxy_WhenTargetIsNotAvailable_Should_Return_Co } [Fact] - public async Task WireMockServer_ProxyAndRecordSettings_ShouldProxyAll() + public async Task WireMockServer_ProxyAndRecordSettings_SameRequest_ShouldProxyAll() { + //Arrange WireMockServerSettings wireMockServerSettings = new WireMockServerSettings { - Urls = new[] { "http://localhost:9091/" }, - StartAdminInterface = false, - ReadStaticMappings = true, - WatchStaticMappings = true, - WatchStaticMappingsInSubdirectories = true, + Urls = new[] { "http://localhost:9091" }, ProxyAndRecordSettings = new ProxyAndRecordSettings { - Url = "http://postman-echo.com/post", + Url = "http://postman-echo.com", SaveMapping = true, ProxyAll = false, - SaveMappingToFile = true, + SaveMappingToFile = false, ExcludedHeaders = new[] { "Postman-Token" }, ExcludedCookies = new[] { "sails.sid" } } }; - WireMockServer server = WireMockServer.Start(wireMockServerSettings); - /*server - .Given(Request.Create().UsingPost().WithPath("/post")) - .RespondWith(Response.Create().WithProxy("ok")); - */ - - var requestBody = "{\"key1\": \"value1\", \"key2\": \"value2\"}"; + WireMockServer server = WireMockServer.Start(wireMockServerSettings); + var requestBody = "{\"key1\": \"value1\", \"key2\": \"value2\"}"; var request = new HttpRequestMessage { Method = HttpMethod.Post, @@ -966,10 +958,11 @@ public async Task WireMockServer_ProxyAndRecordSettings_ShouldProxyAll() }; server.ResetMappings(); + //Act await new HttpClient().SendAsync(request); - Check.That(server.Mappings.Count()).IsEqualTo(2); - await new HttpClient().SendAsync(request2); + + //Assert Check.That(server.Mappings.Count()).IsEqualTo(3); server.Dispose(); From eeb30f8f05ae5342738c5d425b70d63b40480bbf Mon Sep 17 00:00:00 2001 From: Sameena Syed Date: Mon, 11 Dec 2023 11:06:32 +0100 Subject: [PATCH 3/7] Reverting extra spaces added --- test/WireMock.Net.Tests/WireMockServer.Proxy2.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/test/WireMock.Net.Tests/WireMockServer.Proxy2.cs b/test/WireMock.Net.Tests/WireMockServer.Proxy2.cs index d637846e0..bace30b65 100644 --- a/test/WireMock.Net.Tests/WireMockServer.Proxy2.cs +++ b/test/WireMock.Net.Tests/WireMockServer.Proxy2.cs @@ -45,6 +45,5 @@ public async Task WireMockServer_ProxyAndRecordSettings_ShouldProxy() server.Dispose(); serverAsProxy.Dispose(); } - } } \ No newline at end of file From 793b8296730b3a893f1dba30cc0ee4a9fd409f46 Mon Sep 17 00:00:00 2001 From: Sameena Syed Date: Mon, 11 Dec 2023 11:38:40 +0100 Subject: [PATCH 4/7] Make proxyall to true --- test/WireMock.Net.Tests/WireMockServer.Proxy.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/WireMock.Net.Tests/WireMockServer.Proxy.cs b/test/WireMock.Net.Tests/WireMockServer.Proxy.cs index bf5f0fbef..58746e775 100644 --- a/test/WireMock.Net.Tests/WireMockServer.Proxy.cs +++ b/test/WireMock.Net.Tests/WireMockServer.Proxy.cs @@ -934,7 +934,7 @@ public async Task WireMockServer_ProxyAndRecordSettings_SameRequest_ShouldProxyA { Url = "http://postman-echo.com", SaveMapping = true, - ProxyAll = false, + ProxyAll = true, SaveMappingToFile = false, ExcludedHeaders = new[] { "Postman-Token" }, ExcludedCookies = new[] { "sails.sid" } From a54240ae6a6c190af6b6386d4af6ff14d014776f Mon Sep 17 00:00:00 2001 From: Sameena Syed Date: Mon, 11 Dec 2023 13:47:06 +0100 Subject: [PATCH 5/7] Resolving review comments for proxyall --- .../Settings/ProxyAndRecordSettingsModel.cs | 5 ++ .../Owin/WireMockMiddlewareOptions.cs | 2 + .../Server/WireMockServer.Proxy.cs | 4 +- .../WireMockServer.Proxy.cs | 46 +++++++++---------- 4 files changed, 33 insertions(+), 24 deletions(-) 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/WireMockMiddlewareOptions.cs b/src/WireMock.Net/Owin/WireMockMiddlewareOptions.cs index afecd8ac8..d2f270352 100644 --- a/src/WireMock.Net/Owin/WireMockMiddlewareOptions.cs +++ b/src/WireMock.Net/Owin/WireMockMiddlewareOptions.cs @@ -96,5 +96,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 0a98c175f..aca72b283 100644 --- a/src/WireMock.Net/Server/WireMockServer.Proxy.cs +++ b/src/WireMock.Net/Server/WireMockServer.Proxy.cs @@ -31,10 +31,12 @@ private void InitProxyAndRecord(WireMockServerSettings settings) proxyRespondProvider.AtPriority(WireMockConstants.ProxyPriority); } - if(settings.ProxyAndRecordSettings.ProxyAll){ + if(settings.ProxyAndRecordSettings.ProxyAll) + { proxyRespondProvider.AtPriority(int.MinValue); } + proxyRespondProvider.RespondWith(new ProxyAsyncResponseProvider(ProxyAndRecordAsync, settings)); } diff --git a/test/WireMock.Net.Tests/WireMockServer.Proxy.cs b/test/WireMock.Net.Tests/WireMockServer.Proxy.cs index 58746e775..95f85e0cd 100644 --- a/test/WireMock.Net.Tests/WireMockServer.Proxy.cs +++ b/test/WireMock.Net.Tests/WireMockServer.Proxy.cs @@ -927,7 +927,7 @@ public async Task WireMockServer_Proxy_WhenTargetIsNotAvailable_Should_Return_Co public async Task WireMockServer_ProxyAndRecordSettings_SameRequest_ShouldProxyAll() { //Arrange - WireMockServerSettings wireMockServerSettings = new WireMockServerSettings + var wireMockServerSettings = new WireMockServerSettings { Urls = new[] { "http://localhost:9091" }, ProxyAndRecordSettings = new ProxyAndRecordSettings @@ -941,30 +941,30 @@ public async Task WireMockServer_ProxyAndRecordSettings_SameRequest_ShouldProxyA } }; - WireMockServer server = WireMockServer.Start(wireMockServerSettings); + 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(); + 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); + //Act + await new HttpClient().SendAsync(request); + await new HttpClient().SendAsync(request2); - //Assert - Check.That(server.Mappings.Count()).IsEqualTo(3); + //Assert + Check.That(server.Mappings.Count()).IsEqualTo(3); - server.Dispose(); - } + server.Dispose(); + } } \ No newline at end of file From e78d7a6a114309e5a8a00fa12d0ca3220cef83be Mon Sep 17 00:00:00 2001 From: Sameena Syed Date: Mon, 11 Dec 2023 14:03:53 +0100 Subject: [PATCH 6/7] Resolving codefactor spaces in the code --- src/WireMock.Net/Owin/WireMockMiddlewareOptions.cs | 1 - src/WireMock.Net/Server/WireMockServer.Proxy.cs | 1 - 2 files changed, 2 deletions(-) diff --git a/src/WireMock.Net/Owin/WireMockMiddlewareOptions.cs b/src/WireMock.Net/Owin/WireMockMiddlewareOptions.cs index d2f270352..2e52f7f2b 100644 --- a/src/WireMock.Net/Owin/WireMockMiddlewareOptions.cs +++ b/src/WireMock.Net/Owin/WireMockMiddlewareOptions.cs @@ -97,6 +97,5 @@ 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 aca72b283..1cfaa2465 100644 --- a/src/WireMock.Net/Server/WireMockServer.Proxy.cs +++ b/src/WireMock.Net/Server/WireMockServer.Proxy.cs @@ -36,7 +36,6 @@ private void InitProxyAndRecord(WireMockServerSettings settings) proxyRespondProvider.AtPriority(int.MinValue); } - proxyRespondProvider.RespondWith(new ProxyAsyncResponseProvider(ProxyAndRecordAsync, settings)); } From 152b078f6e5491fcad7fe09516ccaa27c1815fd6 Mon Sep 17 00:00:00 2001 From: Sameena Syed Date: Mon, 11 Dec 2023 15:52:48 +0100 Subject: [PATCH 7/7] Adding proxyall parser in wmserversettingsparser --- src/WireMock.Net/Settings/WireMockServerSettingsParser.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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);