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

Commit

Permalink
Raising error in HTTP11Response if created with illegal attributes
Browse files Browse the repository at this point in the history
An HTTP response must either specify a content-length header, specify
'close' for a connection header to signal that the connection will be
closed after the response, or be a chunked response.

If none of these conditions are true, we raise a detailed ValueError,
instead of the plain assertion.
  • Loading branch information
Aviv Cohn committed Apr 25, 2016
1 parent 065b539 commit 1fb6f2b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
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

0 comments on commit 1fb6f2b

Please sign in to comment.