Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set property to enable streaming in WASM and gRPC-Web #872

Merged
merged 1 commit into from
Apr 20, 2020
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.
Jump to
Jump to file
Failed to load files.
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