diff --git a/CHANGES.rst b/CHANGES.rst index 0de015d9cdc..a45c6102053 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -6,6 +6,8 @@ Changes - Add doc for add_head, update doc for add_get. #1944 +- Fixed consecutive calls for `Response.write_eof`. + - Retain method attributes (e.g. :code:`__doc__`) when registering synchronous handlers for resources. #1953 diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 29eb599723b..787cb1c6cd6 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -129,6 +129,7 @@ Pankaj Pandey Pau Freixes Paul Colomiets Paulus Schoutsen +Pavel Kamaev Pawel Miech Philipp A. Rafael Viotti diff --git a/aiohttp/web_response.py b/aiohttp/web_response.py index f096c86556a..65179635e57 100644 --- a/aiohttp/web_response.py +++ b/aiohttp/web_response.py @@ -563,6 +563,8 @@ def content_length(self, value): @asyncio.coroutine def write_eof(self): + if self._eof_sent: + return body = self._body if body is not None: if (self._req._method == hdrs.METH_HEAD or diff --git a/tests/test_web_response.py b/tests/test_web_response.py index 6a407cbdcd1..12ffdbd4fcc 100644 --- a/tests/test_web_response.py +++ b/tests/test_web_response.py @@ -889,6 +889,20 @@ def test_send_set_cookie_header(buf, writer): 'Server: .+\r\n\r\n', txt) +@asyncio.coroutine +def test_consecutive_write_eof(): + req = make_request('GET', '/') + data = b'data' + resp = Response(body=data) + + yield from resp.prepare(req) + with mock.patch('aiohttp.web.StreamResponse.write_eof') as super_write_eof: + yield from resp.write_eof() + resp._eof_sent = True + yield from resp.write_eof() + super_write_eof.assert_called_once_with(data) + + def test_set_text_with_content_type(): resp = Response() resp.content_type = "text/html"