mirror of
https://github.com/BepInEx/dotnet-runtime.git
synced 2026-11-25 07:06:20 -10:00
Commit migrated from https://github.com/dotnet/corefx/commit/b26e2a66e5c41c3263caf5fdaa8a1811dd360e33
64 lines
2.9 KiB
C#
64 lines
2.9 KiB
C#
// Licensed to the .NET Foundation under one or more agreements.
|
|
// The .NET Foundation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using System.Diagnostics;
|
|
using System.Reflection;
|
|
|
|
namespace System.Net.Http.Functional.Tests
|
|
{
|
|
public abstract class HttpClientHandlerTestBase : RemoteExecutorTestBase
|
|
{
|
|
protected virtual bool UseSocketsHttpHandler => true;
|
|
|
|
protected bool IsWinHttpHandler => !UseSocketsHttpHandler && PlatformDetection.IsWindows && !PlatformDetection.IsUap && !PlatformDetection.IsFullFramework;
|
|
protected bool IsCurlHandler => !UseSocketsHttpHandler && !PlatformDetection.IsWindows;
|
|
protected bool IsNetfxHandler => PlatformDetection.IsWindows && PlatformDetection.IsFullFramework;
|
|
protected bool IsUapHandler => PlatformDetection.IsWindows && PlatformDetection.IsUap;
|
|
|
|
protected bool BackendSupportsAlpn => PlatformDetection.SupportsAlpn;
|
|
|
|
protected HttpClient CreateHttpClient() => new HttpClient(CreateHttpClientHandler());
|
|
|
|
protected HttpClientHandler CreateHttpClientHandler() => CreateHttpClientHandler(UseSocketsHttpHandler);
|
|
|
|
protected static HttpClient CreateHttpClient(string useSocketsHttpHandlerBoolString) =>
|
|
new HttpClient(CreateHttpClientHandler(useSocketsHttpHandlerBoolString));
|
|
|
|
protected static HttpClientHandler CreateHttpClientHandler(string useSocketsHttpHandlerBoolString) =>
|
|
CreateHttpClientHandler(bool.Parse(useSocketsHttpHandlerBoolString));
|
|
|
|
protected static HttpClientHandler CreateHttpClientHandler(bool useSocketsHttpHandler)
|
|
{
|
|
HttpClientHandler handler;
|
|
|
|
if (PlatformDetection.IsUap || PlatformDetection.IsFullFramework || useSocketsHttpHandler)
|
|
{
|
|
handler = new HttpClientHandler();
|
|
}
|
|
else
|
|
{
|
|
// Create platform specific handler.
|
|
ConstructorInfo ctor = typeof(HttpClientHandler).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(bool) }, null);
|
|
Debug.Assert(ctor != null, "Couldn't find test constructor on HttpClientHandler");
|
|
|
|
handler = (HttpClientHandler)ctor.Invoke(new object[] { useSocketsHttpHandler });
|
|
Debug.Assert(useSocketsHttpHandler == IsSocketsHttpHandler(handler), "Unexpected handler.");
|
|
}
|
|
|
|
TestHelper.EnsureHttp2Feature(handler);
|
|
|
|
return handler;
|
|
}
|
|
|
|
protected static bool IsSocketsHttpHandler(HttpClientHandler handler) =>
|
|
GetUnderlyingSocketsHttpHandler(handler) != null;
|
|
|
|
protected static object GetUnderlyingSocketsHttpHandler(HttpClientHandler handler)
|
|
{
|
|
FieldInfo field = typeof(HttpClientHandler).GetField("_socketsHttpHandler", BindingFlags.Instance | BindingFlags.NonPublic);
|
|
return field?.GetValue(handler);
|
|
}
|
|
}
|
|
}
|