| 1 |
# -*- coding; utf-8 -*- |
| 2 |
""" |
| 3 |
test_head_request |
| 4 |
~~~~~~~~~~~~~~~~~ |
| 5 |
""" |
| 6 |
import h2.connection |
| 7 |
import pytest |
| 8 |
|
| 9 |
|
| 10 |
class TestHeadRequest(object): |
| 11 |
example_request_headers = [ |
| 12 |
(b':authority', b'example.com'), |
| 13 |
(b':path', b'/'), |
| 14 |
(b':scheme', b'https'), |
| 15 |
(b':method', b'HEAD'), |
| 16 |
] |
| 17 |
|
| 18 |
example_response_headers = [ |
| 19 |
(b':status', b'200'), |
| 20 |
(b'server', b'fake-serv/0.1.0'), |
| 21 |
(b'content_length', b'1'), |
| 22 |
] |
| 23 |
|
| 24 |
def test_non_zero_content_and_no_body(self, frame_factory): |
| 25 |
|
| 26 |
c = h2.connection.H2Connection() |
| 27 |
c.initiate_connection() |
| 28 |
c.send_headers(1, self.example_request_headers, end_stream=True) |
| 29 |
|
| 30 |
f = frame_factory.build_headers_frame( |
| 31 |
self.example_response_headers, |
| 32 |
flags=['END_STREAM'] |
| 33 |
) |
| 34 |
events = c.receive_data(f.serialize()) |
| 35 |
|
| 36 |
assert len(events) == 2 |
| 37 |
event = events[0] |
| 38 |
|
| 39 |
assert isinstance(event, h2.events.ResponseReceived) |
| 40 |
assert event.stream_id == 1 |
| 41 |
assert event.headers == self.example_response_headers |
| 42 |
|
| 43 |
def test_reject_non_zero_content_and_body(self, frame_factory): |
| 44 |
c = h2.connection.H2Connection() |
| 45 |
c.initiate_connection() |
| 46 |
c.send_headers(1, self.example_request_headers) |
| 47 |
|
| 48 |
headers = frame_factory.build_headers_frame( |
| 49 |
self.example_response_headers |
| 50 |
) |
| 51 |
data = frame_factory.build_data_frame(data=b'\x01') |
| 52 |
|
| 53 |
c.receive_data(headers.serialize()) |
| 54 |
with pytest.raises(h2.exceptions.InvalidBodyLengthError): |
| 55 |
c.receive_data(data.serialize()) |