microsoft/healthcare-shared-components

Public

mirrored from https://github.com/microsoft/healthcare-shared-componentsAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
main

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

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
6using System;
7using System.Net.Http;
8using System.Threading;
9using System.Threading.Tasks;
10using Microsoft.Health.Client.Authentication;
11using NSubstitute;
12using Xunit;
13
14namespace Microsoft.Health.Client.UnitTests;
15
16public 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