Beispiel #1
0
        public async Task POST_ServerCompletesWithoutReadingRequestBody_ClientGetsResponse()
        {
            // Arrange
            var builder = CreateHostBuilder(async context =>
            {
                var body = context.Request.Body;

                var data = await body.ReadAtLeastLengthAsync(TestData.Length).DefaultTimeout();

                await context.Response.Body.WriteAsync(data);
            });

            using (var host = builder.Build())
                using (var client = CreateClient())
                {
                    await host.StartAsync();

                    var requestContent = new StreamingHttpContext();

                    var request = new HttpRequestMessage(HttpMethod.Post, $"https://127.0.0.1:{host.GetPort()}/");
                    request.Content       = requestContent;
                    request.Version       = HttpVersion.Version30;
                    request.VersionPolicy = HttpVersionPolicy.RequestVersionExact;

                    // Act
                    var responseTask = client.SendAsync(request, CancellationToken.None);

                    var requestStream = await requestContent.GetStreamAsync();

                    // Send headers
                    await requestStream.FlushAsync();

                    // Write content
                    await requestStream.WriteAsync(TestData);

                    var response = await responseTask;

                    // Assert
                    response.EnsureSuccessStatusCode();
                    Assert.Equal(HttpVersion.Version30, response.Version);
                    var responseText = await response.Content.ReadAsStringAsync();

                    Assert.Equal("Hello world", responseText);

                    await host.StopAsync();
                }
        }
Beispiel #2
0
        public async Task POST_ClientCancellationUpload_RequestAbortRaised(HttpProtocols protocol)
        {
            // Arrange
            var syncPoint     = new SyncPoint();
            var cancelledTcs  = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
            var readAsyncTask = new TaskCompletionSource <Task>(TaskCreationOptions.RunContinuationsAsynchronously);

            var builder = CreateHostBuilder(async context =>
            {
                context.RequestAborted.Register(() =>
                {
                    Logger.LogInformation("Server received cancellation");
                    cancelledTcs.SetResult();
                });

                var body = context.Request.Body;

                Logger.LogInformation("Server reading content");
                await body.ReadAtLeastLengthAsync(TestData.Length).DefaultTimeout();

                // Sync with client
                await syncPoint.WaitToContinue();

                Logger.LogInformation("Server waiting for cancellation");
                await cancelledTcs.Task;

                readAsyncTask.SetResult(body.ReadAsync(new byte[1024]).AsTask());
            }, protocol: protocol);

            using (var host = builder.Build())
                using (var client = CreateClient())
                {
                    await host.StartAsync().DefaultTimeout();

                    var cts            = new CancellationTokenSource();
                    var requestContent = new StreamingHttpContext();

                    var request = new HttpRequestMessage(HttpMethod.Post, $"https://127.0.0.1:{host.GetPort()}/");
                    request.Content       = requestContent;
                    request.Version       = GetProtocol(protocol);
                    request.VersionPolicy = HttpVersionPolicy.RequestVersionExact;

                    // Act
                    var responseTask = client.SendAsync(request, cts.Token);

                    var requestStream = await requestContent.GetStreamAsync().DefaultTimeout();

                    Logger.LogInformation("Client sending request headers");
                    await requestStream.FlushAsync().DefaultTimeout();

                    Logger.LogInformation("Client sending request content");
                    await requestStream.WriteAsync(TestData).DefaultTimeout();

                    await requestStream.FlushAsync().DefaultTimeout();

                    Logger.LogInformation("Client waiting until content is read on server");
                    await syncPoint.WaitForSyncPoint().DefaultTimeout();

                    Logger.LogInformation("Client cancelling");
                    cts.Cancel();

                    // Continue on server
                    syncPoint.Continue();

                    // Assert
                    await Assert.ThrowsAnyAsync <OperationCanceledException>(() => responseTask).DefaultTimeout();

                    await cancelledTcs.Task.DefaultTimeout();

                    var serverWriteTask = await readAsyncTask.Task.DefaultTimeout();

                    await Assert.ThrowsAnyAsync <Exception>(() => serverWriteTask).DefaultTimeout();

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