diff --git a/sdk/core/Azure.Core/CHANGELOG.md b/sdk/core/Azure.Core/CHANGELOG.md index 5ebcdbfe6bd8b..d0de413130eb6 100644 --- a/sdk/core/Azure.Core/CHANGELOG.md +++ b/sdk/core/Azure.Core/CHANGELOG.md @@ -5,6 +5,9 @@ ### Added - `System.Text.Json.JsonConverter` implementation for the `ETag` +### Fixes +- System proxy settings are correctly applied on .NET Framework + ## 1.6.0 (2020-10-28) ### Added diff --git a/sdk/core/Azure.Core/src/Pipeline/HttpWebRequestTransport.cs b/sdk/core/Azure.Core/src/Pipeline/HttpWebRequestTransport.cs index 92a23144299a4..f086f61fb1d1e 100644 --- a/sdk/core/Azure.Core/src/Pipeline/HttpWebRequestTransport.cs +++ b/sdk/core/Azure.Core/src/Pipeline/HttpWebRequestTransport.cs @@ -20,7 +20,7 @@ namespace Azure.Core.Pipeline internal class HttpWebRequestTransport : HttpPipelineTransport { public static readonly HttpWebRequestTransport Shared = new HttpWebRequestTransport(); - private readonly IWebProxy? _proxy; + private readonly IWebProxy? _environmentProxy; /// /// Creates a new instance of @@ -29,7 +29,7 @@ public HttpWebRequestTransport() { if (HttpEnvironmentProxy.TryCreate(out IWebProxy webProxy)) { - _proxy = webProxy; + _environmentProxy = webProxy; } } @@ -105,7 +105,11 @@ private HttpWebRequest CreateRequest(Request messageRequest) { var request = WebRequest.CreateHttp(messageRequest.Uri.ToUri()); request.Method = messageRequest.Method.Method; - request.Proxy = _proxy; + // Don't disable the default proxy when there is no environment proxy configured + if (_environmentProxy != null) + { + request.Proxy = _environmentProxy; + } foreach (var messageRequestHeader in messageRequest.Headers) { diff --git a/sdk/core/Azure.Core/tests/TransportFunctionalTests.cs b/sdk/core/Azure.Core/tests/TransportFunctionalTests.cs index dc39216c0346f..c9e2d31bf6e50 100644 --- a/sdk/core/Azure.Core/tests/TransportFunctionalTests.cs +++ b/sdk/core/Azure.Core/tests/TransportFunctionalTests.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Net; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -698,6 +699,44 @@ public async Task ProxySettingsAreReadFromEnvironment(string envVar, string url) } } +#if NET461 // GlobalProxySelection.Select not supported on netcoreapp + [NonParallelizable] + [Test] + public async Task DefaultProxySettingsArePreserved() + { +#pragma warning disable 618 // Use of obsolete symbol + var oldGlobalProxySelection = GlobalProxySelection.Select; +#pragma warning restore 618 + try + { + using (TestServer testServer = new TestServer(async context => + { + context.Response.Headers["Via"] = "Test-Proxy"; + byte[] buffer = Encoding.UTF8.GetBytes("Hello"); + await context.Response.Body.WriteAsync(buffer, 0, buffer.Length); + })) + { +#pragma warning disable 618 // Use of obsolete symbol + GlobalProxySelection.Select = new WebProxy(testServer.Address.ToString()); +#pragma warning restore 618 + + var transport = GetTransport(); + Request request = transport.CreateRequest(); + request.Uri.Reset(new Uri("http://microsoft.com")); + Response response = await ExecuteRequest(request, transport); + Assert.True(response.Headers.TryGetValue("Via", out var via)); + Assert.AreEqual("Test-Proxy", via); + } + } + finally + { +#pragma warning disable 618 // Use of obsolete symbol + GlobalProxySelection.Select = oldGlobalProxySelection; +#pragma warning restore 618 + } + } +#endif + [Test] public async Task ResponseHeadersAreSplit() {