Skip to content

Commit

Permalink
Store status code in exception for ProxyTunnelError (#105610)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Jul 31, 2024
1 parent 8aeff36 commit aa06639
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,11 @@ public async Task AuthenticatedProxyTunnelRequest_PostAsyncWithNoCreds_Throws()
using (HttpClient client = CreateHttpClient(handler))
{
HttpRequestException e = await Assert.ThrowsAnyAsync<HttpRequestException>(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
}
}
}
Expand All @@ -313,7 +317,11 @@ public async Task Proxy_SslProxyUnsupported_Throws()
{
handler.Proxy = new WebProxy($"https://{Guid.NewGuid():N}");

await Assert.ThrowsAsync<HttpRequestException>(() => client.GetAsync($"http://{Guid.NewGuid():N}"));
HttpRequestException e = await Assert.ThrowsAsync<HttpRequestException>(() => client.GetAsync($"http://{Guid.NewGuid():N}"));
#if !WINHTTPHANDLER_TEST
Assert.Null(e.StatusCode);
Assert.Equal(HttpRequestError.ProxyTunnelError, e.HttpRequestError);
#endif
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ private async ValueTask<Stream> 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
Expand Down

0 comments on commit aa06639

Please sign in to comment.