public async Task RetrieveToken_Success() { // ARRANGE var appCache = (IAppCache)_context.Properties["appCache"]; var bearerToken = new BearerTokenResponse { access_token = "token" }; var json = System.Text.Json.JsonSerializer.Serialize(bearerToken); var clientHandlerStub = new DelegatingHandlerStub((request, cancellationToken) => { var response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(json, Encoding.UTF8) }; return(Task.FromResult(response)); } ); var client = new HttpClient(clientHandlerStub); var httpClientFactoryMock = new Mock <IHttpClientFactory>(); httpClientFactoryMock.Setup(mock => mock.CreateClient(It.IsAny <string>())) .Returns(client) .Verifiable(); var configurationMock = new Mock <IConfiguration>(); configurationMock.Setup(mock => mock[It.IsAny <string>()]) .Returns("http://test.url") .Verifiable(); var keyVaultMock = new Mock <IAzureKeyVaultService>(); var unitUnderTest = new BearerTokenProvider(appCache, httpClientFactoryMock.Object, configurationMock.Object, keyVaultMock.Object); // ACT var result = await unitUnderTest.RetrieveToken(); // ASSERT configurationMock.Verify(); httpClientFactoryMock.Verify(); Assert.IsNotNull(result); Assert.AreEqual(bearerToken.access_token, result); }
public void TestInitialize() { mockLogger = new Mock <ILogger>(); mockAdalTokenProvider = new Mock <IAdalTokenProvider>(); mockAdalTokenProviderFactory = new Mock <IAdalTokenProviderFactory>(); mockAdalTokenProviderFactory .Setup(x => x.Get(It.IsAny <string>())) .Returns(mockAdalTokenProvider.Object); mockAuthUtil = new Mock <IAuthUtil>(); mockAuthUtil .Setup(x => x.GetAadAuthorityUriAsync(It.IsAny <Uri>(), It.IsAny <CancellationToken>())) .Returns(Task.FromResult(testAuthority)); bearerTokenProvider = new BearerTokenProvider(mockLogger.Object, mockAdalTokenProviderFactory.Object, mockAuthUtil.Object); }
public AuthenticatedMessageHandler(BearerTokenProvider getBearerToken) : this(new HttpClientHandler(), getBearerToken) { }
public AuthenticatedMessageHandler(HttpMessageHandler innerHandler, BearerTokenProvider getBearerToken) { InnerHandler = innerHandler; mGetBearerToken = getBearerToken; }