Skip to content

Commit f937797

Browse files
ManickaPMihaZupan
andauthored
[HTTP] Catch and ignore exception from an external server (#89452)
* Catch and ignore exception from an external server * Update src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs Co-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com> * Fixed framework build --------- Co-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com>
1 parent 72efb9c commit f937797

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.RemoteServer.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.IO;
66
using System.Linq;
77
using System.Net.Http.Headers;
8+
using System.Net.Sockets;
89
using System.Text;
910
using System.Threading;
1011
using System.Threading.Tasks;
@@ -1289,7 +1290,22 @@ public async Task GetAsync_SetAutomaticDecompression_ContentDecompressed_Deflate
12891290
handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
12901291
using (HttpClient client = CreateHttpClient(handler))
12911292
{
1292-
Assert.Contains(expectedContent, await client.GetStringAsync(uri));
1293+
try
1294+
{
1295+
using HttpResponseMessage response = await client.GetAsync(uri);
1296+
if (response.StatusCode is HttpStatusCode.GatewayTimeout or HttpStatusCode.BadGateway)
1297+
{
1298+
// Ignore the erroneous status code, the test depends on an external server that is out of our control.
1299+
_output.WriteLine(response.ToString());
1300+
return;
1301+
}
1302+
Assert.Contains(expectedContent, await response.Content.ReadAsStringAsync());
1303+
}
1304+
catch (HttpRequestException hre) when (hre.InnerException is SocketException se && (se.SocketErrorCode is SocketError.WouldBlock or SocketError.TryAgain))
1305+
{
1306+
// Ignore the exception, the test depends on an external server that is out of our control.
1307+
_output.WriteLine(hre.ToString());
1308+
}
12931309
}
12941310
}
12951311

0 commit comments

Comments
 (0)