code
stringlengths
66
870k
docstring
stringlengths
19
26.7k
func_name
stringlengths
1
138
language
stringclasses
1 value
repo
stringlengths
7
68
path
stringlengths
5
324
url
stringlengths
46
389
license
stringclasses
7 values
def test_receiving_out_of_order_headers(self, frame_factory, end_stream) -> None: """ When receiving a informational response after the actual response headers we consider it a ProtocolError and raise it. """ c = h2.connection.H2Connection() c.initiate_connection() c.send_headers( stream_id=1, headers=self.example_request_headers, end_stream=end_stream, ) f1 = frame_factory.build_headers_frame( headers=self.example_response_headers, stream_id=1, ) f2 = frame_factory.build_headers_frame( headers=self.example_informational_headers, stream_id=1, ) c.receive_data(f1.serialize()) c.clear_outbound_data_buffer() with pytest.raises(h2.exceptions.ProtocolError): c.receive_data(f2.serialize()) expected = frame_factory.build_goaway_frame( last_stream_id=0, error_code=1, ) assert c.data_to_send() == expected.serialize()
When receiving a informational response after the actual response headers we consider it a ProtocolError and raise it.
test_receiving_out_of_order_headers
python
python-hyper/h2
tests/test_informational_responses.py
https://github.com/python-hyper/h2/blob/master/tests/test_informational_responses.py
MIT
def test_sending_multiple_header_blocks(self, frame_factory, hdrs, request_headers, end_stream) -> None: """ At least three header blocks can be sent: informational, headers, trailers. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) flags = ["END_STREAM"] if end_stream else [] f = frame_factory.build_headers_frame( headers=request_headers, stream_id=1, flags=flags, ) c.receive_data(f.serialize()) c.clear_outbound_data_buffer() frame_factory.refresh_encoder() # Send the three header blocks. c.send_headers( stream_id=1, headers=hdrs, ) c.send_headers( stream_id=1, headers=self.example_response_headers, ) c.send_headers( stream_id=1, headers=self.example_trailers, end_stream=True, ) # Check that we sent them properly. f1 = frame_factory.build_headers_frame( headers=hdrs, stream_id=1, ) f2 = frame_factory.build_headers_frame( headers=self.example_response_headers, stream_id=1, ) f3 = frame_factory.build_headers_frame( headers=self.example_trailers, stream_id=1, flags=["END_STREAM"], ) assert ( c.data_to_send() == f1.serialize() + f2.serialize() + f3.serialize() )
At least three header blocks can be sent: informational, headers, trailers.
test_sending_multiple_header_blocks
python
python-hyper/h2
tests/test_informational_responses.py
https://github.com/python-hyper/h2/blob/master/tests/test_informational_responses.py
MIT
def test_sending_multiple_informational_responses(self, frame_factory, hdrs, request_headers, end_stream) -> None: """ More than one informational response is allowed. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) flags = ["END_STREAM"] if end_stream else [] f = frame_factory.build_headers_frame( headers=request_headers, stream_id=1, flags=flags, ) c.receive_data(f.serialize()) c.clear_outbound_data_buffer() frame_factory.refresh_encoder() # Send two informational responses. c.send_headers( stream_id=1, headers=hdrs, ) c.send_headers( stream_id=1, headers=[(b":status", b"101")], ) # Check we sent them both. f1 = frame_factory.build_headers_frame( headers=hdrs, stream_id=1, ) f2 = frame_factory.build_headers_frame( headers=[(":status", "101")], stream_id=1, ) assert c.data_to_send() == f1.serialize() + f2.serialize()
More than one informational response is allowed.
test_sending_multiple_informational_responses
python
python-hyper/h2
tests/test_informational_responses.py
https://github.com/python-hyper/h2/blob/master/tests/test_informational_responses.py
MIT
def test_send_provisional_response_with_end_stream(self, frame_factory, hdrs, request_headers, end_stream) -> None: """ Sending provisional responses with END_STREAM set causes ProtocolErrors. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) flags = ["END_STREAM"] if end_stream else [] f = frame_factory.build_headers_frame( headers=request_headers, stream_id=1, flags=flags, ) c.receive_data(f.serialize()) with pytest.raises(h2.exceptions.ProtocolError): c.send_headers( stream_id=1, headers=hdrs, end_stream=True, )
Sending provisional responses with END_STREAM set causes ProtocolErrors.
test_send_provisional_response_with_end_stream
python
python-hyper/h2
tests/test_informational_responses.py
https://github.com/python-hyper/h2/blob/master/tests/test_informational_responses.py
MIT
def test_reject_sending_out_of_order_headers(self, frame_factory, hdrs, request_headers, end_stream) -> None: """ When sending an informational response after the actual response headers we consider it a ProtocolError and raise it. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) flags = ["END_STREAM"] if end_stream else [] f = frame_factory.build_headers_frame( headers=request_headers, stream_id=1, flags=flags, ) c.receive_data(f.serialize()) c.send_headers( stream_id=1, headers=self.example_response_headers, ) with pytest.raises(h2.exceptions.ProtocolError): c.send_headers( stream_id=1, headers=hdrs, )
When sending an informational response after the actual response headers we consider it a ProtocolError and raise it.
test_reject_sending_out_of_order_headers
python
python-hyper/h2
tests/test_informational_responses.py
https://github.com/python-hyper/h2/blob/master/tests/test_informational_responses.py
MIT
def test_basic_request_response(self, request_headers) -> None: """ A request issued by hyper-h2 can be responded to by hyper-h2. """ def client(): c = h2.connection.H2Connection() # Do the handshake. First send the preamble. c.initiate_connection() data = yield c.data_to_send() # Next, handle the remote preamble. events = c.receive_data(data) assert len(events) == 2 assert isinstance(events[0], h2.events.SettingsAcknowledged) assert isinstance(events[1], h2.events.RemoteSettingsChanged) changed = events[1].changed_settings assert ( changed[ h2.settings.SettingCodes.MAX_CONCURRENT_STREAMS ].new_value == 100 ) # Send a request. events = c.send_headers(1, request_headers, end_stream=True) assert not events data = yield c.data_to_send() # Validate the response. events = c.receive_data(data) assert len(events) == 2 assert isinstance(events[0], h2.events.ResponseReceived) assert events[0].stream_id == 1 assert events[0].headers == self.response_headers assert isinstance(events[1], h2.events.StreamEnded) assert events[1].stream_id == 1 @self.server def server(): c = h2.connection.H2Connection(config=self.server_config) # First, read for the preamble. data = yield events = c.receive_data(data) assert len(events) == 1 assert isinstance(events[0], h2.events.RemoteSettingsChanged) changed = events[0].changed_settings assert ( changed[ h2.settings.SettingCodes.MAX_CONCURRENT_STREAMS ].new_value == 100 ) # Send our preamble back. c.initiate_connection() data = yield c.data_to_send() # Listen for the request. events = c.receive_data(data) assert len(events) == 3 assert isinstance(events[0], h2.events.SettingsAcknowledged) assert isinstance(events[1], h2.events.RequestReceived) assert events[1].stream_id == 1 assert events[1].headers == self.request_headers_bytes assert isinstance(events[2], h2.events.StreamEnded) assert events[2].stream_id == 1 # Send our response. events = c.send_headers(1, self.response_headers, end_stream=True) assert not events yield c.data_to_send() self.run_until_complete(client(), server())
A request issued by hyper-h2 can be responded to by hyper-h2.
test_basic_request_response
python
python-hyper/h2
tests/test_interacting_stacks.py
https://github.com/python-hyper/h2/blob/master/tests/test_interacting_stacks.py
MIT
def test_too_much_data(self, frame_factory, request_headers) -> None: """ Remote peers sending data in excess of content-length causes Protocol Errors. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) headers = frame_factory.build_headers_frame( headers=request_headers, ) first_data = frame_factory.build_data_frame(data=b"\x01"*15) c.receive_data(headers.serialize() + first_data.serialize()) c.clear_outbound_data_buffer() second_data = frame_factory.build_data_frame(data=b"\x01") with pytest.raises(h2.exceptions.InvalidBodyLengthError) as exp: c.receive_data(second_data.serialize()) assert exp.value.expected_length == 15 assert exp.value.actual_length == 16 assert str(exp.value) == ( "InvalidBodyLengthError: Expected 15 bytes, received 16" ) expected_frame = frame_factory.build_goaway_frame( last_stream_id=1, error_code=h2.errors.ErrorCodes.PROTOCOL_ERROR, ) assert c.data_to_send() == expected_frame.serialize()
Remote peers sending data in excess of content-length causes Protocol Errors.
test_too_much_data
python
python-hyper/h2
tests/test_invalid_content_lengths.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_content_lengths.py
MIT
def test_insufficient_data(self, frame_factory, request_headers) -> None: """ Remote peers sending less data than content-length causes Protocol Errors. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) headers = frame_factory.build_headers_frame( headers=request_headers, ) first_data = frame_factory.build_data_frame(data=b"\x01"*13) c.receive_data(headers.serialize() + first_data.serialize()) c.clear_outbound_data_buffer() second_data = frame_factory.build_data_frame( data=b"\x01", flags=["END_STREAM"], ) with pytest.raises(h2.exceptions.InvalidBodyLengthError) as exp: c.receive_data(second_data.serialize()) assert exp.value.expected_length == 15 assert exp.value.actual_length == 14 assert str(exp.value) == ( "InvalidBodyLengthError: Expected 15 bytes, received 14" ) expected_frame = frame_factory.build_goaway_frame( last_stream_id=1, error_code=h2.errors.ErrorCodes.PROTOCOL_ERROR, ) assert c.data_to_send() == expected_frame.serialize()
Remote peers sending less data than content-length causes Protocol Errors.
test_insufficient_data
python
python-hyper/h2
tests/test_invalid_content_lengths.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_content_lengths.py
MIT
def test_insufficient_data_empty_frame(self, frame_factory, request_headers) -> None: """ Remote peers sending less data than content-length where the last data frame is empty causes Protocol Errors. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) headers = frame_factory.build_headers_frame( headers=request_headers, ) first_data = frame_factory.build_data_frame(data=b"\x01"*14) c.receive_data(headers.serialize() + first_data.serialize()) c.clear_outbound_data_buffer() second_data = frame_factory.build_data_frame( data=b"", flags=["END_STREAM"], ) with pytest.raises(h2.exceptions.InvalidBodyLengthError) as exp: c.receive_data(second_data.serialize()) assert exp.value.expected_length == 15 assert exp.value.actual_length == 14 assert str(exp.value) == ( "InvalidBodyLengthError: Expected 15 bytes, received 14" ) expected_frame = frame_factory.build_goaway_frame( last_stream_id=1, error_code=h2.errors.ErrorCodes.PROTOCOL_ERROR, ) assert c.data_to_send() == expected_frame.serialize()
Remote peers sending less data than content-length where the last data frame is empty causes Protocol Errors.
test_insufficient_data_empty_frame
python
python-hyper/h2
tests/test_invalid_content_lengths.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_content_lengths.py
MIT
def test_cannot_send_on_closed_stream(self, request_headers) -> None: """ When we've closed a stream locally, we cannot send further data. """ c = h2.connection.H2Connection() c.initiate_connection() c.send_headers(1, request_headers, end_stream=True) with pytest.raises(h2.exceptions.ProtocolError): c.send_data(1, b"some data")
When we've closed a stream locally, we cannot send further data.
test_cannot_send_on_closed_stream
python
python-hyper/h2
tests/test_invalid_frame_sequences.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_frame_sequences.py
MIT
def test_missing_preamble_errors(self) -> None: """ Server side connections require the preamble. """ c = h2.connection.H2Connection(config=self.server_config) encoded_headers_frame = ( b"\x00\x00\r\x01\x04\x00\x00\x00\x01" b"A\x88/\x91\xd3]\x05\\\x87\xa7\x84\x87\x82" ) with pytest.raises(h2.exceptions.ProtocolError): c.receive_data(encoded_headers_frame)
Server side connections require the preamble.
test_missing_preamble_errors
python
python-hyper/h2
tests/test_invalid_frame_sequences.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_frame_sequences.py
MIT
def test_server_connections_reject_even_streams(self, frame_factory, request_headers) -> None: """ Servers do not allow clients to initiate even-numbered streams. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) f = frame_factory.build_headers_frame( request_headers, stream_id=2, ) with pytest.raises(h2.exceptions.ProtocolError): c.receive_data(f.serialize())
Servers do not allow clients to initiate even-numbered streams.
test_server_connections_reject_even_streams
python
python-hyper/h2
tests/test_invalid_frame_sequences.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_frame_sequences.py
MIT
def test_clients_reject_odd_stream_pushes(self, frame_factory, request_headers) -> None: """ Clients do not allow servers to push odd numbered streams. """ c = h2.connection.H2Connection() c.initiate_connection() c.send_headers(1, request_headers, end_stream=True) f = frame_factory.build_push_promise_frame( stream_id=1, headers=request_headers, promised_stream_id=3, ) with pytest.raises(h2.exceptions.ProtocolError): c.receive_data(f.serialize())
Clients do not allow servers to push odd numbered streams.
test_clients_reject_odd_stream_pushes
python
python-hyper/h2
tests/test_invalid_frame_sequences.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_frame_sequences.py
MIT
def test_can_handle_frames_with_invalid_padding(self, frame_factory, request_headers) -> None: """ Frames with invalid padding cause connection teardown. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) f = frame_factory.build_headers_frame(request_headers) c.receive_data(f.serialize()) c.clear_outbound_data_buffer() invalid_data_frame = ( b"\x00\x00\x05\x00\x0b\x00\x00\x00\x01\x06\x54\x65\x73\x74" ) with pytest.raises(h2.exceptions.ProtocolError): c.receive_data(invalid_data_frame) expected_frame = frame_factory.build_goaway_frame( last_stream_id=1, error_code=1, ) assert c.data_to_send() == expected_frame.serialize()
Frames with invalid padding cause connection teardown.
test_can_handle_frames_with_invalid_padding
python
python-hyper/h2
tests/test_invalid_frame_sequences.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_frame_sequences.py
MIT
def test_receiving_frames_with_insufficent_size(self, frame_factory) -> None: """ Frames with not enough data cause connection teardown. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) c.clear_outbound_data_buffer() invalid_window_update_frame = ( b"\x00\x00\x03\x08\x00\x00\x00\x00\x00\x00\x00\x02" ) with pytest.raises(h2.exceptions.FrameDataMissingError): c.receive_data(invalid_window_update_frame) expected_frame = frame_factory.build_goaway_frame( last_stream_id=0, error_code=h2.errors.ErrorCodes.FRAME_SIZE_ERROR, ) assert c.data_to_send() == expected_frame.serialize()
Frames with not enough data cause connection teardown.
test_receiving_frames_with_insufficent_size
python
python-hyper/h2
tests/test_invalid_frame_sequences.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_frame_sequences.py
MIT
def test_reject_data_on_closed_streams(self, frame_factory, request_headers) -> None: """ When a stream is not open to the remote peer, we reject receiving data frames from them. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) f = frame_factory.build_headers_frame( request_headers, flags=["END_STREAM"], ) c.receive_data(f.serialize()) c.clear_outbound_data_buffer() bad_frame = frame_factory.build_data_frame( data=b"some data", ) c.receive_data(bad_frame.serialize()) expected = frame_factory.build_rst_stream_frame( stream_id=1, error_code=h2.errors.ErrorCodes.STREAM_CLOSED, ).serialize() assert c.data_to_send() == expected
When a stream is not open to the remote peer, we reject receiving data frames from them.
test_reject_data_on_closed_streams
python
python-hyper/h2
tests/test_invalid_frame_sequences.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_frame_sequences.py
MIT
def test_unexpected_continuation_on_closed_stream(self, frame_factory, request_headers) -> None: """ CONTINUATION frames received on closed streams cause connection errors of type PROTOCOL_ERROR. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) f = frame_factory.build_headers_frame( request_headers, flags=["END_STREAM"], ) c.receive_data(f.serialize()) c.clear_outbound_data_buffer() bad_frame = frame_factory.build_continuation_frame( header_block=b"hello", ) with pytest.raises(h2.exceptions.ProtocolError): c.receive_data(bad_frame.serialize()) expected_frame = frame_factory.build_goaway_frame( error_code=h2.errors.ErrorCodes.PROTOCOL_ERROR, last_stream_id=1, ) assert c.data_to_send() == expected_frame.serialize()
CONTINUATION frames received on closed streams cause connection errors of type PROTOCOL_ERROR.
test_unexpected_continuation_on_closed_stream
python
python-hyper/h2
tests/test_invalid_frame_sequences.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_frame_sequences.py
MIT
def test_prevent_continuation_dos(self, frame_factory, request_headers) -> None: """ Receiving too many CONTINUATION frames in one block causes a protocol error. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) f = frame_factory.build_headers_frame( request_headers, ) f.flags = {"END_STREAM"} c.receive_data(f.serialize()) c.clear_outbound_data_buffer() # Send 63 additional frames. for _ in range(63): extra_frame = frame_factory.build_continuation_frame( header_block=b"hello", ) c.receive_data(extra_frame.serialize()) # The final continuation frame should cause a protocol error. extra_frame = frame_factory.build_continuation_frame( header_block=b"hello", ) with pytest.raises(h2.exceptions.ProtocolError): c.receive_data(extra_frame.serialize()) expected_frame = frame_factory.build_goaway_frame( last_stream_id=0, error_code=0x1, ) assert c.data_to_send() == expected_frame.serialize()
Receiving too many CONTINUATION frames in one block causes a protocol error.
test_prevent_continuation_dos
python
python-hyper/h2
tests/test_invalid_frame_sequences.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_frame_sequences.py
MIT
def test_reject_invalid_settings_values(self, frame_factory, settings) -> None: """ When a SETTINGS frame is received with invalid settings values it causes connection teardown with the appropriate error code. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) f = frame_factory.build_settings_frame(settings=settings) with pytest.raises(h2.exceptions.InvalidSettingsValueError) as e: c.receive_data(f.serialize()) assert e.value.error_code == ( h2.errors.ErrorCodes.FLOW_CONTROL_ERROR if 0x4 in settings else h2.errors.ErrorCodes.PROTOCOL_ERROR )
When a SETTINGS frame is received with invalid settings values it causes connection teardown with the appropriate error code.
test_reject_invalid_settings_values
python
python-hyper/h2
tests/test_invalid_frame_sequences.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_frame_sequences.py
MIT
def test_invalid_frame_headers_are_protocol_errors(self, frame_factory, request_headers) -> None: """ When invalid frame headers are received they cause ProtocolErrors to be raised. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) f = frame_factory.build_headers_frame( headers=request_headers, ) # Do some annoying bit twiddling here: the stream ID is currently set # to '1', change it to '0'. Grab the first 9 bytes (the frame header), # replace any instances of the byte '\x01', and then graft it onto the # remaining bytes. frame_data = f.serialize() frame_data = frame_data[:9].replace(b"\x01", b"\x00") + frame_data[9:] with pytest.raises(h2.exceptions.ProtocolError) as e: c.receive_data(frame_data) assert "Received frame with invalid header" in str(e.value)
When invalid frame headers are received they cause ProtocolErrors to be raised.
test_invalid_frame_headers_are_protocol_errors
python
python-hyper/h2
tests/test_invalid_frame_sequences.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_frame_sequences.py
MIT
def test_data_before_headers(self, frame_factory, request_headers) -> None: """ When data frames are received before headers they cause ProtocolErrors to be raised. """ c = h2.connection.H2Connection(config=self.client_config) c.initiate_connection() # transition stream into OPEN c.send_headers(1, request_headers) f = frame_factory.build_data_frame(b"hello") with pytest.raises(h2.exceptions.ProtocolError) as e: c.receive_data(f.serialize()) assert "cannot receive data before headers" in str(e.value)
When data frames are received before headers they cause ProtocolErrors to be raised.
test_data_before_headers
python
python-hyper/h2
tests/test_invalid_frame_sequences.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_frame_sequences.py
MIT
def test_get_stream_reset_event_on_auto_reset(self, frame_factory, request_headers) -> None: """ When hyper-h2 resets a stream automatically, a StreamReset event fires. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) f = frame_factory.build_headers_frame( request_headers, flags=["END_STREAM"], ) c.receive_data(f.serialize()) c.clear_outbound_data_buffer() bad_frame = frame_factory.build_data_frame( data=b"some data", ) events = c.receive_data(bad_frame.serialize()) expected = frame_factory.build_rst_stream_frame( stream_id=1, error_code=h2.errors.ErrorCodes.STREAM_CLOSED, ).serialize() assert c.data_to_send() == expected assert len(events) == 1 event = events[0] assert isinstance(event, h2.events.StreamReset) assert event.stream_id == 1 assert event.error_code == h2.errors.ErrorCodes.STREAM_CLOSED assert not event.remote_reset
When hyper-h2 resets a stream automatically, a StreamReset event fires.
test_get_stream_reset_event_on_auto_reset
python
python-hyper/h2
tests/test_invalid_frame_sequences.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_frame_sequences.py
MIT
def test_one_one_stream_reset(self, frame_factory, request_headers) -> None: """ When hyper-h2 resets a stream automatically, a StreamReset event fires, but only for the first reset: the others are silent. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) f = frame_factory.build_headers_frame( request_headers, flags=["END_STREAM"], ) c.receive_data(f.serialize()) c.clear_outbound_data_buffer() bad_frame = frame_factory.build_data_frame( data=b"some data", ) # Receive 5 frames. events = c.receive_data(bad_frame.serialize() * 5) expected = frame_factory.build_rst_stream_frame( stream_id=1, error_code=h2.errors.ErrorCodes.STREAM_CLOSED, ).serialize() assert c.data_to_send() == expected * 5 assert len(events) == 1 event = events[0] assert isinstance(event, h2.events.StreamReset) assert event.stream_id == 1 assert event.error_code == h2.errors.ErrorCodes.STREAM_CLOSED assert not event.remote_reset
When hyper-h2 resets a stream automatically, a StreamReset event fires, but only for the first reset: the others are silent.
test_one_one_stream_reset
python
python-hyper/h2
tests/test_invalid_frame_sequences.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_frame_sequences.py
MIT
def test_error_on_invalid_content_length(self, frame_factory, value, request_headers) -> None: """ When an invalid content-length is received, a ProtocolError is thrown. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) c.clear_outbound_data_buffer() f = frame_factory.build_headers_frame( stream_id=1, headers=[*request_headers, ("content-length", value)], ) with pytest.raises(h2.exceptions.ProtocolError): c.receive_data(f.serialize()) expected_frame = frame_factory.build_goaway_frame( last_stream_id=1, error_code=h2.errors.ErrorCodes.PROTOCOL_ERROR, ) assert c.data_to_send() == expected_frame.serialize()
When an invalid content-length is received, a ProtocolError is thrown.
test_error_on_invalid_content_length
python
python-hyper/h2
tests/test_invalid_frame_sequences.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_frame_sequences.py
MIT
def test_invalid_header_data_protocol_error(self, frame_factory, request_headers) -> None: """ If an invalid header block is received, we raise a ProtocolError. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) c.clear_outbound_data_buffer() f = frame_factory.build_headers_frame( stream_id=1, headers=request_headers, ) f.data = b"\x00\x00\x00\x00" with pytest.raises(h2.exceptions.ProtocolError): c.receive_data(f.serialize()) expected_frame = frame_factory.build_goaway_frame( last_stream_id=0, error_code=h2.errors.ErrorCodes.PROTOCOL_ERROR, ) assert c.data_to_send() == expected_frame.serialize()
If an invalid header block is received, we raise a ProtocolError.
test_invalid_header_data_protocol_error
python
python-hyper/h2
tests/test_invalid_frame_sequences.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_frame_sequences.py
MIT
def test_invalid_push_promise_data_protocol_error(self, frame_factory, request_headers) -> None: """ If an invalid header block is received on a PUSH_PROMISE, we raise a ProtocolError. """ c = h2.connection.H2Connection() c.initiate_connection() c.send_headers(stream_id=1, headers=request_headers) c.clear_outbound_data_buffer() f = frame_factory.build_push_promise_frame( stream_id=1, promised_stream_id=2, headers=request_headers, ) f.data = b"\x00\x00\x00\x00" with pytest.raises(h2.exceptions.ProtocolError): c.receive_data(f.serialize()) expected_frame = frame_factory.build_goaway_frame( last_stream_id=0, error_code=h2.errors.ErrorCodes.PROTOCOL_ERROR, ) assert c.data_to_send() == expected_frame.serialize()
If an invalid header block is received on a PUSH_PROMISE, we raise a ProtocolError.
test_invalid_push_promise_data_protocol_error
python
python-hyper/h2
tests/test_invalid_frame_sequences.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_frame_sequences.py
MIT
def test_cannot_receive_push_on_pushed_stream(self, frame_factory, request_headers) -> None: """ If a PUSH_PROMISE frame is received with the parent stream ID being a pushed stream, this is rejected with a PROTOCOL_ERROR. """ c = h2.connection.H2Connection() c.initiate_connection() c.send_headers( stream_id=1, headers=request_headers, end_stream=True, ) f1 = frame_factory.build_push_promise_frame( stream_id=1, promised_stream_id=2, headers=request_headers, ) f2 = frame_factory.build_headers_frame( stream_id=2, headers=self.example_response_headers, ) c.receive_data(f1.serialize() + f2.serialize()) c.clear_outbound_data_buffer() f = frame_factory.build_push_promise_frame( stream_id=2, promised_stream_id=4, headers=request_headers, ) with pytest.raises(h2.exceptions.ProtocolError): c.receive_data(f.serialize()) expected_frame = frame_factory.build_goaway_frame( last_stream_id=2, error_code=h2.errors.ErrorCodes.PROTOCOL_ERROR, ) assert c.data_to_send() == expected_frame.serialize()
If a PUSH_PROMISE frame is received with the parent stream ID being a pushed stream, this is rejected with a PROTOCOL_ERROR.
test_cannot_receive_push_on_pushed_stream
python
python-hyper/h2
tests/test_invalid_frame_sequences.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_frame_sequences.py
MIT
def test_cannot_send_push_on_pushed_stream(self, frame_factory, request_headers) -> None: """ If a user tries to send a PUSH_PROMISE frame with the parent stream ID being a pushed stream, this is rejected with a PROTOCOL_ERROR. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) f = frame_factory.build_headers_frame( stream_id=1, headers=request_headers, ) c.receive_data(f.serialize()) c.push_stream( stream_id=1, promised_stream_id=2, request_headers=request_headers, ) c.send_headers(stream_id=2, headers=self.example_response_headers) with pytest.raises(h2.exceptions.ProtocolError): c.push_stream( stream_id=2, promised_stream_id=4, request_headers=request_headers, )
If a user tries to send a PUSH_PROMISE frame with the parent stream ID being a pushed stream, this is rejected with a PROTOCOL_ERROR.
test_cannot_send_push_on_pushed_stream
python
python-hyper/h2
tests/test_invalid_frame_sequences.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_frame_sequences.py
MIT
def test_headers_event(self, frame_factory, headers) -> None: """ Test invalid headers are rejected with PROTOCOL_ERROR. """ c = h2.connection.H2Connection(config=self.server_config) c.receive_data(frame_factory.preamble()) c.clear_outbound_data_buffer() f = frame_factory.build_headers_frame(headers) data = f.serialize() with pytest.raises(h2.exceptions.ProtocolError): c.receive_data(data) expected_frame = frame_factory.build_goaway_frame( last_stream_id=1, error_code=h2.errors.ErrorCodes.PROTOCOL_ERROR, ) assert c.data_to_send() == expected_frame.serialize()
Test invalid headers are rejected with PROTOCOL_ERROR.
test_headers_event
python
python-hyper/h2
tests/test_invalid_headers.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_headers.py
MIT
def test_push_promise_event(self, frame_factory, headers) -> None: """ If a PUSH_PROMISE header frame is received with an invalid header block it is rejected with a PROTOCOL_ERROR. """ c = h2.connection.H2Connection() c.initiate_connection() c.send_headers( stream_id=1, headers=self.base_request_headers, end_stream=True, ) c.clear_outbound_data_buffer() f = frame_factory.build_push_promise_frame( stream_id=1, promised_stream_id=2, headers=headers, ) data = f.serialize() with pytest.raises(h2.exceptions.ProtocolError): c.receive_data(data) expected_frame = frame_factory.build_goaway_frame( last_stream_id=0, error_code=h2.errors.ErrorCodes.PROTOCOL_ERROR, ) assert c.data_to_send() == expected_frame.serialize()
If a PUSH_PROMISE header frame is received with an invalid header block it is rejected with a PROTOCOL_ERROR.
test_push_promise_event
python
python-hyper/h2
tests/test_invalid_headers.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_headers.py
MIT
def test_push_promise_skipping_validation(self, frame_factory, headers) -> None: """ If we have ``validate_inbound_headers`` disabled, then invalid header blocks in push promise frames are allowed to pass. """ config = h2.config.H2Configuration( client_side=True, validate_inbound_headers=False, ) c = h2.connection.H2Connection(config=config) c.initiate_connection() c.send_headers( stream_id=1, headers=self.base_request_headers, end_stream=True, ) c.clear_outbound_data_buffer() f = frame_factory.build_push_promise_frame( stream_id=1, promised_stream_id=2, headers=headers, ) data = f.serialize() events = c.receive_data(data) assert len(events) == 1 pp_event = events[0] assert pp_event.headers == h2.utilities.utf8_encode_headers(headers)
If we have ``validate_inbound_headers`` disabled, then invalid header blocks in push promise frames are allowed to pass.
test_push_promise_skipping_validation
python
python-hyper/h2
tests/test_invalid_headers.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_headers.py
MIT
def test_headers_event_skipping_validation(self, frame_factory, headers) -> None: """ If we have ``validate_inbound_headers`` disabled, then all of these invalid header blocks are allowed to pass. """ config = h2.config.H2Configuration( client_side=False, validate_inbound_headers=False, ) c = h2.connection.H2Connection(config=config) c.receive_data(frame_factory.preamble()) f = frame_factory.build_headers_frame(headers) data = f.serialize() events = c.receive_data(data) assert len(events) == 1 request_event = events[0] assert request_event.headers == h2.utilities.utf8_encode_headers(headers)
If we have ``validate_inbound_headers`` disabled, then all of these invalid header blocks are allowed to pass.
test_headers_event_skipping_validation
python
python-hyper/h2
tests/test_invalid_headers.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_headers.py
MIT
def test_te_trailers_is_valid(self, frame_factory) -> None: """ `te: trailers` is allowed by the filter. """ headers = ( [*self.base_request_headers, ("te", "trailers")] ) c = h2.connection.H2Connection(config=self.server_config) c.receive_data(frame_factory.preamble()) f = frame_factory.build_headers_frame(headers) data = f.serialize() events = c.receive_data(data) assert len(events) == 1 request_event = events[0] assert request_event.headers == headers
`te: trailers` is allowed by the filter.
test_te_trailers_is_valid
python
python-hyper/h2
tests/test_invalid_headers.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_headers.py
MIT
def test_pseudo_headers_rejected_in_trailer(self, frame_factory) -> None: """ Ensure we reject pseudo headers included in trailers """ trailers = [(":path", "/"), ("extra", "value")] c = h2.connection.H2Connection(config=self.server_config) c.receive_data(frame_factory.preamble()) c.clear_outbound_data_buffer() header_frame = frame_factory.build_headers_frame( self.base_request_headers, ) trailer_frame = frame_factory.build_headers_frame( trailers, flags=["END_STREAM"], ) head = header_frame.serialize() trailer = trailer_frame.serialize() c.receive_data(head) # Raise exception if pseudo header in trailer with pytest.raises(h2.exceptions.ProtocolError) as e: c.receive_data(trailer) assert "pseudo-header in trailer" in str(e.value) # Test appropriate response frame is generated expected_frame = frame_factory.build_goaway_frame( last_stream_id=1, error_code=h2.errors.ErrorCodes.PROTOCOL_ERROR, ) assert c.data_to_send() == expected_frame.serialize()
Ensure we reject pseudo headers included in trailers
test_pseudo_headers_rejected_in_trailer
python
python-hyper/h2
tests/test_invalid_headers.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_headers.py
MIT
def test_headers_event(self, frame_factory, headers) -> None: """ Test sending invalid headers raise a ProtocolError. """ c = h2.connection.H2Connection() c.initiate_connection() # Clear the data, then try to send headers. c.clear_outbound_data_buffer() with pytest.raises(h2.exceptions.ProtocolError): c.send_headers(1, headers)
Test sending invalid headers raise a ProtocolError.
test_headers_event
python
python-hyper/h2
tests/test_invalid_headers.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_headers.py
MIT
def test_send_push_promise(self, frame_factory, headers) -> None: """ Sending invalid headers in a push promise raises a ProtocolError. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) header_frame = frame_factory.build_headers_frame( self.base_request_headers, ) c.receive_data(header_frame.serialize()) # Clear the data, then try to send a push promise. c.clear_outbound_data_buffer() with pytest.raises(h2.exceptions.ProtocolError): c.push_stream( stream_id=1, promised_stream_id=2, request_headers=headers, )
Sending invalid headers in a push promise raises a ProtocolError.
test_send_push_promise
python
python-hyper/h2
tests/test_invalid_headers.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_headers.py
MIT
def test_headers_event_skipping_validation(self, frame_factory, headers) -> None: """ If we have ``validate_outbound_headers`` disabled, then all of these invalid header blocks are allowed to pass. """ config = h2.config.H2Configuration( validate_outbound_headers=False, ) c = h2.connection.H2Connection(config=config) c.initiate_connection() # Clear the data, then send headers. c.clear_outbound_data_buffer() c.send_headers(1, headers) # Ensure headers are still normalized. headers = h2.utilities.utf8_encode_headers(headers) norm_headers = h2.utilities.normalize_outbound_headers( headers, None, False, ) f = frame_factory.build_headers_frame(norm_headers) assert c.data_to_send() == f.serialize()
If we have ``validate_outbound_headers`` disabled, then all of these invalid header blocks are allowed to pass.
test_headers_event_skipping_validation
python
python-hyper/h2
tests/test_invalid_headers.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_headers.py
MIT
def test_push_promise_skipping_validation(self, frame_factory, headers) -> None: """ If we have ``validate_outbound_headers`` disabled, then all of these invalid header blocks are allowed to pass. """ config = h2.config.H2Configuration( client_side=False, validate_outbound_headers=False, ) c = h2.connection.H2Connection(config=config) c.initiate_connection() c.receive_data(frame_factory.preamble()) header_frame = frame_factory.build_headers_frame( self.base_request_headers, ) c.receive_data(header_frame.serialize()) frame_factory.refresh_encoder() # Create push promise frame with normalized headers. headers = h2.utilities.utf8_encode_headers(headers) norm_headers = h2.utilities.normalize_outbound_headers( headers, None, False, ) pp_frame = frame_factory.build_push_promise_frame( stream_id=1, promised_stream_id=2, headers=norm_headers, ) # Clear the data, then send a push promise. c.clear_outbound_data_buffer() c.push_stream( stream_id=1, promised_stream_id=2, request_headers=headers, ) assert c.data_to_send() == pp_frame.serialize()
If we have ``validate_outbound_headers`` disabled, then all of these invalid header blocks are allowed to pass.
test_push_promise_skipping_validation
python
python-hyper/h2
tests/test_invalid_headers.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_headers.py
MIT
def test_headers_event_skip_normalization(self, frame_factory, headers) -> None: """ If we have ``normalize_outbound_headers`` disabled, then all of these invalid header blocks are sent through unmodified. """ config = h2.config.H2Configuration( validate_outbound_headers=False, normalize_outbound_headers=False, ) c = h2.connection.H2Connection(config=config) c.initiate_connection() f = frame_factory.build_headers_frame( headers, stream_id=1, ) # Clear the data, then send headers. c.clear_outbound_data_buffer() c.send_headers(1, headers) assert c.data_to_send() == f.serialize()
If we have ``normalize_outbound_headers`` disabled, then all of these invalid header blocks are sent through unmodified.
test_headers_event_skip_normalization
python
python-hyper/h2
tests/test_invalid_headers.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_headers.py
MIT
def test_push_promise_skip_normalization(self, frame_factory, headers) -> None: """ If we have ``normalize_outbound_headers`` disabled, then all of these invalid header blocks are allowed to pass unmodified. """ config = h2.config.H2Configuration( client_side=False, validate_outbound_headers=False, normalize_outbound_headers=False, ) c = h2.connection.H2Connection(config=config) c.initiate_connection() c.receive_data(frame_factory.preamble()) header_frame = frame_factory.build_headers_frame( self.base_request_headers, ) c.receive_data(header_frame.serialize()) frame_factory.refresh_encoder() pp_frame = frame_factory.build_push_promise_frame( stream_id=1, promised_stream_id=2, headers=headers, ) # Clear the data, then send a push promise. c.clear_outbound_data_buffer() c.push_stream( stream_id=1, promised_stream_id=2, request_headers=headers, ) assert c.data_to_send() == pp_frame.serialize()
If we have ``normalize_outbound_headers`` disabled, then all of these invalid header blocks are allowed to pass unmodified.
test_push_promise_skip_normalization
python
python-hyper/h2
tests/test_invalid_headers.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_headers.py
MIT
def test_strippable_headers(self, frame_factory, headers) -> None: """ Test connection related headers are removed before sending. """ c = h2.connection.H2Connection() c.initiate_connection() # Clear the data, then try to send headers. c.clear_outbound_data_buffer() c.send_headers(1, headers) f = frame_factory.build_headers_frame(self.base_request_headers) assert c.data_to_send() == f.serialize()
Test connection related headers are removed before sending.
test_strippable_headers
python
python-hyper/h2
tests/test_invalid_headers.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_headers.py
MIT
def test_range_of_acceptable_outputs(self, headers, validation_function, hdr_validation_flags) -> None: """ The header validation functions either return the data unchanged or throw a ProtocolError. """ try: assert headers == list(validation_function( headers, hdr_validation_flags)) except h2.exceptions.ProtocolError: assert True
The header validation functions either return the data unchanged or throw a ProtocolError.
test_range_of_acceptable_outputs
python
python-hyper/h2
tests/test_invalid_headers.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_headers.py
MIT
def test_matching_authority_host_headers(self, validation_function, hdr_validation_flags) -> None: """ If a header block has :authority and Host headers and they match, the headers should pass through unchanged. """ headers = [ (b":authority", b"example.com"), (b":path", b"/"), (b":scheme", b"https"), (b":method", b"GET"), (b"host", b"example.com"), ] assert headers == list(h2.utilities.validate_headers( headers, hdr_validation_flags, ))
If a header block has :authority and Host headers and they match, the headers should pass through unchanged.
test_matching_authority_host_headers
python
python-hyper/h2
tests/test_invalid_headers.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_headers.py
MIT
def test_outbound_req_header_extra_pseudo_headers(self, hdr_validation_flags, invalid_header) -> None: """ Outbound request header blocks containing the forbidden request headers fail validation. """ headers = [ (b":path", b"/"), (b":scheme", b"https"), (b":authority", b"google.com"), (b":method", b"GET"), ] headers.append((invalid_header, b"some value")) with pytest.raises(h2.exceptions.ProtocolError): list( h2.utilities.validate_outbound_headers( headers, hdr_validation_flags, ), )
Outbound request header blocks containing the forbidden request headers fail validation.
test_outbound_req_header_extra_pseudo_headers
python
python-hyper/h2
tests/test_invalid_headers.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_headers.py
MIT
def test_inbound_req_header_extra_pseudo_headers(self, hdr_validation_flags, invalid_header) -> None: """ Inbound request header blocks containing the forbidden request headers fail validation. """ headers = [ (b":path", b"/"), (b":scheme", b"https"), (b":authority", b"google.com"), (b":method", b"GET"), ] headers.append((invalid_header, b"some value")) with pytest.raises(h2.exceptions.ProtocolError): list(h2.utilities.validate_headers(headers, hdr_validation_flags))
Inbound request header blocks containing the forbidden request headers fail validation.
test_inbound_req_header_extra_pseudo_headers
python
python-hyper/h2
tests/test_invalid_headers.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_headers.py
MIT
def test_outbound_resp_header_extra_pseudo_headers(self, hdr_validation_flags, invalid_header) -> None: """ Outbound response header blocks containing the forbidden response headers fail validation. """ headers = [(b":status", b"200")] headers.append((invalid_header, b"some value")) with pytest.raises(h2.exceptions.ProtocolError): list( h2.utilities.validate_outbound_headers( headers, hdr_validation_flags, ), )
Outbound response header blocks containing the forbidden response headers fail validation.
test_outbound_resp_header_extra_pseudo_headers
python
python-hyper/h2
tests/test_invalid_headers.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_headers.py
MIT
def test_inbound_resp_header_extra_pseudo_headers(self, hdr_validation_flags, invalid_header) -> None: """ Inbound response header blocks containing the forbidden response headers fail validation. """ headers = [(b":status", b"200")] headers.append((invalid_header, b"some value")) with pytest.raises(h2.exceptions.ProtocolError): list(h2.utilities.validate_headers(headers, hdr_validation_flags))
Inbound response header blocks containing the forbidden response headers fail validation.
test_inbound_resp_header_extra_pseudo_headers
python
python-hyper/h2
tests/test_invalid_headers.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_headers.py
MIT
def test_hpack_bomb_request(self, frame_factory) -> None: """ A HPACK bomb request causes the connection to be torn down with the error code ENHANCE_YOUR_CALM. """ c = h2.connection.H2Connection(config=self.server_config) c.receive_data(frame_factory.preamble()) c.clear_outbound_data_buffer() f = frame_factory.build_headers_frame( self.request_header_block + self.first_header_block, ) data = f.serialize() c.receive_data(data) # Build the attack payload. attack_frame = hyperframe.frame.HeadersFrame(stream_id=3) attack_frame.data = self.second_header_block attack_frame.flags.add("END_HEADERS") data = attack_frame.serialize() with pytest.raises(h2.exceptions.DenialOfServiceError): c.receive_data(data) expected_frame = frame_factory.build_goaway_frame( last_stream_id=1, error_code=h2.errors.ErrorCodes.ENHANCE_YOUR_CALM, ) assert c.data_to_send() == expected_frame.serialize()
A HPACK bomb request causes the connection to be torn down with the error code ENHANCE_YOUR_CALM.
test_hpack_bomb_request
python
python-hyper/h2
tests/test_invalid_headers.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_headers.py
MIT
def test_hpack_bomb_response(self, frame_factory) -> None: """ A HPACK bomb response causes the connection to be torn down with the error code ENHANCE_YOUR_CALM. """ c = h2.connection.H2Connection() c.initiate_connection() c.send_headers( stream_id=1, headers=self.request_header_block, ) c.send_headers( stream_id=3, headers=self.request_header_block, ) c.clear_outbound_data_buffer() f = frame_factory.build_headers_frame( self.response_header_block + self.first_header_block, ) data = f.serialize() c.receive_data(data) # Build the attack payload. attack_frame = hyperframe.frame.HeadersFrame(stream_id=3) attack_frame.data = self.second_header_block attack_frame.flags.add("END_HEADERS") data = attack_frame.serialize() with pytest.raises(h2.exceptions.DenialOfServiceError): c.receive_data(data) expected_frame = frame_factory.build_goaway_frame( last_stream_id=0, error_code=h2.errors.ErrorCodes.ENHANCE_YOUR_CALM, ) assert c.data_to_send() == expected_frame.serialize()
A HPACK bomb response causes the connection to be torn down with the error code ENHANCE_YOUR_CALM.
test_hpack_bomb_response
python
python-hyper/h2
tests/test_invalid_headers.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_headers.py
MIT
def test_hpack_bomb_push(self, frame_factory) -> None: """ A HPACK bomb push causes the connection to be torn down with the error code ENHANCE_YOUR_CALM. """ c = h2.connection.H2Connection() c.initiate_connection() c.send_headers( stream_id=1, headers=self.request_header_block, ) c.clear_outbound_data_buffer() f = frame_factory.build_headers_frame( self.response_header_block + self.first_header_block, ) data = f.serialize() c.receive_data(data) # Build the attack payload. We need to shrink it by four bytes because # the promised_stream_id consumes four bytes of body. attack_frame = hyperframe.frame.PushPromiseFrame(stream_id=3) attack_frame.promised_stream_id = 2 attack_frame.data = self.second_header_block[:-4] attack_frame.flags.add("END_HEADERS") data = attack_frame.serialize() with pytest.raises(h2.exceptions.DenialOfServiceError): c.receive_data(data) expected_frame = frame_factory.build_goaway_frame( last_stream_id=0, error_code=h2.errors.ErrorCodes.ENHANCE_YOUR_CALM, ) assert c.data_to_send() == expected_frame.serialize()
A HPACK bomb push causes the connection to be torn down with the error code ENHANCE_YOUR_CALM.
test_hpack_bomb_push
python
python-hyper/h2
tests/test_invalid_headers.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_headers.py
MIT
def test_reject_headers_when_list_size_shrunk(self, frame_factory) -> None: """ When we've shrunk the header list size, we reject new header blocks that violate the new size. """ c = h2.connection.H2Connection(config=self.server_config) c.receive_data(frame_factory.preamble()) c.clear_outbound_data_buffer() # Receive the first request, which causes no problem. f = frame_factory.build_headers_frame( stream_id=1, headers=self.request_header_block, ) data = f.serialize() c.receive_data(data) # Now, send a settings change. It's un-ACKed at this time. A new # request arrives, also without incident. c.update_settings({h2.settings.SettingCodes.MAX_HEADER_LIST_SIZE: 50}) c.clear_outbound_data_buffer() f = frame_factory.build_headers_frame( stream_id=3, headers=self.request_header_block, ) data = f.serialize() c.receive_data(data) # We get a SETTINGS ACK. f = frame_factory.build_settings_frame({}, ack=True) data = f.serialize() c.receive_data(data) # Now a third request comes in. This explodes. f = frame_factory.build_headers_frame( stream_id=5, headers=self.request_header_block, ) data = f.serialize() with pytest.raises(h2.exceptions.DenialOfServiceError): c.receive_data(data) expected_frame = frame_factory.build_goaway_frame( last_stream_id=3, error_code=h2.errors.ErrorCodes.ENHANCE_YOUR_CALM, ) assert c.data_to_send() == expected_frame.serialize()
When we've shrunk the header list size, we reject new header blocks that violate the new size.
test_reject_headers_when_list_size_shrunk
python
python-hyper/h2
tests/test_invalid_headers.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_headers.py
MIT
def test_reject_headers_when_table_size_shrunk(self, frame_factory) -> None: """ When we've shrunk the header table size, we reject header blocks that do not respect the change. """ c = h2.connection.H2Connection(config=self.server_config) c.receive_data(frame_factory.preamble()) c.clear_outbound_data_buffer() # Receive the first request, which causes no problem. f = frame_factory.build_headers_frame( stream_id=1, headers=self.request_header_block, ) data = f.serialize() c.receive_data(data) # Now, send a settings change. It's un-ACKed at this time. A new # request arrives, also without incident. c.update_settings({h2.settings.SettingCodes.HEADER_TABLE_SIZE: 128}) c.clear_outbound_data_buffer() f = frame_factory.build_headers_frame( stream_id=3, headers=self.request_header_block, ) data = f.serialize() c.receive_data(data) # We get a SETTINGS ACK. f = frame_factory.build_settings_frame({}, ack=True) data = f.serialize() c.receive_data(data) # Now a third request comes in. This explodes, as it does not contain # a dynamic table size update. f = frame_factory.build_headers_frame( stream_id=5, headers=self.request_header_block, ) data = f.serialize() with pytest.raises(h2.exceptions.ProtocolError): c.receive_data(data) expected_frame = frame_factory.build_goaway_frame( last_stream_id=3, error_code=h2.errors.ErrorCodes.PROTOCOL_ERROR, ) assert c.data_to_send() == expected_frame.serialize()
When we've shrunk the header table size, we reject header blocks that do not respect the change.
test_reject_headers_when_table_size_shrunk
python
python-hyper/h2
tests/test_invalid_headers.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_headers.py
MIT
def test_reject_headers_exceeding_table_size(self, frame_factory) -> None: """ When the remote peer sends a dynamic table size update that exceeds our setting, we reject it. """ c = h2.connection.H2Connection(config=self.server_config) c.receive_data(frame_factory.preamble()) c.clear_outbound_data_buffer() # Receive the first request, which causes no problem. f = frame_factory.build_headers_frame( stream_id=1, headers=self.request_header_block, ) data = f.serialize() c.receive_data(data) # Now a second request comes in that sets the table size too high. # This explodes. frame_factory.change_table_size(c.local_settings.header_table_size + 1) f = frame_factory.build_headers_frame( stream_id=5, headers=self.request_header_block, ) data = f.serialize() with pytest.raises(h2.exceptions.ProtocolError): c.receive_data(data) expected_frame = frame_factory.build_goaway_frame( last_stream_id=1, error_code=h2.errors.ErrorCodes.PROTOCOL_ERROR, ) assert c.data_to_send() == expected_frame.serialize()
When the remote peer sends a dynamic table size update that exceeds our setting, we reject it.
test_reject_headers_exceeding_table_size
python
python-hyper/h2
tests/test_invalid_headers.py
https://github.com/python-hyper/h2/blob/master/tests/test_invalid_headers.py
MIT
def test_streams_may_not_depend_on_themselves(self, frame_factory, request_headers) -> None: """ A stream adjusted to depend on itself causes a Protocol Error. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) c.clear_outbound_data_buffer() f = frame_factory.build_headers_frame( headers=request_headers, stream_id=3, flags=["PRIORITY"], stream_weight=15, depends_on=1, exclusive=True, ) c.receive_data(f.serialize()) c.clear_outbound_data_buffer() f = frame_factory.build_priority_frame( stream_id=3, depends_on=3, weight=15, ) with pytest.raises(h2.exceptions.ProtocolError): c.receive_data(f.serialize()) expected_frame = frame_factory.build_goaway_frame( last_stream_id=3, error_code=h2.errors.ErrorCodes.PROTOCOL_ERROR, ) assert c.data_to_send() == expected_frame.serialize()
A stream adjusted to depend on itself causes a Protocol Error.
test_streams_may_not_depend_on_themselves
python
python-hyper/h2
tests/test_priority.py
https://github.com/python-hyper/h2/blob/master/tests/test_priority.py
MIT
def test_emit_headers_with_priority_info(self, depends_on, weight, exclusive, frame_factory, request_headers) -> None: """ It is possible to send a headers frame with priority information on it. """ c = h2.connection.H2Connection() c.initiate_connection() c.clear_outbound_data_buffer() c.send_headers( headers=request_headers, stream_id=3, priority_weight=weight, priority_depends_on=depends_on, priority_exclusive=exclusive, ) f = frame_factory.build_headers_frame( headers=request_headers, stream_id=3, flags=["PRIORITY"], stream_weight=weight - 1, depends_on=depends_on, exclusive=exclusive, ) assert c.data_to_send() == f.serialize()
It is possible to send a headers frame with priority information on it.
test_emit_headers_with_priority_info
python
python-hyper/h2
tests/test_priority.py
https://github.com/python-hyper/h2/blob/master/tests/test_priority.py
MIT
def test_may_not_prioritize_stream_to_depend_on_self(self, frame_factory, request_headers) -> None: """ A stream adjusted to depend on itself causes a Protocol Error. """ c = h2.connection.H2Connection() c.initiate_connection() c.receive_data(frame_factory.preamble()) c.send_headers( headers=request_headers, stream_id=3, priority_weight=255, priority_depends_on=0, priority_exclusive=False, ) c.clear_outbound_data_buffer() with pytest.raises(h2.exceptions.ProtocolError): c.prioritize( stream_id=3, depends_on=3, ) assert not c.data_to_send()
A stream adjusted to depend on itself causes a Protocol Error.
test_may_not_prioritize_stream_to_depend_on_self
python
python-hyper/h2
tests/test_priority.py
https://github.com/python-hyper/h2/blob/master/tests/test_priority.py
MIT
def test_may_not_initially_set_stream_depend_on_self(self, frame_factory, request_headers) -> None: """ A stream that starts by depending on itself causes a Protocol Error. """ c = h2.connection.H2Connection() c.initiate_connection() c.receive_data(frame_factory.preamble()) c.clear_outbound_data_buffer() with pytest.raises(h2.exceptions.ProtocolError): c.send_headers( headers=request_headers, stream_id=3, priority_depends_on=3, ) assert not c.data_to_send()
A stream that starts by depending on itself causes a Protocol Error.
test_may_not_initially_set_stream_depend_on_self
python
python-hyper/h2
tests/test_priority.py
https://github.com/python-hyper/h2/blob/master/tests/test_priority.py
MIT
def test_prioritize_requires_valid_weight(self, weight) -> None: """ A call to prioritize with an invalid weight causes a ProtocolError. """ c = h2.connection.H2Connection() c.initiate_connection() c.clear_outbound_data_buffer() with pytest.raises(h2.exceptions.ProtocolError): c.prioritize(stream_id=1, weight=weight) assert not c.data_to_send()
A call to prioritize with an invalid weight causes a ProtocolError.
test_prioritize_requires_valid_weight
python
python-hyper/h2
tests/test_priority.py
https://github.com/python-hyper/h2/blob/master/tests/test_priority.py
MIT
def test_send_headers_requires_valid_weight(self, weight, request_headers) -> None: """ A call to send_headers with an invalid weight causes a ProtocolError. """ c = h2.connection.H2Connection() c.initiate_connection() c.clear_outbound_data_buffer() with pytest.raises(h2.exceptions.ProtocolError): c.send_headers( stream_id=1, headers=request_headers, priority_weight=weight, ) assert not c.data_to_send()
A call to send_headers with an invalid weight causes a ProtocolError.
test_send_headers_requires_valid_weight
python
python-hyper/h2
tests/test_priority.py
https://github.com/python-hyper/h2/blob/master/tests/test_priority.py
MIT
def test_servers_cannot_prioritize(self, frame_factory, request_headers) -> None: """ Server stacks are not allowed to call ``prioritize()``. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) c.clear_outbound_data_buffer() f = frame_factory.build_headers_frame( stream_id=1, headers=request_headers, ) c.receive_data(f.serialize()) with pytest.raises(h2.exceptions.RFC1122Error): c.prioritize(stream_id=1)
Server stacks are not allowed to call ``prioritize()``.
test_servers_cannot_prioritize
python
python-hyper/h2
tests/test_priority.py
https://github.com/python-hyper/h2/blob/master/tests/test_priority.py
MIT
def test_servers_cannot_prioritize_with_headers(self, frame_factory, request_headers) -> None: """ Server stacks are not allowed to prioritize on headers either. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) c.clear_outbound_data_buffer() f = frame_factory.build_headers_frame( stream_id=1, headers=request_headers, ) c.receive_data(f.serialize()) with pytest.raises(h2.exceptions.RFC1122Error): c.send_headers( stream_id=1, headers=self.example_response_headers, priority_weight=16, )
Server stacks are not allowed to prioritize on headers either.
test_servers_cannot_prioritize_with_headers
python
python-hyper/h2
tests/test_priority.py
https://github.com/python-hyper/h2/blob/master/tests/test_priority.py
MIT
def test_request_received_related_all(self, frame_factory, request_headers) -> None: """ RequestReceived has two possible related events: PriorityUpdated and StreamEnded, all fired when a single HEADERS frame is received. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) input_frame = frame_factory.build_headers_frame( headers=request_headers, flags=["END_STREAM", "PRIORITY"], stream_weight=15, depends_on=0, exclusive=False, ) events = c.receive_data(input_frame.serialize()) assert len(events) == 3 base_event = events[0] other_events = events[1:] assert base_event.stream_ended in other_events assert isinstance(base_event.stream_ended, h2.events.StreamEnded) assert base_event.priority_updated in other_events assert isinstance( base_event.priority_updated, h2.events.PriorityUpdated, )
RequestReceived has two possible related events: PriorityUpdated and StreamEnded, all fired when a single HEADERS frame is received.
test_request_received_related_all
python
python-hyper/h2
tests/test_related_events.py
https://github.com/python-hyper/h2/blob/master/tests/test_related_events.py
MIT
def test_request_received_related_priority(self, frame_factory, request_headers) -> None: """ RequestReceived can be related to PriorityUpdated. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) input_frame = frame_factory.build_headers_frame( headers=request_headers, flags=["PRIORITY"], stream_weight=15, depends_on=0, exclusive=False, ) events = c.receive_data(input_frame.serialize()) assert len(events) == 2 base_event = events[0] priority_updated_event = events[1] assert base_event.priority_updated is priority_updated_event assert base_event.stream_ended is None assert isinstance( base_event.priority_updated, h2.events.PriorityUpdated, )
RequestReceived can be related to PriorityUpdated.
test_request_received_related_priority
python
python-hyper/h2
tests/test_related_events.py
https://github.com/python-hyper/h2/blob/master/tests/test_related_events.py
MIT
def test_request_received_related_stream_ended(self, frame_factory, request_headers) -> None: """ RequestReceived can be related to StreamEnded. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) input_frame = frame_factory.build_headers_frame( headers=request_headers, flags=["END_STREAM"], ) events = c.receive_data(input_frame.serialize()) assert len(events) == 2 base_event = events[0] stream_ended_event = events[1] assert base_event.stream_ended is stream_ended_event assert base_event.priority_updated is None assert isinstance(base_event.stream_ended, h2.events.StreamEnded)
RequestReceived can be related to StreamEnded.
test_request_received_related_stream_ended
python
python-hyper/h2
tests/test_related_events.py
https://github.com/python-hyper/h2/blob/master/tests/test_related_events.py
MIT
def test_response_received_related_nothing(self, frame_factory, request_headers) -> None: """ ResponseReceived is ordinarily related to no events. """ c = h2.connection.H2Connection() c.initiate_connection() c.send_headers(stream_id=1, headers=request_headers) input_frame = frame_factory.build_headers_frame( headers=self.example_response_headers, ) events = c.receive_data(input_frame.serialize()) assert len(events) == 1 base_event = events[0] assert base_event.stream_ended is None assert base_event.priority_updated is None
ResponseReceived is ordinarily related to no events.
test_response_received_related_nothing
python
python-hyper/h2
tests/test_related_events.py
https://github.com/python-hyper/h2/blob/master/tests/test_related_events.py
MIT
def test_response_received_related_all(self, frame_factory, request_headers) -> None: """ ResponseReceived has two possible related events: PriorityUpdated and StreamEnded, all fired when a single HEADERS frame is received. """ c = h2.connection.H2Connection() c.initiate_connection() c.send_headers(stream_id=1, headers=request_headers) input_frame = frame_factory.build_headers_frame( headers=self.example_response_headers, flags=["END_STREAM", "PRIORITY"], stream_weight=15, depends_on=0, exclusive=False, ) events = c.receive_data(input_frame.serialize()) assert len(events) == 3 base_event = events[0] other_events = events[1:] assert base_event.stream_ended in other_events assert isinstance(base_event.stream_ended, h2.events.StreamEnded) assert base_event.priority_updated in other_events assert isinstance( base_event.priority_updated, h2.events.PriorityUpdated, )
ResponseReceived has two possible related events: PriorityUpdated and StreamEnded, all fired when a single HEADERS frame is received.
test_response_received_related_all
python
python-hyper/h2
tests/test_related_events.py
https://github.com/python-hyper/h2/blob/master/tests/test_related_events.py
MIT
def test_response_received_related_priority(self, frame_factory, request_headers) -> None: """ ResponseReceived can be related to PriorityUpdated. """ c = h2.connection.H2Connection() c.initiate_connection() c.send_headers(stream_id=1, headers=request_headers) input_frame = frame_factory.build_headers_frame( headers=self.example_response_headers, flags=["PRIORITY"], stream_weight=15, depends_on=0, exclusive=False, ) events = c.receive_data(input_frame.serialize()) assert len(events) == 2 base_event = events[0] priority_updated_event = events[1] assert base_event.priority_updated is priority_updated_event assert base_event.stream_ended is None assert isinstance( base_event.priority_updated, h2.events.PriorityUpdated, )
ResponseReceived can be related to PriorityUpdated.
test_response_received_related_priority
python
python-hyper/h2
tests/test_related_events.py
https://github.com/python-hyper/h2/blob/master/tests/test_related_events.py
MIT
def test_response_received_related_stream_ended(self, frame_factory, request_headers) -> None: """ ResponseReceived can be related to StreamEnded. """ c = h2.connection.H2Connection() c.initiate_connection() c.send_headers(stream_id=1, headers=request_headers) input_frame = frame_factory.build_headers_frame( headers=self.example_response_headers, flags=["END_STREAM"], ) events = c.receive_data(input_frame.serialize()) assert len(events) == 2 base_event = events[0] stream_ended_event = events[1] assert base_event.stream_ended is stream_ended_event assert base_event.priority_updated is None assert isinstance(base_event.stream_ended, h2.events.StreamEnded)
ResponseReceived can be related to StreamEnded.
test_response_received_related_stream_ended
python
python-hyper/h2
tests/test_related_events.py
https://github.com/python-hyper/h2/blob/master/tests/test_related_events.py
MIT
def test_trailers_received_related_all(self, frame_factory, request_headers) -> None: """ TrailersReceived has two possible related events: PriorityUpdated and StreamEnded, all fired when a single HEADERS frame is received. """ c = h2.connection.H2Connection() c.initiate_connection() c.send_headers(stream_id=1, headers=request_headers) f = frame_factory.build_headers_frame( headers=self.example_response_headers, ) c.receive_data(f.serialize()) input_frame = frame_factory.build_headers_frame( headers=self.example_trailers, flags=["END_STREAM", "PRIORITY"], stream_weight=15, depends_on=0, exclusive=False, ) events = c.receive_data(input_frame.serialize()) assert len(events) == 3 base_event = events[0] other_events = events[1:] assert base_event.stream_ended in other_events assert isinstance(base_event.stream_ended, h2.events.StreamEnded) assert base_event.priority_updated in other_events assert isinstance( base_event.priority_updated, h2.events.PriorityUpdated, )
TrailersReceived has two possible related events: PriorityUpdated and StreamEnded, all fired when a single HEADERS frame is received.
test_trailers_received_related_all
python
python-hyper/h2
tests/test_related_events.py
https://github.com/python-hyper/h2/blob/master/tests/test_related_events.py
MIT
def test_trailers_received_related_stream_ended(self, frame_factory, request_headers) -> None: """ TrailersReceived can be related to StreamEnded by itself. """ c = h2.connection.H2Connection() c.initiate_connection() c.send_headers(stream_id=1, headers=request_headers) f = frame_factory.build_headers_frame( headers=self.example_response_headers, ) c.receive_data(f.serialize()) input_frame = frame_factory.build_headers_frame( headers=self.example_trailers, flags=["END_STREAM"], ) events = c.receive_data(input_frame.serialize()) assert len(events) == 2 base_event = events[0] stream_ended_event = events[1] assert base_event.stream_ended is stream_ended_event assert base_event.priority_updated is None assert isinstance(base_event.stream_ended, h2.events.StreamEnded)
TrailersReceived can be related to StreamEnded by itself.
test_trailers_received_related_stream_ended
python
python-hyper/h2
tests/test_related_events.py
https://github.com/python-hyper/h2/blob/master/tests/test_related_events.py
MIT
def test_informational_response_related_nothing(self, frame_factory, request_headers) -> None: """ InformationalResponseReceived in the standard case is related to nothing. """ c = h2.connection.H2Connection() c.initiate_connection() c.send_headers(stream_id=1, headers=request_headers) input_frame = frame_factory.build_headers_frame( headers=self.informational_response_headers, ) events = c.receive_data(input_frame.serialize()) assert len(events) == 1 base_event = events[0] assert base_event.priority_updated is None
InformationalResponseReceived in the standard case is related to nothing.
test_informational_response_related_nothing
python
python-hyper/h2
tests/test_related_events.py
https://github.com/python-hyper/h2/blob/master/tests/test_related_events.py
MIT
def test_informational_response_received_related_all(self, frame_factory, request_headers) -> None: """ InformationalResponseReceived has one possible related event: PriorityUpdated, fired when a single HEADERS frame is received. """ c = h2.connection.H2Connection() c.initiate_connection() c.send_headers(stream_id=1, headers=request_headers) input_frame = frame_factory.build_headers_frame( headers=self.informational_response_headers, flags=["PRIORITY"], stream_weight=15, depends_on=0, exclusive=False, ) events = c.receive_data(input_frame.serialize()) assert len(events) == 2 base_event = events[0] priority_updated_event = events[1] assert base_event.priority_updated is priority_updated_event assert isinstance( base_event.priority_updated, h2.events.PriorityUpdated, )
InformationalResponseReceived has one possible related event: PriorityUpdated, fired when a single HEADERS frame is received.
test_informational_response_received_related_all
python
python-hyper/h2
tests/test_related_events.py
https://github.com/python-hyper/h2/blob/master/tests/test_related_events.py
MIT
def test_data_received_normally_relates_to_nothing(self, frame_factory, request_headers) -> None: """ A plain DATA frame leads to DataReceieved with no related events. """ c = h2.connection.H2Connection() c.initiate_connection() c.send_headers(stream_id=1, headers=request_headers) f = frame_factory.build_headers_frame( headers=self.example_response_headers, ) c.receive_data(f.serialize()) input_frame = frame_factory.build_data_frame( data=b"some data", ) events = c.receive_data(input_frame.serialize()) assert len(events) == 1 base_event = events[0] assert base_event.stream_ended is None
A plain DATA frame leads to DataReceieved with no related events.
test_data_received_normally_relates_to_nothing
python
python-hyper/h2
tests/test_related_events.py
https://github.com/python-hyper/h2/blob/master/tests/test_related_events.py
MIT
def test_data_received_related_stream_ended(self, frame_factory, request_headers) -> None: """ DataReceived can be related to StreamEnded by itself. """ c = h2.connection.H2Connection() c.initiate_connection() c.send_headers(stream_id=1, headers=request_headers) f = frame_factory.build_headers_frame( headers=self.example_response_headers, ) c.receive_data(f.serialize()) input_frame = frame_factory.build_data_frame( data=b"some data", flags=["END_STREAM"], ) events = c.receive_data(input_frame.serialize()) assert len(events) == 2 base_event = events[0] stream_ended_event = events[1] assert base_event.stream_ended is stream_ended_event assert isinstance(base_event.stream_ended, h2.events.StreamEnded)
DataReceived can be related to StreamEnded by itself.
test_data_received_related_stream_ended
python
python-hyper/h2
tests/test_related_events.py
https://github.com/python-hyper/h2/blob/master/tests/test_related_events.py
MIT
def test_receiving_altsvc_stream_zero(self, frame_factory) -> None: """ An ALTSVC frame received on stream zero correctly transposes all the fields from the frames. """ c = h2.connection.H2Connection() c.initiate_connection() c.clear_outbound_data_buffer() f = frame_factory.build_alt_svc_frame( stream_id=0, origin=b"example.com", field=b'h2=":8000"; ma=60', ) events = c.receive_data(f.serialize()) assert len(events) == 1 event = events[0] assert isinstance(event, h2.events.AlternativeServiceAvailable) assert event.origin == b"example.com" assert event.field_value == b'h2=":8000"; ma=60' # No data gets sent. assert not c.data_to_send()
An ALTSVC frame received on stream zero correctly transposes all the fields from the frames.
test_receiving_altsvc_stream_zero
python
python-hyper/h2
tests/test_rfc7838.py
https://github.com/python-hyper/h2/blob/master/tests/test_rfc7838.py
MIT
def test_receiving_altsvc_stream_zero_no_origin(self, frame_factory) -> None: """ An ALTSVC frame received on stream zero without an origin field is ignored. """ c = h2.connection.H2Connection() c.initiate_connection() c.clear_outbound_data_buffer() f = frame_factory.build_alt_svc_frame( stream_id=0, origin=b"", field=b'h2=":8000"; ma=60', ) events = c.receive_data(f.serialize()) assert not events assert not c.data_to_send()
An ALTSVC frame received on stream zero without an origin field is ignored.
test_receiving_altsvc_stream_zero_no_origin
python
python-hyper/h2
tests/test_rfc7838.py
https://github.com/python-hyper/h2/blob/master/tests/test_rfc7838.py
MIT
def test_receiving_altsvc_on_stream(self, frame_factory, request_headers) -> None: """ An ALTSVC frame received on a stream correctly transposes all the fields from the frame and attaches the expected origin. """ c = h2.connection.H2Connection() c.initiate_connection() c.send_headers(stream_id=1, headers=request_headers) c.clear_outbound_data_buffer() f = frame_factory.build_alt_svc_frame( stream_id=1, origin=b"", field=b'h2=":8000"; ma=60', ) events = c.receive_data(f.serialize()) assert len(events) == 1 event = events[0] assert isinstance(event, h2.events.AlternativeServiceAvailable) assert event.origin == b"example.com" assert event.field_value == b'h2=":8000"; ma=60' # No data gets sent. assert not c.data_to_send()
An ALTSVC frame received on a stream correctly transposes all the fields from the frame and attaches the expected origin.
test_receiving_altsvc_on_stream
python
python-hyper/h2
tests/test_rfc7838.py
https://github.com/python-hyper/h2/blob/master/tests/test_rfc7838.py
MIT
def test_receiving_altsvc_on_stream_with_origin(self, frame_factory, request_headers) -> None: """ An ALTSVC frame received on a stream with an origin field present gets ignored. """ c = h2.connection.H2Connection() c.initiate_connection() c.send_headers(stream_id=1, headers=request_headers) c.clear_outbound_data_buffer() f = frame_factory.build_alt_svc_frame( stream_id=1, origin=b"example.com", field=b'h2=":8000"; ma=60', ) events = c.receive_data(f.serialize()) assert len(events) == 0 assert not c.data_to_send()
An ALTSVC frame received on a stream with an origin field present gets ignored.
test_receiving_altsvc_on_stream_with_origin
python
python-hyper/h2
tests/test_rfc7838.py
https://github.com/python-hyper/h2/blob/master/tests/test_rfc7838.py
MIT
def test_receiving_altsvc_on_stream_not_yet_opened(self, frame_factory) -> None: """ When an ALTSVC frame is received on a stream the client hasn't yet opened, the frame is ignored. """ c = h2.connection.H2Connection() c.initiate_connection() c.clear_outbound_data_buffer() # We'll test this twice, once on a client-initiated stream ID and once # on a server initiated one. f1 = frame_factory.build_alt_svc_frame( stream_id=1, origin=b"", field=b'h2=":8000"; ma=60', ) f2 = frame_factory.build_alt_svc_frame( stream_id=2, origin=b"", field=b'h2=":8000"; ma=60', ) events = c.receive_data(f1.serialize() + f2.serialize()) assert len(events) == 0 assert not c.data_to_send()
When an ALTSVC frame is received on a stream the client hasn't yet opened, the frame is ignored.
test_receiving_altsvc_on_stream_not_yet_opened
python
python-hyper/h2
tests/test_rfc7838.py
https://github.com/python-hyper/h2/blob/master/tests/test_rfc7838.py
MIT
def test_receiving_altsvc_before_sending_headers(self, frame_factory) -> None: """ When an ALTSVC frame is received but the client hasn't sent headers yet it gets ignored. """ c = h2.connection.H2Connection() c.initiate_connection() # We need to create the idle stream. We have to do it by calling # a private API. While this can't naturally happen in hyper-h2 (we # don't currently have a mechanism by which this could occur), it could # happen in the future and we defend against it. c._begin_new_stream( stream_id=1, allowed_ids=h2.connection.AllowedStreamIDs.ODD, ) c.clear_outbound_data_buffer() f = frame_factory.build_alt_svc_frame( stream_id=1, origin=b"", field=b'h2=":8000"; ma=60', ) events = c.receive_data(f.serialize()) assert len(events) == 0 assert not c.data_to_send()
When an ALTSVC frame is received but the client hasn't sent headers yet it gets ignored.
test_receiving_altsvc_before_sending_headers
python
python-hyper/h2
tests/test_rfc7838.py
https://github.com/python-hyper/h2/blob/master/tests/test_rfc7838.py
MIT
def test_receiving_altsvc_after_receiving_headers(self, frame_factory, request_headers) -> None: """ When an ALTSVC frame is received but the server has already sent headers it gets ignored. """ c = h2.connection.H2Connection() c.initiate_connection() c.send_headers(stream_id=1, headers=request_headers) f = frame_factory.build_headers_frame( headers=self.example_response_headers, ) c.receive_data(f.serialize()) c.clear_outbound_data_buffer() f = frame_factory.build_alt_svc_frame( stream_id=1, origin=b"", field=b'h2=":8000"; ma=60', ) events = c.receive_data(f.serialize()) assert len(events) == 0 assert not c.data_to_send()
When an ALTSVC frame is received but the server has already sent headers it gets ignored.
test_receiving_altsvc_after_receiving_headers
python
python-hyper/h2
tests/test_rfc7838.py
https://github.com/python-hyper/h2/blob/master/tests/test_rfc7838.py
MIT
def test_receiving_altsvc_on_closed_stream(self, frame_factory, request_headers) -> None: """ When an ALTSVC frame is received on a closed stream, we ignore it. """ c = h2.connection.H2Connection() c.initiate_connection() c.send_headers( stream_id=1, headers=request_headers, end_stream=True, ) f = frame_factory.build_headers_frame( headers=self.example_response_headers, flags=["END_STREAM"], ) c.receive_data(f.serialize()) c.clear_outbound_data_buffer() f = frame_factory.build_alt_svc_frame( stream_id=1, origin=b"", field=b'h2=":8000"; ma=60', ) events = c.receive_data(f.serialize()) assert len(events) == 0 assert not c.data_to_send()
When an ALTSVC frame is received on a closed stream, we ignore it.
test_receiving_altsvc_on_closed_stream
python
python-hyper/h2
tests/test_rfc7838.py
https://github.com/python-hyper/h2/blob/master/tests/test_rfc7838.py
MIT
def test_receiving_altsvc_on_pushed_stream(self, frame_factory, request_headers) -> None: """ When an ALTSVC frame is received on a stream that the server pushed, the frame is accepted. """ c = h2.connection.H2Connection() c.initiate_connection() c.send_headers(stream_id=1, headers=request_headers) f = frame_factory.build_push_promise_frame( stream_id=1, promised_stream_id=2, headers=request_headers, ) c.receive_data(f.serialize()) c.clear_outbound_data_buffer() f = frame_factory.build_alt_svc_frame( stream_id=2, origin=b"", field=b'h2=":8000"; ma=60', ) events = c.receive_data(f.serialize()) assert len(events) == 1 event = events[0] assert isinstance(event, h2.events.AlternativeServiceAvailable) assert event.origin == b"example.com" assert event.field_value == b'h2=":8000"; ma=60' # No data gets sent. assert not c.data_to_send()
When an ALTSVC frame is received on a stream that the server pushed, the frame is accepted.
test_receiving_altsvc_on_pushed_stream
python
python-hyper/h2
tests/test_rfc7838.py
https://github.com/python-hyper/h2/blob/master/tests/test_rfc7838.py
MIT
def test_cannot_send_explicit_alternative_service(self, frame_factory, request_headers) -> None: """ A client cannot send an explicit alternative service. """ c = h2.connection.H2Connection() c.initiate_connection() c.send_headers(stream_id=1, headers=request_headers) c.clear_outbound_data_buffer() with pytest.raises(h2.exceptions.ProtocolError): c.advertise_alternative_service( field_value=b'h2=":8000"; ma=60', origin=b"example.com", )
A client cannot send an explicit alternative service.
test_cannot_send_explicit_alternative_service
python
python-hyper/h2
tests/test_rfc7838.py
https://github.com/python-hyper/h2/blob/master/tests/test_rfc7838.py
MIT
def test_cannot_send_implicit_alternative_service(self, frame_factory, request_headers) -> None: """ A client cannot send an implicit alternative service. """ c = h2.connection.H2Connection() c.initiate_connection() c.send_headers(stream_id=1, headers=request_headers) c.clear_outbound_data_buffer() with pytest.raises(h2.exceptions.ProtocolError): c.advertise_alternative_service( field_value=b'h2=":8000"; ma=60', stream_id=1, )
A client cannot send an implicit alternative service.
test_cannot_send_implicit_alternative_service
python
python-hyper/h2
tests/test_rfc7838.py
https://github.com/python-hyper/h2/blob/master/tests/test_rfc7838.py
MIT
def test_receiving_altsvc_as_server_stream_zero(self, frame_factory) -> None: """ When an ALTSVC frame is received on stream zero and we are a server, we ignore it. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) c.clear_outbound_data_buffer() f = frame_factory.build_alt_svc_frame( stream_id=0, origin=b"example.com", field=b'h2=":8000"; ma=60', ) events = c.receive_data(f.serialize()) assert len(events) == 0 assert not c.data_to_send()
When an ALTSVC frame is received on stream zero and we are a server, we ignore it.
test_receiving_altsvc_as_server_stream_zero
python
python-hyper/h2
tests/test_rfc7838.py
https://github.com/python-hyper/h2/blob/master/tests/test_rfc7838.py
MIT
def test_receiving_altsvc_as_server_on_stream(self, frame_factory, request_headers) -> None: """ When an ALTSVC frame is received on a stream and we are a server, we ignore it. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) f = frame_factory.build_headers_frame( headers=request_headers, ) c.receive_data(f.serialize()) c.clear_outbound_data_buffer() f = frame_factory.build_alt_svc_frame( stream_id=1, origin=b"", field=b'h2=":8000"; ma=60', ) events = c.receive_data(f.serialize()) assert len(events) == 0 assert not c.data_to_send()
When an ALTSVC frame is received on a stream and we are a server, we ignore it.
test_receiving_altsvc_as_server_on_stream
python
python-hyper/h2
tests/test_rfc7838.py
https://github.com/python-hyper/h2/blob/master/tests/test_rfc7838.py
MIT
def test_sending_explicit_alternative_service(self, frame_factory) -> None: """ A server can send an explicit alternative service. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) c.clear_outbound_data_buffer() c.advertise_alternative_service( field_value=b'h2=":8000"; ma=60', origin=b"example.com", ) f = frame_factory.build_alt_svc_frame( stream_id=0, origin=b"example.com", field=b'h2=":8000"; ma=60', ) assert c.data_to_send() == f.serialize()
A server can send an explicit alternative service.
test_sending_explicit_alternative_service
python
python-hyper/h2
tests/test_rfc7838.py
https://github.com/python-hyper/h2/blob/master/tests/test_rfc7838.py
MIT
def test_sending_implicit_alternative_service(self, frame_factory, request_headers) -> None: """ A server can send an implicit alternative service. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) f = frame_factory.build_headers_frame( headers=request_headers, ) c.receive_data(f.serialize()) c.clear_outbound_data_buffer() c.advertise_alternative_service( field_value=b'h2=":8000"; ma=60', stream_id=1, ) f = frame_factory.build_alt_svc_frame( stream_id=1, origin=b"", field=b'h2=":8000"; ma=60', ) assert c.data_to_send() == f.serialize()
A server can send an implicit alternative service.
test_sending_implicit_alternative_service
python
python-hyper/h2
tests/test_rfc7838.py
https://github.com/python-hyper/h2/blob/master/tests/test_rfc7838.py
MIT
def test_no_implicit_alternative_service_before_headers(self, frame_factory) -> None: """ If headers haven't been received yet, the server forbids sending an implicit alternative service. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) c.clear_outbound_data_buffer() with pytest.raises(h2.exceptions.ProtocolError): c.advertise_alternative_service( field_value=b'h2=":8000"; ma=60', stream_id=1, )
If headers haven't been received yet, the server forbids sending an implicit alternative service.
test_no_implicit_alternative_service_before_headers
python
python-hyper/h2
tests/test_rfc7838.py
https://github.com/python-hyper/h2/blob/master/tests/test_rfc7838.py
MIT
def test_no_implicit_alternative_service_after_response(self, frame_factory, request_headers) -> None: """ If the server has sent response headers, hyper-h2 forbids sending an implicit alternative service. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) f = frame_factory.build_headers_frame( headers=request_headers, ) c.receive_data(f.serialize()) c.send_headers(stream_id=1, headers=self.example_response_headers) c.clear_outbound_data_buffer() with pytest.raises(h2.exceptions.ProtocolError): c.advertise_alternative_service( field_value=b'h2=":8000"; ma=60', stream_id=1, )
If the server has sent response headers, hyper-h2 forbids sending an implicit alternative service.
test_no_implicit_alternative_service_after_response
python
python-hyper/h2
tests/test_rfc7838.py
https://github.com/python-hyper/h2/blob/master/tests/test_rfc7838.py
MIT
def test_cannot_provide_origin_and_stream_id(self, frame_factory, request_headers) -> None: """ The user cannot provide both the origin and stream_id arguments when advertising alternative services. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) f = frame_factory.build_headers_frame( headers=request_headers, ) c.receive_data(f.serialize()) with pytest.raises(ValueError): c.advertise_alternative_service( field_value=b'h2=":8000"; ma=60', origin=b"example.com", stream_id=1, )
The user cannot provide both the origin and stream_id arguments when advertising alternative services.
test_cannot_provide_origin_and_stream_id
python
python-hyper/h2
tests/test_rfc7838.py
https://github.com/python-hyper/h2/blob/master/tests/test_rfc7838.py
MIT
def test_cannot_provide_unicode_altsvc_field(self, frame_factory) -> None: """ The user cannot provide the field value for alternative services as a unicode string. """ c = h2.connection.H2Connection(config=self.server_config) c.initiate_connection() c.receive_data(frame_factory.preamble()) with pytest.raises(ValueError): c.advertise_alternative_service( field_value='h2=":8000"; ma=60', origin=b"example.com", )
The user cannot provide the field value for alternative services as a unicode string.
test_cannot_provide_unicode_altsvc_field
python
python-hyper/h2
tests/test_rfc7838.py
https://github.com/python-hyper/h2/blob/master/tests/test_rfc7838.py
MIT
def test_settings_defaults_client(self) -> None: """ The Settings object begins with the appropriate defaults for clients. """ s = h2.settings.Settings(client=True) assert s[h2.settings.SettingCodes.HEADER_TABLE_SIZE] == 4096 assert s[h2.settings.SettingCodes.ENABLE_PUSH] == 1 assert s[h2.settings.SettingCodes.INITIAL_WINDOW_SIZE] == 65535 assert s[h2.settings.SettingCodes.MAX_FRAME_SIZE] == 16384 assert s[h2.settings.SettingCodes.ENABLE_CONNECT_PROTOCOL] == 0
The Settings object begins with the appropriate defaults for clients.
test_settings_defaults_client
python
python-hyper/h2
tests/test_settings.py
https://github.com/python-hyper/h2/blob/master/tests/test_settings.py
MIT
def test_settings_defaults_server(self) -> None: """ The Settings object begins with the appropriate defaults for servers. """ s = h2.settings.Settings(client=False) assert s[h2.settings.SettingCodes.HEADER_TABLE_SIZE] == 4096 assert s[h2.settings.SettingCodes.ENABLE_PUSH] == 0 assert s[h2.settings.SettingCodes.INITIAL_WINDOW_SIZE] == 65535 assert s[h2.settings.SettingCodes.MAX_FRAME_SIZE] == 16384 assert s[h2.settings.SettingCodes.ENABLE_CONNECT_PROTOCOL] == 0
The Settings object begins with the appropriate defaults for servers.
test_settings_defaults_server
python
python-hyper/h2
tests/test_settings.py
https://github.com/python-hyper/h2/blob/master/tests/test_settings.py
MIT
def test_can_set_initial_values(self, client) -> None: """ The Settings object can be provided initial values that override the defaults. """ overrides = { h2.settings.SettingCodes.HEADER_TABLE_SIZE: 8080, h2.settings.SettingCodes.MAX_FRAME_SIZE: 16388, h2.settings.SettingCodes.MAX_CONCURRENT_STREAMS: 100, h2.settings.SettingCodes.MAX_HEADER_LIST_SIZE: 2**16, h2.settings.SettingCodes.ENABLE_CONNECT_PROTOCOL: 1, } s = h2.settings.Settings(client=client, initial_values=overrides) assert s[h2.settings.SettingCodes.HEADER_TABLE_SIZE] == 8080 assert s[h2.settings.SettingCodes.ENABLE_PUSH] == bool(client) assert s[h2.settings.SettingCodes.INITIAL_WINDOW_SIZE] == 65535 assert s[h2.settings.SettingCodes.MAX_FRAME_SIZE] == 16388 assert s[h2.settings.SettingCodes.MAX_CONCURRENT_STREAMS] == 100 assert s[h2.settings.SettingCodes.MAX_HEADER_LIST_SIZE] == 2**16 assert s[h2.settings.SettingCodes.ENABLE_CONNECT_PROTOCOL] == 1
The Settings object can be provided initial values that override the defaults.
test_can_set_initial_values
python
python-hyper/h2
tests/test_settings.py
https://github.com/python-hyper/h2/blob/master/tests/test_settings.py
MIT
def test_cannot_set_invalid_initial_values(self, setting, value) -> None: """ The Settings object can be provided initial values that override the defaults. """ overrides = {setting: value} with pytest.raises(h2.exceptions.InvalidSettingsValueError): h2.settings.Settings(initial_values=overrides)
The Settings object can be provided initial values that override the defaults.
test_cannot_set_invalid_initial_values
python
python-hyper/h2
tests/test_settings.py
https://github.com/python-hyper/h2/blob/master/tests/test_settings.py
MIT
def test_applying_value_doesnt_take_effect_immediately(self) -> None: """ When a value is applied to the settings object, it doesn't immediately take effect. """ s = h2.settings.Settings(client=True) s[h2.settings.SettingCodes.HEADER_TABLE_SIZE] == 8000 assert s[h2.settings.SettingCodes.HEADER_TABLE_SIZE] == 4096
When a value is applied to the settings object, it doesn't immediately take effect.
test_applying_value_doesnt_take_effect_immediately
python
python-hyper/h2
tests/test_settings.py
https://github.com/python-hyper/h2/blob/master/tests/test_settings.py
MIT
def test_acknowledging_values(self) -> None: """ When we acknowledge settings, the values change. """ s = h2.settings.Settings(client=True) old_settings = dict(s) new_settings = { h2.settings.SettingCodes.HEADER_TABLE_SIZE: 4000, h2.settings.SettingCodes.ENABLE_PUSH: 0, h2.settings.SettingCodes.INITIAL_WINDOW_SIZE: 60, h2.settings.SettingCodes.MAX_FRAME_SIZE: 16385, h2.settings.SettingCodes.ENABLE_CONNECT_PROTOCOL: 1, } s.update(new_settings) assert dict(s) == old_settings s.acknowledge() assert dict(s) == new_settings
When we acknowledge settings, the values change.
test_acknowledging_values
python
python-hyper/h2
tests/test_settings.py
https://github.com/python-hyper/h2/blob/master/tests/test_settings.py
MIT
def test_acknowledging_only_returns_changed_settings(self) -> None: """ Acknowledging settings does not return unchanged settings. """ s = h2.settings.Settings(client=True) s[h2.settings.SettingCodes.INITIAL_WINDOW_SIZE] = 70 changes = s.acknowledge() assert len(changes) == 1 assert list(changes.keys()) == [ h2.settings.SettingCodes.INITIAL_WINDOW_SIZE, ]
Acknowledging settings does not return unchanged settings.
test_acknowledging_only_returns_changed_settings
python
python-hyper/h2
tests/test_settings.py
https://github.com/python-hyper/h2/blob/master/tests/test_settings.py
MIT