Skip to content

Commit

Permalink
Increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Ndiritu committed Nov 5, 2024
1 parent 2c77cce commit 77f946f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tests/http/httpClient/KiotaClientFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
using System.Linq;
using System.Net;
using System.Net.Http;
using Microsoft.Kiota.Abstractions.Authentication;
using Microsoft.Kiota.Http.HttpClientLibrary.Middleware;
using Microsoft.Kiota.Http.HttpClientLibrary.Middleware.Options;
using Microsoft.Kiota.Http.HttpClientLibrary.Tests.Mocks;
using Moq;
using Xunit;

namespace Microsoft.Kiota.Http.HttpClientLibrary.Tests
Expand Down Expand Up @@ -138,5 +140,12 @@ public void CreateWithCustomMiddlewarePipelineReturnsHttpClient()
var client = KiotaClientFactory.Create(handlers);
Assert.IsType<HttpClient>(client);
}

[Fact]
public void CreateWithAuthenticationProvider()
{
var client = KiotaClientFactory.Create(new BaseBearerTokenAuthenticationProvider(new Mock<IAccessTokenProvider>().Object));
Assert.IsType<HttpClient>(client);
}
}
}
49 changes: 49 additions & 0 deletions tests/http/httpClient/Middleware/AuthorizationHandlerTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.ComponentModel;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
Expand Down Expand Up @@ -54,6 +55,16 @@ public void Dispose()
GC.SuppressFinalize(this);
}

[Fact]
public void AuthorizationHandlerConstructor()
{
// Arrange
BaseBearerTokenAuthenticationProvider? authenticationProvider = null;

// Assert
Assert.Throws<ArgumentNullException>(() => new AuthorizationHandler(authenticationProvider!));
}

[Fact]
public async Task AuthorizationHandlerShouldAddAuthHeaderIfNotPresent()
{
Expand Down Expand Up @@ -91,6 +102,24 @@ public async Task AuthorizationHandlerShouldNotAddAuthHeaderIfPresent()
Assert.Equal($"Bearer existing", response.RequestMessage.Headers.GetValues("Authorization").First());
}

[Fact]
public async Task AuthorizationHandlerShouldNotAddAuthHeaderIfHostIsNotValid()
{
// Arrange
HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "https://example.com");

HttpResponseMessage httpResponse = new HttpResponseMessage(HttpStatusCode.OK);

this._testHttpMessageHandler.SetHttpResponse(httpResponse);// set the mock response

// Act
HttpResponseMessage response = await this._invoker.SendAsync(httpRequestMessage, new CancellationToken());

// Assert
Assert.NotNull(response.RequestMessage);
Assert.False(response.RequestMessage.Headers.Contains("Authorization"));
}

[Fact]
public async Task AuthorizationHandlerShouldAttemptCAEClaimsChallenge()
{
Expand All @@ -113,5 +142,25 @@ public async Task AuthorizationHandlerShouldAttemptCAEClaimsChallenge()
Assert.Equal($"Bearer {_expectedAccessTokenAfterCAE}", response.RequestMessage.Headers.GetValues("Authorization").First());
Assert.Equal("test", await response.RequestMessage.Content!.ReadAsStringAsync());
}

[Fact]
public async Task AuthorizationHandlerShouldReturnInitialResponseIfClaimsHeaderIsEmpty()
{
// Arrange
HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "https://graph.microsoft.com");
httpRequestMessage.Content = new ByteArrayContent(Encoding.UTF8.GetBytes("test"));

HttpResponseMessage httpResponse = new HttpResponseMessage(HttpStatusCode.Unauthorized);
httpResponse.Headers.WwwAuthenticate.Add(new AuthenticationHeaderValue("Bearer", "authorization_uri=\"https://login.windows.net/common/oauth2/authorize\""));

this._testHttpMessageHandler.SetHttpResponse(httpResponse, new HttpResponseMessage(HttpStatusCode.OK));// set the mock response

// Act
HttpResponseMessage response = await this._invoker.SendAsync(httpRequestMessage, new CancellationToken());

// Assert
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
Assert.Equal("test", await response.RequestMessage!.Content!.ReadAsStringAsync());
}
}
}

0 comments on commit 77f946f

Please sign in to comment.