microsoft/healthcare-shared-components
Publicmirrored from https://github.com/microsoft/healthcare-shared-componentsAvailable
src/Microsoft.Health.Client.UnitTests/AuthenticationHttpMessageHandlerTests.cs
47lines · modecode
| 1 | // ------------------------------------------------------------------------------------------------- |
| 2 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | // Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. |
| 4 | // ------------------------------------------------------------------------------------------------- |
| 5 | |
| 6 | using System; |
| 7 | using System.Net.Http; |
| 8 | using System.Threading; |
| 9 | using System.Threading.Tasks; |
| 10 | using Microsoft.Health.Client.Authentication; |
| 11 | using NSubstitute; |
| 12 | using Xunit; |
| 13 | |
| 14 | namespace Microsoft.Health.Client.UnitTests; |
| 15 | |
| 16 | public sealed class AuthenticationHttpMessageHandlerTests : IDisposable |
| 17 | { |
| 18 | private readonly AuthenticationHttpMessageHandler _authenticationHttpMessageHandler; |
| 19 | |
| 20 | public AuthenticationHttpMessageHandlerTests() |
| 21 | { |
| 22 | var credentialProvider = Substitute.For<ICredentialProvider>(); |
| 23 | credentialProvider.GetBearerTokenAsync(Arg.Any<CancellationToken>()).Returns("token"); |
| 24 | _authenticationHttpMessageHandler = new AuthenticationHttpMessageHandler(credentialProvider) |
| 25 | { |
| 26 | InnerHandler = new TestInnerHandler(), |
| 27 | }; |
| 28 | } |
| 29 | |
| 30 | [Fact] |
| 31 | public async Task GivenARequest_WhenSendAsyncCalled_AuthorizationHeaderIsSet() |
| 32 | { |
| 33 | using var invoker = new HttpMessageInvoker(_authenticationHttpMessageHandler); |
| 34 | using var message = new HttpRequestMessage(); |
| 35 | |
| 36 | var result = await invoker.SendAsync(message, CancellationToken.None); |
| 37 | |
| 38 | Assert.Equal("Bearer", result.RequestMessage.Headers.Authorization.Scheme); |
| 39 | Assert.Equal("token", result.RequestMessage.Headers.Authorization.Parameter); |
| 40 | } |
| 41 | |
| 42 | public void Dispose() |
| 43 | { |
| 44 | _authenticationHttpMessageHandler.Dispose(); |
| 45 | GC.SuppressFinalize(this); |
| 46 | } |
| 47 | } |
| 48 | |