dotnet / dotnet/aspnetcore

When using the `WebApplicationFactory`'s `HttpClient` the resulting `RequestMessage`'s contents are empty

Open
#49,537 1 comment 0 reactions 0 assignees View on GitHub
area-networking
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

When using the `WebApplicationFactory`'s `HttpClient` the resulting `RequestMessage`'s contents are empty.

```csharp
await using var factory = new WebApplicationFactory();

var response = await factory.CreateClient().PostAsJsonAsync(
"/",
new CreatePostRequest("some message"));

// 👇This is empty
var requestContents = await response.RequestMessage.Content.ReadAsStringAsync();
```

### Expected Behavior

I expected it to match the behavior when calling the same endpoint using a new `HttpClient`

```csharp
var response = await new HttpClient().PostAsJsonAsync(
"http://localhost:5000/",
new CreatePostRequest("some message"));

// 👇This is {"message":"some message"}
var requestContents = await response.RequestMessage.Content.ReadAsStringAsync();
```

### Steps To Reproduce

I have created a minimalistic project which reproduces this issue here:
https://github.com/michael-wolfenden/integration-testing-issue

### Exceptions (if any)

_No response_

### .NET Version

.net 7

### Anything else?

I suspect the issue may be due to these lines of code, as skipping the if branch and executing the else branch causes the tests to pass.

https://github.com/dotnet/aspnetcore/blob/4161f16141a984ff7e4faaebe0ebe8623a3a178f/src/Hosting/TestHost/src/ClientHandler.cs#L88C0-L100C18

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.