Esempio n. 1
0
        public async Task SendAsync_WhenCredentialServiceThrows_Returns401()
        {
            // 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> ()))
            .Throws(new InvalidOperationException("Credential service failed acquring user credentials"));

            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());
        }
Esempio n. 2
0
        public async Task SendAsync_WithWrongCredentials_StopsRetryingAfter3Times()
        {
            // 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);

            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(HttpSourceAuthenticationHandler.MaxAuthRetries + 1, retryCount);

            Mock.Get(credentialService)
            .Verify(
                x => x.GetCredentialsAsync(
                    packageSource,
                    It.IsAny <IWebProxy> (),
                    CredentialType.RequestCredentials,
                    It.IsAny <bool> (),
                    It.IsAny <CancellationToken> ()),
                Times.Exactly(HttpSourceAuthenticationHandler.MaxAuthRetries));
        }
Esempio n. 3
0
        public async Task SendAsync_WhenOperationCanceledExceptionThrownDuringAcquiringCredentials_Throws()
        {
            // Arrange
            var packageSource = new Uri("http://package.source.net");
            var clientHandler = new DefaultHttpClientHandler(null, new HttpClientSettings());

            var cts = new CancellationTokenSource();

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

            Mock.Get(credentialService)
            .Setup(
                x => x.GetCredentialsAsync(
                    packageSource,
                    It.IsAny <IWebProxy> (),
                    CredentialType.RequestCredentials,
                    It.IsAny <bool> (),
                    It.IsAny <CancellationToken> ()))
            .ThrowsAsync(new OperationCanceledException())
            .Callback(() => cts.Cancel());

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

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

            handler.InnerHandler = innerHandler;

            // Act & Assert
            await AssertThrowsAsync <OperationCanceledException> (
                () => SendAsync (handler));

            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);
        }
        public async Task SendAsync_WhenCancelledDuringAcquiringCredentials_Throws()
        {
            // Arrange
            var defaultClientHandler = GetDefaultClientHandler();

            var cts = new CancellationTokenSource();

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

            Mock.Get(service)
            .Setup(
                x => x.GetCredentialsAsync(
                    ProxyAddress,
                    It.IsAny <IWebProxy> (),
                    CredentialType.ProxyCredentials,
                    It.IsAny <bool> (),
                    It.IsAny <CancellationToken> ()))
            .ThrowsAsync(new TaskCanceledException())
            .Callback(() => cts.Cancel());

            var handler = new ProxyAuthenticationHandler(defaultClientHandler, service, new ProxyCache());

            var responses = new Queue <HttpStatusCode> (
                new [] { HttpStatusCode.ProxyAuthenticationRequired, HttpStatusCode.OK });
            var innerHandler = new LambdaMessageHandler(
                _ => new HttpResponseMessage(responses.Dequeue()));

            handler.InnerHandler = innerHandler;

            // Act
            await AssertThrowsAsync <TaskCanceledException> (
                () => SendAsync (handler, cancellationToken : cts.Token));

            Mock.Get(service)
            .Verify(
                x => x.GetCredentialsAsync(
                    ProxyAddress,
                    It.IsAny <IWebProxy> (),
                    CredentialType.ProxyCredentials,
                    It.IsAny <bool> (),
                    It.IsAny <CancellationToken> ()),
                Times.Once);
        }
        public async Task SendAsync_WithWrongCredentials_StopsRetryingAfter3Times()
        {
            var defaultClientHandler = GetDefaultClientHandler();

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

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

            var handler = new ProxyAuthenticationHandler(defaultClientHandler, service, new ProxyCache());

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

            handler.InnerHandler = innerHandler;

            var response = await SendAsync(handler);

            Assert.NotNull(response);
            Assert.AreEqual(HttpStatusCode.ProxyAuthenticationRequired, response.StatusCode);

            Assert.AreEqual(ProxyAuthenticationHandler.MaxAuthRetries, retryCount);

            Mock.Get(service)
            .Verify(
                x => x.GetCredentialsAsync(
                    ProxyAddress,
                    It.IsAny <IWebProxy> (),
                    CredentialType.ProxyCredentials,
                    It.IsAny <bool> (),
                    It.IsAny <CancellationToken> ()),
                Times.Exactly(2));
        }
        public async Task SendAsync_WithAcquiredCredentials_RetriesRequest()
        {
            var defaultClientHandler = GetDefaultClientHandler();

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

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

            var handler = new ProxyAuthenticationHandler(defaultClientHandler, service, new ProxyCache());

            var responses = new Queue <HttpStatusCode> (
                new [] { HttpStatusCode.ProxyAuthenticationRequired, HttpStatusCode.OK });
            var innerHandler = new LambdaMessageHandler(
                _ => new HttpResponseMessage(responses.Dequeue()));

            handler.InnerHandler = innerHandler;

            var response = await SendAsync(handler);

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

            Mock.Get(service)
            .Verify(
                x => x.GetCredentialsAsync(
                    ProxyAddress,
                    It.IsAny <IWebProxy> (),
                    CredentialType.ProxyCredentials,
                    It.IsAny <bool> (),
                    It.IsAny <CancellationToken> ()),
                Times.Once());
        }