From 8b5e16c9914206b322a6ddd407b08078932807d4 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Sun, 17 May 2020 20:43:43 +0200 Subject: [PATCH 1/4] wip --- azure-pipelines-ci.yml | 6 +-- src/WireMock.Net/Owin/WireMockMiddleware.cs | 2 +- .../Serialization/LogEntryMapper.cs | 3 +- .../WireMock.Net.Tests.csproj | 2 +- .../WireMockServer.Proxy.cs | 48 ++++++++++++++----- 5 files changed, 43 insertions(+), 18 deletions(-) diff --git a/azure-pipelines-ci.yml b/azure-pipelines-ci.yml index 6d7f8fbcd..5e7c5b1af 100644 --- a/azure-pipelines-ci.yml +++ b/azure-pipelines-ci.yml @@ -28,11 +28,11 @@ steps: displayName: Begin SonarScanner condition: and(succeeded(), eq(variables['RUN_SONAR'], 'yes')) -# Build source, tests and run tests for net452 and netcoreapp2.1 (with coverage) +# Build source, tests and run tests for net452 and netcoreapp3.1 (with coverage) - script: | dotnet test ./test/WireMock.Net.Tests/WireMock.Net.Tests.csproj --configuration Debug --framework net452 - dotnet test ./test/WireMock.Net.Tests/WireMock.Net.Tests.csproj --configuration Debug --framework netcoreapp2.1 --logger trx /p:CollectCoverage=true /p:CoverletOutputFormat=opencover - displayName: 'Build source, tests and run tests for net452 and netcoreapp2.1 (with coverage)' + dotnet test ./test/WireMock.Net.Tests/WireMock.Net.Tests.csproj --configuration Debug --framework netcoreapp3.1 --logger trx /p:CollectCoverage=true /p:CoverletOutputFormat=opencover + displayName: 'Build source, tests and run tests for net452 and netcoreapp3.1 (with coverage)' # End SonarScanner - script: | diff --git a/src/WireMock.Net/Owin/WireMockMiddleware.cs b/src/WireMock.Net/Owin/WireMockMiddleware.cs index 580a78578..c225c94f7 100644 --- a/src/WireMock.Net/Owin/WireMockMiddleware.cs +++ b/src/WireMock.Net/Owin/WireMockMiddleware.cs @@ -129,7 +129,7 @@ private async Task InvokeInternal(IContext ctx) catch (Exception ex) { _options.Logger.Error($"Providing a Response for Mapping '{result?.Mapping?.Guid}' failed. HttpStatusCode set to 500. Exception: {ex}"); - response = ResponseMessageBuilder.Create(JsonConvert.SerializeObject(ex), 500); + response = ResponseMessageBuilder.Create(ex.Message, 500); } finally { diff --git a/src/WireMock.Net/Serialization/LogEntryMapper.cs b/src/WireMock.Net/Serialization/LogEntryMapper.cs index 46ef10854..610c77ed1 100644 --- a/src/WireMock.Net/Serialization/LogEntryMapper.cs +++ b/src/WireMock.Net/Serialization/LogEntryMapper.cs @@ -2,9 +2,8 @@ using WireMock.Admin.Mappings; using WireMock.Admin.Requests; using WireMock.Logging; -using WireMock.Types; using WireMock.ResponseBuilders; -using WireMock.Util; +using WireMock.Types; namespace WireMock.Serialization { diff --git a/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj b/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj index 135651539..f3a1436e0 100644 --- a/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj +++ b/test/WireMock.Net.Tests/WireMock.Net.Tests.csproj @@ -3,7 +3,7 @@ Stef Heyenrath - netcoreapp2.1 + netcoreapp3.1 full WireMock.Net.Tests WireMock.Net.Tests diff --git a/test/WireMock.Net.Tests/WireMockServer.Proxy.cs b/test/WireMock.Net.Tests/WireMockServer.Proxy.cs index 9ee8a68b6..7e867cbfb 100644 --- a/test/WireMock.Net.Tests/WireMockServer.Proxy.cs +++ b/test/WireMock.Net.Tests/WireMockServer.Proxy.cs @@ -50,7 +50,7 @@ public async Task WireMockServer_Proxy_Should_log_proxied_requests() public async Task WireMockServer_Proxy_Should_proxy_responses() { // Assign - string path = $"/prx_{Guid.NewGuid().ToString()}"; + string path = $"/prx_{Guid.NewGuid()}"; var server = WireMockServer.Start(); server .Given(Request.Create().WithPath(path)) @@ -76,7 +76,7 @@ public async Task WireMockServer_Proxy_Should_proxy_responses() public async Task WireMockServer_Proxy_Should_preserve_content_header_in_proxied_request() { // Assign - string path = $"/prx_{Guid.NewGuid().ToString()}"; + string path = $"/prx_{Guid.NewGuid()}"; var serverForProxyForwarding = WireMockServer.Start(); serverForProxyForwarding .Given(Request.Create().WithPath(path)) @@ -123,7 +123,7 @@ public async Task WireMockServer_Proxy_Should_preserve_content_header_in_proxied public async Task WireMockServer_Proxy_Should_exclude_blacklisted_content_header_in_mapping() { // Assign - string path = $"/prx_{Guid.NewGuid().ToString()}"; + string path = $"/prx_{Guid.NewGuid()}"; var serverForProxyForwarding = WireMockServer.Start(); serverForProxyForwarding .Given(Request.Create().WithPath(path)) @@ -165,7 +165,7 @@ public async Task WireMockServer_Proxy_Should_exclude_blacklisted_content_header public async Task WireMockServer_Proxy_Should_exclude_blacklisted_cookies_in_mapping() { // Assign - string path = $"/prx_{Guid.NewGuid().ToString()}"; + string path = $"/prx_{Guid.NewGuid()}"; var serverForProxyForwarding = WireMockServer.Start(); serverForProxyForwarding .Given(Request.Create().WithPath(path)) @@ -214,7 +214,7 @@ public async Task WireMockServer_Proxy_Should_exclude_blacklisted_cookies_in_map public async Task WireMockServer_Proxy_Should_preserve_content_header_in_proxied_request_with_empty_content() { // Assign - string path = $"/prx_{Guid.NewGuid().ToString()}"; + string path = $"/prx_{Guid.NewGuid()}"; var serverForProxyForwarding = WireMockServer.Start(); serverForProxyForwarding .Given(Request.Create().WithPath(path)) @@ -246,7 +246,7 @@ public async Task WireMockServer_Proxy_Should_preserve_content_header_in_proxied public async Task WireMockServer_Proxy_Should_preserve_content_header_in_proxied_response() { // Assign - string path = $"/prx_{Guid.NewGuid().ToString()}"; + string path = $"/prx_{Guid.NewGuid()}"; var serverForProxyForwarding = WireMockServer.Start(); serverForProxyForwarding .Given(Request.Create().WithPath(path)) @@ -277,7 +277,7 @@ public async Task WireMockServer_Proxy_Should_preserve_content_header_in_proxied public async Task WireMockServer_Proxy_Should_change_absolute_location_header_in_proxied_response() { // Assign - string path = $"/prx_{Guid.NewGuid().ToString()}"; + string path = $"/prx_{Guid.NewGuid()}"; var settings = new WireMockServerSettings { AllowPartialMapping = false }; var serverForProxyForwarding = WireMockServer.Start(settings); @@ -310,7 +310,7 @@ public async Task WireMockServer_Proxy_Should_change_absolute_location_header_in public async Task WireMockServer_Proxy_Should_preserve_cookie_header_in_proxied_request() { // Assign - string path = $"/prx_{Guid.NewGuid().ToString()}"; + string path = $"/prx_{Guid.NewGuid()}"; var serverForProxyForwarding = WireMockServer.Start(); serverForProxyForwarding .Given(Request.Create().WithPath(path)) @@ -379,7 +379,7 @@ public async Task WireMockServer_Proxy_Should_preserve_binary_request_content() public async Task WireMockServer_Proxy_Should_set_BodyAsJson_in_proxied_response() { // Assign - string path = $"/prx_{Guid.NewGuid().ToString()}"; + string path = $"/prx_{Guid.NewGuid()}"; var serverForProxyForwarding = WireMockServer.Start(); serverForProxyForwarding .Given(Request.Create().WithPath(path)) @@ -410,7 +410,7 @@ public async Task WireMockServer_Proxy_Should_set_BodyAsJson_in_proxied_response public async Task WireMockServer_Proxy_Should_set_Body_in_multipart_proxied_response() { // Assign - string path = $"/prx_{Guid.NewGuid().ToString()}"; + string path = $"/prx_{Guid.NewGuid()}"; var serverForProxyForwarding = WireMockServer.Start(); serverForProxyForwarding .Given(Request.Create().WithPath(path)) @@ -440,7 +440,7 @@ public async Task WireMockServer_Proxy_Should_set_Body_in_multipart_proxied_resp public async Task WireMockServer_Proxy_Should_Not_overrule_AdminMappings() { // Assign - string path = $"/prx_{Guid.NewGuid().ToString()}"; + string path = $"/prx_{Guid.NewGuid()}"; var serverForProxyForwarding = WireMockServer.Start(); serverForProxyForwarding .Given(Request.Create().WithPath(path)) @@ -482,5 +482,31 @@ public async Task WireMockServer_Proxy_Should_Not_overrule_AdminMappings() 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://localhost: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 }; + await new HttpClient(httpClientHandler).SendAsync(requestMessage); + + // Assert + Check.That(server.LogEntries).HasSize(1); + } } } \ No newline at end of file From 8e25d515b75da9fe152afb778fa10dc596e3c82b Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Mon, 18 May 2020 07:35:49 +0000 Subject: [PATCH 2/4] fix --- test/WireMock.Net.Tests/WireMockServer.Proxy.cs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/test/WireMock.Net.Tests/WireMockServer.Proxy.cs b/test/WireMock.Net.Tests/WireMockServer.Proxy.cs index 7e867cbfb..a83921ae1 100644 --- a/test/WireMock.Net.Tests/WireMockServer.Proxy.cs +++ b/test/WireMock.Net.Tests/WireMockServer.Proxy.cs @@ -5,7 +5,10 @@ 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; @@ -491,7 +494,7 @@ public async Task WireMockServer_Proxy_WhenTargetIsNotAvailable_Should_Return_Co { ProxyAndRecordSettings = new ProxyAndRecordSettings { - Url = $"http://localhost:12345" + Url = $"http://error{Guid.NewGuid()}:12345" } }; var server = WireMockServer.Start(settings); @@ -503,10 +506,16 @@ public async Task WireMockServer_Proxy_WhenTargetIsNotAvailable_Should_Return_Co RequestUri = new Uri(server.Urls[0]) }; var httpClientHandler = new HttpClientHandler { AllowAutoRedirect = false }; - await new HttpClient(httpClientHandler).SendAsync(requestMessage); + var result = await new HttpClient(httpClientHandler).SendAsync(requestMessage); // Assert - Check.That(server.LogEntries).HasSize(1); + result.StatusCode.Should().Be(500); + + var content = await result.Content.ReadAsStringAsync(); + content.Should().Contain("No such host is known."); + + server.LogEntries.Should().HaveCount(1); + ((StatusModel) server.LogEntries.First().ResponseMessage.BodyData.BodyAsJson).Status.Should().Be("No such host is known."); } } } \ No newline at end of file From 7b38ba0ffd582a08ff7cbe25e6a0ba1efde045db Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Mon, 18 May 2020 07:38:57 +0000 Subject: [PATCH 3/4] 31 --- azure-pipelines-ci-linux.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azure-pipelines-ci-linux.yml b/azure-pipelines-ci-linux.yml index 1713472fd..aadfe4d47 100644 --- a/azure-pipelines-ci-linux.yml +++ b/azure-pipelines-ci-linux.yml @@ -6,7 +6,7 @@ variables: steps: - script: | - dotnet test ./test/WireMock.Net.Tests/WireMock.Net.Tests.csproj --configuration $(buildConfiguration) --framework netcoreapp2.1 --logger trx + dotnet test ./test/WireMock.Net.Tests/WireMock.Net.Tests.csproj --configuration $(buildConfiguration) --framework netcoreapp3.1 --logger trx - task: PublishTestResults@2 inputs: testRunner: VSTest From 0acb55c51eae7cd8890ab9e2631acde095e42fb5 Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Mon, 18 May 2020 07:44:29 +0000 Subject: [PATCH 4/4] known --- test/WireMock.Net.Tests/WireMockServer.Proxy.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/WireMock.Net.Tests/WireMockServer.Proxy.cs b/test/WireMock.Net.Tests/WireMockServer.Proxy.cs index a83921ae1..1215ec029 100644 --- a/test/WireMock.Net.Tests/WireMockServer.Proxy.cs +++ b/test/WireMock.Net.Tests/WireMockServer.Proxy.cs @@ -512,10 +512,10 @@ public async Task WireMockServer_Proxy_WhenTargetIsNotAvailable_Should_Return_Co result.StatusCode.Should().Be(500); var content = await result.Content.ReadAsStringAsync(); - content.Should().Contain("No such host is known."); + 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().Be("No such host is known."); + ((StatusModel) server.LogEntries.First().ResponseMessage.BodyData.BodyAsJson).Status.Should().Contain("known"); } } } \ No newline at end of file