Skip to content

Commit

Permalink
Fix detecting inherited WinHttpHandler (#2288)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK authored Oct 3, 2023
1 parent 7e62218 commit 97f6fbd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
22 changes: 18 additions & 4 deletions src/Shared/HttpRequestHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#region Copyright notice and license
#region Copyright notice and license

// Copyright 2019 The gRPC Authors
//
Expand Down Expand Up @@ -53,7 +53,7 @@ public static bool HasHttpHandlerType(HttpMessageHandler handler, string handler

public static HttpMessageHandler? GetHttpHandlerType(HttpMessageHandler handler, string handlerTypeName)
{
if (handler?.GetType().FullName == handlerTypeName)
if (IsType(handler.GetType(), handlerTypeName))
{
return handler;
}
Expand All @@ -62,8 +62,7 @@ public static bool HasHttpHandlerType(HttpMessageHandler handler, string handler
while (currentHandler is DelegatingHandler delegatingHandler)
{
currentHandler = delegatingHandler.InnerHandler;

if (currentHandler?.GetType().FullName == handlerTypeName)
if (currentHandler != null && IsType(currentHandler.GetType(), handlerTypeName))
{
return currentHandler;
}
Expand All @@ -72,6 +71,21 @@ public static bool HasHttpHandlerType(HttpMessageHandler handler, string handler
return null;
}

private static bool IsType(Type type, string handlerTypeName)
{
Type? currentType = type;
do
{
if (currentType.FullName == handlerTypeName)
{
return true;
}

} while ((currentType = currentType.BaseType) != null);

return false;
}

public static bool HasHttpHandlerType<T>(HttpMessageHandler handler) where T : HttpMessageHandler
{
return GetHttpHandlerType<T>(handler) != null;
Expand Down
20 changes: 15 additions & 5 deletions test/Grpc.Net.Client.Tests/GrpcChannelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -649,8 +649,11 @@ public void WinHttpHandler_UnsupportedWindows_Throw()
"For more information, see https://aka.ms/aspnet/grpc/netframework.");
}

[Test]
public void WinHttpHandler_SupportedWindows_Success()
#pragma warning disable CS0436 // Just need to have a type called WinHttpHandler to activate new behavior.
[TestCase(typeof(WinHttpHandler))]
#pragma warning restore CS0436
[TestCase(typeof(WinHttpHandlerInherited))]
public void WinHttpHandler_SupportedWindows_Success(Type handlerType)
{
// Arrange
var services = new ServiceCollection();
Expand All @@ -660,9 +663,7 @@ public void WinHttpHandler_SupportedWindows_Success()
OSVersion = Version.Parse("10.0.20348.169")
});

#pragma warning disable CS0436 // Just need to have a type called WinHttpHandler to activate new behavior.
var winHttpHandler = new WinHttpHandler(new TestHttpMessageHandler());
#pragma warning restore CS0436
var winHttpHandler = (HttpMessageHandler)Activator.CreateInstance(handlerType, new TestHttpMessageHandler())!;

// Act
var channel = GrpcChannel.ForAddress("https://localhost", new GrpcChannelOptions
Expand All @@ -675,6 +676,15 @@ public void WinHttpHandler_SupportedWindows_Success()
Assert.AreEqual(HttpHandlerType.WinHttpHandler, channel.HttpHandlerType);
}

#pragma warning disable CS0436 // Just need to have a type called WinHttpHandler to activate new behavior.
private class WinHttpHandlerInherited : WinHttpHandler
{
public WinHttpHandlerInherited(HttpMessageHandler innerHandler) : base(innerHandler)
{
}
}
#pragma warning restore CS0436

#if SUPPORT_LOAD_BALANCING
[Test]
public void Resolver_SocketHttpHandlerWithConnectCallback_Error()
Expand Down

0 comments on commit 97f6fbd

Please sign in to comment.