From b3e55118371adac4b716c4cb746b156a1969693d Mon Sep 17 00:00:00 2001 From: ManickaP Date: Tue, 25 Jul 2023 19:05:03 +0200 Subject: [PATCH 1/3] Catch and ignore exception from an external server --- .../Net/Http/HttpClientHandlerTest.RemoteServer.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs index 5e8b94d33705cc..fb5495cb2ba850 100644 --- a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs +++ b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs @@ -5,6 +5,7 @@ using System.IO; using System.Linq; using System.Net.Http.Headers; +using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -1289,7 +1290,16 @@ public async Task GetAsync_SetAutomaticDecompression_ContentDecompressed_Deflate handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; using (HttpClient client = CreateHttpClient(handler)) { - Assert.Contains(expectedContent, await client.GetStringAsync(uri)); + try + { + Assert.Contains(expectedContent, await client.GetStringAsync(uri)); + } + catch (HttpRequestException hre) when (hre.StatusCode == HttpStatusCode.GatewayTimeout || hre.StatusCode == HttpStatusCode.BadGateway || + (hre.InnerException is SocketException se && (se.SocketErrorCode == SocketError.WouldBlock || se.SocketErrorCode == SocketError.TryAgain))) + { + // Ignore the exception, the test depends on an external server that is out of our control. + _output.WriteLine(hre.ToString()); + } } } From 89e87eea274019a2caa6228a7650a94ef53e9d73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marie=20P=C3=ADchov=C3=A1?= <11718369+ManickaP@users.noreply.github.com> Date: Wed, 26 Jul 2023 10:03:09 +0200 Subject: [PATCH 2/3] Update src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs Co-authored-by: Miha Zupan --- .../System/Net/Http/HttpClientHandlerTest.RemoteServer.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs index fb5495cb2ba850..120f737a2affea 100644 --- a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs +++ b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs @@ -1294,8 +1294,8 @@ public async Task GetAsync_SetAutomaticDecompression_ContentDecompressed_Deflate { Assert.Contains(expectedContent, await client.GetStringAsync(uri)); } - catch (HttpRequestException hre) when (hre.StatusCode == HttpStatusCode.GatewayTimeout || hre.StatusCode == HttpStatusCode.BadGateway || - (hre.InnerException is SocketException se && (se.SocketErrorCode == SocketError.WouldBlock || se.SocketErrorCode == SocketError.TryAgain))) + catch (HttpRequestException hre) when (hre.StatusCode is HttpStatusCode.GatewayTimeout or HttpStatusCode.BadGateway || + (hre.InnerException is SocketException se && (se.SocketErrorCode is SocketError.WouldBlock or SocketError.TryAgain))) { // Ignore the exception, the test depends on an external server that is out of our control. _output.WriteLine(hre.ToString()); From 3596db24825f426960e036b9e0a80ff7d31782e0 Mon Sep 17 00:00:00 2001 From: ManickaP Date: Wed, 26 Jul 2023 13:23:37 +0200 Subject: [PATCH 3/3] Fixed framework build --- .../Net/Http/HttpClientHandlerTest.RemoteServer.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs index 120f737a2affea..ae054af8a93b33 100644 --- a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs +++ b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs @@ -1292,10 +1292,16 @@ public async Task GetAsync_SetAutomaticDecompression_ContentDecompressed_Deflate { try { - Assert.Contains(expectedContent, await client.GetStringAsync(uri)); + using HttpResponseMessage response = await client.GetAsync(uri); + if (response.StatusCode is HttpStatusCode.GatewayTimeout or HttpStatusCode.BadGateway) + { + // Ignore the erroneous status code, the test depends on an external server that is out of our control. + _output.WriteLine(response.ToString()); + return; + } + Assert.Contains(expectedContent, await response.Content.ReadAsStringAsync()); } - catch (HttpRequestException hre) when (hre.StatusCode is HttpStatusCode.GatewayTimeout or HttpStatusCode.BadGateway || - (hre.InnerException is SocketException se && (se.SocketErrorCode is SocketError.WouldBlock or SocketError.TryAgain))) + catch (HttpRequestException hre) when (hre.InnerException is SocketException se && (se.SocketErrorCode is SocketError.WouldBlock or SocketError.TryAgain)) { // Ignore the exception, the test depends on an external server that is out of our control. _output.WriteLine(hre.ToString());