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
CanSeek
When overridden in a derived class, gets a value indicating whether the current stream supports seeking.
public override bool CanSeek { get; }
Property Value
CanWrite
When overridden in a derived class, gets a value indicating whether the current stream supports writing.
public override bool CanWrite { get; }
Property Value
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
Length
Not supported: the stream is forward-only.
public override long Length { get; }
Property Value
Exceptions
- NotSupportedException
Always.
Position
Not supported: the stream is forward-only.
public override long Position { get; set; }
Property Value
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
Methods
Dispose(bool)
Releases the unmanaged resources used by the Stream and optionally releases the managed resources.
protected override void Dispose(bool disposing)
Parameters
disposingbooltrue 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
clientHttpClientThe client to send the request with.
requestHttpRequestMessageThe request. An
Accept: text/event-streamheader is added when it has noAcceptheader.cancellationTokenCancellationTokenCancels the request and the wait for the first frame.
Returns
Exceptions
- ArgumentNullException
clientorrequestis 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
cancellationTokenwas 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
clientHttpClientThe client to send the request with, for example one created by a
WebApplicationFactory.requestUriUriThe endpoint, absolute or relative to the client's base address.
cancellationTokenCancellationTokenCancels the request and the wait for the first frame.
Returns
Exceptions
- ArgumentNullException
clientorrequestUriis 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
cancellationTokenwas 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
bufferbyte[]An array of bytes. When this method returns, the buffer contains the specified byte array with the values between
offsetand (offset+count- 1) replaced by the bytes read from the current source.offsetintThe zero-based byte offset in
bufferat which to begin storing the data read from the current stream.countintThe 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
countis 0 or the end of the stream has been reached.
Exceptions
- ArgumentException
The sum of
offsetandcountis larger than the buffer length.- ArgumentNullException
bufferis null.- ArgumentOutOfRangeException
offsetorcountis 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
bufferSpan<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
bufferbyte[]The buffer to write the data into.
offsetintThe byte offset in
bufferat which to begin writing data from the stream.countintThe maximum number of bytes to read.
cancellationTokenCancellationTokenThe 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
TResultparameter 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) ifcountis 0 or if the end of the stream has been reached.
Exceptions
- ArgumentNullException
bufferis null.- ArgumentOutOfRangeException
offsetorcountis negative.- ArgumentException
The sum of
offsetandcountis 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
bufferMemory<byte>The region of memory to write the data into.
cancellationTokenCancellationTokenThe 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
cancellationTokenCancellationTokenCancels 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
offsetlongIgnored.
originSeekOriginIgnored.
Returns
- long
Never returns.
Exceptions
- NotSupportedException
Always.
SetLength(long)
Not supported: the stream is read-only.
public override void SetLength(long value)
Parameters
valuelongIgnored.
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
Exceptions
- NotSupportedException
Always.