mirror of
https://github.com/BepInEx/dotnet-runtime.git
synced 2026-11-27 13:19:31 -10:00
The task from ReadAsync was never moving to a terminal state (i.e. "hanging") if the token was marked for cancellation after starting the task. The problem was that our task continuation wasn't being called if the token was cancelled before the continuation was scheduled. The fix is to avoid passing in the token during the .ContinueWith call. Instead, the token is checked inside the continuation. This puts all the logic in the same place for checking the final status of the ReadAsync operation (which has multiple inner operations) including whether it should fault due to an error or have a successful completion. Commit migrated from https://github.com/dotnet/corefx/commit/3ae96358a215023f3ecbba13fb4d860693f221c0
125 lines
4.4 KiB
C#
125 lines
4.4 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
using System;
|
|
using System.IO;
|
|
using System.Net.Tests;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
using Xunit;
|
|
using Xunit.Abstractions;
|
|
|
|
namespace System.Net.Http.Functional.Tests
|
|
{
|
|
public class ResponseStreamTest
|
|
{
|
|
private readonly ITestOutputHelper _output;
|
|
|
|
public ResponseStreamTest(ITestOutputHelper output)
|
|
{
|
|
_output = output;
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetStreamAsync_ReadToEnd_Success()
|
|
{
|
|
HttpClient client = GetHttpClient();
|
|
|
|
Stream stream = await client.GetStreamAsync(HttpTestServers.RemoteEchoServer);
|
|
using (var reader = new StreamReader(stream))
|
|
{
|
|
string responseBody = reader.ReadToEnd();
|
|
_output.WriteLine(responseBody);
|
|
Assert.True(IsValidResponseBody(responseBody));
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetAsync_UseResponseHeadersReadAndCallLoadIntoBuffer_Success()
|
|
{
|
|
HttpClient client = GetHttpClient();
|
|
|
|
HttpResponseMessage response =
|
|
await client.GetAsync(HttpTestServers.RemoteEchoServer, HttpCompletionOption.ResponseHeadersRead);
|
|
await response.Content.LoadIntoBufferAsync();
|
|
|
|
string responseBody = await response.Content.ReadAsStringAsync();
|
|
_output.WriteLine(responseBody);
|
|
Assert.True(IsValidResponseBody(responseBody));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetAsync_UseResponseHeadersReadAndCopyToMemoryStream_Success()
|
|
{
|
|
HttpClient client = GetHttpClient();
|
|
|
|
HttpResponseMessage response =
|
|
await client.GetAsync(HttpTestServers.RemoteEchoServer, HttpCompletionOption.ResponseHeadersRead);
|
|
|
|
var memoryStream = new MemoryStream();
|
|
await response.Content.CopyToAsync(memoryStream);
|
|
memoryStream.Position = 0;
|
|
|
|
using (var reader = new StreamReader(memoryStream))
|
|
{
|
|
string responseBody = reader.ReadToEnd();
|
|
_output.WriteLine(responseBody);
|
|
Assert.True(IsValidResponseBody(responseBody));
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ReadAsStreamAsync_Cancel_TaskIsCanceled()
|
|
{
|
|
var cts = new CancellationTokenSource();
|
|
|
|
using (var client = new HttpClient())
|
|
using (HttpResponseMessage response =
|
|
await client.GetAsync(HttpTestServers.RemoteEchoServer, HttpCompletionOption.ResponseHeadersRead))
|
|
using (Stream stream = await response.Content.ReadAsStreamAsync())
|
|
{
|
|
var buffer = new byte[2048];
|
|
Task task = stream.ReadAsync(buffer, 0, buffer.Length, cts.Token);
|
|
cts.Cancel();
|
|
|
|
// Verify that the task completes successfully or is canceled.
|
|
Assert.True(((IAsyncResult)task).AsyncWaitHandle.WaitOne(new TimeSpan(0, 0, 3)));
|
|
Assert.True(task.Status == TaskStatus.RanToCompletion || task.Status == TaskStatus.Canceled);
|
|
}
|
|
}
|
|
|
|
// These methods help to validate the response body since the endpoint will echo
|
|
// all request headers.
|
|
//
|
|
// TODO: This validation will be improved in the future once the test server endpoint
|
|
// is able to provide a custom response header with a SHA1 hash of the expected response body.
|
|
private readonly string[] CustomHeaderValues = new string[] {"abcdefg", "12345678", "A1B2C3D4"};
|
|
private HttpClient GetHttpClient()
|
|
{
|
|
var client = new HttpClient();
|
|
int ndx = 1;
|
|
foreach (string headerValue in CustomHeaderValues)
|
|
{
|
|
client.DefaultRequestHeaders.Add("X-Custom" + ndx.ToString(), headerValue);
|
|
ndx++;
|
|
}
|
|
|
|
return client;
|
|
}
|
|
|
|
private bool IsValidResponseBody(string responseBody)
|
|
{
|
|
foreach (string headerValue in CustomHeaderValues)
|
|
{
|
|
if (!responseBody.Contains(headerValue))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
}
|