Skip to content

HttpRequest.ContentLength is null when using TestHost #18463

@benmccallum

Description

@benmccallum

Describe the bug

⚠️Update: I'm no longer taking this approach and inspecting the Content-Length header's value, we found a much better way with help from David Fowler, but the issue is still valid. For reproducability, I've branched the commit at the time here.

Apologies if I'm completely wrong here. Over here I'm upgrading a middleware to 3.x and System.Text.Json.

I'm trying to optimize a deserialization from the HttpRequest.Body by pre-allocating a buffer using the shared array pool given HttpRequest.ContentLength. See here. Problem is, when our integration tests run, which use HttpClient.PostAsync(..., new StringContent(...)) to post to a TestHost, the Content-Length header is always null.

Is this is a limitation of the test host, or a bug? I guess I could write a unit test the specific internals of that function.

To Reproduce

  1. Clone PRbranch linked above.
  2. Put a breakpoint in GraphQLHttpMiddleware in the JsonContentType case of the switch statement.
  3. Test Explorer > Samples.Server.Tests (netcoreapp3.1) > right-click "Single_Query..." and Debug.
  4. Breakpoint will be hit and httpRequest.ContentLength will be null.

Further technical details

  • ASP.NET Core version: 3.1.0
  • Include the output of dotnet --info
    image
  • The IDE (VS / VS Code/ VS4Mac) you're running on, and it's version: VS Community 2019 v16.4.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    DoneThis issue has been fixedNeeds: Attention 👋This issue needs the attention of a contributor, typically because the OP has provided an update.area-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractionsenhancementThis issue represents an ask for new feature or an enhancement to an existing onegood first issueGood for newcomers.help wantedUp for grabs. We would accept a PR to help resolve this issue

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions