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 ContentLength for TestHost #24107

Merged
merged 5 commits into from
Jul 30, 2020
Merged

Conversation

Kahbazi
Copy link
Member

@Kahbazi Kahbazi commented Jul 20, 2020

Fixes #18463

@ghost ghost added the area-hosting label Jul 20, 2020
@Kahbazi Kahbazi marked this pull request as ready for review July 20, 2020 13:07
@Kahbazi Kahbazi requested a review from Tratcher as a code owner July 20, 2020 13:07
Comment on lines 120 to 126
else if (string.Equals(header.Key, HeaderNames.ContentLength, StringComparison.OrdinalIgnoreCase))
{
if (long.TryParse(header.Value.First(), out var contentLength))
{
req.ContentLength = contentLength;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

This shouldn't have affected the behavior, the Asp.Net ContentLength property is already read from the header. You should get the same test results if you remove this section. Does the test fail before adding this and pass after?

The expected workaround was reading System.Net.Http's ContentLength property because it has the side-effect of generating the header. #18463 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

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

Does the test fail before adding this and pass after?

Yes, but I changed the code as you said. Reading the property would set the header. Now if there's no body on the request the ContentLength is Zero. Is this the expected behavior?

@@ -144,6 +144,10 @@ internal ClientHandler(PathString pathBase, ApplicationWrapper application)

if (requestContent != null)
Copy link
Member

Choose a reason for hiding this comment

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

This code's a bit outdated, requestContent is never null, it's always set on line 69.

{
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?

Copy link
Member

@Tratcher Tratcher left a comment

Choose a reason for hiding this comment

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

I expect we'll churn on this more as part of #24175, but this is good for now.

@Tratcher Tratcher merged commit d0cca51 into dotnet:master Jul 30, 2020
@Tratcher
Copy link
Member

Thanks

@Tratcher Tratcher added this to the 5.0.0-rc1 milestone Jul 30, 2020
@Kahbazi Kahbazi deleted the kahbazi/TestServer branch July 31, 2020 03:50
@amcasey amcasey added the area-hosting Includes Hosting label Jun 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-hosting Includes Hosting
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HttpRequest.ContentLength is null when using TestHost
4 participants