diff --git a/Directory.Build.props b/Directory.Build.props index 7fe74eae..725c1e13 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -4,7 +4,7 @@ - 1.2.18 + 1.3.0 See CHANGELOG.md https://raw.githubusercontent.com/WireMock-Net/WireMock.Net/master/WireMock.Net-Logo.png https://github.com/WireMock-Net/WireMock.Net diff --git a/WireMock.Net Solution.sln b/WireMock.Net Solution.sln index ae7957fe..2daabca0 100644 --- a/WireMock.Net Solution.sln +++ b/WireMock.Net Solution.sln @@ -63,13 +63,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net.Console.NETCor EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net.Console.RequestLogTest", "examples\WireMock.Net.Console.RequestLogTest\WireMock.Net.Console.RequestLogTest.csproj", "{A9D039B9-7509-4CF1-9EFD-87EB82998575}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WireMock.Net.Client.Net472", "examples\WireMock.Net.Client.Net472\WireMock.Net.Client.Net472.csproj", "{02082E34-DEF2-47D0-AF0B-3326FAA908CE}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net.OpenApiParser", "src\WireMock.Net.OpenApiParser\WireMock.Net.OpenApiParser.csproj", "{D3804228-91F4-4502-9595-39584E5AADAD}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net.OpenApiParser.ConsoleApp", "examples\WireMock.Net.OpenApiParser.ConsoleApp\WireMock.Net.OpenApiParser.ConsoleApp.csproj", "{5C09FB93-1535-4F92-AF26-21E8A061EE4A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WireMock.Net.FluentAssertions", "src\WireMock.Net.FluentAssertions\WireMock.Net.FluentAssertions.csproj", "{2C837E73-5EDD-43AD-B65A-194E4A3AD9FE}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net.FluentAssertions", "src\WireMock.Net.FluentAssertions\WireMock.Net.FluentAssertions.csproj", "{B6269AAC-170A-4346-8B9A-579DED3D9A95}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -157,10 +155,6 @@ Global {A9D039B9-7509-4CF1-9EFD-87EB82998575}.Debug|Any CPU.Build.0 = Debug|Any CPU {A9D039B9-7509-4CF1-9EFD-87EB82998575}.Release|Any CPU.ActiveCfg = Release|Any CPU {A9D039B9-7509-4CF1-9EFD-87EB82998575}.Release|Any CPU.Build.0 = Release|Any CPU - {02082E34-DEF2-47D0-AF0B-3326FAA908CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {02082E34-DEF2-47D0-AF0B-3326FAA908CE}.Debug|Any CPU.Build.0 = Debug|Any CPU - {02082E34-DEF2-47D0-AF0B-3326FAA908CE}.Release|Any CPU.ActiveCfg = Release|Any CPU - {02082E34-DEF2-47D0-AF0B-3326FAA908CE}.Release|Any CPU.Build.0 = Release|Any CPU {D3804228-91F4-4502-9595-39584E5AADAD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {D3804228-91F4-4502-9595-39584E5AADAD}.Debug|Any CPU.Build.0 = Debug|Any CPU {D3804228-91F4-4502-9595-39584E5AADAD}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -169,10 +163,10 @@ Global {5C09FB93-1535-4F92-AF26-21E8A061EE4A}.Debug|Any CPU.Build.0 = Debug|Any CPU {5C09FB93-1535-4F92-AF26-21E8A061EE4A}.Release|Any CPU.ActiveCfg = Release|Any CPU {5C09FB93-1535-4F92-AF26-21E8A061EE4A}.Release|Any CPU.Build.0 = Release|Any CPU - {2C837E73-5EDD-43AD-B65A-194E4A3AD9FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {2C837E73-5EDD-43AD-B65A-194E4A3AD9FE}.Debug|Any CPU.Build.0 = Debug|Any CPU - {2C837E73-5EDD-43AD-B65A-194E4A3AD9FE}.Release|Any CPU.ActiveCfg = Release|Any CPU - {2C837E73-5EDD-43AD-B65A-194E4A3AD9FE}.Release|Any CPU.Build.0 = Release|Any CPU + {B6269AAC-170A-4346-8B9A-579DED3D9A95}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B6269AAC-170A-4346-8B9A-579DED3D9A95}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B6269AAC-170A-4346-8B9A-579DED3D9A95}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B6269AAC-170A-4346-8B9A-579DED3D9A95}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -198,10 +192,9 @@ Global {668F689E-57B4-422E-8846-C0FF643CA268} = {985E0ADB-D4B4-473A-AA40-567E279B7946} {83645809-9E01-4E81-8733-BA9497554ABF} = {985E0ADB-D4B4-473A-AA40-567E279B7946} {A9D039B9-7509-4CF1-9EFD-87EB82998575} = {985E0ADB-D4B4-473A-AA40-567E279B7946} - {02082E34-DEF2-47D0-AF0B-3326FAA908CE} = {985E0ADB-D4B4-473A-AA40-567E279B7946} {D3804228-91F4-4502-9595-39584E5AADAD} = {8F890C6F-9ACC-438D-928A-AD61CDA862F2} {5C09FB93-1535-4F92-AF26-21E8A061EE4A} = {985E0ADB-D4B4-473A-AA40-567E279B7946} - {2C837E73-5EDD-43AD-B65A-194E4A3AD9FE} = {8F890C6F-9ACC-438D-928A-AD61CDA862F2} + {B6269AAC-170A-4346-8B9A-579DED3D9A95} = {8F890C6F-9ACC-438D-928A-AD61CDA862F2} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {DC539027-9852-430C-B19F-FD035D018458} diff --git a/examples/WireMock.Net.Console.Net452.Classic/MainApp.cs b/examples/WireMock.Net.Console.Net452.Classic/MainApp.cs index 69423b30..5e7180a7 100644 --- a/examples/WireMock.Net.Console.Net452.Classic/MainApp.cs +++ b/examples/WireMock.Net.Console.Net452.Classic/MainApp.cs @@ -133,7 +133,7 @@ namespace WireMock.Net.ConsoleApplication .WithPath("/proxy-execute-keep-alive") ) .RespondWith(Response.Create() - .WithProxy(new ProxyAndRecordSettings { Url = "http://localhost:9999", BlackListedHeaders = new[] { "Keep-Alive" } }) + .WithProxy(new ProxyAndRecordSettings { Url = "http://localhost:9999", ExcludedHeaders = new[] { "Keep-Alive" } }) .WithHeader("Keep-Alive-Test", "stef") ); diff --git a/examples/WireMock.Net.Console.Proxy.NETCoreApp2/Program.cs b/examples/WireMock.Net.Console.Proxy.NETCoreApp2/Program.cs index 4f1effe9..c7f923e1 100644 --- a/examples/WireMock.Net.Console.Proxy.NETCoreApp2/Program.cs +++ b/examples/WireMock.Net.Console.Proxy.NETCoreApp2/Program.cs @@ -28,7 +28,7 @@ namespace WireMock.Net.Console.Proxy.NETCoreApp2 //ClientX509Certificate2ThumbprintOrSubjectName = "www.yourclientcertname.com OR yourcertificatethumbprint (only if the service you're proxying to requires it)", SaveMapping = true, SaveMappingToFile = false, - BlackListedHeaders = new[] { "dnt", "Content-Length" } + ExcludedHeaders = new[] { "dnt", "Content-Length" } } }); diff --git a/examples/WireMock.Net.Console.Proxy.Net452/Program.cs b/examples/WireMock.Net.Console.Proxy.Net452/Program.cs index af5cda2b..b4fc367d 100644 --- a/examples/WireMock.Net.Console.Proxy.Net452/Program.cs +++ b/examples/WireMock.Net.Console.Proxy.Net452/Program.cs @@ -22,7 +22,7 @@ namespace WireMock.Net.Console.Proxy.Net452 //ClientX509Certificate2ThumbprintOrSubjectName = "www.yourclientcertname.com OR yourcertificatethumbprint (only if the service you're proxying to requires it)", SaveMapping = true, SaveMappingToFile = false, - BlackListedHeaders = new[] { "dnt", "Content-Length" } + ExcludedHeaders = new[] { "dnt", "Content-Length" } } }); diff --git a/examples/WireMock.Net.Console.Record.NETCoreApp/Program.cs b/examples/WireMock.Net.Console.Record.NETCoreApp/Program.cs index 3471b9f3..547266bc 100644 --- a/examples/WireMock.Net.Console.Record.NETCoreApp/Program.cs +++ b/examples/WireMock.Net.Console.Record.NETCoreApp/Program.cs @@ -19,7 +19,7 @@ namespace WireMock.Net.Console.Proxy.NETCoreApp //ClientX509Certificate2ThumbprintOrSubjectName = "www.yourclientcertname.com OR yourcertificatethumbprint (only if the service you're proxying to requires it)", SaveMapping = true, SaveMappingToFile = false, - BlackListedHeaders = new [] { "dnt", "Content-Length" } + ExcludedHeaders = new [] { "dnt", "Content-Length" } } }); diff --git a/examples/WireMock.Net.Console.RequestLogTest/Program.cs b/examples/WireMock.Net.Console.RequestLogTest/Program.cs index 891f4af7..6519f9f4 100644 --- a/examples/WireMock.Net.Console.RequestLogTest/Program.cs +++ b/examples/WireMock.Net.Console.RequestLogTest/Program.cs @@ -14,7 +14,7 @@ namespace WireMock.Net.Console.RequestLogTest { static async Task Main(string[] args) { - var server = StandAloneApp.Start(new FluentMockServerSettings + var server = StandAloneApp.Start(new WireMockServerSettings { Port = 19019, StartAdminInterface = true, diff --git a/examples/WireMock.Net.Service/WireMock.Net.Service.csproj b/examples/WireMock.Net.Service/WireMock.Net.Service.csproj index f26b47b0..b7e0add6 100644 --- a/examples/WireMock.Net.Service/WireMock.Net.Service.csproj +++ b/examples/WireMock.Net.Service/WireMock.Net.Service.csproj @@ -80,8 +80,8 @@ - - ..\..\packages\XPath2.1.0.10\lib\net40\XPath2.dll + + ..\..\packages\XPath2.1.1.0\lib\net40\XPath2.dll diff --git a/examples/WireMock.Net.Service/packages.config b/examples/WireMock.Net.Service/packages.config index 9e9d08e9..ed65c4aa 100644 --- a/examples/WireMock.Net.Service/packages.config +++ b/examples/WireMock.Net.Service/packages.config @@ -13,5 +13,5 @@ - + \ No newline at end of file diff --git a/examples/WireMock.Net.StandAlone.Net461/WireMock.Net.StandAlone.Net461.csproj b/examples/WireMock.Net.StandAlone.Net461/WireMock.Net.StandAlone.Net461.csproj index dfdc782f..e7af8c79 100644 --- a/examples/WireMock.Net.StandAlone.Net461/WireMock.Net.StandAlone.Net461.csproj +++ b/examples/WireMock.Net.StandAlone.Net461/WireMock.Net.StandAlone.Net461.csproj @@ -280,8 +280,8 @@ - - ..\..\packages\XPath2.1.0.6.1\lib\net40\XPath2.dll + + ..\..\packages\XPath2.1.1.0\lib\net40\XPath2.dll diff --git a/examples/WireMock.Net.StandAlone.Net461/packages.config b/examples/WireMock.Net.StandAlone.Net461/packages.config index e824a2bf..cbf94601 100644 --- a/examples/WireMock.Net.StandAlone.Net461/packages.config +++ b/examples/WireMock.Net.StandAlone.Net461/packages.config @@ -78,5 +78,5 @@ - + \ No newline at end of file diff --git a/src/WireMock.Net.OpenApiParser/WireMock.Net.OpenApiParser.csproj b/src/WireMock.Net.OpenApiParser/WireMock.Net.OpenApiParser.csproj index 656ea018..0dc46ace 100644 --- a/src/WireMock.Net.OpenApiParser/WireMock.Net.OpenApiParser.csproj +++ b/src/WireMock.Net.OpenApiParser/WireMock.Net.OpenApiParser.csproj @@ -19,7 +19,7 @@ - + diff --git a/src/WireMock.Net/Server/FluentMockServer.cs b/src/WireMock.Net/Server/FluentMockServer.cs deleted file mode 100644 index df5deef3..00000000 --- a/src/WireMock.Net/Server/FluentMockServer.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using WireMock.Settings; - -namespace WireMock.Server -{ - [Obsolete("Use WireMockServer. This will removed in next version (1.3.x)")] - public class FluentMockServer : WireMockServer - { - public FluentMockServer(IFluentMockServerSettings settings) : base((IWireMockServerSettings) settings) - { - } - } -} \ No newline at end of file diff --git a/src/WireMock.Net/Server/WireMockServer.Admin.cs b/src/WireMock.Net/Server/WireMockServer.Admin.cs index eb23b4a0..d0bd7d73 100644 --- a/src/WireMock.Net/Server/WireMockServer.Admin.cs +++ b/src/WireMock.Net/Server/WireMockServer.Admin.cs @@ -277,8 +277,8 @@ namespace WireMock.Server var mapping = ToMapping( requestMessage, responseMessage, - settings.ProxyAndRecordSettings.BlackListedHeaders ?? new string[] { }, - settings.ProxyAndRecordSettings.BlackListedCookies ?? new string[] { } + settings.ProxyAndRecordSettings.ExcludedHeaders ?? new string[] { }, + settings.ProxyAndRecordSettings.ExcludedCookies ?? new string[] { } ); if (settings.ProxyAndRecordSettings.SaveMapping) diff --git a/src/WireMock.Net/Settings/FluentMockServerSettings.cs b/src/WireMock.Net/Settings/FluentMockServerSettings.cs deleted file mode 100644 index 585eea8a..00000000 --- a/src/WireMock.Net/Settings/FluentMockServerSettings.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace WireMock.Settings -{ - /// - /// FluentMockServerSettings - /// - [Obsolete("Use WireMockServerSettings. This will removed in next version (1.3.x)")] - public class FluentMockServerSettings : WireMockServerSettings - { - } -} \ No newline at end of file diff --git a/src/WireMock.Net/Settings/IFluentMockServerSettings.cs b/src/WireMock.Net/Settings/IFluentMockServerSettings.cs deleted file mode 100644 index d7906dd4..00000000 --- a/src/WireMock.Net/Settings/IFluentMockServerSettings.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace WireMock.Settings -{ - /// - /// IFluentMockServerSettings - /// - [Obsolete("Use IWireMockServerSettings. This will removed in next version (1.3.x)")] - public interface IFluentMockServerSettings : IWireMockServerSettings - { - } -} \ No newline at end of file diff --git a/src/WireMock.Net/Settings/IProxyAndRecordSettings.cs b/src/WireMock.Net/Settings/IProxyAndRecordSettings.cs index b5fc5842..d34d73d2 100644 --- a/src/WireMock.Net/Settings/IProxyAndRecordSettings.cs +++ b/src/WireMock.Net/Settings/IProxyAndRecordSettings.cs @@ -1,5 +1,4 @@ -using System; -using JetBrains.Annotations; +using JetBrains.Annotations; namespace WireMock.Settings { @@ -39,14 +38,12 @@ namespace WireMock.Settings /// /// Defines a list from headers which will be excluded from the saved mappings. /// - [Obsolete("Will be renamed to ExcludedHeaders in next version (1.3.x)")] - string[] BlackListedHeaders { get; set; } + string[] ExcludedHeaders { get; set; } /// /// Defines a list of cookies which will be excluded from the saved mappings. /// - [Obsolete("Will be renamed to ExcludedCookies in next version (1.3.x)")] - string[] BlackListedCookies { get; set; } + string[] ExcludedCookies { get; set; } /// /// Defines the WebProxySettings. diff --git a/src/WireMock.Net/Settings/ProxyAndRecordSettings.cs b/src/WireMock.Net/Settings/ProxyAndRecordSettings.cs index 0e726a12..04d492f8 100644 --- a/src/WireMock.Net/Settings/ProxyAndRecordSettings.cs +++ b/src/WireMock.Net/Settings/ProxyAndRecordSettings.cs @@ -1,5 +1,4 @@ -using System; -using JetBrains.Annotations; +using JetBrains.Annotations; namespace WireMock.Settings { @@ -40,15 +39,13 @@ namespace WireMock.Settings [PublicAPI] public string ClientX509Certificate2ThumbprintOrSubjectName { get; set; } - /// + /// [PublicAPI] - [Obsolete("Will be renamed to ExcludedHeaders in next version (1.3.x)")] - public string[] BlackListedHeaders { get; set; } + public string[] ExcludedHeaders { get; set; } - /// + /// [PublicAPI] - [Obsolete("Will be renamed to ExcludedCookies in next version (1.3.x)")] - public string[] BlackListedCookies { get; set; } + public string[] ExcludedCookies { get; set; } /// [PublicAPI] diff --git a/src/WireMock.Net/Settings/WireMockServerSettingsParser.cs b/src/WireMock.Net/Settings/WireMockServerSettingsParser.cs index 1c0236b0..be16b68a 100644 --- a/src/WireMock.Net/Settings/WireMockServerSettingsParser.cs +++ b/src/WireMock.Net/Settings/WireMockServerSettingsParser.cs @@ -70,8 +70,8 @@ namespace WireMock.Settings SaveMappingToFile = parser.GetBoolValue("SaveMappingToFile"), SaveMappingForStatusCodePattern = parser.GetStringValue("SaveMappingForStatusCodePattern"), ClientX509Certificate2ThumbprintOrSubjectName = parser.GetStringValue("ClientX509Certificate2ThumbprintOrSubjectName"), - BlackListedHeaders = parser.GetValues("BlackListedHeaders"), - BlackListedCookies = parser.GetValues("BlackListedCookies"), + ExcludedHeaders = parser.GetValues("ExcludedHeaders"), + ExcludedCookies = parser.GetValues("ExcludedCookies"), AllowAutoRedirect = parser.GetBoolValue("AllowAutoRedirect") }; diff --git a/src/WireMock.Net/WireMock.Net.csproj b/src/WireMock.Net/WireMock.Net.csproj index 22135bd3..7f4f0a8a 100644 --- a/src/WireMock.Net/WireMock.Net.csproj +++ b/src/WireMock.Net/WireMock.Net.csproj @@ -56,7 +56,7 @@ - + @@ -69,7 +69,7 @@ - + @@ -101,7 +101,11 @@ - + + + + + @@ -114,12 +118,18 @@ - + + + + - + + + + diff --git a/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj b/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj index 931ea9ab..3c71473b 100644 --- a/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj +++ b/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj @@ -44,17 +44,17 @@ - + - + - + - + all runtime; build; native; contentfiles; analyzers diff --git a/test/WireMock.Net.Tests/WireMockServer.Proxy.cs b/test/WireMock.Net.Tests/WireMockServer.Proxy.cs index 2803210e..35584213 100644 --- a/test/WireMock.Net.Tests/WireMockServer.Proxy.cs +++ b/test/WireMock.Net.Tests/WireMockServer.Proxy.cs @@ -1,521 +1,521 @@ -using System; -using System.Linq; -using System.Net; -using System.Net.Http; -using System.Net.Http.Headers; -using System.Text; -using System.Threading.Tasks; -using FluentAssertions; -using NFluent; -using WireMock.Admin.Mappings; -using WireMock.Logging; -using WireMock.Matchers.Request; -using WireMock.RequestBuilders; -using WireMock.ResponseBuilders; -using WireMock.Server; -using WireMock.Settings; -using Xunit; - -namespace WireMock.Net.Tests -{ - public class WireMockServerProxyTests - { - [Fact] - public async Task WireMockServer_Proxy_Should_log_proxied_requests() - { - // Assign - var settings = new WireMockServerSettings - { - ProxyAndRecordSettings = new ProxyAndRecordSettings - { - Url = "http://www.google.com", - SaveMapping = true, - SaveMappingToFile = false - } - }; +using System; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Text; +using System.Threading.Tasks; +using FluentAssertions; +using NFluent; +using WireMock.Admin.Mappings; +using WireMock.Logging; +using WireMock.Matchers.Request; +using WireMock.RequestBuilders; +using WireMock.ResponseBuilders; +using WireMock.Server; +using WireMock.Settings; +using Xunit; + +namespace WireMock.Net.Tests +{ + public class WireMockServerProxyTests + { + [Fact] + public async Task WireMockServer_Proxy_Should_log_proxied_requests() + { + // Assign + var settings = new WireMockServerSettings + { + ProxyAndRecordSettings = new ProxyAndRecordSettings + { + Url = "http://www.google.com", + SaveMapping = true, + SaveMappingToFile = false + } + }; var server = WireMockServer.Start(settings); // Act - var requestMessage = new HttpRequestMessage - { - Method = HttpMethod.Get, - RequestUri = new Uri(server.Urls[0]) - }; - var httpClientHandler = new HttpClientHandler { AllowAutoRedirect = false }; - await new HttpClient(httpClientHandler).SendAsync(requestMessage); - - // Assert - Check.That(server.Mappings).HasSize(2); - Check.That(server.LogEntries).HasSize(1); + var requestMessage = new HttpRequestMessage + { + Method = HttpMethod.Get, + RequestUri = new Uri(server.Urls[0]) + }; + var httpClientHandler = new HttpClientHandler { AllowAutoRedirect = false }; + await new HttpClient(httpClientHandler).SendAsync(requestMessage); + + // Assert + Check.That(server.Mappings).HasSize(2); + Check.That(server.LogEntries).HasSize(1); } - [Fact] - public async Task WireMockServer_Proxy_Should_proxy_responses() - { - // Assign - string path = $"/prx_{Guid.NewGuid()}"; - var server = WireMockServer.Start(); - server - .Given(Request.Create().WithPath(path)) - .RespondWith(Response.Create().WithProxy("http://www.google.com")); - - // Act - var requestMessage = new HttpRequestMessage - { - Method = HttpMethod.Get, - RequestUri = new Uri($"{server.Urls[0]}{path}") - }; - var httpClientHandler = new HttpClientHandler { AllowAutoRedirect = false }; - var response = await new HttpClient(httpClientHandler).SendAsync(requestMessage); - string content = await response.Content.ReadAsStringAsync(); - - // Assert - Check.That(server.Mappings).HasSize(1); - Check.That(server.LogEntries).HasSize(1); - Check.That(content).Contains("google"); - } - - [Fact] - public async Task WireMockServer_Proxy_Should_preserve_content_header_in_proxied_request() - { - // Assign - string path = $"/prx_{Guid.NewGuid()}"; - var serverForProxyForwarding = WireMockServer.Start(); - serverForProxyForwarding - .Given(Request.Create().WithPath(path)) - .RespondWith(Response.Create()); - - var settings = new WireMockServerSettings - { - ProxyAndRecordSettings = new ProxyAndRecordSettings - { - Url = serverForProxyForwarding.Urls[0], - SaveMapping = true, - SaveMappingToFile = false - } - }; - var server = WireMockServer.Start(settings); - - // Act - var requestMessage = new HttpRequestMessage - { - Method = HttpMethod.Post, - RequestUri = new Uri($"{server.Urls[0]}{path}"), - Content = new StringContent("stringContent", Encoding.ASCII) - }; - requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain"); - requestMessage.Content.Headers.Add("bbb", "test"); - await new HttpClient().SendAsync(requestMessage); - - // Assert - var receivedRequest = serverForProxyForwarding.LogEntries.First().RequestMessage; - Check.That(receivedRequest.BodyData.BodyAsString).IsEqualTo("stringContent"); - Check.That(receivedRequest.Headers).ContainsKey("Content-Type"); - Check.That(receivedRequest.Headers["Content-Type"].First()).Contains("text/plain"); - Check.That(receivedRequest.Headers).ContainsKey("bbb"); - - // check that new proxied mapping is added - Check.That(server.Mappings).HasSize(2); - - //var newMapping = _server.Mappings.First(m => m.Guid != guid); - //var matcher = ((Request)newMapping.RequestMatcher).GetRequestMessageMatchers().FirstOrDefault(m => m.Name == "bbb"); - //Check.That(matcher).IsNotNull(); - } - - [Fact] - public async Task WireMockServer_Proxy_Should_exclude_ExcludedHeaders_in_mapping() - { - // Assign - string path = $"/prx_{Guid.NewGuid()}"; - var serverForProxyForwarding = WireMockServer.Start(); - serverForProxyForwarding - .Given(Request.Create().WithPath(path)) - .RespondWith(Response.Create()); - - var settings = new WireMockServerSettings - { - ProxyAndRecordSettings = new ProxyAndRecordSettings - { - Url = serverForProxyForwarding.Urls[0], - SaveMapping = true, - SaveMappingToFile = false, - BlackListedHeaders = new[] { "excluded-header-X" } - } - }; - var server = WireMockServer.Start(settings); - var defaultMapping = server.Mappings.First(); - - // Act - var requestMessage = new HttpRequestMessage - { - Method = HttpMethod.Post, - RequestUri = new Uri($"{server.Urls[0]}{path}"), - Content = new StringContent("stringContent") - }; - requestMessage.Headers.Add("foobar", "exact_match"); - requestMessage.Headers.Add("ok", "ok-value"); - await new HttpClient().SendAsync(requestMessage); - - // Assert - var mapping = server.Mappings.FirstOrDefault(m => m.Guid != defaultMapping.Guid); - Check.That(mapping).IsNotNull(); - var matchers = ((Request)mapping.RequestMatcher).GetRequestMessageMatchers().Select(m => m.Name).ToList(); - Check.That(matchers).Not.Contains("excluded-header-X"); - Check.That(matchers).Contains("ok"); - } - - [Fact] - public async Task WireMockServer_Proxy_Should_exclude_ExcludedCookies_in_mapping() - { - // Assign - string path = $"/prx_{Guid.NewGuid()}"; - var serverForProxyForwarding = WireMockServer.Start(); - serverForProxyForwarding - .Given(Request.Create().WithPath(path)) - .RespondWith(Response.Create()); - - var settings = new WireMockServerSettings - { - ProxyAndRecordSettings = new ProxyAndRecordSettings - { - Url = serverForProxyForwarding.Urls[0], - SaveMapping = true, - SaveMappingToFile = false, - BlackListedCookies = new[] { "ASP.NET_SessionId" } - } - }; - var server = WireMockServer.Start(settings); - var defaultMapping = server.Mappings.First(); - - // Act - var requestMessage = new HttpRequestMessage - { - Method = HttpMethod.Post, - RequestUri = new Uri($"{server.Urls[0]}{path}"), - Content = new StringContent("stringContent") - }; - - var cookieContainer = new CookieContainer(3); - cookieContainer.Add(new Uri("http://localhost"), new Cookie("ASP.NET_SessionId", "exact_match")); - cookieContainer.Add(new Uri("http://localhost"), new Cookie("AsP.NeT_SessIonID", "case_mismatch")); - cookieContainer.Add(new Uri("http://localhost"), new Cookie("GoodCookie", "I_should_pass")); - - var handler = new HttpClientHandler { CookieContainer = cookieContainer }; - await new HttpClient(handler).SendAsync(requestMessage); - - // Assert - var mapping = server.Mappings.FirstOrDefault(m => m.Guid != defaultMapping.Guid); - Check.That(mapping).IsNotNull(); - - var matchers = ((Request)mapping.RequestMatcher).GetRequestMessageMatchers().Select(m => m.Name).ToList(); - Check.That(matchers).Not.Contains("ASP.NET_SessionId"); - Check.That(matchers).Not.Contains("AsP.NeT_SessIonID"); - Check.That(matchers).Contains("GoodCookie"); - } - - [Fact] - public async Task WireMockServer_Proxy_Should_preserve_content_header_in_proxied_request_with_empty_content() - { - // Assign - string path = $"/prx_{Guid.NewGuid()}"; - var serverForProxyForwarding = WireMockServer.Start(); - serverForProxyForwarding - .Given(Request.Create().WithPath(path)) - .RespondWith(Response.Create()); - - var server = WireMockServer.Start(); - server - .Given(Request.Create().WithPath("/*")) - .RespondWith(Response.Create().WithProxy(serverForProxyForwarding.Urls[0])); - - // Act - var requestMessage = new HttpRequestMessage - { - Method = HttpMethod.Post, - RequestUri = new Uri($"{server.Urls[0]}{path}"), - Content = new StringContent("") - }; - requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain"); - await new HttpClient().SendAsync(requestMessage); - - // Assert - var receivedRequest = serverForProxyForwarding.LogEntries.First().RequestMessage; - Check.That(receivedRequest.BodyData.BodyAsString).IsEqualTo(""); - Check.That(receivedRequest.Headers).ContainsKey("Content-Type"); - Check.That(receivedRequest.Headers["Content-Type"].First()).Contains("text/plain"); - } - - [Fact] - public async Task WireMockServer_Proxy_Should_preserve_content_header_in_proxied_response() - { - // Assign - string path = $"/prx_{Guid.NewGuid()}"; - var serverForProxyForwarding = WireMockServer.Start(); - serverForProxyForwarding - .Given(Request.Create().WithPath(path)) - .RespondWith(Response.Create() - .WithBody("body") - .WithHeader("Content-Type", "text/plain")); - - var server = WireMockServer.Start(); - server - .Given(Request.Create().WithPath(path)) - .RespondWith(Response.Create().WithProxy(serverForProxyForwarding.Urls[0])); - - // Act - var requestMessage = new HttpRequestMessage - { - Method = HttpMethod.Get, - RequestUri = new Uri($"{server.Urls[0]}{path}") - }; - var response = await new HttpClient().SendAsync(requestMessage); - - // Assert - Check.That(await response.Content.ReadAsStringAsync()).IsEqualTo("body"); - Check.That(response.Content.Headers.Contains("Content-Type")).IsTrue(); - Check.That(response.Content.Headers.GetValues("Content-Type")).ContainsExactly("text/plain"); - } - - [Fact] - public async Task WireMockServer_Proxy_Should_change_absolute_location_header_in_proxied_response() - { - // Assign - string path = $"/prx_{Guid.NewGuid()}"; - var settings = new WireMockServerSettings { AllowPartialMapping = false }; - - var serverForProxyForwarding = WireMockServer.Start(settings); - serverForProxyForwarding - .Given(Request.Create().WithPath(path)) - .RespondWith(Response.Create() - .WithStatusCode(HttpStatusCode.Redirect) - .WithHeader("Location", "/testpath")); - - var server = WireMockServer.Start(settings); - server - .Given(Request.Create().WithPath(path).UsingAnyMethod()) - .RespondWith(Response.Create().WithProxy(serverForProxyForwarding.Urls[0])); - - // Act - var requestMessage = new HttpRequestMessage - { - Method = HttpMethod.Get, - RequestUri = new Uri($"{server.Urls[0]}{path}") - }; - var httpClientHandler = new HttpClientHandler { AllowAutoRedirect = false }; - var response = await new HttpClient(httpClientHandler).SendAsync(requestMessage); - - // Assert - Check.That(response.Headers.Contains("Location")).IsTrue(); - Check.That(response.Headers.GetValues("Location")).ContainsExactly("/testpath"); - } - - [Fact] - public async Task WireMockServer_Proxy_Should_preserve_cookie_header_in_proxied_request() - { - // Assign - string path = $"/prx_{Guid.NewGuid()}"; - var serverForProxyForwarding = WireMockServer.Start(); - serverForProxyForwarding - .Given(Request.Create().WithPath(path)) - .RespondWith(Response.Create()); - - var server = WireMockServer.Start(); - server - .Given(Request.Create().WithPath(path)) - .RespondWith(Response.Create().WithProxy(serverForProxyForwarding.Urls[0])); - - // Act - var requestUri = new Uri($"{server.Urls[0]}{path}"); - var requestMessage = new HttpRequestMessage - { - Method = HttpMethod.Get, - RequestUri = requestUri - }; - var clientHandler = new HttpClientHandler(); - clientHandler.CookieContainer.Add(requestUri, new Cookie("name", "value")); - await new HttpClient(clientHandler).SendAsync(requestMessage); - - // then - var receivedRequest = serverForProxyForwarding.LogEntries.First().RequestMessage; - Check.That(receivedRequest.Cookies).IsNotNull(); - Check.That(receivedRequest.Cookies).ContainsPair("name", "value"); - } - - /// - /// Send some binary content in a request through the proxy and check that the same content - /// arrived at the target. As example a JPEG/JIFF header is used, which is not representable - /// in UTF8 and breaks if it is not treated as binary content. - /// - [Fact] - public async Task WireMockServer_Proxy_Should_preserve_binary_request_content() - { - // arrange - var jpegHeader = new byte[] { 0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00 }; - var brokenJpegHeader = new byte[] - {0xEF, 0xBF, 0xBD, 0xEF, 0xBF, 0xBD, 0xEF, 0xBF, 0xBD, 0xEF, 0xBF, 0xBD, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00}; - - bool HasCorrectHeader(byte[] bytes) => bytes.SequenceEqual(jpegHeader); - bool HasBrokenHeader(byte[] bytes) => bytes.SequenceEqual(brokenJpegHeader); - - var serverForProxyForwarding = WireMockServer.Start(); - serverForProxyForwarding - .Given(Request.Create().WithBody(HasCorrectHeader)) - .RespondWith(Response.Create().WithSuccess()); - - serverForProxyForwarding - .Given(Request.Create().WithBody(HasBrokenHeader)) - .RespondWith(Response.Create().WithStatusCode(HttpStatusCode.InternalServerError)); - - var server = WireMockServer.Start(); - server - .Given(Request.Create()) - .RespondWith(Response.Create().WithProxy(serverForProxyForwarding.Urls[0])); - - // act - var response = await new HttpClient().PostAsync(server.Urls[0], new ByteArrayContent(jpegHeader)); - - // assert - Check.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK); - } - - [Fact] - public async Task WireMockServer_Proxy_Should_set_BodyAsJson_in_proxied_response() - { - // Assign - string path = $"/prx_{Guid.NewGuid()}"; - var serverForProxyForwarding = WireMockServer.Start(); - serverForProxyForwarding - .Given(Request.Create().WithPath(path)) - .RespondWith(Response.Create() - .WithBodyAsJson(new { i = 42 }) - .WithHeader("Content-Type", "application/json; charset=utf-8")); - - var server = WireMockServer.Start(); - server - .Given(Request.Create().WithPath(path)) - .RespondWith(Response.Create().WithProxy(serverForProxyForwarding.Urls[0])); - - // Act - var requestMessage = new HttpRequestMessage - { - Method = HttpMethod.Get, - RequestUri = new Uri($"{server.Urls[0]}{path}") - }; - var response = await new HttpClient().SendAsync(requestMessage); - - // Assert - string content = await response.Content.ReadAsStringAsync(); - Check.That(content).IsEqualTo("{\"i\":42}"); - Check.That(response.Content.Headers.GetValues("Content-Type")).ContainsExactly("application/json; charset=utf-8"); - } - - [Fact] - public async Task WireMockServer_Proxy_Should_set_Body_in_multipart_proxied_response() - { - // Assign - string path = $"/prx_{Guid.NewGuid()}"; - var serverForProxyForwarding = WireMockServer.Start(); - serverForProxyForwarding - .Given(Request.Create().WithPath(path)) - .RespondWith(Response.Create() - .WithBodyAsJson(new { i = 42 }) - ); - - var server = WireMockServer.Start(); - server - .Given(Request.Create().WithPath(path)) - .RespondWith(Response.Create().WithProxy(serverForProxyForwarding.Urls[0])); - - // Act - var uri = new Uri($"{server.Urls[0]}{path}"); - var form = new MultipartFormDataContent - { - { new StringContent("data"), "test", "test.txt" } - }; - var response = await new HttpClient().PostAsync(uri, form); - - // Assert - string content = await response.Content.ReadAsStringAsync(); - Check.That(content).IsEqualTo("{\"i\":42}"); - } - - [Fact] - public async Task WireMockServer_Proxy_Should_Not_overrule_AdminMappings() - { - // Assign - string path = $"/prx_{Guid.NewGuid()}"; - var serverForProxyForwarding = WireMockServer.Start(); - serverForProxyForwarding - .Given(Request.Create().WithPath(path)) - .RespondWith(Response.Create().WithBody("ok")); - - var server = WireMockServer.Start(new WireMockServerSettings - { - StartAdminInterface = true, - ReadStaticMappings = false, - ProxyAndRecordSettings = new ProxyAndRecordSettings - { - Url = serverForProxyForwarding.Urls[0], - SaveMapping = false, - SaveMappingToFile = false - } - }); - - // Act 1 - var requestMessage1 = new HttpRequestMessage - { - Method = HttpMethod.Get, - RequestUri = new Uri($"{server.Urls[0]}{path}") - }; - var response1 = await new HttpClient().SendAsync(requestMessage1); - - // Assert 1 - string content1 = await response1.Content.ReadAsStringAsync(); - Check.That(content1).IsEqualTo("ok"); - - // Act 2 - var requestMessage2 = new HttpRequestMessage - { - Method = HttpMethod.Get, - RequestUri = new Uri($"{server.Urls[0]}/__admin/mappings") - }; - var response2 = await new HttpClient().SendAsync(requestMessage2); - - // Assert 2 - string content2 = await response2.Content.ReadAsStringAsync(); - Check.That(content2).IsEqualTo("[]"); - } - - [Fact] - public async Task WireMockServer_Proxy_WhenTargetIsNotAvailable_Should_Return_CorrectResponse() - { - // Assign - var settings = new WireMockServerSettings - { - ProxyAndRecordSettings = new ProxyAndRecordSettings - { - Url = $"http://error{Guid.NewGuid()}:12345" - } - }; - var server = WireMockServer.Start(settings); - - // Act - var requestMessage = new HttpRequestMessage - { - Method = HttpMethod.Get, - RequestUri = new Uri(server.Urls[0]) - }; - var httpClientHandler = new HttpClientHandler { AllowAutoRedirect = false }; - var result = await new HttpClient(httpClientHandler).SendAsync(requestMessage); - - // Assert - result.StatusCode.Should().Be(500); - - var content = await result.Content.ReadAsStringAsync(); - content.Should().Contain("known"); // On Linux it's "Name or service not known". On Windows it's "No such host is known.". - - server.LogEntries.Should().HaveCount(1); - ((StatusModel)server.LogEntries.First().ResponseMessage.BodyData.BodyAsJson).Status.Should().Contain("known"); - } - } + [Fact] + public async Task WireMockServer_Proxy_Should_proxy_responses() + { + // Assign + string path = $"/prx_{Guid.NewGuid()}"; + var server = WireMockServer.Start(); + server + .Given(Request.Create().WithPath(path)) + .RespondWith(Response.Create().WithProxy("http://www.google.com")); + + // Act + var requestMessage = new HttpRequestMessage + { + Method = HttpMethod.Get, + RequestUri = new Uri($"{server.Urls[0]}{path}") + }; + var httpClientHandler = new HttpClientHandler { AllowAutoRedirect = false }; + var response = await new HttpClient(httpClientHandler).SendAsync(requestMessage); + string content = await response.Content.ReadAsStringAsync(); + + // Assert + Check.That(server.Mappings).HasSize(1); + Check.That(server.LogEntries).HasSize(1); + Check.That(content).Contains("google"); + } + + [Fact] + public async Task WireMockServer_Proxy_Should_preserve_content_header_in_proxied_request() + { + // Assign + string path = $"/prx_{Guid.NewGuid()}"; + var serverForProxyForwarding = WireMockServer.Start(); + serverForProxyForwarding + .Given(Request.Create().WithPath(path)) + .RespondWith(Response.Create()); + + var settings = new WireMockServerSettings + { + ProxyAndRecordSettings = new ProxyAndRecordSettings + { + Url = serverForProxyForwarding.Urls[0], + SaveMapping = true, + SaveMappingToFile = false + } + }; + var server = WireMockServer.Start(settings); + + // Act + var requestMessage = new HttpRequestMessage + { + Method = HttpMethod.Post, + RequestUri = new Uri($"{server.Urls[0]}{path}"), + Content = new StringContent("stringContent", Encoding.ASCII) + }; + requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain"); + requestMessage.Content.Headers.Add("bbb", "test"); + await new HttpClient().SendAsync(requestMessage); + + // Assert + var receivedRequest = serverForProxyForwarding.LogEntries.First().RequestMessage; + Check.That(receivedRequest.BodyData.BodyAsString).IsEqualTo("stringContent"); + Check.That(receivedRequest.Headers).ContainsKey("Content-Type"); + Check.That(receivedRequest.Headers["Content-Type"].First()).Contains("text/plain"); + Check.That(receivedRequest.Headers).ContainsKey("bbb"); + + // check that new proxied mapping is added + Check.That(server.Mappings).HasSize(2); + + //var newMapping = _server.Mappings.First(m => m.Guid != guid); + //var matcher = ((Request)newMapping.RequestMatcher).GetRequestMessageMatchers().FirstOrDefault(m => m.Name == "bbb"); + //Check.That(matcher).IsNotNull(); + } + + [Fact] + public async Task WireMockServer_Proxy_Should_exclude_ExcludedHeaders_in_mapping() + { + // Assign + string path = $"/prx_{Guid.NewGuid()}"; + var serverForProxyForwarding = WireMockServer.Start(); + serverForProxyForwarding + .Given(Request.Create().WithPath(path)) + .RespondWith(Response.Create()); + + var settings = new WireMockServerSettings + { + ProxyAndRecordSettings = new ProxyAndRecordSettings + { + Url = serverForProxyForwarding.Urls[0], + SaveMapping = true, + SaveMappingToFile = false, + ExcludedHeaders = new[] { "excluded-header-X" } + } + }; + var server = WireMockServer.Start(settings); + var defaultMapping = server.Mappings.First(); + + // Act + var requestMessage = new HttpRequestMessage + { + Method = HttpMethod.Post, + RequestUri = new Uri($"{server.Urls[0]}{path}"), + Content = new StringContent("stringContent") + }; + requestMessage.Headers.Add("foobar", "exact_match"); + requestMessage.Headers.Add("ok", "ok-value"); + await new HttpClient().SendAsync(requestMessage); + + // Assert + var mapping = server.Mappings.FirstOrDefault(m => m.Guid != defaultMapping.Guid); + Check.That(mapping).IsNotNull(); + var matchers = ((Request)mapping.RequestMatcher).GetRequestMessageMatchers().Select(m => m.Name).ToList(); + Check.That(matchers).Not.Contains("excluded-header-X"); + Check.That(matchers).Contains("ok"); + } + + [Fact] + public async Task WireMockServer_Proxy_Should_exclude_ExcludedCookies_in_mapping() + { + // Assign + string path = $"/prx_{Guid.NewGuid()}"; + var serverForProxyForwarding = WireMockServer.Start(); + serverForProxyForwarding + .Given(Request.Create().WithPath(path)) + .RespondWith(Response.Create()); + + var settings = new WireMockServerSettings + { + ProxyAndRecordSettings = new ProxyAndRecordSettings + { + Url = serverForProxyForwarding.Urls[0], + SaveMapping = true, + SaveMappingToFile = false, + ExcludedCookies = new[] { "ASP.NET_SessionId" } + } + }; + var server = WireMockServer.Start(settings); + var defaultMapping = server.Mappings.First(); + + // Act + var requestMessage = new HttpRequestMessage + { + Method = HttpMethod.Post, + RequestUri = new Uri($"{server.Urls[0]}{path}"), + Content = new StringContent("stringContent") + }; + + var cookieContainer = new CookieContainer(3); + cookieContainer.Add(new Uri("http://localhost"), new Cookie("ASP.NET_SessionId", "exact_match")); + cookieContainer.Add(new Uri("http://localhost"), new Cookie("AsP.NeT_SessIonID", "case_mismatch")); + cookieContainer.Add(new Uri("http://localhost"), new Cookie("GoodCookie", "I_should_pass")); + + var handler = new HttpClientHandler { CookieContainer = cookieContainer }; + await new HttpClient(handler).SendAsync(requestMessage); + + // Assert + var mapping = server.Mappings.FirstOrDefault(m => m.Guid != defaultMapping.Guid); + Check.That(mapping).IsNotNull(); + + var matchers = ((Request)mapping.RequestMatcher).GetRequestMessageMatchers().Select(m => m.Name).ToList(); + Check.That(matchers).Not.Contains("ASP.NET_SessionId"); + Check.That(matchers).Not.Contains("AsP.NeT_SessIonID"); + Check.That(matchers).Contains("GoodCookie"); + } + + [Fact] + public async Task WireMockServer_Proxy_Should_preserve_content_header_in_proxied_request_with_empty_content() + { + // Assign + string path = $"/prx_{Guid.NewGuid()}"; + var serverForProxyForwarding = WireMockServer.Start(); + serverForProxyForwarding + .Given(Request.Create().WithPath(path)) + .RespondWith(Response.Create()); + + var server = WireMockServer.Start(); + server + .Given(Request.Create().WithPath("/*")) + .RespondWith(Response.Create().WithProxy(serverForProxyForwarding.Urls[0])); + + // Act + var requestMessage = new HttpRequestMessage + { + Method = HttpMethod.Post, + RequestUri = new Uri($"{server.Urls[0]}{path}"), + Content = new StringContent("") + }; + requestMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain"); + await new HttpClient().SendAsync(requestMessage); + + // Assert + var receivedRequest = serverForProxyForwarding.LogEntries.First().RequestMessage; + Check.That(receivedRequest.BodyData.BodyAsString).IsEqualTo(""); + Check.That(receivedRequest.Headers).ContainsKey("Content-Type"); + Check.That(receivedRequest.Headers["Content-Type"].First()).Contains("text/plain"); + } + + [Fact] + public async Task WireMockServer_Proxy_Should_preserve_content_header_in_proxied_response() + { + // Assign + string path = $"/prx_{Guid.NewGuid()}"; + var serverForProxyForwarding = WireMockServer.Start(); + serverForProxyForwarding + .Given(Request.Create().WithPath(path)) + .RespondWith(Response.Create() + .WithBody("body") + .WithHeader("Content-Type", "text/plain")); + + var server = WireMockServer.Start(); + server + .Given(Request.Create().WithPath(path)) + .RespondWith(Response.Create().WithProxy(serverForProxyForwarding.Urls[0])); + + // Act + var requestMessage = new HttpRequestMessage + { + Method = HttpMethod.Get, + RequestUri = new Uri($"{server.Urls[0]}{path}") + }; + var response = await new HttpClient().SendAsync(requestMessage); + + // Assert + Check.That(await response.Content.ReadAsStringAsync()).IsEqualTo("body"); + Check.That(response.Content.Headers.Contains("Content-Type")).IsTrue(); + Check.That(response.Content.Headers.GetValues("Content-Type")).ContainsExactly("text/plain"); + } + + [Fact] + public async Task WireMockServer_Proxy_Should_change_absolute_location_header_in_proxied_response() + { + // Assign + string path = $"/prx_{Guid.NewGuid()}"; + var settings = new WireMockServerSettings { AllowPartialMapping = false }; + + var serverForProxyForwarding = WireMockServer.Start(settings); + serverForProxyForwarding + .Given(Request.Create().WithPath(path)) + .RespondWith(Response.Create() + .WithStatusCode(HttpStatusCode.Redirect) + .WithHeader("Location", "/testpath")); + + var server = WireMockServer.Start(settings); + server + .Given(Request.Create().WithPath(path).UsingAnyMethod()) + .RespondWith(Response.Create().WithProxy(serverForProxyForwarding.Urls[0])); + + // Act + var requestMessage = new HttpRequestMessage + { + Method = HttpMethod.Get, + RequestUri = new Uri($"{server.Urls[0]}{path}") + }; + var httpClientHandler = new HttpClientHandler { AllowAutoRedirect = false }; + var response = await new HttpClient(httpClientHandler).SendAsync(requestMessage); + + // Assert + Check.That(response.Headers.Contains("Location")).IsTrue(); + Check.That(response.Headers.GetValues("Location")).ContainsExactly("/testpath"); + } + + [Fact] + public async Task WireMockServer_Proxy_Should_preserve_cookie_header_in_proxied_request() + { + // Assign + string path = $"/prx_{Guid.NewGuid()}"; + var serverForProxyForwarding = WireMockServer.Start(); + serverForProxyForwarding + .Given(Request.Create().WithPath(path)) + .RespondWith(Response.Create()); + + var server = WireMockServer.Start(); + server + .Given(Request.Create().WithPath(path)) + .RespondWith(Response.Create().WithProxy(serverForProxyForwarding.Urls[0])); + + // Act + var requestUri = new Uri($"{server.Urls[0]}{path}"); + var requestMessage = new HttpRequestMessage + { + Method = HttpMethod.Get, + RequestUri = requestUri + }; + var clientHandler = new HttpClientHandler(); + clientHandler.CookieContainer.Add(requestUri, new Cookie("name", "value")); + await new HttpClient(clientHandler).SendAsync(requestMessage); + + // then + var receivedRequest = serverForProxyForwarding.LogEntries.First().RequestMessage; + Check.That(receivedRequest.Cookies).IsNotNull(); + Check.That(receivedRequest.Cookies).ContainsPair("name", "value"); + } + + /// + /// Send some binary content in a request through the proxy and check that the same content + /// arrived at the target. As example a JPEG/JIFF header is used, which is not representable + /// in UTF8 and breaks if it is not treated as binary content. + /// + [Fact] + public async Task WireMockServer_Proxy_Should_preserve_binary_request_content() + { + // arrange + var jpegHeader = new byte[] { 0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00 }; + var brokenJpegHeader = new byte[] + {0xEF, 0xBF, 0xBD, 0xEF, 0xBF, 0xBD, 0xEF, 0xBF, 0xBD, 0xEF, 0xBF, 0xBD, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00}; + + bool HasCorrectHeader(byte[] bytes) => bytes.SequenceEqual(jpegHeader); + bool HasBrokenHeader(byte[] bytes) => bytes.SequenceEqual(brokenJpegHeader); + + var serverForProxyForwarding = WireMockServer.Start(); + serverForProxyForwarding + .Given(Request.Create().WithBody(HasCorrectHeader)) + .RespondWith(Response.Create().WithSuccess()); + + serverForProxyForwarding + .Given(Request.Create().WithBody(HasBrokenHeader)) + .RespondWith(Response.Create().WithStatusCode(HttpStatusCode.InternalServerError)); + + var server = WireMockServer.Start(); + server + .Given(Request.Create()) + .RespondWith(Response.Create().WithProxy(serverForProxyForwarding.Urls[0])); + + // act + var response = await new HttpClient().PostAsync(server.Urls[0], new ByteArrayContent(jpegHeader)); + + // assert + Check.That(response.StatusCode).IsEqualTo(HttpStatusCode.OK); + } + + [Fact] + public async Task WireMockServer_Proxy_Should_set_BodyAsJson_in_proxied_response() + { + // Assign + string path = $"/prx_{Guid.NewGuid()}"; + var serverForProxyForwarding = WireMockServer.Start(); + serverForProxyForwarding + .Given(Request.Create().WithPath(path)) + .RespondWith(Response.Create() + .WithBodyAsJson(new { i = 42 }) + .WithHeader("Content-Type", "application/json; charset=utf-8")); + + var server = WireMockServer.Start(); + server + .Given(Request.Create().WithPath(path)) + .RespondWith(Response.Create().WithProxy(serverForProxyForwarding.Urls[0])); + + // Act + var requestMessage = new HttpRequestMessage + { + Method = HttpMethod.Get, + RequestUri = new Uri($"{server.Urls[0]}{path}") + }; + var response = await new HttpClient().SendAsync(requestMessage); + + // Assert + string content = await response.Content.ReadAsStringAsync(); + Check.That(content).IsEqualTo("{\"i\":42}"); + Check.That(response.Content.Headers.GetValues("Content-Type")).ContainsExactly("application/json; charset=utf-8"); + } + + [Fact] + public async Task WireMockServer_Proxy_Should_set_Body_in_multipart_proxied_response() + { + // Assign + string path = $"/prx_{Guid.NewGuid()}"; + var serverForProxyForwarding = WireMockServer.Start(); + serverForProxyForwarding + .Given(Request.Create().WithPath(path)) + .RespondWith(Response.Create() + .WithBodyAsJson(new { i = 42 }) + ); + + var server = WireMockServer.Start(); + server + .Given(Request.Create().WithPath(path)) + .RespondWith(Response.Create().WithProxy(serverForProxyForwarding.Urls[0])); + + // Act + var uri = new Uri($"{server.Urls[0]}{path}"); + var form = new MultipartFormDataContent + { + { new StringContent("data"), "test", "test.txt" } + }; + var response = await new HttpClient().PostAsync(uri, form); + + // Assert + string content = await response.Content.ReadAsStringAsync(); + Check.That(content).IsEqualTo("{\"i\":42}"); + } + + [Fact] + public async Task WireMockServer_Proxy_Should_Not_overrule_AdminMappings() + { + // Assign + string path = $"/prx_{Guid.NewGuid()}"; + var serverForProxyForwarding = WireMockServer.Start(); + serverForProxyForwarding + .Given(Request.Create().WithPath(path)) + .RespondWith(Response.Create().WithBody("ok")); + + var server = WireMockServer.Start(new WireMockServerSettings + { + StartAdminInterface = true, + ReadStaticMappings = false, + ProxyAndRecordSettings = new ProxyAndRecordSettings + { + Url = serverForProxyForwarding.Urls[0], + SaveMapping = false, + SaveMappingToFile = false + } + }); + + // Act 1 + var requestMessage1 = new HttpRequestMessage + { + Method = HttpMethod.Get, + RequestUri = new Uri($"{server.Urls[0]}{path}") + }; + var response1 = await new HttpClient().SendAsync(requestMessage1); + + // Assert 1 + string content1 = await response1.Content.ReadAsStringAsync(); + Check.That(content1).IsEqualTo("ok"); + + // Act 2 + var requestMessage2 = new HttpRequestMessage + { + Method = HttpMethod.Get, + RequestUri = new Uri($"{server.Urls[0]}/__admin/mappings") + }; + var response2 = await new HttpClient().SendAsync(requestMessage2); + + // Assert 2 + string content2 = await response2.Content.ReadAsStringAsync(); + Check.That(content2).IsEqualTo("[]"); + } + + [Fact] + public async Task WireMockServer_Proxy_WhenTargetIsNotAvailable_Should_Return_CorrectResponse() + { + // Assign + var settings = new WireMockServerSettings + { + ProxyAndRecordSettings = new ProxyAndRecordSettings + { + Url = $"http://error{Guid.NewGuid()}:12345" + } + }; + var server = WireMockServer.Start(settings); + + // Act + var requestMessage = new HttpRequestMessage + { + Method = HttpMethod.Get, + RequestUri = new Uri(server.Urls[0]) + }; + var httpClientHandler = new HttpClientHandler { AllowAutoRedirect = false }; + var result = await new HttpClient(httpClientHandler).SendAsync(requestMessage); + + // Assert + result.StatusCode.Should().Be(500); + + var content = await result.Content.ReadAsStringAsync(); + content.Should().Contain("known"); // On Linux it's "Name or service not known". On Windows it's "No such host is known.". + + server.LogEntries.Should().HaveCount(1); + ((StatusModel)server.LogEntries.First().ResponseMessage.BodyData.BodyAsJson).Status.Should().Contain("known"); + } + } } \ No newline at end of file