Class TestTokenCredential
- Namespace
- XBullet.EasyTesting.Azure
- Assembly
- XBullet.EasyTesting.Azure.dll
A deterministic Azure credential that records token requests.
public sealed class TestTokenCredential : TokenCredential, ITestScenarioResource
- Inheritance
-
AuthenticationTokenProviderTestTokenCredential
- Implements
- Inherited Members
Constructors
TestTokenCredential(string, DateTimeOffset?)
Creates a successful credential with a deterministic token and expiry.
public TestTokenCredential(string token = "xbullet-test-token", DateTimeOffset? expiresOn = null)
Parameters
tokenstringThe non-empty, non-whitespace token returned by synchronous and asynchronous requests. The default is
xbullet-test-token. Treat custom values as secrets.expiresOnDateTimeOffset?The absolute token expiry, or null to use January 1, 2100 at 00:00 UTC.
Properties
RequestCount
Gets the number of captured token requests.
public int RequestCount { get; }
Property Value
- int
The total number of successful and arranged-failure requests recorded since construction or reset.
Requests
Gets a stable copy of captured token requests.
public IReadOnlyList<TestTokenRequest> Requests { get; }
Property Value
- IReadOnlyList<TestTokenRequest>
A newly allocated snapshot in request order. The caller may retain the list. Individual requests can contain unredacted claims and identifiers that should be treated as sensitive.
Methods
CaptureDiagnosticsAsync(CancellationToken)
Captures the current state before a failed scenario is cleaned up.
public ValueTask<object?> CaptureDiagnosticsAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenCancels diagnostic collection for this resource. The default token does not request cancellation. A thrown exception, including cancellation, is recorded as a diagnostic capture failure so cleanup can continue.
Returns
- ValueTask<object>
A serializable diagnostic value, or null when the resource has no state to report. Ownership remains with the resource unless its implementation states otherwise.
FailWith(Exception)
Configures the credential to throw for subsequent token requests.
public TestTokenCredential FailWith(Exception exception)
Parameters
exceptionExceptionThe non-null exception instance retained and thrown after each subsequent request is recorded. The same instance is used until successful behavior is configured.
Returns
- TestTokenCredential
This credential, for chaining. The configured token and recorded requests are preserved.
GetToken(TokenRequestContext, CancellationToken)
Gets an AccessToken for the specified set of scopes.
public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken)
Parameters
requestContextTokenRequestContextThe TokenRequestContext with authentication information.
cancellationTokenCancellationTokenThe CancellationToken to use.
Returns
- AccessToken
A valid AccessToken.
Remarks
Caching and management of the lifespan for the AccessToken is considered the responsibility of the caller: each call should request a fresh token being requested.
GetTokenAsync(TokenRequestContext, CancellationToken)
Gets an AccessToken for the specified set of scopes.
public override ValueTask<AccessToken> GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken)
Parameters
requestContextTokenRequestContextThe TokenRequestContext with authentication information.
cancellationTokenCancellationTokenThe CancellationToken to use.
Returns
- ValueTask<AccessToken>
A valid AccessToken.
Remarks
Caching and management of the lifespan for the AccessToken is considered the responsibility of the caller: each call should request a fresh token being requested.
Reset()
Clears captured requests while preserving the arranged credential behavior.
public TestTokenCredential Reset()
Returns
- TestTokenCredential
This credential, for chaining. The token, expiry, and any arranged failure remain unchanged.
ResetAsync(CancellationToken)
Clears mutable state before and after a scenario.
public ValueTask ResetAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenCancels this reset attempt. The default token does not request cancellation. The factory continues resetting the remaining registered resources and reports all reset failures together.
Returns
- ValueTask
A task that completes when the resource has finished resetting its state.
SucceedWith(string, DateTimeOffset?)
Configures the credential to return a token and clears any arranged failure.
public TestTokenCredential SucceedWith(string token, DateTimeOffset? expiresOn = null)
Parameters
tokenstringThe non-empty, non-whitespace token to return. Treat the value as a secret.
expiresOnDateTimeOffset?The new absolute expiry, or null to preserve the currently configured expiry.
Returns
- TestTokenCredential
This credential, for chaining. The arranged failure is cleared; recorded requests are preserved.