Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Grpc.Net.Client.Web/GrpcWebHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ namespace Grpc.Net.Client.Web
/// </remarks>
public sealed class GrpcWebHandler : DelegatingHandler
{
internal const string WebAssemblyEnableStreamingResponseKey = "WebAssemblyEnableStreamingResponse";

/// <summary>
/// Gets or sets the HTTP version to use when making gRPC-Web calls.
/// <para>
Expand Down Expand Up @@ -114,6 +116,16 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques
private async Task<HttpResponseMessage> SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
{
request.Content = new GrpcWebRequestContent(request.Content, GrpcWebMode);

// Set WebAssemblyEnableStreamingResponse to true on gRPC-Web request.
// https://github.com/mono/mono/blob/a0d69a4e876834412ba676f544d447ec331e7c01/sdks/wasm/framework/src/System.Net.Http.WebAssemblyHttpHandler/WebAssemblyHttpHandler.cs#L149
//
// This must be set so WASM will stream the response. Without this setting the WASM HTTP handler will only
// return content once the entire response has been downloaded. This breaks server streaming.
//
// https://github.com/mono/mono/issues/18718
request.Properties[WebAssemblyEnableStreamingResponseKey] = true;

if (HttpVersion != null)
{
// This doesn't guarantee that the specified version is used. Some handlers will ignore it.
Expand Down
51 changes: 51 additions & 0 deletions test/Grpc.Net.Client.Tests/Web/GrpcWebHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,64 @@ public async Task HttpVersion_Set_HttpRequestMessageVersionChanged()
Assert.AreEqual(HttpVersion.Version20, response.Version);
}

[Test]
public async Task SendAsync_GrpcCall_ResponseStreamingPropertySet()
{
// Arrange
var request = new HttpRequestMessage
{
Version = HttpVersion.Version20,
Content = new ByteArrayContent(Array.Empty<byte>())
{
Headers = { ContentType = new MediaTypeHeaderValue("application/grpc") }
}
};
var testHttpHandler = new TestHttpHandler();
var grpcWebHandler = new GrpcWebHandler(GrpcWebMode.GrpcWeb, testHttpHandler);
var messageInvoker = new HttpMessageInvoker(grpcWebHandler);

// Act
await messageInvoker.SendAsync(request, CancellationToken.None);

// Assert
Assert.AreEqual(true, testHttpHandler.WebAssemblyEnableStreamingResponse);
}

[Test]
public async Task SendAsync_NonGrpcCall_ResponseStreamingPropertyNotSet()
{
// Arrange
var request = new HttpRequestMessage
{
Version = HttpVersion.Version20,
Content = new ByteArrayContent(Array.Empty<byte>())
{
Headers = { ContentType = new MediaTypeHeaderValue("application/text") }
}
};
var testHttpHandler = new TestHttpHandler();
var grpcWebHandler = new GrpcWebHandler(GrpcWebMode.GrpcWeb, testHttpHandler);
var messageInvoker = new HttpMessageInvoker(grpcWebHandler);

// Act
await messageInvoker.SendAsync(request, CancellationToken.None);

// Assert
Assert.AreEqual(null, testHttpHandler.WebAssemblyEnableStreamingResponse);
}

private class TestHttpHandler : HttpMessageHandler
{
public Version? RequestVersion { get; private set; }
public bool? WebAssemblyEnableStreamingResponse { get; private set; }

protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
RequestVersion = request.Version;
if (request.Properties.TryGetValue(GrpcWebHandler.WebAssemblyEnableStreamingResponseKey, out var enableStreaming))
{
WebAssemblyEnableStreamingResponse = (bool)enableStreaming;
}

return Task.FromResult(new HttpResponseMessage()
{
Expand Down