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 875dcc6
Show file tree
Hide file tree
Showing 2 changed files with 38 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);
}
}
}
29 changes: 29 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 Down

0 comments on commit 875dcc6

Please sign in to comment.