コード例 #1
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());
        }
コード例 #2
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));
        }
コード例 #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);
        }
コード例 #4
0
        internal static HttpHandlerResourceV3 CreateResource(PackageSource packageSource, ICredentialService credentialService, bool nonInteractive = false)
        {
            var sourceUri = packageSource.SourceUri;
            var settings  = new HttpClientSettings {
                AutomaticDecompression       = DecompressionMethods.GZip | DecompressionMethods.Deflate,
                SourceAuthenticationRequired = false,
                NonInteractive = nonInteractive
            };
            var rootHandler = HttpClientProvider.CreateHttpMessageHandler(sourceUri, settings);

            // HTTP handler pipeline can be injected here, around the client handler
            HttpMessageHandler messageHandler = new MonoDevelopServerWarningLogHandler(rootHandler);

            var innerHandler = messageHandler;

            messageHandler = new StsAuthenticationHandler(packageSource, TokenStore.Instance)
            {
                InnerHandler = messageHandler
            };

            innerHandler = messageHandler;
            var credentialsHandler = GetHttpCredentialsHandler(rootHandler);

            messageHandler = new NuGetHttpSourceAuthenticationHandler(packageSource, credentialsHandler, credentialService)
            {
                InnerHandler = innerHandler
            };

            // Have to pass a dummy HttpClientProvider since it may not be used by a native implementation, such as on the Mac.
            // It looks like the only place this is used in NuGet is with the DownloadResourcePluginProvider in order to pass the
            // HttpClientHandler's Proxy to the GetCredentialsRequestHandler. There is no plugin support yet in MonoDevelop but
            // this may be a problem in the future. Possibly a custom DownloadResourcePluginProvider would be required or possibly
            // a HttpClientProvider created with just its Proxy property set based on the current proxy for that url.
            var clientHandler = GetOrCreateHttpClientHandler(rootHandler);

            // Get the proxy from NuGet's ProxyCache which has support for proxy information define in the NuGet.Config file.
            var proxy = ProxyCache.Instance.GetUserConfiguredProxy();

            if (proxy != null)
            {
                clientHandler.Proxy = proxy;
            }
            var resource = new HttpHandlerResourceV3(clientHandler, messageHandler);

            return(resource);
        }
コード例 #5
0
        public async Task SendAsync_WithUnauthenticatedSource_PassesThru()
        {
            var packageSource = new PackageSource("http://package.source.net");
            var clientHandler = new TestHttpClientHandler();

            var credentialService = new Mock <ICredentialService> (MockBehavior.Strict);

            credentialService.SetupGet(x => x.HandlesDefaultCredentials)
            .Returns(false);
            var handler = new NuGetHttpSourceAuthenticationHandler(packageSource, clientHandler, credentialService.Object)
            {
                InnerHandler = GetLambdaMessageHandler(HttpStatusCode.OK)
            };

            var response = await SendAsync(handler);

            Assert.NotNull(response);
            Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
        }
コード例 #6
0
        public void Constructor_WithSourceCredentials_InitializesClientHandler()
        {
            var packageSource = new PackageSource("http://package.source.net", "source")
            {
                Credentials = new PackageSourceCredential("source", "user", "password", isPasswordClearText: true)
            };
            var clientHandler     = new TestHttpClientHandler();
            var credentialService = Mock.Of <ICredentialService> ();

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

            Assert.NotNull(clientHandler.Credentials);

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

            Assert.NotNull(actualCredentials);
            Assert.AreEqual("user", actualCredentials.UserName);
            Assert.AreEqual("password", actualCredentials.Password);
        }
コード例 #7
0
        public async Task SendAsync_WithAcquiredCredentialsOn403_RetriesRequest()
        {
            // 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.Forbidden,
                    It.IsAny <string> (),
                    It.IsAny <CancellationToken> ()))
            .Returns(() => Task.FromResult <ICredentials> (new NetworkCredential()));

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