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
36 changes: 36 additions & 0 deletions src/libraries/Common/tests/System/Net/Http/ResponseStreamTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,43 @@ await client.GetAsync(remoteServer.EchoUri, HttpCompletionOption.ResponseHeaders
}
}
}

#if NETCOREAPP
[OuterLoop]
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBrowser))]
public async Task BrowserHttpHandler_Streaming()
{
var WebAssemblyEnableStreamingResponseKey = new HttpRequestOptionsKey<bool>("WebAssemblyEnableStreamingResponse");

var size = 1500 * 1024 * 1024;
var req = new HttpRequestMessage(HttpMethod.Get, Configuration.Http.RemoteSecureHttp11Server.BaseUri + "large.ashx?size=" + size);

req.Options.Set(WebAssemblyEnableStreamingResponseKey, true);

using (HttpClient client = CreateHttpClientForRemoteServer(Configuration.Http.RemoteSecureHttp11Server))
// we need to switch off Response buffering of default ResponseContentRead option
using (HttpResponseMessage response = await client.SendAsync(req, HttpCompletionOption.ResponseHeadersRead))
{
Assert.Equal(typeof(StreamContent), response.Content.GetType());

Assert.Equal("application/octet-stream", response.Content.Headers.ContentType.MediaType);
Assert.True(size == response.Content.Headers.ContentLength, "ContentLength");

var stream = await response.Content.ReadAsStreamAsync();
Assert.Equal("ReadOnlyStream", stream.GetType().Name);
var buffer = new byte[1024 * 1024];
int totalCount = 0;
int fetchedCount = 0;
do
{
// with WebAssemblyEnableStreamingResponse option set, we will be using https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultReader/read
fetchedCount = await stream.ReadAsync(buffer, 0, buffer.Length);
totalCount += fetchedCount;
} while (fetchedCount != 0);
Assert.Equal(size, totalCount);
}
}

[Theory]
[InlineData(TransferType.ContentLength, TransferError.ContentLengthTooLarge)]
[InlineData(TransferType.Chunked, TransferError.MissingChunkTerminator)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ public async Task Invoke(HttpContext context)
await TestHandler.InvokeAsync(context);
return;
}
if (path.Equals(new PathString("/large.ashx")))
{
await LargeResponseHandler.InvokeAsync(context);
return;
}

// Default handling.
await EchoHandler.InvokeAsync(context);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

namespace NetCoreServer
{
public class LargeResponseHandler
{
public static async Task InvokeAsync(HttpContext context)
{
RequestHelper.AddResponseCookies(context);

if (!AuthenticationHelper.HandleAuthentication(context))
{
return;
}

// Add original request method verb as a custom response header.
context.Response.Headers.Add("X-HttpRequest-Method", context.Request.Method);

var size = 1024;
if (context.Request.Query.TryGetValue("size", out var value))
{
size = Int32.Parse(value);
}
context.Response.ContentType = "application/octet-stream";
context.Response.ContentLength = size;
const int bufferSize = 1024 * 100;
var bytes = new byte[bufferSize];
Random.Shared.NextBytes(bytes);
var remaining = size;
while (remaining > 0)
{
var send = Math.Min(remaining, bufferSize);
await context.Response.Body.WriteAsync(bytes, 0, send);
remaining -= send;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<Compile Include="Handlers\EchoWebSocketHeadersHandler.cs" />
<Compile Include="Handlers\EmptyContentHandler.cs" />
<Compile Include="Handlers\GZipHandler.cs" />
<Compile Include="Handlers\LargeResponse.cs" />
<Compile Include="Handlers\RedirectHandler.cs" />
<Compile Include="Handlers\StatusCodeHandler.cs" />
<Compile Include="Handlers\TestHandler.cs" />
Expand Down