From d54a0efd26ed0b5924f97057108f658756a7f0c5 Mon Sep 17 00:00:00 2001 From: Philip Gichuhi Date: Tue, 5 Nov 2024 09:11:28 +0200 Subject: [PATCH] Increase test coverage --- .../http/httpClient/KiotaClientFactoryTests.cs | 9 +++++++++ .../Middleware/AuthorizationHandlerTests.cs | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/tests/http/httpClient/KiotaClientFactoryTests.cs b/tests/http/httpClient/KiotaClientFactoryTests.cs index 675acf0..9b28cbc 100644 --- a/tests/http/httpClient/KiotaClientFactoryTests.cs +++ b/tests/http/httpClient/KiotaClientFactoryTests.cs @@ -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 @@ -138,5 +140,12 @@ public void CreateWithCustomMiddlewarePipelineReturnsHttpClient() var client = KiotaClientFactory.Create(handlers); Assert.IsType(client); } + + [Fact] + public void CreateWithAuthenticationProvider() + { + var client = KiotaClientFactory.Create(new BaseBearerTokenAuthenticationProvider(new Mock().Object)); + Assert.IsType(client); + } } } diff --git a/tests/http/httpClient/Middleware/AuthorizationHandlerTests.cs b/tests/http/httpClient/Middleware/AuthorizationHandlerTests.cs index 1082e9a..b6bec59 100644 --- a/tests/http/httpClient/Middleware/AuthorizationHandlerTests.cs +++ b/tests/http/httpClient/Middleware/AuthorizationHandlerTests.cs @@ -91,6 +91,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() {