Mock HTTP client factory which gets a specific message handler (for mocking) and use for creating the HTTP client.
Inheritance: HttpClientFactory
 /// <summary>Subtest for receiving an error token response.</summary>
 /// <param name="handler">The message handler.</param>
 private void SubtestFetchTokenAsync_Error(FetchTokenMessageHandler handler)
 {
     MockHttpClientFactory mockFactory = new MockHttpClientFactory(handler);
     var flow = CreateFlow(httpClientFactory: mockFactory);
     try
     {
         var request =
             (TokenRequest)handler.AuthorizationCodeTokenRequest ?? (TokenRequest)handler.RefreshTokenRequest;
         var result = flow.FetchTokenAsync("user", request, CancellationToken.None).Result;
         Assert.Fail();
     }
     catch (AggregateException aex)
     {
         var ex = aex.InnerException as TokenResponseException;
         Assert.IsNotNull(ex);
         var result = ex.Error;
         Assert.That(result.Error, Is.EqualTo("error"));
         Assert.That(result.ErrorDescription, Is.EqualTo("desc"));
         Assert.That(result.ErrorUri, Is.EqualTo("uri"));
     }
 }
        public void TestFetchTokenAsync_RefreshTokenRequest()
        {
            var handler = new FetchTokenMessageHandler();
            handler.RefreshTokenRequest = new RefreshTokenRequest()
                {
                    RefreshToken = "REFRESH",
                    Scope = "a"
                };

            MockHttpClientFactory mockFactory = new MockHttpClientFactory(handler);

            var flow = CreateFlow(httpClientFactory: mockFactory);
            var response = flow.FetchTokenAsync("user", handler.RefreshTokenRequest, CancellationToken.None).Result;
            SubtestTokenResponse(response);
        }
        public void TestRefreshTokenAsync()
        {
            var mock = new Mock<IDataStore>();
            var handler = new FetchTokenMessageHandler();
            handler.RefreshTokenRequest = new RefreshTokenRequest()
            {
                RefreshToken = "REFRESH",
                Scope = "a"
            };
            MockHttpClientFactory mockFactory = new MockHttpClientFactory(handler);

            TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
            tcs.SetResult(null);
            mock.Setup(ds => ds.StoreAsync("uSer", It.IsAny<TokenResponse>())).Returns(tcs.Task);

            var flow = CreateFlow(httpClientFactory: mockFactory, scopes: new[] { "a" }, dataStore: mock.Object);
            var response = flow.RefreshTokenAsync("uSer", "REFRESH", CancellationToken.None).Result;
            SubtestTokenResponse(response);


            mock.Verify(ds => ds.StoreAsync("uSer", It.IsAny<TokenResponse>()));
        }
        public void TestFetchTokenAsync_AuthorizationCodeRequest()
        {
            var handler = new FetchTokenMessageHandler();
            handler.AuthorizationCodeTokenRequest = new AuthorizationCodeTokenRequest()
            {
                Code = "c0de",
                RedirectUri = "redIrect",
                Scope = "a"
            };
            MockHttpClientFactory mockFactory = new MockHttpClientFactory(handler);

            var flow = CreateFlow(httpClientFactory: mockFactory);
            var response = flow.FetchTokenAsync("user", handler.AuthorizationCodeTokenRequest,
                CancellationToken.None).Result;
            SubtestTokenResponse(response);
        }
        public void TestExchangeCodeForTokenAsync()
        {
            var mock = new Mock<IDataStore>();
            var handler = new FetchTokenMessageHandler();
            handler.AuthorizationCodeTokenRequest = new AuthorizationCodeTokenRequest()
            {
                Code = "c0de",
                RedirectUri = "redIrect",
                Scope = "a"
            };
            MockHttpClientFactory mockFactory = new MockHttpClientFactory(handler);

            TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
            tcs.SetResult(null);
            mock.Setup(ds => ds.StoreAsync("uSer", It.IsAny<TokenResponse>())).Returns(tcs.Task);

            var flow = CreateFlow(httpClientFactory: mockFactory, scopes: new[] { "a" }, dataStore: mock.Object);
            var response = flow.ExchangeCodeForTokenAsync("uSer", "c0de", "redIrect", CancellationToken.None).Result;
            SubtestTokenResponse(response);

            mock.Verify(ds => ds.StoreAsync("uSer", It.IsAny<TokenResponse>()));
        }