public async Task SendAsync_WithUnauthenticatedSource_PassesThru() { var packageSource = new PackageSource("http://package.source.test"); var clientHandler = new HttpClientHandler(); var credentialService = new Mock <ICredentialService>(MockBehavior.Strict); credentialService.SetupGet(x => x.HandlesDefaultCredentials) .Returns(false); var handler = new HttpSourceAuthenticationHandler(packageSource, clientHandler, credentialService.Object) { InnerHandler = GetLambdaMessageHandler(HttpStatusCode.OK) }; var response = await SendAsync(handler); Assert.NotNull(response); Assert.Equal(HttpStatusCode.OK, response.StatusCode); }
public void Constructor_WithSourceCredentials_InitializesClientHandler() { var packageSource = new PackageSource("http://package.source.test", "source") { Credentials = new PackageSourceCredential("source", "user", "password", isPasswordClearText: true, validAuthenticationTypesText: null) }; var clientHandler = new HttpClientHandler(); var credentialService = Mock.Of <ICredentialService>(); var handler = new HttpSourceAuthenticationHandler(packageSource, clientHandler, credentialService); Assert.NotNull(clientHandler.Credentials); var actualCredentials = clientHandler.Credentials.GetCredential(packageSource.SourceUri, "Basic"); Assert.NotNull(actualCredentials); Assert.Equal("user", actualCredentials.UserName); Assert.Equal("password", actualCredentials.Password); }
private HttpHandlerResourceV3 CreateResource(PackageSource packageSource) { var sourceUri = packageSource.SourceUri; var proxy = ProxyCache.Instance.GetProxy(sourceUri); // replace the handler with the proxy aware handler var clientHandler = new HttpClientHandler { Proxy = proxy, AutomaticDecompression = (DecompressionMethods.GZip | DecompressionMethods.Deflate) }; // HTTP handler pipeline can be injected here, around the client handler HttpMessageHandler messageHandler = new ProgressHttpMessageHandler(clientHandler, _progressAction); if (proxy != null) { messageHandler = new ProxyAuthenticationHandler(clientHandler, HttpHandlerResourceV3.CredentialService.Value, ProxyCache.Instance); } { //var innerHandler = messageHandler; // TODO: Investigate what changed in this type // messageHandler = new StsAuthenticationHandler(packageSource, TokenStore.Instance) // { // InnerHandler = innerHandler // }; } { var innerHandler = messageHandler; messageHandler = new HttpSourceAuthenticationHandler(packageSource, clientHandler, HttpHandlerResourceV3.CredentialService.Value) { InnerHandler = innerHandler }; } var resource = new HttpHandlerResourceV3(clientHandler, messageHandler); return(resource); }
public async Task SendAsync_WithProtocolDiagnosticsStopwatches_PausesStopwatches() { var packageSource = new PackageSource("http://package.source.test"); Stopwatch stopwatch = Stopwatch.StartNew(); var clientHandler = new HttpClientHandler(); 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())) .Callback(() => { Assert.False(stopwatch.IsRunning, "Stopwatch should be stopped during " + nameof(credentialService.GetCredentialsAsync)); }); var handler = new HttpSourceAuthenticationHandler(packageSource, clientHandler, credentialService) { InnerHandler = GetLambdaMessageHandler( HttpStatusCode.Unauthorized, HttpStatusCode.OK) }; var response = await SendAsync(handler); Assert.True(stopwatch.IsRunning, "Stopwatch should be running after SendAsync returns"); Mock.Get(credentialService) .Verify( x => x.GetCredentialsAsync( It.IsAny <Uri>(), It.IsAny <IWebProxy>(), It.IsAny <CredentialRequestType>(), It.IsAny <string>(), It.IsAny <CancellationToken>()), Times.Once()); }
public async Task SendAsync_WithAcquiredCredentialsOn403_RetriesRequest() { // Arrange var packageSource = new PackageSource("http://package.source.test"); var clientHandler = new HttpClientHandler(); 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 HttpSourceAuthenticationHandler(packageSource, clientHandler, credentialService) { InnerHandler = GetLambdaMessageHandler( HttpStatusCode.Forbidden, HttpStatusCode.OK) }; // Act var response = await SendAsync(handler); // Assert Assert.NotNull(response); Assert.Equal(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()); }
public async Task SendAsync_WithMissingCredentials_Returns401() { // Arrange var packageSource = new PackageSource("http://package.source.test"); var clientHandler = new HttpClientHandler(); 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.Equal(HttpStatusCode.Unauthorized, response.StatusCode); Assert.Equal(1, retryCount); Mock.Get(credentialService) .Verify( x => x.GetCredentialsAsync( packageSource.SourceUri, It.IsAny <IWebProxy>(), CredentialRequestType.Unauthorized, It.IsAny <string>(), It.IsAny <CancellationToken>()), Times.Once()); }
public async Task SendAsync_RetryWithClonedPostRequest(HttpContent httpContent) { var packageSource = new PackageSource("http://package.source.test"); var clientHandler = new HttpClientHandler(); 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 requests = 0; var handler = new HttpSourceAuthenticationHandler(packageSource, clientHandler, credentialService) { InnerHandler = new LambdaMessageHandler( async request => { Assert.Null(request.Headers.Authorization); request.Headers.Authorization = new AuthenticationHeaderValue("Basic", "TEST"); await request.Content.ReadAsStringAsync(); requests++; return(new HttpResponseMessage(HttpStatusCode.Unauthorized)); }) }; var response = await SendAsync(handler, new HttpRequestMessage(HttpMethod.Post, "http://package.source.test") { Content = httpContent }); Assert.True(requests > 1, "No retries"); Assert.NotNull(response); Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); }