|
| 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 | +namespace Microsoft.Graph.Authentication.Test.Helpers |
| 5 | +{ |
| 6 | + using System; |
| 7 | + using System.Net.Http; |
| 8 | + using Microsoft.Graph.PowerShell.Authentication; |
| 9 | + using Microsoft.Graph.PowerShell.Authentication.Helpers; |
| 10 | + using Xunit; |
| 11 | + |
| 12 | + public class HttpHelpersTests |
| 13 | + { |
| 14 | + [Fact] |
| 15 | + public void GetGraphHttpClientWithDefaultParametersShouldReturnHttpClientWithDefaultTimeout() |
| 16 | + { |
| 17 | + GraphSession.Initialize(() => new GraphSession()); |
| 18 | + GraphSession.Instance.AuthContext = new AuthContext |
| 19 | + { |
| 20 | + AuthType = AuthenticationType.UserProvidedAccessToken, |
| 21 | + ContextScope = ContextScope.Process |
| 22 | + }; |
| 23 | + |
| 24 | + HttpClient httpClient = HttpHelpers.GetGraphHttpClient(); |
| 25 | + |
| 26 | + Assert.Equal(GraphSession.Instance.AuthContext.ClientTimeout, TimeSpan.FromSeconds(Constants.ClientTimeout)); |
| 27 | + Assert.Equal(httpClient.Timeout, GraphSession.Instance.AuthContext.ClientTimeout); |
| 28 | + |
| 29 | + // reset static instance. |
| 30 | + GraphSession.Reset(); |
| 31 | + } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public void GetGraphHttpClientWithClientTimeoutParameterShouldReturnHttpClientWithSpecifiedTimeout() |
| 35 | + { |
| 36 | + GraphSession.Initialize(() => new GraphSession()); |
| 37 | + TimeSpan timeSpan = TimeSpan.FromSeconds(10); |
| 38 | + var authContext = new AuthContext |
| 39 | + { |
| 40 | + AuthType = AuthenticationType.UserProvidedAccessToken, |
| 41 | + ContextScope = ContextScope.Process |
| 42 | + }; |
| 43 | + IAuthenticationProvider authProvider = AuthenticationHelpers.GetAuthProvider(authContext); |
| 44 | + |
| 45 | + HttpClient httpClient = HttpHelpers.GetGraphHttpClient(authProvider, timeSpan); |
| 46 | + |
| 47 | + Assert.Equal(authContext.ClientTimeout, TimeSpan.FromSeconds(Constants.ClientTimeout)); |
| 48 | + Assert.Equal(httpClient.Timeout, timeSpan); |
| 49 | + |
| 50 | + // reset static instance. |
| 51 | + GraphSession.Reset(); |
| 52 | + } |
| 53 | + |
| 54 | + [Fact] |
| 55 | + public void GetGraphHttpClientShouldReturnHttpClientWithCustomerProvidedTimeout() |
| 56 | + { |
| 57 | + GraphSession.Initialize(() => new GraphSession()); |
| 58 | + TimeSpan timeSpan = TimeSpan.FromSeconds(10); |
| 59 | + GraphSession.Instance.AuthContext = new AuthContext |
| 60 | + { |
| 61 | + AuthType = AuthenticationType.UserProvidedAccessToken, |
| 62 | + ContextScope = ContextScope.Process, |
| 63 | + ClientTimeout = timeSpan |
| 64 | + }; |
| 65 | + |
| 66 | + HttpClient httpClient = HttpHelpers.GetGraphHttpClient(); |
| 67 | + |
| 68 | + Assert.Equal(GraphSession.Instance.AuthContext.ClientTimeout, timeSpan); |
| 69 | + Assert.Equal(httpClient.Timeout, timeSpan); |
| 70 | + |
| 71 | + // reset static instance. |
| 72 | + GraphSession.Reset(); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments