Clearing up confusion when using WebApplicationFactory and TestServer
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 290
Description
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Is your feature request related to a problem? Please describe the problem.
There are two ways to send HTTP requests to [WebApplicationFactory](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.testing.webapplicationfactory-1).
One is to use [WebApplicationFactory.CreateClient](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.testing.webapplicationfactory-1.createclient) to obtain an `HttpClient`, which will result in an `HttpClient` that reflects the configuration by [ConfigureClient](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.testing.webapplicationfactory-1.configureclient).
The other is to use [Server](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.testing.webapplicationfactory-1.server) to obtain a [TestServer](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.testhost.testserver), and then use [TestServer.CreateClient](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.testhost.testserver.createclient) to obtain an `HttpClient`. This method has high-level functions such as [RequestBuilder](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.testhost.requestbuilder), but does not reflect the configuration by `WebApplicationFactory.ConfigureClient`.
I was using `ConfigureClient` to configure the client, but when I switched to using `RequestBuilder`, I was confused because that configuration did not work.
Also, `TestServer` has [CreateWebSocketClient](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.testhost.testserver.createwebsocketclient), but `WebApplicationFactory` does not.
Thus, since `WebApplicationFactory` exposes only a portion of `TestServer`'s functionality, developers may need to use `TestServer` directly.
### Describe the solution you'd like
I don't have a clear idea of what exactly should be done.
Maybe functions such as `RequestBuilder` or `WebSocketClient` should be exposed through `WebApplicationFactory`, so that their configuration by `ConfigureClient` is reflected.
Also, there could be documentation or an analyzer that warns against using `ConfigureClient` and `TestServer` together.
I am aware that the `WebApplicationFactory` is [currently being refactored](https://github.com/dotnet/aspnetcore/issues/60370).
I would appreciate it if you would also consider a fix to clarify this type of confusion.
### Additional context
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.