未验证 提交 6830ba14 编写于 作者: G Geoff Kizer 提交者: GitHub

small improvements to HTTP Cancellation Tests and rename (#54018)

* improve HttpClientHandler_Http11_Cancellation_Test and rename to SocketsHttpHandler_Http11_Cancellation_Test
Co-authored-by: NGeoffrey Kizer <geoffrek@windows.microsoft.com>
上级 627fe8c9
......@@ -63,10 +63,10 @@ protected Http3LoopbackServer CreateHttp3LoopbackServer(Http3Options options = d
CreateHttpClientHandler(Version.Parse(useVersionString));
protected static object GetUnderlyingSocketsHttpHandler(HttpClientHandler handler)
protected static SocketsHttpHandler GetUnderlyingSocketsHttpHandler(HttpClientHandler handler)
{
FieldInfo field = typeof(HttpClientHandler).GetField("_underlyingHandler", BindingFlags.Instance | BindingFlags.NonPublic);
return field?.GetValue(handler);
return (SocketsHttpHandler)field?.GetValue(handler);
}
protected static HttpRequestMessage CreateRequest(HttpMethod method, Uri uri, Version version, bool exactVersion = false) =>
......
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Test.Common;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
......@@ -15,45 +11,69 @@
namespace System.Net.Http.Functional.Tests
{
public abstract class HttpClientHandler_Http11_Cancellation_Test : HttpClientHandler_Cancellation_Test
public abstract class SocketsHttpHandler_Cancellation_Test : HttpClientHandler_Cancellation_Test
{
protected HttpClientHandler_Http11_Cancellation_Test(ITestOutputHelper output) : base(output) { }
protected SocketsHttpHandler_Cancellation_Test(ITestOutputHelper output) : base(output) { }
private async Task ValidateConnectTimeout(HttpMessageInvoker invoker, Uri uri, int minElapsed, int maxElapsed)
{
var sw = Stopwatch.StartNew();
await Assert.ThrowsAnyAsync<OperationCanceledException>(() =>
invoker.SendAsync(TestAsync, new HttpRequestMessage(HttpMethod.Get, uri) { Version = UseVersion }, default));
sw.Stop();
Assert.InRange(sw.ElapsedMilliseconds, minElapsed, maxElapsed);
}
[OuterLoop]
[Fact]
public async Task ConnectTimeout_TimesOutSSLAuth_Throws()
{
var releaseServer = new TaskCompletionSource();
await LoopbackServer.CreateClientAndServerAsync(async uri =>
await LoopbackServerFactory.CreateClientAndServerAsync(async uri =>
{
using (var handler = new SocketsHttpHandler())
using (var invoker = new HttpMessageInvoker(handler))
{
handler.ConnectTimeout = TimeSpan.FromSeconds(1);
var sw = Stopwatch.StartNew();
await Assert.ThrowsAnyAsync<OperationCanceledException>(() =>
invoker.SendAsync(TestAsync, new HttpRequestMessage(HttpMethod.Get,
new UriBuilder(uri) { Scheme = "https" }.ToString()) { Version = UseVersion }, default));
sw.Stop();
await ValidateConnectTimeout(invoker, new UriBuilder(uri) { Scheme = "https" }.Uri, 500, 85_000);
Assert.InRange(sw.ElapsedMilliseconds, 500, 85_000);
releaseServer.SetResult();
}
}, server => releaseServer.Task); // doesn't establish SSL connection
}
[OuterLoop]
[Fact]
public async Task ConnectTimeout_ConnectCallbackTimesOut_Throws()
{
await LoopbackServerFactory.CreateClientAndServerAsync(async uri =>
{
using (var handler = CreateHttpClientHandler())
using (var invoker = new HttpMessageInvoker(handler))
{
var socketsHandler = GetUnderlyingSocketsHttpHandler(handler);
socketsHandler.ConnectTimeout = TimeSpan.FromSeconds(1);
socketsHandler.ConnectCallback = async (context, token) => { await Task.Delay(-1, token); return null; };
await ValidateConnectTimeout(invoker, uri, 500, 85_000);
}
}, server => Task.CompletedTask); // doesn't actually connect to server
}
[OuterLoop("Incurs significant delay")]
[Fact]
public async Task Expect100Continue_WaitsExpectedPeriodOfTimeBeforeSendingContent()
{
await LoopbackServer.CreateClientAndServerAsync(async uri =>
await LoopbackServerFactory.CreateClientAndServerAsync(async uri =>
{
using (var handler = new SocketsHttpHandler())
using (var handler = CreateHttpClientHandler())
using (var invoker = new HttpMessageInvoker(handler))
{
var socketsHandler = GetUnderlyingSocketsHttpHandler(handler);
TimeSpan delay = TimeSpan.FromSeconds(3);
handler.Expect100ContinueTimeout = delay;
socketsHandler.Expect100ContinueTimeout = delay;
var tcs = new TaskCompletionSource<bool>();
var content = new SetTcsContent(new MemoryStream(new byte[1]), tcs);
......@@ -69,9 +89,7 @@ public async Task Expect100Continue_WaitsExpectedPeriodOfTimeBeforeSendingConten
{
await server.AcceptConnectionAsync(async connection =>
{
await connection.ReadRequestHeaderAsync();
await connection.ReadAsync(new byte[1], 0, 1);
await connection.SendResponseAsync();
await connection.HandleRequestAsync();
});
});
}
......
......@@ -1030,9 +1030,9 @@ public sealed class SocketsHttpHandlerTest_Cookies_Http11 : HttpClientHandlerTes
}
[ConditionalClass(typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))]
public sealed class SocketsHttpHandler_HttpClientHandler_Cancellation_Test : HttpClientHandler_Http11_Cancellation_Test
public sealed class SocketsHttpHandler_HttpClientHandler_Http11_Cancellation_Test : SocketsHttpHandler_Cancellation_Test
{
public SocketsHttpHandler_HttpClientHandler_Cancellation_Test(ITestOutputHelper output) : base(output) { }
public SocketsHttpHandler_HttpClientHandler_Http11_Cancellation_Test(ITestOutputHelper output) : base(output) { }
[Fact]
public void ConnectTimeout_Default()
......@@ -3070,7 +3070,7 @@ public sealed class SocketsHttpHandlerTest_HttpClientHandlerTest_Headers_Http2 :
}
[ConditionalClass(typeof(PlatformDetection), nameof(PlatformDetection.SupportsAlpn))]
public sealed class SocketsHttpHandler_HttpClientHandler_Cancellation_Test_Http2 : HttpClientHandler_Cancellation_Test
public sealed class SocketsHttpHandler_HttpClientHandler_Cancellation_Test_Http2 : SocketsHttpHandler_Cancellation_Test
{
public SocketsHttpHandler_HttpClientHandler_Cancellation_Test_Http2(ITestOutputHelper output) : base(output) { }
protected override Version UseVersion => HttpVersion.Version20;
......@@ -3144,7 +3144,7 @@ public sealed class SocketsHttpHandlerTest_HttpClientHandlerTest_Headers_Http3_M
// TODO: Many cancellation tests are failing for HTTP3.
[ActiveIssue("https://github.com/dotnet/runtime/issues/53093")]
[ConditionalClass(typeof(HttpClientHandlerTestBase), nameof(IsMsQuicSupported))]
public sealed class SocketsHttpHandler_HttpClientHandler_Cancellation_Test_Http3_MsQuic : HttpClientHandler_Cancellation_Test
public sealed class SocketsHttpHandler_HttpClientHandler_Cancellation_Test_Http3_MsQuic : SocketsHttpHandler_Cancellation_Test
{
public SocketsHttpHandler_HttpClientHandler_Cancellation_Test_Http3_MsQuic(ITestOutputHelper output) : base(output) { }
protected override Version UseVersion => HttpVersion.Version30;
......@@ -3153,7 +3153,7 @@ public sealed class SocketsHttpHandler_HttpClientHandler_Cancellation_Test_Http3
[ActiveIssue("https://github.com/dotnet/runtime/issues/53093")]
[ConditionalClass(typeof(HttpClientHandlerTestBase), nameof(IsMsQuicSupported))]
public sealed class SocketsHttpHandler_HttpClientHandler_Cancellation_Test_Http3_Mock : HttpClientHandler_Cancellation_Test
public sealed class SocketsHttpHandler_HttpClientHandler_Cancellation_Test_Http3_Mock : SocketsHttpHandler_Cancellation_Test
{
public SocketsHttpHandler_HttpClientHandler_Cancellation_Test_Http3_Mock(ITestOutputHelper output) : base(output) { }
protected override Version UseVersion => HttpVersion.Version30;
......
......@@ -83,7 +83,7 @@ public sealed class SyncHttpHandlerTest_Cookies_Http11 : HttpClientHandlerTest_C
}
[ConditionalClass(typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))]
public sealed class SyncHttpHandler_HttpClientHandler_Cancellation_Test : HttpClientHandler_Http11_Cancellation_Test
public sealed class SyncHttpHandler_HttpClientHandler_Cancellation_Test : SocketsHttpHandler_Cancellation_Test
{
public SyncHttpHandler_HttpClientHandler_Cancellation_Test(ITestOutputHelper output) : base(output) { }
protected override bool TestAsync => false;
......
......@@ -124,7 +124,7 @@
<Compile Include="$(CommonTestPath)System\Net\Http\HttpClientHandlerTest.DefaultProxyCredentials.cs"
Link="Common\System\Net\Http\HttpClientHandlerTest.DefaultProxyCredentials.cs" />
<Compile Include="HttpClientHandlerTest.AltSvc.cs" />
<Compile Include="HttpClientHandlerTest.Cancellation.cs" />
<Compile Include="SocketsHttpHandlerTest.Cancellation.cs" />
<Compile Include="HttpClientHandlerTest.Connect.cs" />
<Compile Include="HttpClientHandlerTest.Finalization.cs" />
<Compile Include="HttpClientHandlerTest.Headers.cs" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册