From aa06639062b79224a181450c6b267a9e6a93cc44 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Wed, 31 Jul 2024 12:15:01 -0400 Subject: [PATCH] Store status code in exception for ProxyTunnelError (#105610) --- .../System/Net/Http/HttpClientHandlerTest.Proxy.cs | 12 ++++++++++-- .../ConnectionPool/HttpConnectionPool.cs | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.Proxy.cs b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.Proxy.cs index d3e00ce0327b0..bb664f9129856 100644 --- a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.Proxy.cs +++ b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.Proxy.cs @@ -300,7 +300,11 @@ public async Task AuthenticatedProxyTunnelRequest_PostAsyncWithNoCreds_Throws() using (HttpClient client = CreateHttpClient(handler)) { HttpRequestException e = await Assert.ThrowsAnyAsync(async () => await client.PostAsync("https://nosuchhost.invalid", new StringContent(content))); - Assert.Contains("407", e.Message); + Assert.Contains(((int)HttpStatusCode.ProxyAuthenticationRequired).ToString(), e.Message); +#if !WINHTTPHANDLER_TEST + Assert.Equal(HttpStatusCode.ProxyAuthenticationRequired, e.StatusCode); + Assert.Equal(HttpRequestError.ProxyTunnelError, e.HttpRequestError); +#endif } } } @@ -313,7 +317,11 @@ public async Task Proxy_SslProxyUnsupported_Throws() { handler.Proxy = new WebProxy($"https://{Guid.NewGuid():N}"); - await Assert.ThrowsAsync(() => client.GetAsync($"http://{Guid.NewGuid():N}")); + HttpRequestException e = await Assert.ThrowsAsync(() => client.GetAsync($"http://{Guid.NewGuid():N}")); +#if !WINHTTPHANDLER_TEST + Assert.Null(e.StatusCode); + Assert.Equal(HttpRequestError.ProxyTunnelError, e.HttpRequestError); +#endif } } diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectionPool/HttpConnectionPool.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectionPool/HttpConnectionPool.cs index 4770b49203624..3c91fc4ccde43 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectionPool/HttpConnectionPool.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/ConnectionPool/HttpConnectionPool.cs @@ -794,7 +794,7 @@ private async ValueTask EstablishProxyTunnelAsync(bool async, Cancellati if (tunnelResponse.StatusCode != HttpStatusCode.OK) { tunnelResponse.Dispose(); - throw new HttpRequestException(HttpRequestError.ProxyTunnelError, SR.Format(SR.net_http_proxy_tunnel_returned_failure_status_code, _proxyUri, (int)tunnelResponse.StatusCode)); + throw new HttpRequestException(HttpRequestError.ProxyTunnelError, SR.Format(SR.net_http_proxy_tunnel_returned_failure_status_code, _proxyUri, (int)tunnelResponse.StatusCode), statusCode: tunnelResponse.StatusCode); } try