Esempio n. 1
0
        public async Task HostProviderRegistry_GetProvider_Auto_NetworkProbe_TimeoutNegative_NoNetworkCall()
        {
            var context   = new TestCommandContext();
            var registry  = new HostProviderRegistry(context);
            var remoteUri = new Uri("https://onprem.example.com");
            var input     = new InputArguments(
                new Dictionary <string, string>
            {
                ["protocol"] = remoteUri.Scheme,
                ["host"]     = remoteUri.Host
            }
                );

            var providerMock = new Mock <IHostProvider>();

            providerMock.Setup(x => x.IsSupported(It.IsAny <InputArguments>())).Returns(false);
            providerMock.Setup(x => x.IsSupported(It.IsAny <HttpResponseMessage>())).Returns(true);

            var responseMessage = new HttpResponseMessage(HttpStatusCode.Unauthorized);
            var httpHandler     = new TestHttpMessageHandler();

            httpHandler.Setup(HttpMethod.Head, remoteUri, responseMessage);
            context.HttpClientFactory.MessageHandler = httpHandler;

            registry.Register(providerMock.Object, HostProviderPriority.Normal);

            context.Settings.AutoDetectProviderTimeout = -1;

            await Assert.ThrowsAnyAsync <Exception>(() => registry.GetProviderAsync(input));

            httpHandler.AssertRequest(HttpMethod.Head, remoteUri, 0);
        }
        private static async Task TestGetIsSupportedAsync(string[] wwwAuthHeaders, bool expected)
        {
            var context = new TestCommandContext();
            var uri     = new Uri("https://example.com");

            var wiaResponse = new HttpResponseMessage(HttpStatusCode.Unauthorized);

            foreach (string headerValue in wwwAuthHeaders)
            {
                wiaResponse.Headers.WwwAuthenticate.ParseAdd(headerValue);
            }

            var httpHandler = new TestHttpMessageHandler {
                ThrowOnUnexpectedRequest = true
            };

            httpHandler.Setup(HttpMethod.Head, uri, wiaResponse);

            context.HttpClientFactory.MessageHandler = httpHandler;
            var wiaAuth = new WindowsIntegratedAuthentication(context);

            bool actual = await wiaAuth.GetIsSupportedAsync(uri);

            Assert.Equal(expected, actual);
            httpHandler.AssertRequest(HttpMethod.Head, uri, expectedNumberOfCalls: 1);
        }
Esempio n. 3
0
        public async Task HostProviderRegistry_GetProvider_Auto_NetworkProbe_ReturnsSupportedProvider()
        {
            var            context   = new TestCommandContext();
            var            registry  = new HostProviderRegistry(context);
            var            remoteUri = new Uri("https://provider2.onprem.example.com");
            InputArguments input     = CreateInputArguments(remoteUri);

            var provider1Mock = new Mock <IHostProvider>();

            provider1Mock.Setup(x => x.IsSupported(It.IsAny <InputArguments>())).Returns(false);
            provider1Mock.Setup(x => x.IsSupported(It.IsAny <HttpResponseMessage>())).Returns(false);

            var provider2Mock = new Mock <IHostProvider>();

            provider2Mock.Setup(x => x.IsSupported(It.IsAny <InputArguments>())).Returns(false);
            provider2Mock.Setup(x => x.IsSupported(It.IsAny <HttpResponseMessage>())).Returns(true);

            var responseMessage = new HttpResponseMessage(HttpStatusCode.Unauthorized)
            {
                Headers = { { "X-Provider2", "true" } }
            };

            var httpHandler = new TestHttpMessageHandler();

            httpHandler.Setup(HttpMethod.Head, remoteUri, responseMessage);
            context.HttpClientFactory.MessageHandler = httpHandler;

            registry.Register(provider1Mock.Object, HostProviderPriority.Normal);
            registry.Register(provider2Mock.Object, HostProviderPriority.Normal);

            IHostProvider result = await registry.GetProviderAsync(input);

            httpHandler.AssertRequest(HttpMethod.Head, remoteUri, 1);
            Assert.Same(provider2Mock.Object, result);
        }
        public async Task BitbucketRestApi_GetUserInformationAsync_ReturnsUserInfo_ForSuccessfulRequest(string username, string password, bool isBearerToken)
        {
            var twoFactorAuthenticationEnabled = false;
            var uuid      = Guid.NewGuid();
            var accountId = "1234";

            var context = new TestCommandContext();

            var expectedRequestUri = new Uri("https://api.bitbucket.org/2.0/user");

            var userinfoResponseJson = $"{{ \"username\": \"{username}\" , \"has_2fa_enabled\": \"{twoFactorAuthenticationEnabled}\", \"account_id\": \"{accountId}\", \"uuid\": \"{uuid}\"}}";

            var httpResponse = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(userinfoResponseJson)
            };

            var httpHandler = new TestHttpMessageHandler();

            httpHandler.Setup(HttpMethod.Get, expectedRequestUri, request =>
            {
                if (isBearerToken)
                {
                    RestTestUtilities.AssertBearerAuth(request, password);
                }
                else
                {
                    RestTestUtilities.AssertBasicAuth(request, username, password);
                }

                return(httpResponse);
            });
            context.HttpClientFactory.MessageHandler = httpHandler;

            var api    = new BitbucketRestApi(context);
            var result = await api.GetUserInformationAsync(username, password, isBearerToken);

            Assert.NotNull(result);
            Assert.Equal(username, result.Response.UserName);
            Assert.Equal(accountId, result.Response.AccountId);
            Assert.Equal(uuid, result.Response.Uuid);
            Assert.Equal(twoFactorAuthenticationEnabled, result.Response.IsTwoFactorAuthenticationEnabled);

            httpHandler.AssertRequest(HttpMethod.Get, expectedRequestUri, 1);
        }
Esempio n. 5
0
        public async Task HostProviderRegistry_GetProvider_Auto_NetworkProbe_NoNetwork_ReturnsLastProvider()
        {
            var context   = new TestCommandContext();
            var registry  = new HostProviderRegistry(context);
            var remoteUri = new Uri("https://provider2.onprem.example.com");
            var input     = new InputArguments(
                new Dictionary <string, string>
            {
                ["protocol"] = remoteUri.Scheme,
                ["host"]     = remoteUri.Host
            }
                );

            var highProviderMock = new Mock <IHostProvider>();

            highProviderMock.Setup(x => x.IsSupported(It.IsAny <InputArguments>())).Returns(false);
            highProviderMock.Setup(x => x.IsSupported(It.IsAny <HttpResponseMessage>())).Returns(false);
            registry.Register(highProviderMock.Object, HostProviderPriority.Normal);

            var lowProviderMock = new Mock <IHostProvider>();

            lowProviderMock.Setup(x => x.IsSupported(It.IsAny <InputArguments>())).Returns(true);
            registry.Register(lowProviderMock.Object, HostProviderPriority.Low);

            var responseMessage = new HttpResponseMessage(HttpStatusCode.Unauthorized)
            {
                Headers = { { "X-Provider2", "true" } }
            };

            var httpHandler = new TestHttpMessageHandler
            {
                SimulateNoNetwork = true,
            };

            httpHandler.Setup(HttpMethod.Head, remoteUri, responseMessage);
            context.HttpClientFactory.MessageHandler = httpHandler;

            IHostProvider result = await registry.GetProviderAsync(input);

            httpHandler.AssertRequest(HttpMethod.Head, remoteUri, 1);
            Assert.Same(lowProviderMock.Object, result);
        }