Skip to content
This repository has been archived by the owner on Jan 13, 2021. It is now read-only.

Raising error in HTTP11Response if created with illegal attributes #235

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions hyper/http11/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,14 @@ def __init__(self, code, reason, headers, sock, connection=None):
b'chunked' in self.headers.get(b'transfer-encoding', [])
)

# One of the following must be true: we must expect that the connection
# will be closed following the body, or that a content-length was sent,
# or that we're getting a chunked response.
# FIXME: Remove naked assert, replace with something better.
assert self._expect_close or self._length is not None or self._chunked
if not self._expect_close \
and not self._chunked \
and self._length is None:
raise ValueError('A response must either specify a '
'content-length, be a chunked '
'response, or specify that the '
'connection be closed after the response. '
'None of these conditions were met.')

# This object is used for decompressing gzipped request bodies. Right
# now we only support gzip because that's all the RFC mandates of us.
Expand Down
12 changes: 12 additions & 0 deletions test/test_http11.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,18 @@ def test_closing_chunked_reads_dont_call_close_callback(self):
assert r._sock is None
assert connection.close.call_count == 1

def test_regular_response_with_missing_headers_raises_error(self):
headers = {}
sock = DummySocket()
connection = mock.MagicMock()

with pytest.raises(ValueError) as exc_info:
HTTP11Response(200, 'OK', headers, sock, connection)
assert 'A response must either specify a content-length, ' \
'be a chunked response, or specify that the ' \
'connection be closed after the response. ' \
in str(exc_info)


class DummySocket(object):
def __init__(self):
Expand Down