Esempio n. 1
0
        public async Task SendAsync_WithAcquiredCredentialsOn403_RetriesRequest()
        {
            // Arrange
            var packageSource = new Uri("http://package.source.net");
            var clientHandler = new DefaultHttpClientHandler(null, new HttpClientSettings());

            var credentialService = Mock.Of <ICredentialService> ();

            Mock.Get(credentialService)
            .Setup(
                x => x.GetCredentialsAsync(
                    packageSource,
                    It.IsAny <IWebProxy> (),
                    CredentialType.RequestCredentials,
                    It.IsAny <bool> (),
                    It.IsAny <CancellationToken> ()))
            .Returns(() => Task.FromResult <ICredentials> (new NetworkCredential()));

            var handler = new HttpSourceAuthenticationHandler(packageSource, clientHandler, credentialService)
            {
                InnerHandler = GetLambdaMessageHandler(
                    HttpStatusCode.Forbidden, HttpStatusCode.OK)
            };

            // Act
            var response = await SendAsync(handler);

            // Assert
            Assert.NotNull(response);
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);

            Mock.Get(credentialService)
            .Verify(
                x => x.GetCredentialsAsync(
                    packageSource,
                    It.IsAny <IWebProxy> (),
                    CredentialType.RequestCredentials,
                    It.IsAny <bool> (),
                    It.IsAny <CancellationToken> ()),
                Times.Once());
        }
Esempio n. 2
0
        public void Constructor_WithSourceCredentials_InitializesClientHandler()
        {
            if (CredentialCache.DefaultNetworkCredentials == null)
            {
                Assert.Ignore("No default network credentials");
            }

            var packageSource = new Uri("http://package.source.net");
            var credentials   = new NetworkCredential("user", "password");
            var a             = CredentialCache.DefaultNetworkCredentials;

            var clientHandler     = new DefaultHttpClientHandler(null, new HttpClientSettings());
            var credentialService = Mock.Of <ICredentialService> ();

            var handler = new HttpSourceAuthenticationHandler(packageSource, clientHandler, credentialService);

            Assert.NotNull(clientHandler.Credentials);

            var actualCredentials = clientHandler.Credentials.GetCredential(packageSource, "Basic");

            Assert.AreEqual(CredentialCache.DefaultNetworkCredentials, actualCredentials);
        }
Esempio n. 3
0
        public async Task SendAsync_WithMissingCredentials_Returns401()
        {
            // Arrange
            var packageSource = new Uri("http://package.source.net");
            var clientHandler = new DefaultHttpClientHandler(null, new HttpClientSettings());

            var credentialService = Mock.Of <ICredentialService> ();

            var handler = new HttpSourceAuthenticationHandler(packageSource, clientHandler, credentialService);

            int retryCount   = 0;
            var innerHandler = new LambdaMessageHandler(
                _ => {
                retryCount++;
                return(new HttpResponseMessage(HttpStatusCode.Unauthorized));
            });

            handler.InnerHandler = innerHandler;

            // Act
            var response = await SendAsync(handler);

            // Assert
            Assert.NotNull(response);
            Assert.AreEqual(HttpStatusCode.Unauthorized, response.StatusCode);

            Assert.AreEqual(1, retryCount);

            Mock.Get(credentialService)
            .Verify(
                x => x.GetCredentialsAsync(
                    packageSource,
                    It.IsAny <IWebProxy> (),
                    CredentialType.RequestCredentials,
                    It.IsAny <bool> (),
                    It.IsAny <CancellationToken> ()),
                Times.Once());
        }