예제 #1
0
        public void CreateClientCreatesNewClientWithSameHandler()
        {
            using StandaloneHttpClientFactory httpClientFactory = new StandaloneHttpClientFactory();
            using HttpClient client1 = httpClientFactory.CreateClient();
            using HttpClient client2 = httpClientFactory.CreateClient();
            HttpMessageHandler handler1 = GetHandler(client1);
            HttpMessageHandler handler2 = GetHandler(client2);

            Assert.NotSame(client1, client2);
            Assert.Same(handler1, handler2);
        }
예제 #2
0
        public void CreateClientCreatesSameClient()
        {
            using (StandaloneHttpClientFactory httpClientFactory = new StandaloneHttpClientFactory())
            {
                using (HttpClient client1 = httpClientFactory.CreateClient())
                {
                    using (HttpClient client2 = httpClientFactory.CreateClient())
                    {
                        HttpMessageHandler handler1 = GetHandler(client1);
                        HttpMessageHandler handler2 = GetHandler(client2);

                        Assert.Same(client1, client2);
                        Assert.Same(handler1, handler2);
                    }
                }
            }
        }
예제 #3
0
        public void CreateClientUsesDefaultsWhenFactoryUsesDefaults()
        {
            using StandaloneHttpClientFactory httpClientFactory = new StandaloneHttpClientFactory();
            using HttpClient client = httpClientFactory.CreateClient();
            HttpMessageHandler handler = GetHandler(client);

            Assert.IsType <SocketsHttpHandler>(GetHandler(client));
            Assert.Equal(StandaloneHttpClientFactory.DefaultConnectionLifetime, ((SocketsHttpHandler)handler).PooledConnectionLifetime);
        }
예제 #4
0
        public void CreateClientUsesLoggerToCreateLoggingHandler()
        {
            using StandaloneHttpClientFactory httpClientFactory = new StandaloneHttpClientFactory(NullLogger.Instance);
            using HttpClient client = httpClientFactory.CreateClient();
            HttpMessageHandler handler = GetHandler(client);

            Assert.IsType <LoggingHttpMessageHandler>(GetHandler(client));
            LoggingHttpMessageHandler loggingHandler = (LoggingHttpMessageHandler)handler;

            Assert.IsType <SocketsHttpHandler>(loggingHandler.InnerHandler);
        }
예제 #5
0
        public void CreateClientUsesProvidedConnectionLifetime()
        {
            TimeSpan connectionLifetime = TimeSpan.FromMinutes(1);

            using StandaloneHttpClientFactory httpClientFactory = new StandaloneHttpClientFactory(connectionLifetime);
            using HttpClient client = httpClientFactory.CreateClient();
            HttpMessageHandler handler = GetHandler(client);

            Assert.IsType <SocketsHttpHandler>(GetHandler(client));
            Assert.Equal(connectionLifetime, ((SocketsHttpHandler)handler).PooledConnectionLifetime);
        }
예제 #6
0
        public void CreateClientUsesDefaultsWhenFactoryUsesDefaults()
        {
            using (StandaloneHttpClientFactory httpClientFactory = new StandaloneHttpClientFactory())
            {
                using (HttpClient client = httpClientFactory.CreateClient())
                {
                    HttpMessageHandler handler = GetHandler(client);

                    Assert.IsType <ServicePointHttpMessageHandler>(GetHandler(client));
                    Assert.Equal(StandaloneHttpClientFactory.DefaultConnectionLifetime, ((ServicePointHttpMessageHandler)handler).ConnectionLeaseTimeout);
                }
            }
        }
        public void CreateClientUsesLoggerToCreateLoggingHandler()
        {
            using (StandaloneHttpClientFactory httpClientFactory = new StandaloneHttpClientFactory(NullLogger.Instance))
            {
                using (HttpClient client = httpClientFactory.CreateClient())
                {
                    HttpMessageHandler handler = GetHandler(client);

                    Assert.IsType <LoggingHttpMessageHandler>(handler);
                    LoggingHttpMessageHandler loggingHandler = (LoggingHttpMessageHandler)handler;
                    Assert.IsType <ServicePointHttpMessageHandler>(loggingHandler.InnerHandler);
                }
            }
        }
예제 #8
0
        public void CreateClientUsesProvidedConnectionLifetime()
        {
            TimeSpan connectionLifetime = TimeSpan.FromMinutes(1);

            using (StandaloneHttpClientFactory httpClientFactory = new StandaloneHttpClientFactory(connectionLifetime))
            {
                using (HttpClient client = httpClientFactory.CreateClient())
                {
                    HttpMessageHandler handler = GetHandler(client);

                    Assert.IsType <ServicePointHttpMessageHandler>(GetHandler(client));
                    Assert.Equal(connectionLifetime, ((ServicePointHttpMessageHandler)handler).ConnectionLeaseTimeout);
                }
            }
        }
예제 #9
0
        public void CreateClientCreatesHandlerPipeline()
        {
            DelegatingHandler foo = A.Fake <DelegatingHandler>(options => options.CallsBaseMethods());
            DelegatingHandler bar = A.Fake <DelegatingHandler>(options => options.CallsBaseMethods());

            using StandaloneHttpClientFactory httpClientFactory = new StandaloneHttpClientFactory(foo, bar);
            using HttpClient client = httpClientFactory.CreateClient();
            HttpMessageHandler handler = GetHandler(client);

            Assert.Same(foo, handler);
            DelegatingHandler delegatingHandler = (DelegatingHandler)handler;

            Assert.Same(bar, delegatingHandler.InnerHandler);
            delegatingHandler = (DelegatingHandler)delegatingHandler.InnerHandler;
            Assert.IsType <SocketsHttpHandler>(delegatingHandler.InnerHandler);
        }