Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_split_unicode_message(self) -> None:
text_payload = "∂" * 64
payload = text_payload.encode("utf-8")
split = 64
decoder = fp.MessageDecoder()
frame = fp.Frame(
opcode=fp.Opcode.TEXT,
payload=payload[:split],
frame_finished=False,
message_finished=True,
)
frame = decoder.process_frame(frame)
assert frame.opcode is fp.Opcode.TEXT
assert frame.message_finished is False
assert frame.payload == text_payload[: (split // 3)]
frame = fp.Frame(
opcode=fp.Opcode.CONTINUATION,
payload=payload[split:],
frame_finished=True,
message_finished=True,
)
frame = decoder.process_frame(frame)
def initiate(self, host, path, **kwargs):
ws = WSConnection(CLIENT)
ws.send(Request(host=host, target=path, **kwargs))
data = ws.bytes_to_send()
request, headers = data.split(b"\r\n", 1)
method, path, version = request.strip().split()
headers = parse_headers(headers)
return ws, method, path, version, headers
request += b"Upgrade: WebSocket\r\n"
request += b"Sec-WebSocket-Version: 13\r\n"
request += b"Sec-WebSocket-Key: " + nonce + b"\r\n"
request += (
b"Sec-WebSocket-Extensions: "
+ ext.name.encode("ascii")
+ b"; "
+ offered_params.encode("ascii")
+ b"\r\n"
)
request += b"\r\n"
ws.receive_bytes(request)
event = next(ws.events())
assert isinstance(event, Request)
ws.send(AcceptConnection(extensions=[ext]))
data = ws.bytes_to_send()
response, headers = data.split(b"\r\n", 1)
version, code, reason = response.split(b" ")
headers = parse_headers(headers)
assert ext.offered == "%s; %s" % (ext.name, offered_params)
assert headers["sec-websocket-extensions"] == "%s; %s" % (ext.name, ext_params)
ws = WSConnection(SERVER)
nonce = bytes(random.getrandbits(8) for x in range(0, 16))
nonce = base64.b64encode(nonce)
headers = [
(b"Host", b"frob.nitz"),
(b"Connection", b"Upgrade"),
(b"Upgrade", b"WebSocket"),
(b"Sec-WebSocket-Version", b"13"),
(b"Sec-WebSocket-Key", nonce),
]
ws.initiate_upgrade_connection(headers, "/fnord")
event = next(ws.events())
assert isinstance(event, Request)
ws.send(AcceptConnection())
data = ws.bytes_to_send()
response, headers = data.split(b"\r\n", 1)
version, code, reason = response.split(b" ")
headers = parse_headers(headers)
accept_token = ws._generate_accept_token(nonce)
assert int(code) == 101
assert headers["connection"].lower() == "upgrade"
assert headers["upgrade"].lower() == "websocket"
assert headers["sec-websocket-accept"] == accept_token.decode("ascii")
def initiate(self, host, path, **kwargs):
ws = WSConnection(CLIENT)
ws.send(Request(host=host, target=path, **kwargs))
data = ws.bytes_to_send()
request, headers = data.split(b"\r\n", 1)
method, path, version = request.strip().split()
headers = parse_headers(headers)
return ws, method, path, version, headers
def _split_frame_test(
self,
client: bool,
frame_bytes: bytes,
opcode: fp.Opcode,
payload: bytes,
frame_finished: bool,
message_finished: bool,
split: int,
) -> None:
decoder = fp.FrameDecoder(client=client)
decoder.receive_bytes(frame_bytes[:split])
assert decoder.process_buffer() is None
decoder.receive_bytes(frame_bytes[split:])
frame = decoder.process_buffer()
assert frame is not None
assert frame.opcode is opcode
assert frame.payload == payload
assert frame.frame_finished is frame_finished
assert frame.message_finished is message_finished
def _single_frame_test(
self,
client: bool,
frame_bytes: bytes,
opcode: fp.Opcode,
payload: bytes,
frame_finished: bool,
message_finished: bool,
) -> None:
decoder = fp.FrameDecoder(client=client)
decoder.receive_bytes(frame_bytes)
frame = decoder.process_buffer()
assert frame is not None
assert frame.opcode is opcode
assert frame.payload == payload
assert frame.frame_finished is frame_finished
assert frame.message_finished is message_finished
def _parse_failure_test(
self, client: bool, frame_bytes: bytes, close_reason: fp.CloseReason
) -> None:
decoder = fp.FrameDecoder(client=client)
with pytest.raises(fp.ParseFailed) as excinfo:
decoder.receive_bytes(frame_bytes)
decoder.process_buffer()
assert excinfo.value.code is close_reason
def test_very_insufficiently_very_long_message_frame(self) -> None:
payload = b"x" * 64
payload_len = struct.pack("!Q", len(payload))
frame_bytes = b"\x81\x7f" + payload_len + payload
self._parse_failure_test(
client=True,
frame_bytes=frame_bytes,
close_reason=fp.CloseReason.PROTOCOL_ERROR,
)
def test_close_bad_utf8_payload(self) -> None:
payload = unhexlify("cebae1bdb9cf83cebcceb5eda080656469746564")
with pytest.raises(fp.ParseFailed) as exc:
self._close_test(fp.CloseReason.NORMAL_CLOSURE, reason_bytes=payload)
assert exc.value.code == fp.CloseReason.INVALID_FRAME_PAYLOAD_DATA