public async Task AddPolicyHandlerFromRegistry_WithConfigureDelegate_AddsPolicyHandler()
        {
            var options = new PollyPolicyOptions()
            {
                PolicyName = "retrypolicy"
            };

            var serviceCollection = new ServiceCollection();

            serviceCollection.AddSingleton(options);

            serviceCollection.AddPolicyRegistry((serviceProvider, registry) =>
            {
                string policyName = serviceProvider.GetRequiredService <PollyPolicyOptions>().PolicyName;

                registry.Add <IAsyncPolicy <HttpResponseMessage> >(policyName, RetryPolicy);
            });

            HttpMessageHandlerBuilder builder = null;

            // Act1
            serviceCollection.AddHttpClient("example.com", c => c.BaseAddress = new Uri("http://example.com"))
            .AddPolicyHandlerFromRegistry(options.PolicyName)
            .ConfigureHttpMessageHandlerBuilder(b =>
            {
                b.PrimaryHandler = PrimaryHandler;

                builder = b;
            });

            var services = serviceCollection.BuildServiceProvider();
            var factory  = services.GetRequiredService <IHttpClientFactory>();

            // Act2
            var client = factory.CreateClient("example.com");

            // Assert
            Assert.NotNull(client);

            Assert.Collection(
                builder.AdditionalHandlers,
                h => Assert.IsType <LoggingScopeHttpMessageHandler>(h),
                h => Assert.IsType <PolicyHttpMessageHandler>(h),
                h => Assert.IsType <LoggingHttpMessageHandler>(h));

            // Act 3
            var response = await client.SendAsync(new HttpRequestMessage());

            // Assert
            Assert.Equal(HttpStatusCode.Created, response.StatusCode);
        }
        public async Task AddPolicyHandlerFromRegistry_Dynamic_AddsPolicyHandler()
        {
            var serviceCollection = new ServiceCollection();

            var registry = serviceCollection.AddPolicyRegistry();

            registry.Add <IAsyncPolicy <HttpResponseMessage> >("noop", NoOpPolicy);
            registry.Add <IAsyncPolicy <HttpResponseMessage> >("retry", RetryPolicy);

            HttpMessageHandlerBuilder builder = null;

            // Act1
            serviceCollection.AddHttpClient("example.com", c => c.BaseAddress = new Uri("http://example.com"))
            .AddPolicyHandlerFromRegistry((reg, req) =>
            {
                return(req.RequestUri.AbsolutePath == "/" ?
                       reg.Get <IAsyncPolicy <HttpResponseMessage> >("retry") :
                       reg.Get <IAsyncPolicy <HttpResponseMessage> >("noop"));
            })
            .ConfigureHttpMessageHandlerBuilder(b =>
            {
                b.PrimaryHandler = PrimaryHandler;
                builder          = b;
            });

            var services = serviceCollection.BuildServiceProvider();
            var factory  = services.GetRequiredService <IHttpClientFactory>();

            // Act2
            var client = factory.CreateClient("example.com");

            // Assert
            Assert.NotNull(client);

            Assert.Collection(
                builder.AdditionalHandlers,
                h => Assert.IsType <LoggingScopeHttpMessageHandler>(h),
                h => Assert.IsType <PolicyHttpMessageHandler>(h),
                h => Assert.IsType <LoggingHttpMessageHandler>(h));

            // Act 3
            var response = await client.SendAsync(new HttpRequestMessage());

            // Assert
            Assert.Equal(HttpStatusCode.Created, response.StatusCode);

            // Act 4
            await Assert.ThrowsAsync <OverflowException>(() => client.SendAsync(new HttpRequestMessage(HttpMethod.Get, "/throw")));
        }
        public void AddPolicyHandlerFromRegistry_WithRegistry_AddsPolicyRegistries()
        {
            var serviceCollection = new ServiceCollection();
            var registry          = new PolicyRegistry();

            // Act
            serviceCollection.AddPolicyRegistry(registry);

            var services = serviceCollection.BuildServiceProvider();

            // Assert
            Assert.Same(registry, services.GetService <IConcurrentPolicyRegistry <string> >());
            Assert.Same(registry, services.GetService <IPolicyRegistry <string> >());
            Assert.Same(registry, services.GetService <IReadOnlyPolicyRegistry <string> >());
        }
        public void AddPolicyHandlerFromRegistry_WithConfigureDelegate_AddsPolicyRegistries()
        {
            var serviceCollection = new ServiceCollection();

            // Act
            serviceCollection.AddPolicyRegistry((serviceProvider, registry) =>
            {
                // No-op
            });

            var services = serviceCollection.BuildServiceProvider();
            var registry = services.GetService <IPolicyRegistry <string> >();

            // Assert
            Assert.NotNull(registry);
            Assert.Same(registry, services.GetService <IConcurrentPolicyRegistry <string> >());
            Assert.Same(registry, services.GetService <IReadOnlyPolicyRegistry <string> >());
        }
        public async Task AddPolicyHandlerFromRegistry_PolicySelectorWithKey_AddsPolicyHandler()
        {
            var serviceCollection             = new ServiceCollection();
            var registry                      = serviceCollection.AddPolicyRegistry();
            HttpMessageHandlerBuilder builder = null;

            // Act1
            serviceCollection.AddHttpClient("Service")
            .AddPolicyHandler(
                (sp, req, key) =>
            {
                return(RetryPolicy);
            },
                (r) =>
            {
                return(r.RequestUri.Host);
            }
                )
            .ConfigureHttpMessageHandlerBuilder(b =>
            {
                b.PrimaryHandler = PrimaryHandler;
                builder          = b;
            });

            var services = serviceCollection.BuildServiceProvider();
            var factory  = services.GetRequiredService <IHttpClientFactory>();

            // Act2
            var client = factory.CreateClient("Service");

            // Assert
            Assert.NotNull(client);

            Assert.Collection(
                builder.AdditionalHandlers,
                h => Assert.IsType <LoggingScopeHttpMessageHandler>(h),
                h => Assert.IsType <PolicyHttpMessageHandler>(h),
                h => Assert.IsType <LoggingHttpMessageHandler>(h));

            // Act 3
            var request  = new HttpRequestMessage(HttpMethod.Get, "http://host1/Service1/");
            var response = await client.SendAsync(request);

            // Assert
            Assert.Equal(HttpStatusCode.Created, response.StatusCode);
            Assert.True(registry.ContainsKey("host1"));
            Assert.Equal(1, registry.Count);

            // Act 4
            request  = new HttpRequestMessage(HttpMethod.Get, "http://host1/Service1/");
            response = await client.SendAsync(request);

            // Assert
            Assert.Equal(HttpStatusCode.Created, response.StatusCode);
            Assert.True(registry.ContainsKey("host1"));
            Assert.Equal(1, registry.Count);

            // Act 4
            request  = new HttpRequestMessage(HttpMethod.Get, "http://host2/Service1/");
            response = await client.SendAsync(request);

            // Assert policy count
            Assert.Equal(HttpStatusCode.Created, response.StatusCode);
            Assert.Equal(2, registry.Count);
            Assert.True(registry.ContainsKey("host1"));
            Assert.True(registry.ContainsKey("host2"));
        }