Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor WinHttpHandler test to use LoopbackServer.CreateClientAndServer #104400

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -100,36 +100,26 @@ public async Task SendAsync_SlowServerAndCancel_ThrowsTaskCanceledException()
[Fact]
public async Task SendAsync_SlowServerRespondsAfterDefaultReceiveTimeout_ThrowsHttpRequestException()
{
var handler = new WinHttpHandler();
using (var client = new HttpClient(handler))
TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
await LoopbackServer.CreateClientAndServerAsync(async uri =>
{
var triggerResponseWrite = new TaskCompletionSource<bool>();
var triggerRequestWait = new TaskCompletionSource<bool>();

await LoopbackServer.CreateServerAsync(async (server, url) =>
using var client = new HttpClient(new WinHttpHandler());
HttpRequestException ex = await Assert.ThrowsAsync<HttpRequestException>(() => client.GetAsync(uri));
_output.WriteLine($"Exception: {ex}");
liveans marked this conversation as resolved.
Show resolved Hide resolved
Assert.IsType<IOException>(ex.InnerException);
Assert.NotNull(ex.InnerException.InnerException);
Assert.Contains("The operation timed out", ex.InnerException.InnerException.Message);
tcs.TrySetResult(true);
},
async server =>
{
await server.AcceptConnectionAsync(async connection =>
{
Task serverTask = server.AcceptConnectionAsync(async connection =>
{
await connection.SendResponseAsync($"HTTP/1.1 200 OK\r\nContent-Length: 16000\r\n\r\n");

triggerRequestWait.SetResult(true);
await triggerResponseWrite.Task;
});

HttpRequestException ex = await Assert.ThrowsAsync<HttpRequestException>(async () =>
{
Task<HttpResponseMessage> t = client.GetAsync(url);
await triggerRequestWait.Task;
var _ = await t;
});
_output.WriteLine($"ex: {ex}");
Assert.IsType<IOException>(ex.InnerException);
Assert.NotNull(ex.InnerException.InnerException);
Assert.Contains("The operation timed out", ex.InnerException.InnerException.Message);

triggerResponseWrite.SetResult(true);
await connection.ReadRequestDataAsync();
await connection.SendResponseAsync($"HTTP/1.1 200 OK\r\nDate: {DateTimeOffset.UtcNow:R)}\r\nContent-Length: 1000\r\n\r\n");
await tcs.Task;
});
}
});
}

[Fact]
Expand Down
Loading