Skip to content

Commit

Permalink
Raise RuntimeError is you try to set the Content Length and enable ch…
Browse files Browse the repository at this point in the history
…unked encoding at the same time
  • Loading branch information
julien-duponchelle committed Jun 8, 2017
1 parent 6b85bcd commit a064de8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Changes

-

-
- Raise RuntimeError is you try to set the Content Length and enable chunked encoding at the same time

2.1.0 (2017-05-26)
------------------
Expand Down
8 changes: 7 additions & 1 deletion aiohttp/web_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ def output_length(self):
def enable_chunked_encoding(self, chunk_size=None):
"""Enables automatic chunked transfer encoding."""
self._chunked = True

if hdrs.CONTENT_LENGTH in self._headers:
raise RuntimeError("You can't enable chunked encoding when "
"a content length is set")
if chunk_size is not None:
warnings.warn('Chunk size is deprecated #1615', DeprecationWarning)

Expand Down Expand Up @@ -194,7 +198,9 @@ def content_length(self):
def content_length(self, value):
if value is not None:
value = int(value)
# TODO: raise error if chunked enabled
if self._chunked:
raise RuntimeError("You can't set content length when "
"chunked encoding is enable")
self._headers[hdrs.CONTENT_LENGTH] = str(value)
else:
self._headers.pop(hdrs.CONTENT_LENGTH, None)
Expand Down
16 changes: 16 additions & 0 deletions tests/test_web_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ def test_content_length_setter():
assert 234 == resp.content_length


def test_content_length_setter_with_enable_chunked_encoding():
resp = StreamResponse()

resp.enable_chunked_encoding()
with pytest.raises(RuntimeError):
resp.content_length = 234


def test_drop_content_length_header_on_setting_len_to_None():
resp = StreamResponse()

Expand Down Expand Up @@ -223,6 +231,14 @@ def test_chunked_encoding():
assert msg.chunked


def test_enable_chunked_encoding_with_content_length():
resp = StreamResponse()

resp.content_length = 234
with pytest.raises(RuntimeError):
resp.enable_chunked_encoding()


@asyncio.coroutine
def test_chunk_size():
req = make_request('GET', '/', payload_writer=mock.Mock())
Expand Down

0 comments on commit a064de8

Please sign in to comment.