public async Task SendAsync_WhenCredentialServiceThrows_Returns401() { // 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.Unauthorized, It.IsAny <string>(), 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.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_WithWrongCredentials_StopsRetryingAfter3Times() { // 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.Unauthorized, It.IsAny <string>(), 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.Equal(HttpStatusCode.Unauthorized, response.StatusCode); Assert.Equal(HttpSourceAuthenticationHandler.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(HttpSourceAuthenticationHandler.MaxAuthRetries)); }
public async Task SendAsync_WhenOperationCanceledExceptionThrownDuringAcquiringCredentials_Throws() { // Arrange var packageSource = new PackageSource("http://package.source.test"); var clientHandler = new HttpClientHandler(); 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 HttpSourceAuthenticationHandler(packageSource, clientHandler, credentialService); int retryCount = 0; var innerHandler = new LambdaMessageHandler( _ => { retryCount++; return(new HttpResponseMessage(HttpStatusCode.Unauthorized)); }); handler.InnerHandler = innerHandler; // Act & Assert await Assert.ThrowsAnyAsync <OperationCanceledException>( () => SendAsync(handler)); 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_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>(), CredentialRequestType.Proxy, It.IsAny <string>(), It.IsAny <CancellationToken>())) .ThrowsAsync(new TaskCanceledException()) .Callback(() => cts.Cancel()); var handler = new ProxyAuthenticationHandler(defaultClientHandler, service, ProxyCache.Instance); var responses = new Queue <HttpStatusCode>( new[] { HttpStatusCode.ProxyAuthenticationRequired, HttpStatusCode.OK }); var innerHandler = new LambdaMessageHandler( _ => new HttpResponseMessage(responses.Dequeue())); handler.InnerHandler = innerHandler; // Act await Assert.ThrowsAsync <TaskCanceledException>( () => SendAsync(handler, cancellationToken : cts.Token)); Mock.Get(service) .Verify( x => x.GetCredentialsAsync( ProxyAddress, It.IsAny <IWebProxy>(), CredentialRequestType.Proxy, It.IsAny <string>(), 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>(), CredentialRequestType.Proxy, It.IsAny <string>(), It.IsAny <CancellationToken>())) .Returns(() => Task.FromResult <ICredentials>(new NetworkCredential())); var handler = new ProxyAuthenticationHandler(defaultClientHandler, service, ProxyCache.Instance); 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.Equal(HttpStatusCode.ProxyAuthenticationRequired, response.StatusCode); Assert.Equal(ProxyAuthenticationHandler.MaxAuthRetries, retryCount); Mock.Get(service) .Verify( x => x.GetCredentialsAsync( ProxyAddress, It.IsAny <IWebProxy>(), CredentialRequestType.Proxy, It.IsAny <string>(), 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>(), CredentialRequestType.Proxy, It.IsAny <string>(), It.IsAny <CancellationToken>())) .Returns(() => Task.FromResult <ICredentials>(new NetworkCredential())); var handler = new ProxyAuthenticationHandler(defaultClientHandler, service, ProxyCache.Instance); 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.Equal(HttpStatusCode.OK, response.StatusCode); Mock.Get(service) .Verify( x => x.GetCredentialsAsync( ProxyAddress, It.IsAny <IWebProxy>(), CredentialRequestType.Proxy, It.IsAny <string>(), It.IsAny <CancellationToken>()), Times.Once()); }