From feea64b3287c2141899d5790fc984513ee7e378d Mon Sep 17 00:00:00 2001 From: Vitaliy Davydiak Date: Tue, 17 Sep 2019 19:15:23 +0300 Subject: [PATCH] Fix recorded requests skipped by request logger (#346) * Fix recorded requests skipped request logger. - When proxy is enabled the recorded requests are mistaken (IMO) for admin requests and skipped * Add unit test * Use different solution * Introduce IsRecordedByProxy property on Mapping class * Cleanup empty lines * Refactored fix suggested way --- src/WireMock.Net/IMapping.cs | 8 +++++ src/WireMock.Net/Mapping.cs | 3 ++ src/WireMock.Net/Owin/WireMockMiddleware.cs | 3 +- .../FluentMockServerTests.Proxy.cs | 30 +++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/WireMock.Net/IMapping.cs b/src/WireMock.Net/IMapping.cs index e8d95c18..0f92cfae 100644 --- a/src/WireMock.Net/IMapping.cs +++ b/src/WireMock.Net/IMapping.cs @@ -78,6 +78,14 @@ namespace WireMock /// true if this mapping is an Admin Interface; otherwise, false. /// bool IsAdminInterface { get; } + + /// + /// Gets a value indicating whether this mapping to be logged. + /// + /// + /// true if this mapping to be logged; otherwise, false. + /// + bool LogMapping { get; } /// /// ProvideResponseAsync diff --git a/src/WireMock.Net/Mapping.cs b/src/WireMock.Net/Mapping.cs index c7b4578c..81ebaa14 100644 --- a/src/WireMock.Net/Mapping.cs +++ b/src/WireMock.Net/Mapping.cs @@ -48,6 +48,9 @@ namespace WireMock /// public bool IsAdminInterface => Provider is DynamicResponseProvider || Provider is DynamicAsyncResponseProvider || Provider is ProxyAsyncResponseProvider; + /// + public bool LogMapping => !(Provider is DynamicResponseProvider || Provider is DynamicAsyncResponseProvider); + /// /// Initializes a new instance of the class. /// diff --git a/src/WireMock.Net/Owin/WireMockMiddleware.cs b/src/WireMock.Net/Owin/WireMockMiddleware.cs index cf7c1b37..e6173eea 100644 --- a/src/WireMock.Net/Owin/WireMockMiddleware.cs +++ b/src/WireMock.Net/Owin/WireMockMiddleware.cs @@ -7,6 +7,7 @@ using WireMock.Util; using Newtonsoft.Json; using WireMock.Http; using WireMock.Owin.Mappers; +using WireMock.ResponseProviders; using WireMock.Serialization; using WireMock.Validation; #if !USE_ASPNETCORE @@ -99,7 +100,7 @@ namespace WireMock.Owin return; } - logRequest = !targetMapping.IsAdminInterface; + logRequest = !targetMapping.IsAdminInterface || targetMapping.LogMapping; if (targetMapping.IsAdminInterface && _options.AuthorizationMatcher != null) { diff --git a/test/WireMock.Net.Tests/FluentMockServerTests.Proxy.cs b/test/WireMock.Net.Tests/FluentMockServerTests.Proxy.cs index bc1da82a..123f05a8 100644 --- a/test/WireMock.Net.Tests/FluentMockServerTests.Proxy.cs +++ b/test/WireMock.Net.Tests/FluentMockServerTests.Proxy.cs @@ -18,6 +18,35 @@ namespace WireMock.Net.Tests { public class FluentMockServerProxyTests { + [Fact] + public async Task FluentMockServer_Proxy_Should_log_proxied_requests() + { + // Assign + var settings = new FluentMockServerSettings + { + ProxyAndRecordSettings = new ProxyAndRecordSettings + { + Url = "http://www.google.com", + SaveMapping = true, + SaveMappingToFile = false + } + }; + var server = FluentMockServer.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); + } + [Fact] public async Task FluentMockServer_Proxy_Should_proxy_responses() { @@ -40,6 +69,7 @@ namespace WireMock.Net.Tests // Assert Check.That(server.Mappings).HasSize(1); + Check.That(server.LogEntries).HasSize(1); Check.That(content).Contains("google"); }