Table of Contents

Class SseStream

Namespace
SseAssertions
Assembly
SseAssertions.dll

An open Server-Sent Events response stream whose first frame has arrived. OpenAsync(HttpClient, Uri, CancellationToken) returns once the server has written its first frame, which an endpoint does after it has subscribed to the events it streams. That is the moment a test can trigger the events and be sure none is missed.

public sealed class SseStream : Stream, IAsyncDisposable, IDisposable
Inheritance
SseStream
Implements
Inherited Members
Extension Methods

Remarks

The SseStream is the response body: a read-only Stream positioned after the first frame. Every assertion that reads a Stream applies to it, and each one continues where the previous one stopped, so a test can assert one event, trigger the next, and assert again:

await using var sse = await SseStream.OpenAsync(client, new Uri("/events", UriKind.Relative), ct);
await service.TriggerAsync(ct);
await Assert.That(sse).HasSseEvent("cycleCompleted", ct);

Read events directly with ReadEventsAsync(CancellationToken), or with an SseEventReader. Mixing event reads with raw reads of the stream is possible, but a raw read continues after the last line an event read consumed. Not thread-safe. Disposing the stream disposes the response.

Properties

CanRead

When overridden in a derived class, gets a value indicating whether the current stream supports reading.

public override bool CanRead { get; }

Property Value

bool

true if the stream supports reading; otherwise, false.

CanSeek

When overridden in a derived class, gets a value indicating whether the current stream supports seeking.

public override bool CanSeek { get; }

Property Value

bool

true if the stream supports seeking; otherwise, false.

CanWrite

When overridden in a derived class, gets a value indicating whether the current stream supports writing.

public override bool CanWrite { get; }

Property Value

bool

true if the stream supports writing; otherwise, false.

FirstFrame

Gets the first frame the server wrote, for example its retry: directive. Its Data is empty when the frame carried no data, such as a bare retry: or a comment.

public SseEvent FirstFrame { get; }

Property Value

SseEvent

Length

Not supported: the stream is forward-only.

public override long Length { get; }

Property Value

long

Exceptions

NotSupportedException

Always.

Position

Not supported: the stream is forward-only.

public override long Position { get; set; }

Property Value

long

Exceptions

NotSupportedException

Always.

Response

Gets the HTTP response. Its headers and status are available; read the body through this stream, not through the response, which no longer has the bytes already read.

public HttpResponseMessage Response { get; }

Property Value

HttpResponseMessage

Methods

Dispose(bool)

Releases the unmanaged resources used by the Stream and optionally releases the managed resources.

protected override void Dispose(bool disposing)

Parameters

disposing bool

true to release both managed and unmanaged resources; false to release only unmanaged resources.

Flush()

Does nothing: the stream is read-only.

public override void Flush()

OpenAsync(HttpClient, HttpRequestMessage, CancellationToken)

Sends request and returns once the first frame of the Server-Sent Events response has arrived. Use this overload for a request that needs headers (for example Last-Event-ID or authorization) or another method.

public static Task<SseStream> OpenAsync(HttpClient client, HttpRequestMessage request, CancellationToken cancellationToken)

Parameters

client HttpClient

The client to send the request with.

request HttpRequestMessage

The request. An Accept: text/event-stream header is added when it has no Accept header.

cancellationToken CancellationToken

Cancels the request and the wait for the first frame.

Returns

Task<SseStream>

The open stream.

Exceptions

ArgumentNullException

client or request is null.

HttpRequestException

The response status is not a success code.

InvalidOperationException

The response is not text/event-stream, or it ended before its first frame.

OperationCanceledException

cancellationToken was canceled first.

OpenAsync(HttpClient, Uri, CancellationToken)

Sends a GET request for requestUri and returns once the first frame of the Server-Sent Events response has arrived.

public static Task<SseStream> OpenAsync(HttpClient client, Uri requestUri, CancellationToken cancellationToken)

Parameters

client HttpClient

The client to send the request with, for example one created by a WebApplicationFactory.

requestUri Uri

The endpoint, absolute or relative to the client's base address.

cancellationToken CancellationToken

Cancels the request and the wait for the first frame.

Returns

Task<SseStream>

The open stream.

Exceptions

ArgumentNullException

client or requestUri is null.

