MockWebSocket class final

One connection opened through a MockWebSocketTransport: the client uses it as its WebSocketSession, and the test plays the server with the other members.

The test reads what the client sent (sent, sentJson, onSent), sends it messages (push, pushJson) and ends the connection cleanly (closeFromServer) or abruptly (drop).

final server = await sockets.nextConnection;
expect(server.headers['Authorization'], 'Bearer t0k3n');
server.push('welcome');
await server.closeFromServer(CloseStatus.goingAway);

Properties

acceptedProtocol → String?
The subprotocol the two sides agreed on in the handshake, or null when none was asked for or accepted.
no setter
closedByClient → bool
Whether the client closed it (rather than the test, with closeFromServer or drop).
no setter
closeStatus → CloseStatus?
How the connection was closed, by either side: the code and reason of the close frame. null while it is open.
no setter
hashCode → int
The hash code for this object.
no setterinherited
headers → HttpHeaders
The headers the client sent with the handshake (a copy).
final
isClosed → bool
Whether the connection is closed, by either side.
no setter
isOpen → bool
Whether messages can still be sent: false once either side has started to close the connection.
no setter
messages → Stream<Object>
The incoming messages: a String for each text frame, a Uint8List for each binary one. It can be listened to once, and it is done when the connection closes, whichever side closed it.
no setter
onSent → Stream<Object>
Each message the client sends from now on, as it is sent; ends when the connection closes. Use it to wait for a message.
no setter
protocols → List<String>
The sub-protocols the client offered, in order; the first one is taken as the acceptedProtocol.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
sent → List<Object>
Every message the client sent, in order: a String for a text message, a Uint8List for a binary one.
no setter
sentJson → List<Object?>
The text messages the client sent that are JSON, decoded. Other text and binary messages are left out (see sent), and so is a message that is the JSON null.
no setter
uri → Uri
The URL the client connected to.
final

Methods

close([CloseStatus status = CloseStatus.normal]) → Future<void>
Closes the connection, sending status (CloseStatus.normal by default) to the other side. The future completes when the connection is closed.
closeFromServer([CloseStatus status = CloseStatus.normal]) → Future<void>
Closes the connection as the server would, with status (1000, normal, by default); the client sees it as a clean close.
drop() → Future<void>
Loses the connection without a close handshake, as a network failure does: the close status is CloseStatus.noCloseFrame (1006). Use it to test that the client reconnects.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
push(Object message) → void
Sends the client a text (String) or binary (List<int>) message, as the server would. Throws a StateError once the connection is closed.
pushJson(Object? json) → void
Sends the client json, encoded, as a text message.
sendMessage(Object message) → void
Sends message as a text frame when it is a String, or as a binary frame when it is a List<int>. Throws a WebSocketException when the session is no longer open, and an ArgumentError for any other type. It does not wait for the frame to leave: the message waits in memory until the network takes it. On dart:io a session whose other side takes nothing gives up past its limits (512 KB waiting, or 10 seconds) and drops the connection, and IoWebSocketSession.flush waits until what was sent has left; see IoWebSocketSession.
toString() → String
A string representation of this object.
override

Operators

operator ==(Object other) → bool
The equality operator.
inherited