Ejemplo n.º 1
0
        public async Task Echo(string scheme)
        {
            var hostBuilder = new HostBuilder()
                              .ConfigureWebHost(webHostBuilder =>
            {
                ConfigureKestrel(webHostBuilder, scheme);
                webHostBuilder.ConfigureServices(AddTestLogging)
                .Configure(app => app.Run(async context =>
                {
                    await context.Request.BodyReader.CopyToAsync(context.Response.BodyWriter).DefaultTimeout();
                }));
            });

            using var host = await hostBuilder.StartAsync().DefaultTimeout();

            var url = host.MakeUrl(scheme);

            using var client = CreateClient();
            client.DefaultRequestHeaders.ExpectContinue = true;

            using var request  = CreateRequestMessage(HttpMethod.Post, url, new BulkContent());
            using var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead).DefaultTimeout();

            Assert.Equal(HttpVersion.Version20, response.Version);
            await BulkContent.VerifyContent(await response.Content.ReadAsStreamAsync().DefaultTimeout());

            await host.StopAsync().DefaultTimeout();
        }
Ejemplo n.º 2
0
        public async Task MultiplexEcho(string scheme)
        {
            var requestsReceived    = 0;
            var requestCount        = 10;
            var allRequestsReceived = new TaskCompletionSource <int>(TaskCreationOptions.RunContinuationsAsynchronously);
            var hostBuilder         = new HostBuilder()
                                      .ConfigureWebHost(webHostBuilder =>
            {
                ConfigureKestrel(webHostBuilder, scheme);
                webHostBuilder.ConfigureServices(AddTestLogging)
                .Configure(app => app.Run(async context =>
                {
                    if (Interlocked.Increment(ref requestsReceived) == requestCount)
                    {
                        allRequestsReceived.SetResult(0);
                    }
                    await allRequestsReceived.Task;
                    await context.Request.BodyReader.CopyToAsync(context.Response.BodyWriter).DefaultTimeout();
                }));
            });

            using var host = await hostBuilder.StartAsync().DefaultTimeout();

            var url = host.MakeUrl(scheme);

            using var client = CreateClient();
            client.DefaultRequestHeaders.ExpectContinue = true;

            var requestTasks = new List <Task>(requestCount);

            for (var i = 0; i < requestCount; i++)
            {
                requestTasks.Add(RunRequest(url));
            }

            async Task RunRequest(string url)
            {
                using var request  = CreateRequestMessage(HttpMethod.Post, url, new BulkContent());
                using var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead).DefaultTimeout();

                Assert.Equal(HttpVersion.Version20, response.Version);
                await BulkContent.VerifyContent(await response.Content.ReadAsStreamAsync().DefaultTimeout());
            };

            await Task.WhenAll(requestTasks);

            await host.StopAsync().DefaultTimeout();
        }