From 04d265349baf72706db07f6ef2378f826be50583 Mon Sep 17 00:00:00 2001 From: Guillaume Gnaegi <58469901+ggnaegi@users.noreply.github.com> Date: Wed, 3 Apr 2024 11:25:02 +0200 Subject: [PATCH] Adding some unit tests. Modifying SocketsHttpHandler constructor, accepting UseDefaultCredentials --- .../Configuration/HttpHandlerOptions.cs | 2 +- src/Ocelot/Requester/MessageInvokerPool.cs | 1 + .../HttpHandlerOptionsCreatorTests.cs | 38 ++++++++++++++++++- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/Ocelot/Configuration/HttpHandlerOptions.cs b/src/Ocelot/Configuration/HttpHandlerOptions.cs index 4b6cfabea..16a7b0bc3 100644 --- a/src/Ocelot/Configuration/HttpHandlerOptions.cs +++ b/src/Ocelot/Configuration/HttpHandlerOptions.cs @@ -65,6 +65,6 @@ public class HttpHandlerOptions /// if the default credentials are used; otherwise . The default value is . /// The property value is assignable to the one. /// - public bool UseDefaultCredentials { get; private set; } + public bool UseDefaultCredentials { get; } } } diff --git a/src/Ocelot/Requester/MessageInvokerPool.cs b/src/Ocelot/Requester/MessageInvokerPool.cs index 130b4bccb..7303657a4 100644 --- a/src/Ocelot/Requester/MessageInvokerPool.cs +++ b/src/Ocelot/Requester/MessageInvokerPool.cs @@ -77,6 +77,7 @@ private HttpMessageHandler CreateHandler(DownstreamRoute downstreamRoute) UseProxy = downstreamRoute.HttpHandlerOptions.UseProxy, MaxConnectionsPerServer = downstreamRoute.HttpHandlerOptions.MaxConnectionsPerServer, PooledConnectionLifetime = downstreamRoute.HttpHandlerOptions.PooledConnectionLifeTime, + Credentials = downstreamRoute.HttpHandlerOptions.UseDefaultCredentials ? CredentialCache.DefaultCredentials : null, }; if (downstreamRoute.HttpHandlerOptions.UseCookieContainer) diff --git a/test/Ocelot.UnitTests/Configuration/HttpHandlerOptionsCreatorTests.cs b/test/Ocelot.UnitTests/Configuration/HttpHandlerOptionsCreatorTests.cs index e76235327..eb64eed15 100644 --- a/test/Ocelot.UnitTests/Configuration/HttpHandlerOptionsCreatorTests.cs +++ b/test/Ocelot.UnitTests/Configuration/HttpHandlerOptionsCreatorTests.cs @@ -185,6 +185,41 @@ public void should_create_options_fixing_specified_MaxConnectionsPerServer_range .Then(x => ThenTheFollowingOptionsReturned(expectedOptions)) .BDDfy(); } + + [Fact] + public void Should_create_options_with_useDefaultCredentials_false_as_default() + { + var fileRoute = new FileRoute + { + HttpHandlerOptions = new FileHttpHandlerOptions(), + }; + + var expectedOptions = new HttpHandlerOptions(false, false, false, true, int.MaxValue, DefaultPooledConnectionLifeTime, false); + + this.Given(x => GivenTheFollowing(fileRoute)) + .When(x => WhenICreateHttpHandlerOptions()) + .Then(x => ThenTheFollowingOptionsReturned(expectedOptions)) + .BDDfy(); + } + + [Fact] + public void Should_create_options_with_useDefaultCredentials_true_if_set() + { + var fileRoute = new FileRoute + { + HttpHandlerOptions = new FileHttpHandlerOptions + { + UseDefaultCredentials = true, + }, + }; + + var expectedOptions = new HttpHandlerOptions(false, false, false, true, int.MaxValue, DefaultPooledConnectionLifeTime, true); + + this.Given(x => GivenTheFollowing(fileRoute)) + .When(x => WhenICreateHttpHandlerOptions()) + .Then(x => ThenTheFollowingOptionsReturned(expectedOptions)) + .BDDfy(); + } private void GivenTheFollowing(FileRoute fileRoute) { @@ -203,7 +238,8 @@ private void ThenTheFollowingOptionsReturned(HttpHandlerOptions expected) _httpHandlerOptions.UseCookieContainer.ShouldBe(expected.UseCookieContainer); _httpHandlerOptions.UseTracing.ShouldBe(expected.UseTracing); _httpHandlerOptions.UseProxy.ShouldBe(expected.UseProxy); - _httpHandlerOptions.MaxConnectionsPerServer.ShouldBe(expected.MaxConnectionsPerServer); + _httpHandlerOptions.MaxConnectionsPerServer.ShouldBe(expected.MaxConnectionsPerServer); + _httpHandlerOptions.UseDefaultCredentials.ShouldBe(expected.UseDefaultCredentials); } private void GivenARealTracer()