Skip to content

Set ContentLength for TestHost #24107

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

Merged
merged 5 commits into from
Jul 30, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/Hosting/TestHost/src/ClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected override async Task<HttpResponseMessage> SendAsync(
if (request.Version == HttpVersion.Version20)
{
// https://tools.ietf.org/html/rfc7540
req.Protocol = HttpProtocol.Http2;
req.Protocol = HttpProtocol.Http2;
}
else
{
Expand Down Expand Up @@ -142,12 +142,13 @@ protected override async Task<HttpResponseMessage> SendAsync(
}
req.QueryString = QueryString.FromUriComponent(request.RequestUri);

if (requestContent != null)
// Reading the ContentLength will add it to the Headers‼
// https://github.com/dotnet/runtime/blob/874399ab15e47c2b4b7c6533cc37d27d47cb5242/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HttpContentHeaders.cs#L68-L87
_ = requestContent.Headers.ContentLength;

foreach (var header in requestContent.Headers)
{
foreach (var header in requestContent.Headers)
{
req.Headers.Append(header.Key, header.Value.ToArray());
}
req.Headers.Append(header.Key, header.Value.ToArray());
}

req.Body = new AsyncStreamWrapper(reader.AsStream(), () => contextBuilder.AllowSynchronousIO);
Expand Down
61 changes: 61 additions & 0 deletions src/Hosting/TestHost/test/ClientHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
Expand Down Expand Up @@ -103,6 +105,51 @@ public Task UserAgentHeaderWorks()
return httpClient.GetAsync("http://example.com");
}

[Fact]
public Task ContentLengthWithBodyWorks()
{
var contentBytes = Encoding.UTF8.GetBytes("This is a content!");
var handler = new ClientHandler(new PathString(""), new DummyApplication(context =>
{
Assert.Equal(contentBytes.LongLength, context.Request.ContentLength);

return Task.CompletedTask;
}));
var httpClient = new HttpClient(handler);
var content = new ByteArrayContent(contentBytes);

return httpClient.PostAsync("http://example.com", content);
}

[Fact]
public Task ContentLengthWithNoBodyWorks()
{
var handler = new ClientHandler(new PathString(""), new DummyApplication(context =>
{
Assert.Equal(0, context.Request.ContentLength);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. Because TestServer defaults to a StreamContent(Stream.Null), and Stream.Null's default length is 0, all requests that don't specify a body will get Content-Length: 0.

var requestContent = request.Content ?? new StreamContent(Stream.Null);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test where the client intentionally sets Transfer-Encoding: chunked? We don't want to automatically add a content-length in that scenario.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting Transfer-Encoding: chunked for ByteArrayContent didn't do anything, the ContentLength was still 0.
But creating a content which return false on TryComputeLength would make ContentLength null. Is this good enough?


return Task.CompletedTask;
}));
var httpClient = new HttpClient(handler);

return httpClient.GetAsync("http://example.com");
}

[Fact]
public Task ContentLengthWithChunkedTransferEncodingWorks()
{
var handler = new ClientHandler(new PathString(""), new DummyApplication(context =>
{
Assert.Null(context.Request.ContentLength);

return Task.CompletedTask;
}));

var httpClient = new HttpClient(handler);

return httpClient.PostAsync("http://example.com", new UnlimitedContent());
}

[Fact]
public async Task ServerTrailersSetOnResponseAfterContentRead()
{
Expand Down Expand Up @@ -566,5 +613,19 @@ public void Dispose()
}
}
}

private class UnlimitedContent : HttpContent
{
protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
{
return Task.CompletedTask;
}

protected override bool TryComputeLength(out long length)
{
length = -1;
return false;
}
}
}
}