Skip to content

Commit 97f6fbd

Browse files
authored
Fix detecting inherited WinHttpHandler (#2288)
1 parent 7e62218 commit 97f6fbd

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

src/Shared/HttpRequestHelpers.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#region Copyright notice and license
1+
#region Copyright notice and license
22

33
// Copyright 2019 The gRPC Authors
44
//
@@ -53,7 +53,7 @@ public static bool HasHttpHandlerType(HttpMessageHandler handler, string handler
5353

5454
public static HttpMessageHandler? GetHttpHandlerType(HttpMessageHandler handler, string handlerTypeName)
5555
{
56-
if (handler?.GetType().FullName == handlerTypeName)
56+
if (IsType(handler.GetType(), handlerTypeName))
5757
{
5858
return handler;
5959
}
@@ -62,8 +62,7 @@ public static bool HasHttpHandlerType(HttpMessageHandler handler, string handler
6262
while (currentHandler is DelegatingHandler delegatingHandler)
6363
{
6464
currentHandler = delegatingHandler.InnerHandler;
65-
66-
if (currentHandler?.GetType().FullName == handlerTypeName)
65+
if (currentHandler != null && IsType(currentHandler.GetType(), handlerTypeName))
6766
{
6867
return currentHandler;
6968
}
@@ -72,6 +71,21 @@ public static bool HasHttpHandlerType(HttpMessageHandler handler, string handler
7271
return null;
7372
}
7473

74+
private static bool IsType(Type type, string handlerTypeName)
75+
{
76+
Type? currentType = type;
77+
do
78+
{
79+
if (currentType.FullName == handlerTypeName)
80+
{
81+
return true;
82+
}
83+
84+
} while ((currentType = currentType.BaseType) != null);
85+
86+
return false;
87+
}
88+
7589
public static bool HasHttpHandlerType<T>(HttpMessageHandler handler) where T : HttpMessageHandler
7690
{
7791
return GetHttpHandlerType<T>(handler) != null;

test/Grpc.Net.Client.Tests/GrpcChannelTests.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -649,8 +649,11 @@ public void WinHttpHandler_UnsupportedWindows_Throw()
649649
"For more information, see https://aka.ms/aspnet/grpc/netframework.");
650650
}
651651

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

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

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

679+
#pragma warning disable CS0436 // Just need to have a type called WinHttpHandler to activate new behavior.
680+
private class WinHttpHandlerInherited : WinHttpHandler
681+
{
682+
public WinHttpHandlerInherited(HttpMessageHandler innerHandler) : base(innerHandler)
683+
{
684+
}
685+
}
686+
#pragma warning restore CS0436
687+
678688
#if SUPPORT_LOAD_BALANCING
679689
[Test]
680690
public void Resolver_SocketHttpHandlerWithConnectCallback_Error()

0 commit comments

Comments
 (0)