Table of Contents

Class WebApplicationFactoryExtensions

Namespace
Trellis.Testing
Assembly
Trellis.Testing.dll

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

factory WebApplicationFactory<TEntryPoint>

The web application factory.

actorId string

The unique identifier of the test actor.

permissions string[]

The permissions granted to the test actor.

Returns

HttpClient

An HttpClient with the actor header configured.

Type Parameters

TEntryPoint

The 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

factory WebApplicationFactory<TEntryPoint>

The web application factory.

actor Actor

The actor to serialize into the header.

Returns

HttpClient

An HttpClient with the actor header configured.

Type Parameters

TEntryPoint

The 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

factory WebApplicationFactory<TEntryPoint>

The web application factory.

tokenProvider MsalTestTokenProvider

The MSAL token provider configured with test tenant credentials. Create one instance per test class/fixture to benefit from token caching.

testUserName string

The key in TestUsers identifying the test user.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<HttpClient>

An HttpClient with the Authorization Bearer header set.

Type Parameters

TEntryPoint

The 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);