From d45b7d4ded3baedb8be4b3b95702feca22d5744c Mon Sep 17 00:00:00 2001 From: Philip Gichuhi Date: Tue, 5 Nov 2024 12:38:17 +0200 Subject: [PATCH] Fix SonarCloud issues --- src/http/httpClient/HttpClientRequestAdapter.cs | 1 - src/http/httpClient/Middleware/AuthorizationHandler.cs | 9 ++++----- .../httpClient/Middleware/AuthorizationHandlerTests.cs | 6 +++--- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/http/httpClient/HttpClientRequestAdapter.cs b/src/http/httpClient/HttpClientRequestAdapter.cs index 8e05650..3e6f5de 100644 --- a/src/http/httpClient/HttpClientRequestAdapter.cs +++ b/src/http/httpClient/HttpClientRequestAdapter.cs @@ -494,7 +494,6 @@ private async Task ThrowIfFailedResponseAsync(HttpResponseMessage response, Dict } private const string ClaimsKey = "claims"; private const string BearerAuthenticationScheme = "Bearer"; - private static Func filterAuthHeader = static x => x.Scheme.Equals(BearerAuthenticationScheme, StringComparison.OrdinalIgnoreCase); private async Task GetHttpResponseMessageAsync(RequestInformation requestInfo, CancellationToken cancellationToken, Activity? activityForAttributes, string? claims = default, bool isStreamResponse = false) { using var span = activitySource?.StartActivity(nameof(GetHttpResponseMessageAsync)); diff --git a/src/http/httpClient/Middleware/AuthorizationHandler.cs b/src/http/httpClient/Middleware/AuthorizationHandler.cs index 27c9a96..45950b4 100644 --- a/src/http/httpClient/Middleware/AuthorizationHandler.cs +++ b/src/http/httpClient/Middleware/AuthorizationHandler.cs @@ -46,7 +46,6 @@ protected override async Task SendAsync(HttpRequestMessage CancellationToken cancellationToken) { if(request == null) throw new ArgumentNullException(nameof(request)); - Activity? activity = null; if(request.GetRequestOption() is { } obsOptions) { @@ -62,7 +61,7 @@ protected override async Task SendAsync(HttpRequestMessage return await base.SendAsync(request, cancellationToken).ConfigureAwait(false); } Dictionary additionalAuthenticationContext = new Dictionary(); - await AuthenticateRequestAsync(request, additionalAuthenticationContext, cancellationToken, activity).ConfigureAwait(false); + await AuthenticateRequestAsync(request, additionalAuthenticationContext, activity, cancellationToken).ConfigureAwait(false); var response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false); if(response.StatusCode != HttpStatusCode.Unauthorized || response.RequestMessage == null || !response.RequestMessage.IsBuffered()) return response; @@ -73,7 +72,7 @@ protected override async Task SendAsync(HttpRequestMessage activity?.AddEvent(new ActivityEvent("com.microsoft.kiota.handler.authorization.challenge_received")); additionalAuthenticationContext[ContinuousAccessEvaluation.ClaimsKey] = claims; var retryRequest = await response.RequestMessage.CloneAsync(cancellationToken); - await AuthenticateRequestAsync(retryRequest, additionalAuthenticationContext, cancellationToken, activity).ConfigureAwait(false); + await AuthenticateRequestAsync(retryRequest, additionalAuthenticationContext, activity, cancellationToken).ConfigureAwait(false); activity?.SetTag("http.request.resend_count", 1); return await base.SendAsync(retryRequest, cancellationToken).ConfigureAwait(false); } @@ -85,8 +84,8 @@ protected override async Task SendAsync(HttpRequestMessage private async Task AuthenticateRequestAsync(HttpRequestMessage request, Dictionary additionalAuthenticationContext, - CancellationToken cancellationToken, - Activity? activityForAttributes) + Activity? activityForAttributes, + CancellationToken cancellationToken) { var accessTokenProvider = authenticationProvider.AccessTokenProvider; if(request.RequestUri == null || !accessTokenProvider.AllowedHostsValidator.IsUrlHostValid( diff --git a/tests/http/httpClient/Middleware/AuthorizationHandlerTests.cs b/tests/http/httpClient/Middleware/AuthorizationHandlerTests.cs index f8947f2..e4c5e8c 100644 --- a/tests/http/httpClient/Middleware/AuthorizationHandlerTests.cs +++ b/tests/http/httpClient/Middleware/AuthorizationHandlerTests.cs @@ -78,7 +78,7 @@ public async Task AuthorizationHandlerShouldAddAuthHeaderIfNotPresent() // Assert Assert.NotNull(response.RequestMessage); Assert.True(response.RequestMessage.Headers.Contains("Authorization")); - Assert.True(response.RequestMessage.Headers.GetValues("Authorization").Count() == 1); + Assert.Single(response.RequestMessage.Headers.GetValues("Authorization")); Assert.Equal($"Bearer {_expectedAccessToken}", response.RequestMessage.Headers.GetValues("Authorization").First()); } @@ -98,7 +98,7 @@ public async Task AuthorizationHandlerShouldNotAddAuthHeaderIfPresent() // Assert Assert.NotNull(response.RequestMessage); Assert.True(response.RequestMessage.Headers.Contains("Authorization")); - Assert.True(response.RequestMessage.Headers.GetValues("Authorization").Count() == 1); + Assert.Single(response.RequestMessage.Headers.GetValues("Authorization")); Assert.Equal($"Bearer existing", response.RequestMessage.Headers.GetValues("Authorization").First()); } @@ -138,7 +138,7 @@ public async Task AuthorizationHandlerShouldAttemptCAEClaimsChallenge() // Assert Assert.NotNull(response.RequestMessage); Assert.True(response.RequestMessage.Headers.Contains("Authorization")); - Assert.True(response.RequestMessage.Headers.GetValues("Authorization").Count() == 1); + Assert.Single(response.RequestMessage.Headers.GetValues("Authorization")); Assert.Equal($"Bearer {_expectedAccessTokenAfterCAE}", response.RequestMessage.Headers.GetValues("Authorization").First()); Assert.Equal("test", await response.RequestMessage.Content!.ReadAsStringAsync()); }