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
-
Assertion<HttpResponseMessage>SseResponseHasEventAssertion
- Implements
-
IAssertion
- Inherited Members
-
Assertion<HttpResponseMessage>.AssertAsync()Assertion<HttpResponseMessage>.GetAwaiter()Assertion<HttpResponseMessage>.AndAssertion<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
contextAssertionContext<HttpResponseMessage>The assertion context supplied by TUnit.
eventNamestringThe SSE event-type name to look for. Matched case-sensitively against EventName.
strictContentTypeboolWhen true (the default), the assertion fails if the response
Content-Typemedia type is nottext/event-stream. Set to false for test mocks that serve SSE without the canonical header.cancellationTokenCancellationTokenBounds the response-body read; cancellation parses the partial buffer as best-effort SSE.
Exceptions
- ArgumentNullException
eventNameis null.
Methods
AtLeast(int)
Expects at least count matching frames.
public SseResponseHasEventAssertion AtLeast(int count)
Parameters
countintThe minimum match count. Must be non-negative.
Returns
- SseResponseHasEventAssertion
This assertion for chaining.
Exceptions
- ArgumentOutOfRangeException
countis negative.
AtMost(int)
Expects at most count matching frames.
public SseResponseHasEventAssertion AtMost(int count)
Parameters
countintThe maximum match count. Must be non-negative.
Returns
- SseResponseHasEventAssertion
This assertion for chaining.
Exceptions
- ArgumentOutOfRangeException
countis 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
metadataEvaluationMetadata<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
countintThe exact match count. Must be non-negative.
Returns
- SseResponseHasEventAssertion
This assertion for chaining.
Exceptions
- ArgumentOutOfRangeException
countis negative.
GetExpectation()
Gets a human-readable description of what this assertion expects. Used in error messages.
protected override string GetExpectation()
Returns
WithData(Func<string, bool>)
Narrows the assertion to frames whose Data satisfies the supplied predicate.
public SseResponseHasEventAssertion WithData(Func<string, bool> predicate)
Parameters
Returns
- SseResponseHasEventAssertion
This assertion for chaining.
Exceptions
- ArgumentNullException
predicateis 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
parseFunc<string, T>The deserializer applied to each candidate frame's Data. Supply a reflection-free parser (for example a source-generated
JsonSerializer.Deserializewith aJsonTypeInfo<T>) so the assertion stays AOT-compatible. If it throws, the assertion fails naming the deserializer exception and the offending data.predicateFunc<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
TThe type the frame data is parsed into.
Exceptions
- ArgumentNullException
parseorpredicateis null.
WithId(string)
Narrows the assertion to frames carrying the given id: directive.
public SseResponseHasEventAssertion WithId(string id)
Parameters
idstringThe expected Id. Matched case-sensitively; a frame without an
id:directive never matches.
Returns
- SseResponseHasEventAssertion
This assertion for chaining.
Exceptions
- ArgumentNullException
idis null.
WithRetryMillis(Func<int?, bool>)
Narrows the assertion to frames whose RetryMillis satisfies the supplied predicate.
public SseResponseHasEventAssertion WithRetryMillis(Func<int?, bool> predicate)
Parameters
predicateFunc<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
predicateis null.