public void DoesNotInvokeRegistrationForRegisteredResourceProviders() { // Setup Mock <ResourceManagementClient> mockClient = new Mock <ResourceManagementClient>(); Mock <IProviderOperations> mockProvidersOperations = new Mock <IProviderOperations>(); mockClient.Setup(f => f.Providers).Returns(mockProvidersOperations.Object); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, compatibleUri); Dictionary <HttpRequestMessage, List <HttpResponseMessage> > mapping = new Dictionary <HttpRequestMessage, List <HttpResponseMessage> > { { request, new List <HttpResponseMessage> { new HttpResponseMessage(HttpStatusCode.Accepted) { Content = new StringContent("Azure works!") } } } }; List <string> msgs = new List <string>(); RPRegistrationDelegatingHandler rpHandler = new RPRegistrationDelegatingHandler(() => mockClient.Object, s => msgs.Add(s)) { InnerHandler = new MockResponseDelegatingHandler(mapping) }; HttpClient httpClient = new HttpClient(rpHandler); // Test HttpResponseMessage response = httpClient.SendAsync(request).Result; // Assert Assert.Equal(0, msgs.Count); Assert.Equal(response.StatusCode, HttpStatusCode.Accepted); Assert.Equal(response.Content.ReadAsStringAsync().Result, "Azure works!"); }
public void DoesNotHangForLongRegistrationCalls() { // Setup Mock <ResourceManagementClient> mockClient = new Mock <ResourceManagementClient>(); Mock <IProviderOperations> mockProvidersOperations = new Mock <IProviderOperations>(); mockClient.Setup(f => f.Providers).Returns(mockProvidersOperations.Object); mockProvidersOperations.Setup(f => f.GetAsync(It.IsAny <string>(), It.IsAny <CancellationToken>())).Returns( (string rp, CancellationToken token) => { ProviderGetResult r = new ProviderGetResult { Provider = new Provider { RegistrationState = RegistrationState.Pending.ToString() } }; return(Task.FromResult(r)); } ); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, compatibleUri); Dictionary <HttpRequestMessage, List <HttpResponseMessage> > mapping = new Dictionary <HttpRequestMessage, List <HttpResponseMessage> > { { request, new List <HttpResponseMessage> { new HttpResponseMessage(HttpStatusCode.Conflict) { Content = new StringContent("registered to use namespace") }, new HttpResponseMessage(HttpStatusCode.Conflict) { Content = new StringContent("registered to use namespace") } } } }; List <string> msgs = new List <string>(); RPRegistrationDelegatingHandler rpHandler = new RPRegistrationDelegatingHandler(() => mockClient.Object, s => msgs.Add(s)) { InnerHandler = new MockResponseDelegatingHandler(mapping) }; HttpClient httpClient = new HttpClient(rpHandler); // Test HttpResponseMessage response = httpClient.SendAsync(request).Result; // Assert Assert.True(msgs.Any(s => s.Equals("Failed to register resource provider 'microsoft.compute'.Details: 'The operation has timed out.'"))); Assert.Equal(response.StatusCode, HttpStatusCode.Conflict); Assert.Equal(response.Content.ReadAsStringAsync().Result, "registered to use namespace"); mockProvidersOperations.Verify(f => f.RegisterAsync("microsoft.compute", It.IsAny <CancellationToken>()), Times.AtMost(4)); }
public void InvokeRegistrationForUnregisteredResourceProviders() { // Setup Mock <ResourceManagementClient> mockClient = new Mock <ResourceManagementClient>(); Mock <IProviderOperations> mockProvidersOperations = new Mock <IProviderOperations>(); mockClient.Setup(f => f.Providers).Returns(mockProvidersOperations.Object); mockProvidersOperations.Setup(f => f.GetAsync(It.IsAny <string>(), It.IsAny <CancellationToken>())).Returns( (string rp, CancellationToken token) => { ProviderGetResult r = new ProviderGetResult { Provider = new Provider { RegistrationState = RegistrationState.Registered.ToString() } }; return(Task.FromResult(r)); } ); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, compatibleUri); Dictionary <HttpRequestMessage, List <HttpResponseMessage> > mapping = new Dictionary <HttpRequestMessage, List <HttpResponseMessage> > { { request, new List <HttpResponseMessage> { new HttpResponseMessage(HttpStatusCode.Conflict) { Content = new StringContent("registered to use namespace") }, new HttpResponseMessage(HttpStatusCode.Accepted) { Content = new StringContent("Azure works!") } } } }; List <string> msgs = new List <string>(); RPRegistrationDelegatingHandler rpHandler = new RPRegistrationDelegatingHandler(() => mockClient.Object, s => msgs.Add(s)) { InnerHandler = new MockResponseDelegatingHandler(mapping) }; HttpClient httpClient = new HttpClient(rpHandler); // Test HttpResponseMessage response = httpClient.SendAsync(request).Result; // Assert Assert.True(msgs.Any(s => s.Equals("Succeeded to register resource provider 'microsoft.compute'"))); Assert.Equal(response.StatusCode, HttpStatusCode.Accepted); Assert.Equal(response.Content.ReadAsStringAsync().Result, "Azure works!"); }
public void DoesNotThrowForFailedRegistrationCall() { // Setup Mock <ResourceManagementClient> mockClient = new Mock <ResourceManagementClient>() { CallBase = true }; Mock <IProvidersOperations> mockProvidersOperations = new Mock <IProvidersOperations>(); mockClient.Setup(f => f.Providers).Returns(mockProvidersOperations.Object); mockProvidersOperations.Setup(f => f.RegisterWithHttpMessagesAsync(It.IsAny <string>(), null, It.IsAny <CancellationToken>())) .Throws(new CloudException("PR reg failed")); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, compatibleUri); Dictionary <HttpRequestMessage, List <HttpResponseMessage> > mapping = new Dictionary <HttpRequestMessage, List <HttpResponseMessage> > { { request, new List <HttpResponseMessage> { new HttpResponseMessage(HttpStatusCode.Conflict) { Content = new StringContent("registered to use namespace") }, new HttpResponseMessage(HttpStatusCode.Conflict) { Content = new StringContent("registered to use namespace") } } } }; List <string> msgs = new List <string>(); RPRegistrationDelegatingHandler rpHandler = new RPRegistrationDelegatingHandler(() => mockClient.Object, s => msgs.Add(s)) { InnerHandler = new MockResponseDelegatingHandler(mapping) }; HttpClient httpClient = new HttpClient(rpHandler); // Test HttpResponseMessage response = httpClient.SendAsync(request).Result; // Assert Assert.Contains(msgs, s => s.Equals("Failed to register resource provider 'microsoft.compute'.Details: 'PR reg failed'")); Assert.Equal(HttpStatusCode.Conflict, response.StatusCode); Assert.Equal("registered to use namespace", response.Content.ReadAsStringAsync().Result); mockProvidersOperations.Verify(f => f.RegisterWithHttpMessagesAsync("microsoft.compute", null, It.IsAny <CancellationToken>()), Times.AtMost(4)); }
public void DoesNotInvokeRegistrationForIncompatibleUri() { // Setup Mock <ResourceManagementClient> mockClient = new Mock <ResourceManagementClient>() { CallBase = true }; Mock <IProvidersOperations> mockProvidersOperations = new Mock <IProvidersOperations>(); mockClient.Setup(f => f.Providers).Returns(mockProvidersOperations.Object); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, incompatibleUri); Dictionary <HttpRequestMessage, List <HttpResponseMessage> > mapping = new Dictionary <HttpRequestMessage, List <HttpResponseMessage> > { { request, new List <HttpResponseMessage> { new HttpResponseMessage(HttpStatusCode.Conflict) { Content = new StringContent("registered to use namespace") } } } }; List <string> msgs = new List <string>(); RPRegistrationDelegatingHandler rpHandler = new RPRegistrationDelegatingHandler(() => mockClient.Object, s => msgs.Add(s)) { InnerHandler = new MockResponseDelegatingHandler(mapping) }; HttpClient httpClient = new HttpClient(rpHandler); // Test HttpResponseMessage response = httpClient.SendAsync(request).Result; // Assert Assert.Empty(msgs); Assert.Equal(HttpStatusCode.Conflict, response.StatusCode); Assert.Equal("registered to use namespace", response.Content.ReadAsStringAsync().Result); }