Esempio n. 1
0
        public async Task SendAsync_WithWrongCredentials_StopsRetryingAfter3Times()
        {
            // Arrange
            var packageSource = new PackageSource("http://package.source.net");
            var clientHandler = new TestHttpClientHandler();

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

            Mock.Get(credentialService)
            .Setup(
                x => x.GetCredentialsAsync(
                    packageSource.SourceUri,
                    It.IsAny <IWebProxy> (),
                    CredentialRequestType.Unauthorized,
                    It.IsAny <string> (),
                    It.IsAny <CancellationToken> ()))
            .Returns(() => Task.FromResult <ICredentials> (new NetworkCredential()));

            var handler = new NuGetHttpSourceAuthenticationHandler(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(NuGetHttpSourceAuthenticationHandler.MaxAuthRetries + 1, retryCount);

            Mock.Get(credentialService)
            .Verify(
                x => x.GetCredentialsAsync(
                    packageSource.SourceUri,
                    It.IsAny <IWebProxy> (),
                    CredentialRequestType.Unauthorized,
                    It.IsAny <string> (),
                    It.IsAny <CancellationToken> ()),
                Times.Exactly(NuGetHttpSourceAuthenticationHandler.MaxAuthRetries));
        }
Esempio n. 2
0
        public async Task SendAsync_WhenCredentialServiceThrows_Returns401()
        {
            // Arrange
            var packageSource = new PackageSource("http://package.source.net");
            var clientHandler = new TestHttpClientHandler();

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

            Mock.Get(credentialService)
            .Setup(
                x => x.GetCredentialsAsync(
                    packageSource.SourceUri,
                    It.IsAny <IWebProxy> (),
                    CredentialRequestType.Unauthorized,
                    It.IsAny <string> (),
                    It.IsAny <CancellationToken> ()))
            .Throws(new InvalidOperationException("Credential service failed acquring user credentials"));

            var handler = new NuGetHttpSourceAuthenticationHandler(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.SourceUri,
                    It.IsAny <IWebProxy> (),
                    CredentialRequestType.Unauthorized,
                    It.IsAny <string> (),
                    It.IsAny <CancellationToken> ()),
                Times.Once());
        }
Esempio n. 3
0
        public async Task SendAsync_WhenOperationCanceledExceptionThrownDuringAcquiringCredentials_Throws()
        {
            // Arrange
            var packageSource = new PackageSource("http://package.source.net");
            var clientHandler = new TestHttpClientHandler();

            var cts = new CancellationTokenSource();

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

            Mock.Get(credentialService)
            .Setup(
                x => x.GetCredentialsAsync(
                    packageSource.SourceUri,
                    It.IsAny <IWebProxy> (),
                    CredentialRequestType.Unauthorized,
                    It.IsAny <string> (),
                    It.IsAny <CancellationToken> ()))
            .ThrowsAsync(new OperationCanceledException())
            .Callback(() => cts.Cancel());

            var handler = new NuGetHttpSourceAuthenticationHandler(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.SourceUri,
                    It.IsAny <IWebProxy> (),
                    CredentialRequestType.Unauthorized,
                    It.IsAny <string> (),
                    It.IsAny <CancellationToken> ()),
                Times.Once);
        }