Skip to content

Commit 931037c

Browse files
committed
Add tests
1 parent 6e244b8 commit 931037c

File tree

3 files changed

+68
-9
lines changed

3 files changed

+68
-9
lines changed

src/Microsoft.Graph.Core/Requests/GraphClientFactory.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,7 @@ public static HttpClient Create(
159159
HttpMessageHandler finalHandler = null,
160160
bool disposeHandler = true)
161161
{
162-
if (handlers == null)
163-
{
164-
handlers = CreateDefaultHandlers();
165-
}
166-
var handlerList = handlers.ToList();
167-
handlerList.Add(new AuthorizationHandler(
168-
new AzureIdentityAuthenticationProvider(tokenCredential, null, null, true)
169-
));
170-
return Create(handlerList, version, nationalCloud, proxy, finalHandler, disposeHandler);
162+
return Create(new AzureIdentityAuthenticationProvider(tokenCredential, null, null, true), handlers, version, nationalCloud, proxy, finalHandler, disposeHandler);
171163
}
172164

173165
/// <summary>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// ------------------------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
3+
// ------------------------------------------------------------------------------
4+
5+
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Threading;
9+
using System.Threading.Tasks;
10+
using Microsoft.Kiota.Abstractions.Authentication;
11+
using Moq;
12+
13+
namespace Microsoft.Graph.DotnetCore.Core.Test.Mocks
14+
{
15+
public class MockAccessTokenProvider : Mock<IAccessTokenProvider>
16+
{
17+
public MockAccessTokenProvider(string accessToken = null) : base(MockBehavior.Strict)
18+
{
19+
this.Setup(x => x.GetAuthorizationTokenAsync(
20+
It.IsAny<Uri>(),
21+
It.IsAny<Dictionary<string, object>>(),
22+
It.IsAny<CancellationToken>()
23+
)).Returns(Task.FromResult(accessToken));
24+
25+
this.Setup(x => x.AllowedHostsValidator).Returns(
26+
new AllowedHostsValidator(new List<string> { "graph.microsoft.com" })
27+
);
28+
}
29+
}
30+
}

tests/Microsoft.Graph.DotnetCore.Core.Test/Requests/GraphClientFactoryTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ namespace Microsoft.Graph.DotnetCore.Core.Test.Requests
1212
using System.Net.Http.Headers;
1313
using System.Threading;
1414
using System.Threading.Tasks;
15+
using Azure.Core;
16+
using Microsoft.Kiota.Abstractions.Authentication;
1517
using Microsoft.Kiota.Http.HttpClientLibrary.Middleware;
1618
using Microsoft.Kiota.Http.HttpClientLibrary.Middleware.Options;
1719
using Mocks;
20+
using Moq;
1821
using Xunit;
1922

2023
public class GraphClientFactoryTests : IDisposable
@@ -314,6 +317,40 @@ public void CreateClientWithFinalHandlerDisposesTheFinalHandler(bool shouldDispo
314317
Assert.Equal(shouldDisposeHandler, finalHandler.Disposed);
315318
}
316319

320+
[Fact]
321+
public async Task CreateClientWithAuthenticationProviderAuthenticatesRequest()
322+
{
323+
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "https://graph.microsoft.com/me");
324+
var responseMessage = new HttpResponseMessage(HttpStatusCode.OK);
325+
this.testHttpMessageHandler.SetHttpResponse(responseMessage);
326+
327+
var authProvider = new Mock<BaseBearerTokenAuthenticationProvider>(new MockAccessTokenProvider("token").Object);
328+
329+
using (HttpClient client = GraphClientFactory.Create(authenticationProvider: authProvider.Object, finalHandler: this.testHttpMessageHandler))
330+
{
331+
var response = await client.SendAsync(httpRequestMessage, new CancellationToken());
332+
Assert.Equal("Bearer token", response.RequestMessage.Headers.Authorization.ToString());
333+
}
334+
}
335+
336+
[Fact]
337+
public async Task CreateClientWithTokenCredentialAuthenticatesRequest()
338+
{
339+
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "https://graph.microsoft.com/me");
340+
var responseMessage = new HttpResponseMessage(HttpStatusCode.OK);
341+
this.testHttpMessageHandler.SetHttpResponse(responseMessage);
342+
343+
var tokenCredential = new Mock<TokenCredential>();
344+
tokenCredential.Setup(x => x.GetTokenAsync(It.IsAny<TokenRequestContext>(), It.IsAny<CancellationToken>()))
345+
.ReturnsAsync(new AccessToken("mockToken", DateTimeOffset.UtcNow.AddMinutes(10)));
346+
347+
using (HttpClient client = GraphClientFactory.Create(tokenCredential: tokenCredential.Object, finalHandler: this.testHttpMessageHandler))
348+
{
349+
var response = await client.SendAsync(httpRequestMessage, new CancellationToken());
350+
Assert.Equal("Bearer mockToken", response.RequestMessage.Headers.Authorization.ToString());
351+
}
352+
}
353+
317354
private class MockHttpHandler : HttpMessageHandler
318355
{
319356
public bool Disposed;

0 commit comments

Comments
 (0)