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_handle_qpack_decoder_duplicate(self):
"""
We must only receive a single QPACK decoder stream.
"""
quic_client = FakeQuicConnection(
configuration=QuicConfiguration(is_client=True)
)
h3_client = H3Connection(quic_client)
# receive a first decoder stream
h3_client.handle_event(
StreamDataReceived(
stream_id=11,
data=encode_uint_var(StreamType.QPACK_DECODER),
end_stream=False,
)
)
# receive a second decoder stream
h3_client.handle_event(
StreamDataReceived(
stream_id=15,
data=encode_uint_var(StreamType.QPACK_DECODER),
end_stream=False,
)
)
self.assertEqual(
quic_client.closed,
def test_recv_fin(self):
stream = QuicStream(stream_id=0)
self.assertEqual(
stream.add_frame(QuicStreamFrame(offset=0, data=b"01234567")),
StreamDataReceived(data=b"01234567", end_stream=False, stream_id=0),
)
self.assertEqual(
stream.add_frame(QuicStreamFrame(offset=8, data=b"89012345", fin=True)),
StreamDataReceived(data=b"89012345", end_stream=True, stream_id=0),
)
def test_handle_request_frame_wrong_frame_type(self):
quic_server = FakeQuicConnection(
configuration=QuicConfiguration(is_client=False)
)
h3_server = H3Connection(quic_server)
h3_server.handle_event(
StreamDataReceived(
stream_id=0,
data=encode_frame(FrameType.SETTINGS, b""),
end_stream=False,
)
)
self.assertEqual(
quic_server.closed,
(ErrorCode.HTTP_FRAME_UNEXPECTED, "Invalid frame type on request stream"),
)
def test_handle_control_frame_headers(self):
"""
We should not receive HEADERS on the control stream.
"""
quic_server = FakeQuicConnection(
configuration=QuicConfiguration(is_client=False)
)
h3_server = H3Connection(quic_server)
h3_server.handle_event(
StreamDataReceived(
stream_id=2,
data=encode_uint_var(StreamType.CONTROL)
+ encode_frame(FrameType.HEADERS, b""),
end_stream=False,
)
)
self.assertEqual(
quic_server.closed,
(ErrorCode.HTTP_FRAME_UNEXPECTED, "Invalid frame type on control stream"),
)
def test_handle_qpack_decoder_stream_error(self):
"""
Receiving garbage on the QPACK decoder stream triggers an exception.
"""
quic_client = FakeQuicConnection(
configuration=QuicConfiguration(is_client=True)
)
h3_client = H3Connection(quic_client)
h3_client.handle_event(
StreamDataReceived(
stream_id=11,
data=encode_uint_var(StreamType.QPACK_DECODER) + b"\x00",
end_stream=False,
)
)
self.assertEqual(
quic_client.closed, (ErrorCode.HTTP_QPACK_DECODER_STREAM_ERROR, "")
)
def test_recv_fin_without_data(self):
stream = QuicStream(stream_id=0)
self.assertEqual(
stream.add_frame(QuicStreamFrame(offset=0, data=b"", fin=True)),
StreamDataReceived(data=b"", end_stream=True, stream_id=0),
)
def test_handle_request_frame_bad_headers(self):
"""
We should not receive HEADERS which cannot be decoded.
"""
quic_server = FakeQuicConnection(
configuration=QuicConfiguration(is_client=False)
)
h3_server = H3Connection(quic_server)
h3_server.handle_event(
StreamDataReceived(
stream_id=0, data=encode_frame(FrameType.HEADERS, b""), end_stream=False
)
)
self.assertEqual(
quic_server.closed, (ErrorCode.HTTP_QPACK_DECOMPRESSION_FAILED, "")
)
h3_client.send_headers(
stream_id=stream_id,
headers=[
(b":method", b"GET"),
(b":scheme", b"https"),
(b":authority", b"localhost"),
(b":path", b"/"),
],
)
h3_client.send_headers(
stream_id=stream_id, headers=[(b"x-some-trailer", b"foo")], end_stream=True
)
h3_transfer(quic_client, h3_server)
h3_server.handle_event(
StreamDataReceived(
stream_id=0, data=encode_frame(FrameType.HEADERS, b""), end_stream=False
)
)
self.assertEqual(
quic_server.closed,
(
ErrorCode.HTTP_FRAME_UNEXPECTED,
"HEADERS frame is not allowed in this state",
),
def test_handle_qpack_encoder_stream_error(self):
"""
Receiving garbage on the QPACK encoder stream triggers an exception.
"""
quic_client = FakeQuicConnection(
configuration=QuicConfiguration(is_client=True)
)
h3_client = H3Connection(quic_client)
h3_client.handle_event(
StreamDataReceived(
stream_id=7,
data=encode_uint_var(StreamType.QPACK_ENCODER) + b"\x00",
end_stream=False,
)
)
self.assertEqual(
quic_client.closed, (ErrorCode.HTTP_QPACK_ENCODER_STREAM_ERROR, "")
)
def handle_event(self, event: QuicEvent) -> List[H3Event]:
http_events: List[H3Event] = []
if isinstance(event, StreamDataReceived) and (event.stream_id % 4) == 0:
data = event.data
if not self._headers_received.get(event.stream_id, False):
if self._is_client:
http_events.append(
HeadersReceived(
headers=[], stream_ended=False, stream_id=event.stream_id
)
)
else:
method, path = data.rstrip().split(b" ", 1)
http_events.append(
HeadersReceived(
headers=[(b":method", method), (b":path", path)],
stream_ended=False,
stream_id=event.stream_id,
)