From fd6017e9e4776dbc6e47ba2d2b9af6637cf87c12 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Tue, 15 Dec 2020 12:40:41 +0000 Subject: [PATCH 1/4] WireMockServer_Proxy_Should_preserve_Authorization_header_in_proxied_request --- src/WireMock.Net/RequestBuilders/Request.cs | 10 ++++ .../WireMockServer.Proxy.cs | 55 ++++++++++++++++++- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/src/WireMock.Net/RequestBuilders/Request.cs b/src/WireMock.Net/RequestBuilders/Request.cs index aeb42be89..184a810b7 100644 --- a/src/WireMock.Net/RequestBuilders/Request.cs +++ b/src/WireMock.Net/RequestBuilders/Request.cs @@ -55,6 +55,16 @@ private Request(IList requestMatchers) : base(requestMatchers) return _requestMatchers.OfType().FirstOrDefault(); } + /// + /// Gets the request message matcher. + /// + /// Type of IRequestMatcher + /// A RequestMatcher + public T GetRequestMessageMatcher(Func func) where T : IRequestMatcher + { + return _requestMatchers.OfType().FirstOrDefault(func); + } + /// public IRequestBuilder WithClientIP(params IStringMatcher[] matchers) { diff --git a/test/WireMock.Net.Tests/WireMockServer.Proxy.cs b/test/WireMock.Net.Tests/WireMockServer.Proxy.cs index 342e17ad5..52fff3cdf 100644 --- a/test/WireMock.Net.Tests/WireMockServer.Proxy.cs +++ b/test/WireMock.Net.Tests/WireMockServer.Proxy.cs @@ -13,6 +13,7 @@ using WireMock.ResponseBuilders; using WireMock.Server; using WireMock.Settings; +using WireMock.Util; using Xunit; namespace WireMock.Net.Tests @@ -75,8 +76,8 @@ public async Task WireMockServer_Proxy_Should_log_proxied_requests() await new HttpClient(httpClientHandler).SendAsync(requestMessage); // Assert - Check.That(server.Mappings).HasSize(2); - Check.That(server.LogEntries).HasSize(1); + server.Mappings.Should().HaveCount(2); + server.LogEntries.Should().HaveCount(1); } [Fact] @@ -154,6 +155,56 @@ public async Task WireMockServer_Proxy_Should_preserve_content_header_in_proxied //Check.That(matcher).IsNotNull(); } + [Fact] + public async Task WireMockServer_Proxy_Should_preserve_Authorization_header_in_proxied_request() + { + // Assign + string path = $"/prx_{Guid.NewGuid()}"; + var serverForProxyForwarding = WireMockServer.Start(); + serverForProxyForwarding + .Given(Request.Create().WithPath(path)) + .RespondWith(Response.Create().WithCallback(x => new ResponseMessage + { + BodyData = new BodyData + { + BodyAsString = x.Headers["Authorization"].ToString(), + DetectedBodyType = Types.BodyType.String + } + })); + + 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.Headers.Authorization = new AuthenticationHeaderValue("BASIC", "test-A"); + var result = await new HttpClient().SendAsync(requestMessage); + + // Assert + (await result.Content.ReadAsStringAsync()).Should().Be("BASIC test-A"); + + var receivedRequest = serverForProxyForwarding.LogEntries.First().RequestMessage; + var authorizationHeader = receivedRequest.Headers["Authorization"].ToString().Should().Be("BASIC test-A"); + + server.Mappings.Should().HaveCount(2); + var authorizationRequestMessageHeaderMatcher = ((Request)server.Mappings.Last().RequestMatcher) + .GetRequestMessageMatcher(x => x.Matchers.Any(m => m.GetPatterns().Contains("BASIC test-A"))); + authorizationRequestMessageHeaderMatcher.Should().NotBeNull(); + } + [Fact] public async Task WireMockServer_Proxy_Should_exclude_ExcludedHeaders_in_mapping() { From a99f2a0439cb116280451d0f46c06047563ca59c Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Tue, 15 Dec 2020 20:34:39 +0100 Subject: [PATCH 2/4] !admin --- .../Program.cs | 32 ++++--------------- ...eMock.Net.Console.Proxy.NETCoreApp2.csproj | 2 +- .../WireMockServer.Proxy.cs | 2 +- 3 files changed, 9 insertions(+), 27 deletions(-) diff --git a/examples/WireMock.Net.Console.Proxy.NETCoreApp2/Program.cs b/examples/WireMock.Net.Console.Proxy.NETCoreApp2/Program.cs index c7f923e1b..73c9e3403 100644 --- a/examples/WireMock.Net.Console.Proxy.NETCoreApp2/Program.cs +++ b/examples/WireMock.Net.Console.Proxy.NETCoreApp2/Program.cs @@ -3,6 +3,7 @@ using System.Threading; using System.Threading.Tasks; using Newtonsoft.Json; +using WireMock.Logging; using WireMock.RequestBuilders; using WireMock.ResponseBuilders; using WireMock.Server; @@ -14,9 +15,6 @@ class Program { static void Main(string[] args) { - RunTestDifferentPort().Wait(20000); // prints "1" - RunTestDifferentPort().Wait(20000); // prints "1" - var server = WireMockServer.Start(new WireMockServerSettings { Urls = new[] { "http://localhost:9091", "https://localhost:9443" }, @@ -29,34 +27,18 @@ static void Main(string[] args) SaveMapping = true, SaveMappingToFile = false, ExcludedHeaders = new[] { "dnt", "Content-Length" } - } + }, + Logger= new WireMockConsoleLogger() }); - server.LogEntriesChanged += (sender, eventRecordArgs) => - { - System.Console.WriteLine(JsonConvert.SerializeObject(eventRecordArgs.NewItems, Formatting.Indented)); - }; + //server.LogEntriesChanged += (sender, eventRecordArgs) => + //{ + // System.Console.WriteLine(JsonConvert.SerializeObject(eventRecordArgs.NewItems, Formatting.Indented)); + //}; System.Console.WriteLine("Press any key to stop the server"); System.Console.ReadKey(); server.Stop(); } - - private static async Task RunTestDifferentPort() - { - var server = WireMockServer.Start(); - - server.Given(Request.Create().WithPath("/").UsingGet()) - .RespondWith(Response.Create().WithStatusCode(200).WithBody("Hello")); - - Thread.Sleep(1000); - - var response = await new HttpClient().GetAsync(server.Urls[0]); - response.EnsureSuccessStatusCode(); - - System.Console.WriteLine("RunTestDifferentPort - server.LogEntries.Count() = " + server.LogEntries.Count()); - - server.Stop(); - } } } diff --git a/examples/WireMock.Net.Console.Proxy.NETCoreApp2/WireMock.Net.Console.Proxy.NETCoreApp2.csproj b/examples/WireMock.Net.Console.Proxy.NETCoreApp2/WireMock.Net.Console.Proxy.NETCoreApp2.csproj index a02ff1c67..a5fc676e4 100644 --- a/examples/WireMock.Net.Console.Proxy.NETCoreApp2/WireMock.Net.Console.Proxy.NETCoreApp2.csproj +++ b/examples/WireMock.Net.Console.Proxy.NETCoreApp2/WireMock.Net.Console.Proxy.NETCoreApp2.csproj @@ -2,7 +2,7 @@ Exe - netcoreapp2.1 + netcoreapp2.1;netcoreapp3.1;net5.0 ../../WireMock.Net-Logo.ico diff --git a/test/WireMock.Net.Tests/WireMockServer.Proxy.cs b/test/WireMock.Net.Tests/WireMockServer.Proxy.cs index 52fff3cdf..8ba498d15 100644 --- a/test/WireMock.Net.Tests/WireMockServer.Proxy.cs +++ b/test/WireMock.Net.Tests/WireMockServer.Proxy.cs @@ -200,7 +200,7 @@ public async Task WireMockServer_Proxy_Should_preserve_Authorization_header_in_p var authorizationHeader = receivedRequest.Headers["Authorization"].ToString().Should().Be("BASIC test-A"); server.Mappings.Should().HaveCount(2); - var authorizationRequestMessageHeaderMatcher = ((Request)server.Mappings.Last().RequestMatcher) + var authorizationRequestMessageHeaderMatcher = ((Request)server.Mappings.Single(m => !m.IsAdminInterface).RequestMatcher) .GetRequestMessageMatcher(x => x.Matchers.Any(m => m.GetPatterns().Contains("BASIC test-A"))); authorizationRequestMessageHeaderMatcher.Should().NotBeNull(); } From d286275eb73b0524d464df87cb67bcc3c212fa9d Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Tue, 15 Dec 2020 20:35:17 +0100 Subject: [PATCH 3/4] x --- test/WireMock.Net.Tests/WireMockServer.Proxy.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/WireMock.Net.Tests/WireMockServer.Proxy.cs b/test/WireMock.Net.Tests/WireMockServer.Proxy.cs index 8ba498d15..a07bac254 100644 --- a/test/WireMock.Net.Tests/WireMockServer.Proxy.cs +++ b/test/WireMock.Net.Tests/WireMockServer.Proxy.cs @@ -149,10 +149,6 @@ public async Task WireMockServer_Proxy_Should_preserve_content_header_in_proxied // 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] From 5ff217b4b59d6dc7c583848aec8d2352037b501e Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Wed, 16 Dec 2020 15:18:34 +0000 Subject: [PATCH 4/4] . --- .../ResponseBuilders/ResponseWithFaultTests.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/WireMock.Net.Tests/ResponseBuilders/ResponseWithFaultTests.cs b/test/WireMock.Net.Tests/ResponseBuilders/ResponseWithFaultTests.cs index 437ae6a60..d4311fedb 100644 --- a/test/WireMock.Net.Tests/ResponseBuilders/ResponseWithFaultTests.cs +++ b/test/WireMock.Net.Tests/ResponseBuilders/ResponseWithFaultTests.cs @@ -1,6 +1,5 @@ -using FluentAssertions; -using Moq; -using System.Threading.Tasks; +using System.Threading.Tasks; +using FluentAssertions; using WireMock.Models; using WireMock.ResponseBuilders; using WireMock.Settings;