Пример #1
0
        public async Task HttpClientPoolAllocatesOneHandlerPerUriAndCredentialSet()
        {
            var uri        = new Uri("http://localhost/platibus");
            var clientPool = new HttpClientPool();

            var credentialSet = new IEndpointCredentials[]
            {
                new BasicAuthCredentials("user1", "pw1"),
                new BearerCredentials("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ"),
                new DefaultCredentials()
            };

            var initialSize = clientPool.Size;

            foreach (var credentials in credentialSet)
            {
                await clientPool.GetClient(uri, credentials);
            }

            var finalSize = clientPool.Size;

            clientPool.Dispose();

            Assert.Equal(3, finalSize - initialSize);
        }
Пример #2
0
        public async Task HttpClientPoolAllocatesOneHandlerPerUriAndManyOfTheSameCredential()
        {
            var uri        = new Uri("http://localhost/platibus");
            var clientPool = new HttpClientPool();

            var credentialSet = Enumerable.Range(0, 100)
                                .Select(_ => new BasicAuthCredentials("user", "pw"));

            var initialSize = clientPool.Size;

            foreach (var credentials in credentialSet)
            {
                await clientPool.GetClient(uri, credentials);
            }

            var finalSize = clientPool.Size;

            clientPool.Dispose();

            Assert.Equal(1, finalSize - initialSize);
        }
Пример #3
0
        public async Task MultipleRequestsToSameEndpointWithNtlmShouldNotFail()
        {
            var uri                  = new Uri("http://localhost:52179/platibus/");
            var clientPool           = new HttpClientPool();
            var testCompletionSource = new TaskCompletionSource <int>();
            var testComplete         = testCompletionSource.Task;
            var httpListener         = new HttpListener
            {
                AuthenticationSchemes = AuthenticationSchemes.Ntlm,
                Prefixes = { uri.ToString() }
            };

            Task httpListenerLoop = null;

            try
            {
                httpListener.Start();
                httpListenerLoop = Task.Run(async() =>
                {
                    while (httpListener.IsListening)
                    {
                        var contextReceipt = httpListener.GetContextAsync();
                        if (await Task.WhenAny(testComplete, contextReceipt) == testComplete)
                        {
                            break;
                        }
                        var context = await contextReceipt;
                        await context.Request.InputStream.CopyToAsync(context.Response.OutputStream);
                        context.Response.StatusCode = 200;
                        context.Response.Close();
                    }
                });

                var clientTasks = Enumerable.Range(1, 10)
                                  .Select(i => Task.Run(async() =>
                {
                    // ReSharper disable once AccessToDisposedClosure
                    using (var client = await clientPool.GetClient(uri, new DefaultCredentials()))
                    {
                        var content  = "test" + (i + 1);
                        var response = await client.PostAsync("test", new StringContent(content));
                        Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                        Assert.Equal(content, await response.Content.ReadAsStringAsync());
                        return(response);
                    }
                }));

                var responses = await Task.WhenAll(clientTasks);

                Assert.Equal(10, responses.Length);
            }
            finally
            {
                if (httpListenerLoop != null)
                {
                    testCompletionSource.TrySetResult(0);
                    httpListenerLoop.Wait(TimeSpan.FromSeconds(5));
                }

                if (httpListener.IsListening)
                {
                    httpListener.Stop();
                }
                httpListener.Close();
                clientPool.Dispose();
            }
        }