Skip to content

Commit

Permalink
Add GrpcWebHandler test
Browse files Browse the repository at this point in the history
  • Loading branch information
lemaitre-aneo committed May 23, 2024
1 parent 1a440c0 commit ae179f2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ jobs:
runtime: windows-x64
- os: windows-2022
runtime: windows-x64
handler:
- ''
include:
- dotnet:
version: ''
handler: GrpcWebHandler
exclude:
- dotnet:
version: ''
Expand Down Expand Up @@ -87,6 +93,8 @@ jobs:
- name: Test
working-directory: packages/csharp/
shell: bash
env:
GrpcClient__HttpMessageHandler: ${{ matrix.handler }}
run: |
set +e
set -x
Expand Down Expand Up @@ -122,6 +130,6 @@ jobs:
uses: dorny/test-reporter@v1
if: always()
with:
name: ConnectivityTests ${{ matrix.platform.os }} ${{ matrix.dotnet.framework }}
name: ConnectivityTests ${{ matrix.platform.os }} ${{ matrix.dotnet.framework }} ${{ matrix.handler }}
path: ./packages/csharp/ArmoniK.Api.Client.Test/TestResults/test-results.trx
reporter: dotnet-trx
7 changes: 6 additions & 1 deletion packages/csharp/ArmoniK.Api.Client.Test/ConnectivityKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ internal static class ConnectivityKindExt
private static string CertFolder
=> Environment.GetEnvironmentVariable("CertFolder") ?? "../../../../certs";

private static string MessageHandler
=> Environment.GetEnvironmentVariable("GrpcClient__HttpMessageHandler") ?? "";

internal static bool IsTls(this ConnectivityKind kind)
=> kind switch
{
Expand Down Expand Up @@ -90,7 +93,8 @@ internal static (string?, string?) GetClientCertPath(this ConnectivityKind kind)
internal static string GetEndpoint(this ConnectivityKind kind)
=> kind switch
{
ConnectivityKind.Unencrypted => RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework")
ConnectivityKind.Unencrypted => RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework") || MessageHandler.ToLower()
.Contains("web")
? "http://localhost:4999"
: "http://localhost:5000",
ConnectivityKind.TlsInsecure => "https://localhost:5001",
Expand All @@ -113,6 +117,7 @@ internal static GrpcChannel GetChannel(this ConnectivityKind kind)
CertPem = certPath ?? "",
KeyPem = keyPath ?? "",
CaCert = kind.GetCaCertPath() ?? "",
HttpMessageHandler = MessageHandler,
});
}
}
10 changes: 10 additions & 0 deletions packages/csharp/ArmoniK.Api.Client/Options/GrpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,15 @@ public bool HasClientCertificate
/// Timeout for grpc requests. Defaults to no timeout.
/// </summary>
public TimeSpan RequestTimeout { get; set; } = Timeout.InfiniteTimeSpan;

/// <summary>
/// Which HttpMessageHandler to use.
/// Valid options:
/// - `HttpClientHandler`
/// - `WinHttpHandler`
/// - `GrpcWebHandler`
/// If the handler is not set, the best one will be used.
/// </summary>
public string HttpMessageHandler { get; set; } = "";
}
}
18 changes: 18 additions & 0 deletions packages/csharp/ArmoniK.Api.Client/Submitter/GrpcChannelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,14 @@ public static GrpcChannel CreateChannel(GrpcClient optionsGrpcClient,
logger?.LogWarning("OverrideTargetName is not supported");
}

// ReSharper disable once ConvertTypeCheckPatternToNullCheck
if (ParseHandler(optionsGrpcClient.HttpMessageHandler) is HandlerType handlerType)
{
return CreateChannelInternal(optionsGrpcClient,
handlerType,
logger);
}

// If dotnet core (>= Net 5), we can use HttpClientHandler
if (!RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework"))
{
Expand Down Expand Up @@ -489,6 +497,16 @@ public static X509Certificate2 GetCertificate(GrpcClient optionsGrpcClient)
X509KeyStorageFlags.Exportable);
}

private static HandlerType? ParseHandler(string handler)
=> handler.ToLower() switch
{
"" => null,
"httpclienthandler" or "httpclient" or "http" or "client" => HandlerType.Http,
"winhttphandler" or "winhttp" or "win" => HandlerType.Win,
"grpcwebhandler" or "grpcweb" or "web" => HandlerType.Web,
_ => throw new ArgumentException($"Invalid HandlerType: {handler}"),
};

private enum HandlerType
{
/// <summary>
Expand Down

0 comments on commit ae179f2

Please sign in to comment.