Skip to content
Open
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
7 changes: 5 additions & 2 deletions src/wsproto/frame_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from enum import IntEnum
from typing import TYPE_CHECKING, NamedTuple

from .utilities import LocalProtocolError

if TYPE_CHECKING:
from collections.abc import Generator

Expand Down Expand Up @@ -588,13 +590,14 @@ def received_frames(self) -> Generator[Frame, None, None]:

def close(self, code: int | None = None, reason: str | None = None) -> bytearray:
payload = bytearray()
if code is CloseReason.NO_STATUS_RCVD:
if code == CloseReason.NO_STATUS_RCVD:
code = None
if code is None and reason:
msg = "cannot specify a reason without a code"
raise TypeError(msg)
if code in LOCAL_ONLY_CLOSE_REASONS:
code = CloseReason.NORMAL_CLOSURE
msg = f"cannot send a close frame with local-only code {code}"
raise LocalProtocolError(msg)
if code is not None:
payload += bytearray(struct.pack("!H", code))
if reason is not None:
Expand Down
6 changes: 6 additions & 0 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ def test_close_whilst_closing() -> None:
client.send(CloseConnection(code=CloseReason.NORMAL_CLOSURE))


def test_local_only_close_reason_rejected() -> None:
client = Connection(CLIENT)
with pytest.raises(LocalProtocolError):
client.send(CloseConnection(code=1006))


def test_send_after_close() -> None:
client = Connection(CLIENT)
client.send(CloseConnection(code=CloseReason.NORMAL_CLOSURE))
Expand Down
11 changes: 8 additions & 3 deletions tests/test_frame_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from wsproto import extensions as wpext
from wsproto import frame_protocol as fp
from wsproto.utilities import LocalProtocolError


class TestBuffer:
Expand Down Expand Up @@ -1047,10 +1048,14 @@ def test_no_status_rcvd_close_reason(self) -> None:
data = proto.close(code=fp.CloseReason.NO_STATUS_RCVD)
assert data == b"\x88\x00"

def test_local_only_close_reason(self) -> None:
@pytest.mark.parametrize(
"code",
[fp.CloseReason.ABNORMAL_CLOSURE, fp.CloseReason.TLS_HANDSHAKE_FAILED],
)
def test_local_only_close_reason(self, code: fp.CloseReason) -> None:
proto = fp.FrameProtocol(client=False, extensions=[])
data = proto.close(code=fp.CloseReason.ABNORMAL_CLOSURE)
assert data == b"\x88\x02\x03\xe8"
with pytest.raises(LocalProtocolError):
proto.close(code=code)

def test_ping_without_payload(self) -> None:
proto = fp.FrameProtocol(client=False, extensions=[])
Expand Down