Class WebApplicationFactoryExtensions
Extension methods for WebApplicationFactory<TEntryPoint> that simplify creating authenticated HTTP clients for integration tests.
public static class WebApplicationFactoryExtensions
- Inheritance
-
WebApplicationFactoryExtensions
- Inherited Members
Methods
CreateClientWithActor<TEntryPoint>(WebApplicationFactory<TEntryPoint>, string, params string[])
Creates an HttpClient with the X-Test-Actor header pre-set,
encoding the specified actor identity and permissions as JSON.
public static HttpClient CreateClientWithActor<TEntryPoint>(this WebApplicationFactory<TEntryPoint> factory, string actorId, params string[] permissions) where TEntryPoint : class
Parameters
factoryWebApplicationFactory<TEntryPoint>The web application factory.
actorIdstringThe unique identifier of the test actor.
permissionsstring[]The permissions granted to the test actor.
Returns
- HttpClient
An HttpClient with the actor header configured.
Type Parameters
TEntryPointThe entry point class of the web application under test.
Examples
var client = _factory.CreateClientWithActor("user-1", Permissions.OrdersCreate, Permissions.OrdersRead);
var response = await client.PostAsync("/api/orders", content);
response.StatusCode.Should().Be(HttpStatusCode.OK);
CreateClientWithActor<TEntryPoint>(WebApplicationFactory<TEntryPoint>, Actor)
Creates an HttpClient with the X-Test-Actor header pre-set,
encoding the full Actor (including forbidden permissions and attributes) as JSON.
public static HttpClient CreateClientWithActor<TEntryPoint>(this WebApplicationFactory<TEntryPoint> factory, Actor actor) where TEntryPoint : class
Parameters
factoryWebApplicationFactory<TEntryPoint>The web application factory.
actorActorThe actor to serialize into the header.
Returns
- HttpClient
An HttpClient with the actor header configured.
Type Parameters
TEntryPointThe entry point class of the web application under test.
CreateClientWithEntraTokenAsync<TEntryPoint>(WebApplicationFactory<TEntryPoint>, MsalTestTokenProvider, string, CancellationToken)
Creates an HttpClient authenticated with a real Azure Entra ID token acquired via MSAL ROPC flow. Use for E2E integration tests against a real Entra test tenant.
[RequiresUnreferencedCode("MSAL uses reflection for token serialization and is not AOT-compatible.")]
public static Task<HttpClient> CreateClientWithEntraTokenAsync<TEntryPoint>(this WebApplicationFactory<TEntryPoint> factory, MsalTestTokenProvider tokenProvider, string testUserName, CancellationToken cancellationToken = default) where TEntryPoint : class
Parameters
factoryWebApplicationFactory<TEntryPoint>The web application factory.
tokenProviderMsalTestTokenProviderThe MSAL token provider configured with test tenant credentials. Create one instance per test class/fixture to benefit from token caching.
testUserNamestringThe key in TestUsers identifying the test user.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<HttpClient>
An HttpClient with the Authorization Bearer header set.
Type Parameters
TEntryPointThe entry point class of the web application under test.
Examples
var tokenProvider = new MsalTestTokenProvider(msalOptions);
var client = await _factory.CreateClientWithEntraTokenAsync(tokenProvider, "salesRep");
var response = await client.PostAsync("/api/orders", content);
response.StatusCode.Should().Be(HttpStatusCode.OK);