From d8af193d888c0097b5156208177949b2a4fe0c35 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Fri, 19 Jun 2020 01:56:15 +0200 Subject: [PATCH 1/7] fix exception message --- .../System/Net/Http/SocketsHttpHandler/ConnectHelper.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectHelper.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectHelper.cs index 288022837e8321..ed306e3247047f 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectHelper.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectHelper.cs @@ -72,7 +72,7 @@ public static async ValueTask ConnectAsync(string host, int port, Cancel } catch (Exception error) when (!(error is OperationCanceledException)) { - throw CreateWrappedException(error, cancellationToken); + throw CreateWrappedException(error, host, port, cancellationToken); } finally { @@ -206,18 +206,18 @@ public static async ValueTask ConnectQuicAsync(string host, int if (lastException != null) { - throw CreateWrappedException(lastException, cancellationToken); + throw CreateWrappedException(lastException, host, port, cancellationToken); } // TODO: find correct exception to throw here. throw new HttpRequestException("No host found."); } - private static Exception CreateWrappedException(Exception error, CancellationToken cancellationToken) + private static Exception CreateWrappedException(Exception error, string host, int port, CancellationToken cancellationToken) { return CancellationHelper.ShouldWrapInOperationCanceledException(error, cancellationToken) ? CancellationHelper.CreateOperationCanceledException(error, cancellationToken) : - new HttpRequestException(error.Message, error, RequestRetryType.RetryOnNextProxy); + new HttpRequestException($"{error.Message} {host}:{port}", error, RequestRetryType.RetryOnNextProxy); } } } From d92c0deb618f1bff2bd708a8d5b71915ef960774 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Fri, 19 Jun 2020 01:53:37 +0200 Subject: [PATCH 2/7] add test --- .../tests/FunctionalTests/HttpClientTest.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientTest.cs b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientTest.cs index 62ff7ace2e2488..09f176ee2d4c2d 100644 --- a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientTest.cs +++ b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientTest.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Diagnostics; using Microsoft.DotNet.RemoteExecutor; using System.IO; using System.Linq; @@ -205,6 +206,15 @@ public async Task GetContentAsync_ErrorStatusCode_ExpectedExceptionThrown(bool w } } + [Fact] + [OuterLoop("Slow - Negative connection test")] + public async Task GetContentAsync_WhenCanNotConnect_ExceptionContainsHostInfo() + { + using var client = new HttpClient(new SocketsHttpHandler()); + HttpRequestException ex = await Assert.ThrowsAsync(() => client.GetStreamAsync("http://localhost:4242")); + Assert.Contains("localhost:4242", ex.Message); + } + [Fact] public async Task GetContentAsync_NullResponse_Throws() { From 46d6d2ecf12bc3ee18fdac2df3af80061a08b276 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Fri, 19 Jun 2020 01:57:47 +0200 Subject: [PATCH 3/7] remove unnecessary using --- .../System.Net.Http/tests/FunctionalTests/HttpClientTest.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientTest.cs b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientTest.cs index 09f176ee2d4c2d..2e70dab59488e3 100644 --- a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientTest.cs +++ b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientTest.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System.Diagnostics; using Microsoft.DotNet.RemoteExecutor; using System.IO; using System.Linq; From 877ca25bba31ce53b45d99ad85112f6632708c11 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Tue, 30 Jun 2020 22:56:12 +0200 Subject: [PATCH 4/7] improve test --- .../tests/FunctionalTests/HttpClientTest.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientTest.cs b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientTest.cs index 2e70dab59488e3..d91b28894fde8a 100644 --- a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientTest.cs +++ b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientTest.cs @@ -5,6 +5,7 @@ using Microsoft.DotNet.RemoteExecutor; using System.IO; using System.Linq; +using System.Net.Sockets; using System.Net.Test.Common; using System.Text; using System.Threading; @@ -206,12 +207,16 @@ public async Task GetContentAsync_ErrorStatusCode_ExpectedExceptionThrown(bool w } [Fact] - [OuterLoop("Slow - Negative connection test")] + [OuterLoop("Failing connection attempts take long on windows")] public async Task GetContentAsync_WhenCanNotConnect_ExceptionContainsHostInfo() { + using Socket portReserver = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + portReserver.Bind(new IPEndPoint(IPAddress.Loopback, 0)); + IPEndPoint ep = (IPEndPoint)portReserver.LocalEndPoint; + using var client = new HttpClient(new SocketsHttpHandler()); - HttpRequestException ex = await Assert.ThrowsAsync(() => client.GetStreamAsync("http://localhost:4242")); - Assert.Contains("localhost:4242", ex.Message); + HttpRequestException ex = await Assert.ThrowsAsync(() => client.GetStreamAsync($"http://localhost:{ep.Port}")); + Assert.Contains($"localhost:{ep.Port}", ex.Message); } [Fact] From b76bdce962e1eeb02192cd2682c633fee924c7f5 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Tue, 30 Jun 2020 23:05:50 +0200 Subject: [PATCH 5/7] use CreateHttpClient() --- .../System.Net.Http/tests/FunctionalTests/HttpClientTest.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientTest.cs b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientTest.cs index d91b28894fde8a..a43a92a986a935 100644 --- a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientTest.cs +++ b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientTest.cs @@ -214,7 +214,8 @@ public async Task GetContentAsync_WhenCanNotConnect_ExceptionContainsHostInfo() portReserver.Bind(new IPEndPoint(IPAddress.Loopback, 0)); IPEndPoint ep = (IPEndPoint)portReserver.LocalEndPoint; - using var client = new HttpClient(new SocketsHttpHandler()); + using var client = CreateHttpClient(); + HttpRequestException ex = await Assert.ThrowsAsync(() => client.GetStreamAsync($"http://localhost:{ep.Port}")); Assert.Contains($"localhost:{ep.Port}", ex.Message); } From 2bcadb738c7916059d6bfff66719796e0f59cfa5 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Tue, 30 Jun 2020 23:12:04 +0200 Subject: [PATCH 6/7] add brackets --- .../src/System/Net/Http/SocketsHttpHandler/ConnectHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectHelper.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectHelper.cs index ed306e3247047f..19864f9b6887f2 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectHelper.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectHelper.cs @@ -217,7 +217,7 @@ private static Exception CreateWrappedException(Exception error, string host, in { return CancellationHelper.ShouldWrapInOperationCanceledException(error, cancellationToken) ? CancellationHelper.CreateOperationCanceledException(error, cancellationToken) : - new HttpRequestException($"{error.Message} {host}:{port}", error, RequestRetryType.RetryOnNextProxy); + new HttpRequestException($"{error.Message} ({host}:{port})", error, RequestRetryType.RetryOnNextProxy); } } } From 8e561ef1a352cb1dc56baf4fdb2193fc3b909e20 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Tue, 7 Jul 2020 23:11:16 +0200 Subject: [PATCH 7/7] fix build --- .../src/System/Net/Http/SocketsHttpHandler/ConnectHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectHelper.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectHelper.cs index 3b6e659adc87cf..5296684169c59f 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectHelper.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectHelper.cs @@ -101,7 +101,7 @@ private static Stream Connect(string host, int port, CancellationToken cancellat catch (Exception e) { socket.Dispose(); - throw CreateWrappedException(e, cancellationToken); + throw CreateWrappedException(e, host, port, cancellationToken); } return new NetworkStream(socket, ownsSocket: true);