Table of Contents

Class SseResponseHasEventAssertion

Namespace
SseAssertions.TUnit
Assembly
SseAssertions.TUnit.dll

Fluent TUnit assertion that reads a Server-Sent Events HttpResponseMessage body and verifies the presence (or count) of frames matching a given event-type name. Constructed by the TUnit source generator from the HasSseEvent(string, bool, CancellationToken) extension on HttpResponseMessage; 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 SseResponseHasEventAssertion : Assertion<HttpResponseMessage>, IAssertion
Inheritance
SseResponseHasEventAssertion
Implements
IAssertion
Inherited Members
Assertion<HttpResponseMessage>.AssertAsync()
Assertion<HttpResponseMessage>.GetAwaiter()
Assertion<HttpResponseMessage>.And
Assertion<HttpResponseMessage>.Or

Remarks

The body is drained inside CheckAsync(EvaluationMetadata<HttpResponseMessage>) via ReadAsync(Stream, Encoding, CancellationToken) using the response charset (UTF-8 fallback), bounded by the supplied CancellationToken; matching is delegated to the shared SseAssertions.TUnit.SseEventMatcher. When strictContentType is set (the default) a non-text/event-stream response fails with the unexpected-content-type diagnostic before the body is read. When the token cuts the read short and the expectation is unmet, the cancellation-truncated diagnostic takes precedence. 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

SseResponseHasEventAssertion(AssertionContext<HttpResponseMessage>, string, bool, CancellationToken)

Initialises the assertion. Called by the TUnit source generator.

public SseResponseHasEventAssertion(AssertionContext<HttpResponseMessage> context, string eventName, bool strictContentType = true, CancellationToken cancellationToken = default)

Parameters

context AssertionContext<HttpResponseMessage>

The assertion context supplied by TUnit.

eventName string

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

strictContentType bool

When true (the default), the assertion fails if the response Content-Type media type is not text/event-stream. Set to false for test mocks that serve SSE without the canonical header.

cancellationToken CancellationToken

Bounds the response-body 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 SseResponseHasEventAssertion AtLeast(int count)

Parameters

count int

The minimum match count. Must be non-negative.

Returns

SseResponseHasEventAssertion

This assertion for chaining.

Exceptions

ArgumentOutOfRangeException

count is negative.

AtMost(int)

Expects at most count matching frames.

public SseResponseHasEventAssertion AtMost(int count)

Parameters

count int

The maximum match count. Must be non-negative.

Returns

SseResponseHasEventAssertion

This assertion for chaining.

Exceptions

ArgumentOutOfRangeException

count is negative.

CheckAsync(EvaluationMetadata<HttpResponseMessage>)

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<HttpResponseMessage> metadata)

Parameters

metadata EvaluationMetadata<HttpResponseMessage>

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 SseResponseHasEventAssertion Exactly(int count)

Parameters

count int

The exact match count. Must be non-negative.

Returns

SseResponseHasEventAssertion

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 SseResponseHasEventAssertion WithData(Func<string, bool> predicate)

Parameters

predicate Func<string, bool>

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

Returns

SseResponseHasEventAssertion

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 SseResponseHasEventAssertion 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

SseResponseHasEventAssertion

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 SseResponseHasEventAssertion WithId(string id)

Parameters

id string

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

Returns

SseResponseHasEventAssertion

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 SseResponseHasEventAssertion 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

SseResponseHasEventAssertion

This assertion for chaining.

Exceptions

ArgumentNullException

predicate is null.