Table of Contents

Class SseStreamHasEventAssertion

Namespace
SseAssertions.TUnit
Assembly
SseAssertions.TUnit.dll

Fluent TUnit assertion that reads a Server-Sent Events Stream and verifies the presence (or count) of frames matching a given event-type name. Constructed by the TUnit source generator from the HasSseEvent(string, CancellationToken) extension on Stream; the narrower methods (WithData, WithDataParsedAs<T>, WithId, WithRetryMillis) constrain which frames count and the count terminators (AtLeast, AtMost, Exactly) close the assertion.

[AssertionExtension("HasSseEvent")]
public sealed class SseStreamHasEventAssertion : Assertion<Stream>, IAssertion
Inheritance
Assertion<Stream>
SseStreamHasEventAssertion
Implements
IAssertion
Inherited Members
Assertion<Stream>.AssertAsync()
Assertion<Stream>.GetAwaiter()
Assertion<Stream>.And
Assertion<Stream>.Or

Remarks

The stream is drained inside CheckAsync(EvaluationMetadata<Stream>) via ReadAsync(Stream, Encoding, CancellationToken) (UTF-8, per the WHATWG SSE default), bounded by the supplied CancellationToken; matching is delegated to the shared SseAssertions.TUnit.SseEventMatcher so the diagnostics match the string chain. When the token cuts the read short and the expectation is unmet, the cancellation-truncated diagnostic takes precedence over the count/narrower failure. The read consumes the full token window and does not stop early once the expectation is satisfied, so the token should be set to the intended time budget.

Constructors

SseStreamHasEventAssertion(AssertionContext<Stream>, string, CancellationToken)

Initialises the assertion. Called by the TUnit source generator.

public SseStreamHasEventAssertion(AssertionContext<Stream> context, string eventName, CancellationToken cancellationToken = default)

Parameters

context AssertionContext<Stream>

The assertion context supplied by TUnit.

eventName string

The SSE event-type name to look for. Matched case-sensitively against EventName.

cancellationToken CancellationToken

Bounds the stream read; cancellation parses the partial buffer as best-effort SSE.

Exceptions

ArgumentNullException

eventName is null.

Methods

AtLeast(int)

Expects at least count matching frames.

public SseStreamHasEventAssertion AtLeast(int count)

Parameters

count int

The minimum match count. Must be non-negative.

Returns

SseStreamHasEventAssertion

This assertion for chaining.

Exceptions

ArgumentOutOfRangeException

count is negative.

AtMost(int)

Expects at most count matching frames.

public SseStreamHasEventAssertion AtMost(int count)

Parameters

count int

The maximum match count. Must be non-negative.

Returns

SseStreamHasEventAssertion

This assertion for chaining.

Exceptions

ArgumentOutOfRangeException

count is negative.

CheckAsync(EvaluationMetadata<Stream>)

Implements the specific check logic for this assertion. Called after the context has been evaluated. Override this method if your assertion uses the default AssertAsync() flow. If you override AssertAsync() with custom logic (like AndAssertion/OrAssertion), you don't need to implement this.

protected override Task<AssertionResult> CheckAsync(EvaluationMetadata<Stream> metadata)

Parameters

metadata EvaluationMetadata<Stream>

Metadata about the evaluation including value, exception, and timing information

Returns

Task<AssertionResult>

The result of the assertion check

Exactly(int)

Expects exactly count matching frames.

public SseStreamHasEventAssertion Exactly(int count)

Parameters

count int

The exact match count. Must be non-negative.

Returns

SseStreamHasEventAssertion

This assertion for chaining.

Exceptions

ArgumentOutOfRangeException

count is negative.

GetExpectation()

Gets a human-readable description of what this assertion expects. Used in error messages.

protected override string GetExpectation()

Returns

string

WithData(Func<string, bool>)

Narrows the assertion to frames whose Data satisfies the supplied predicate.

public SseStreamHasEventAssertion WithData(Func<string, bool> predicate)

Parameters

predicate Func<string, bool>

The data predicate; frames for which it returns true are counted.

Returns

SseStreamHasEventAssertion

This assertion for chaining.

Exceptions

ArgumentNullException

predicate is null.

WithDataParsedAs<T>(Func<string, T>, Func<T, bool>)

Narrows the assertion to frames whose Data deserializes via parse into a T that satisfies predicate.

public SseStreamHasEventAssertion WithDataParsedAs<T>(Func<string, T> parse, Func<T, bool> predicate)

Parameters

parse Func<string, T>

The deserializer applied to each candidate frame's Data. Supply a reflection-free parser (for example a source-generated JsonSerializer.Deserialize with a JsonTypeInfo<T>) so the assertion stays AOT-compatible. If it throws, the assertion fails naming the deserializer exception and the offending data.

predicate Func<T, bool>

The predicate applied to the parsed value; frames for which it returns true are counted.

Returns

SseStreamHasEventAssertion

This assertion for chaining.

Type Parameters

T

The type the frame data is parsed into.

Exceptions

ArgumentNullException

parse or predicate is null.

WithId(string)

Narrows the assertion to frames carrying the given id: directive.

public SseStreamHasEventAssertion WithId(string id)

Parameters

id string

The expected Id. Matched case-sensitively; a frame without an id: directive never matches.

Returns

SseStreamHasEventAssertion

This assertion for chaining.

Exceptions

ArgumentNullException

id is null.

WithRetryMillis(Func<int?, bool>)

Narrows the assertion to frames whose RetryMillis satisfies the supplied predicate.

public SseStreamHasEventAssertion WithRetryMillis(Func<int?, bool> predicate)

Parameters

predicate Func<int?, bool>

The retry-value predicate (null for frames without a retry: directive); frames for which it returns true are counted.

Returns

SseStreamHasEventAssertion

This assertion for chaining.

Exceptions

ArgumentNullException

predicate is null.