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

Setup Content-Type to application/octet-stream by default #1124

Merged
merged 8 commits into from
Aug 26, 2016
Merged
Show file tree
Hide file tree
Changes from 3 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
26 changes: 12 additions & 14 deletions aiohttp/web_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ def __init__(self, *, body=None, status=200,
self.set_tcp_cork(True)

if body is not None and text is not None:
raise ValueError("body and text are not allowed together.")
raise ValueError('body and text are not allowed together')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use " for exception messages

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the difference in quotes usage was due to different contributors. I'll change it back


if text is not None:
if hdrs.CONTENT_TYPE not in self.headers:
Expand All @@ -794,25 +794,23 @@ def __init__(self, *, body=None, status=200,
self._content_dict = {'charset': charset}
self.body = text.encode(charset)
else:
self.text = text
if content_type or charset:
raise ValueError("Passing both Content-Type header and "
"content_type or charset params "
"is forbidden")
raise ValueError('passing both Content-Type header and '
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"

'content_type or charset params '
'is forbidden')
self.text = text
else:
if hdrs.CONTENT_TYPE in self.headers:
if content_type or charset:
raise ValueError("Passing both Content-Type header and "
"content_type or charset params "
"is forbidden")
if content_type:
self.content_type = content_type
raise ValueError('passing both Content-Type header and '
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"

'content_type or charset params '
'is forbidden')
if content_type is None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this code should be in StreamResponse ctor.
Content-Type should be application/octet-stream if not explicitly specified.
It's true for all responses.

See https://tools.ietf.org/html/rfc7231#section-3.1.1.5

content_type = 'application/octet-stream'
self.content_type = content_type
if charset:
self.charset = charset
if body is not None:
self.body = body
else:
self.body = None
self.body = body

@property
def body(self):
Expand Down
8 changes: 4 additions & 4 deletions docs/web_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -680,14 +680,14 @@ Response

.. attribute:: text

Read-write attribute for storing response's content, represented as str,
:class:`str`.
Read-write attribute for storing response's content, represented as
string, :class:`str`.

Setting :attr:`str` also recalculates
Setting :attr:`text` also recalculates
:attr:`~StreamResponse.content_length` value and
:attr:`~StreamResponse.body` value

Resetting :attr:`body` (assigning ``None``) sets
Resetting :attr:`text` (assigning ``None``) sets
:attr:`~StreamResponse.content_length` to ``None`` too, dropping
*Content-Length* HTTP header.

Expand Down
10 changes: 10 additions & 0 deletions tests/test_web_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,16 @@ def test_set_text_with_charset():
assert "koi8-r" == resp.charset


def test_content_type_with_set_text():
resp = Response(text='text')
assert resp.content_type == 'text/plain'


def test_content_type_with_set_body():
resp = Response(body=b'body')
assert resp.content_type == 'application/octet-stream'


def test_started_when_not_started():
resp = StreamResponse()
assert not resp.prepared
Expand Down