Skip to content

Commit

Permalink
Fix SonarCloud issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Ndiritu committed Nov 5, 2024
1 parent 77f946f commit 43a3f26
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/http/httpClient/ContinuousAccessEvaluation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static internal class ContinuousAccessEvaluation
/// <returns></returns>
public static string GetClaims(HttpResponseMessage response)

Check warning on line 25 in src/http/httpClient/ContinuousAccessEvaluation.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this method to reduce its Cognitive Complexity from 18 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
{
if(response == null) throw new ArgumentNullException(nameof(response));
ArgumentNullException.ThrowIfNull(response);

Check failure on line 27 in src/http/httpClient/ContinuousAccessEvaluation.cs

View workflow job for this annotation

GitHub Actions / build-and-test

'ArgumentNullException' does not contain a definition for 'ThrowIfNull'

Check failure on line 27 in src/http/httpClient/ContinuousAccessEvaluation.cs

View workflow job for this annotation

GitHub Actions / Build

'ArgumentNullException' does not contain a definition for 'ThrowIfNull'
if(response.StatusCode != HttpStatusCode.Unauthorized
|| response.Headers.WwwAuthenticate.Count == 0)
{
Expand Down
1 change: 0 additions & 1 deletion src/http/httpClient/HttpClientRequestAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,6 @@ private async Task ThrowIfFailedResponseAsync(HttpResponseMessage response, Dict
}
private const string ClaimsKey = "claims";
private const string BearerAuthenticationScheme = "Bearer";

Check warning on line 496 in src/http/httpClient/HttpClientRequestAdapter.cs

View workflow job for this annotation

GitHub Actions / Build

Remove the unused private field 'BearerAuthenticationScheme'. (https://rules.sonarsource.com/csharp/RSPEC-1144)

Check warning on line 496 in src/http/httpClient/HttpClientRequestAdapter.cs

View workflow job for this annotation

GitHub Actions / Build

Remove the unused private field 'BearerAuthenticationScheme'. (https://rules.sonarsource.com/csharp/RSPEC-1144)
private static Func<AuthenticationHeaderValue, bool> filterAuthHeader = static x => x.Scheme.Equals(BearerAuthenticationScheme, StringComparison.OrdinalIgnoreCase);
private async Task<HttpResponseMessage> GetHttpResponseMessageAsync(RequestInformation requestInfo, CancellationToken cancellationToken, Activity? activityForAttributes, string? claims = default, bool isStreamResponse = false)
{
using var span = activitySource?.StartActivity(nameof(GetHttpResponseMessageAsync));
Expand Down
6 changes: 3 additions & 3 deletions src/http/httpClient/Middleware/AuthorizationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public AuthorizationHandler(BaseBearerTokenAuthenticationProvider authentication
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
if(request == null) throw new ArgumentNullException(nameof(request));
ArgumentNullException.ThrowIfNull(request);

Check failure on line 48 in src/http/httpClient/Middleware/AuthorizationHandler.cs

View workflow job for this annotation

GitHub Actions / build-and-test

'ArgumentNullException' does not contain a definition for 'ThrowIfNull'

Check failure on line 48 in src/http/httpClient/Middleware/AuthorizationHandler.cs

View workflow job for this annotation

GitHub Actions / Build

'ArgumentNullException' does not contain a definition for 'ThrowIfNull'

Activity? activity = null;
if(request.GetRequestOption<ObservabilityOptions>() is { } obsOptions)
Expand Down Expand Up @@ -85,8 +85,8 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage

private async Task AuthenticateRequestAsync(HttpRequestMessage request,
Dictionary<string, object> additionalAuthenticationContext,
CancellationToken cancellationToken,
Activity? activityForAttributes)
Activity? activityForAttributes,
CancellationToken cancellationToken)
{
var accessTokenProvider = authenticationProvider.AccessTokenProvider;
if(request.RequestUri == null || !accessTokenProvider.AllowedHostsValidator.IsUrlHostValid(
Expand Down
6 changes: 3 additions & 3 deletions tests/http/httpClient/Middleware/AuthorizationHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand All @@ -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());
}

Expand Down Expand Up @@ -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());
}
Expand Down

0 comments on commit 43a3f26

Please sign in to comment.