HttpRequestException

The response status is not a success code.

InvalidOperationException

The response is not text/event-stream, or it ended before its first frame.

OperationCanceledException

cancellationToken was canceled first.

Read(byte[], int, int)

When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.

public override int Read(byte[] buffer, int offset, int count)

Parameters

buffer byte[]

An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.

offset int

The zero-based byte offset in buffer at which to begin storing the data read from the current stream.

count int

The maximum number of bytes to be read from the current stream.

Returns

int

The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if count is 0 or the end of the stream has been reached.

Exceptions

ArgumentException

The sum of offset and count is larger than the buffer length.

ArgumentNullException

buffer is null.

ArgumentOutOfRangeException

offset or count is negative.

IOException

An I/O error occurs.

NotSupportedException

The stream does not support reading.

ObjectDisposedException

Methods were called after the stream was closed.

Read(Span<byte>)

When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.

public override int Read(Span<byte> buffer)

Parameters

buffer Span<byte>

A region of memory. When this method returns, the contents of this region are replaced by the bytes read from the current source.

Returns

int

The total number of bytes read into the buffer. This can be less than the size of the buffer if that many bytes are not currently available, or zero (0) if the buffer's length is zero or the end of the stream has been reached.

ReadAsync(byte[], int, int, CancellationToken)

Asynchronously reads a sequence of bytes from the current stream, advances the position within the stream by the number of bytes read, and monitors cancellation requests.

public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)

Parameters

buffer byte[]

The buffer to write the data into.

offset int

The byte offset in buffer at which to begin writing data from the stream.

count int

The maximum number of bytes to read.

cancellationToken CancellationToken

The token to monitor for cancellation requests. The default value is None.

Returns

Task<int>

A task that represents the asynchronous read operation. The value of the TResult parameter contains the total number of bytes read into the buffer. The result value can be less than the number of bytes requested if the number of bytes currently available is less than the requested number, or it can be 0 (zero) if count is 0 or if the end of the stream has been reached.

Exceptions

ArgumentNullException

buffer is null.

ArgumentOutOfRangeException

offset or count is negative.

ArgumentException

The sum of offset and count is larger than the buffer length.

NotSupportedException

The stream does not support reading.

ObjectDisposedException

The stream has been disposed.

InvalidOperationException

The stream is currently in use by a previous read operation.

OperationCanceledException

The cancellation token was canceled. This exception is stored into the returned task.

ReadAsync(Memory<byte>, CancellationToken)

Asynchronously reads a sequence of bytes from the current stream, advances the position within the stream by the number of bytes read, and monitors cancellation requests.

public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)

Parameters

buffer Memory<byte>

The region of memory to write the data into.

cancellationToken CancellationToken

The token to monitor for cancellation requests. The default value is None.

Returns

ValueTask<int>

A task that represents the asynchronous read operation. The value of its Result property contains the total number of bytes read into the buffer. The result value can be less than the length of the buffer if that many bytes are not currently available, or it can be 0 (zero) if the length of the buffer is 0 or if the end of the stream has been reached.

Exceptions

OperationCanceledException

The cancellation token was canceled. This exception is stored into the returned task.

ReadEventsAsync(CancellationToken)

Reads the events after the first frame as they arrive. A frame without a data: line is skipped, as the SSE specification requires. Stopping the enumeration leaves the stream positioned at the next event.

public IAsyncEnumerable<SseEvent> ReadEventsAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Cancels the wait for the next event.

Returns

IAsyncEnumerable<SseEvent>

The events, until the server ends the stream.

Seek(long, SeekOrigin)

Not supported: the stream is forward-only.

public override long Seek(long offset, SeekOrigin origin)

Parameters

offset long

Ignored.

origin SeekOrigin

Ignored.

Returns

long

Never returns.

Exceptions

NotSupportedException

Always.

SetLength(long)

Not supported: the stream is read-only.

public override void SetLength(long value)

Parameters

value long

Ignored.

Exceptions

NotSupportedException

Always.

Write(byte[], int, int)

Not supported: the stream is read-only.

public override void Write(byte[] buffer, int offset, int count)

Parameters

buffer byte[]

Ignored.

offset int

Ignored.

count int

Ignored.

Exceptions

NotSupportedException

Always.