-
Notifications
You must be signed in to change notification settings - Fork 9.9k
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
Conversation
else if (string.Equals(header.Key, HeaderNames.ContentLength, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
if (long.TryParse(header.Value.First(), out var contentLength)) | ||
{ | ||
req.ContentLength = contentLength; | ||
} | ||
} |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this 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.
Thanks |
Fixes #18463