Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise RuntimeError is you try to set the Content Length with chunked encoding enable #1941

Merged
Merged
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
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