Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions test/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from wsproto.connection import CLIENT, ConnectionState, SERVER, WSConnection
from wsproto.connection import CLIENT, ConnectionState, SERVER, WSConnection, ConnectionStateError
from wsproto.events import (
AcceptConnection,
BytesMessage,
Expand Down Expand Up @@ -35,7 +35,11 @@ def create_connection(self):
return client, server

def test_negotiation(self):
self.create_connection()
client, server = self.create_connection()
assert client.opened
assert server.opened
assert not client.closed
assert not server.closed

@pytest.mark.parametrize(
"as_client,final", [(True, True), (True, False), (False, True), (False, False)]
Expand Down Expand Up @@ -129,6 +133,33 @@ def test_abnormal_closure(self):
assert event.code is CloseReason.ABNORMAL_CLOSURE
assert conn.closed

def test_close_before_open_state(self):
server = WSConnection(SERVER)
client = WSConnection(CLIENT)

assert not server.opened
assert not client.opened

with pytest.raises(ConnectionStateError):
server.send(CloseConnection(code=CloseReason.NORMAL_CLOSURE))

with pytest.raises(ConnectionStateError):
client.send(CloseConnection(code=CloseReason.NORMAL_CLOSURE))

def test_close_already_closing(self):
client, server = self.create_connection()

client.send(CloseConnection(code=CloseReason.NORMAL_CLOSURE))
server.send(CloseConnection(code=CloseReason.NORMAL_CLOSURE))
assert not server.opened
assert not client.opened

with pytest.raises(ConnectionStateError):
client.send(CloseConnection(code=CloseReason.NORMAL_CLOSURE))

with pytest.raises(ConnectionStateError):
server.send(CloseConnection(code=CloseReason.NORMAL_CLOSURE))

def test_bytes_send_all(self):
connection = WSConnection(SERVER)
connection._outgoing = b"fnord fnord"
Expand Down
13 changes: 13 additions & 0 deletions wsproto/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ class ConnectionType(Enum):
SERVER = ConnectionType.SERVER


class ConnectionStateError(Exception):
def __init__(self, msg, state):
super(ConnectionStateError, self).__init__(msg)
self.state = state


class WSConnection(object):
"""
A low-level WebSocket connection object.
Expand Down Expand Up @@ -109,6 +115,9 @@ def send(self, event):
elif isinstance(event, Pong):
self._outgoing += self._proto.pong(event.payload)
elif isinstance(event, CloseConnection):
if not self.opened:
raise ConnectionStateError(
'Must be in OPEN state to perform a CloseConnection', self._state)
self._outgoing += self._proto.close(event.code, event.reason)
self._state = ConnectionState.CLOSING

Expand Down Expand Up @@ -181,6 +190,10 @@ def _accept(self, event):
def closed(self):
return self._state is ConnectionState.CLOSED

@property
def opened(self):
return self._state is ConnectionState.OPEN

def bytes_to_send(self, amount=None):
"""
Returns some data for sending out of the internal data buffer.
Expand Down