From 10d59dbed4a94008beca01e4840ced1ea9e1e02d Mon Sep 17 00:00:00 2001 From: David Poirier Date: Sat, 5 Aug 2017 23:22:21 +1000 Subject: [PATCH 1/2] Deprecated BaseRequest.has_body, replaced with 2 new attributes --- aiohttp/web_request.py | 16 +++++++++++++++- changes/2005.feature | 3 +++ docs/web_reference.rst | 24 +++++++++++++++++++++--- tests/test_web_functional.py | 6 ++++-- 4 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 changes/2005.feature diff --git a/aiohttp/web_request.py b/aiohttp/web_request.py index d59c3a61c5a..9a6d9ebf0f0 100644 --- a/aiohttp/web_request.py +++ b/aiohttp/web_request.py @@ -16,6 +16,7 @@ from . import hdrs, multipart from .helpers import HeadersMixin, SimpleCookie, reify, sentinel +from .streams import EmptyStreamReader from .web_exceptions import HTTPRequestEntityTooLarge @@ -457,9 +458,22 @@ def content(self): @property def has_body(self): - """Return True if request has HTTP BODY, False otherwise.""" + """Return True if request's HTTP BODY can be read, False otherwise.""" + warnings.warn( + "Deprecated, use .can_read_body #2005", + DeprecationWarning, stacklevel=2) + return not self._payload.at_eof() + + @property + def can_read_body(self): + """Return True if request's HTTP BODY can be read, False otherwise.""" return not self._payload.at_eof() + @property + def body_exists(self): + """Return True if request has HTTP BODY, False otherwise.""" + return type(self._payload) is not EmptyStreamReader + @asyncio.coroutine def release(self): """Release request. diff --git a/changes/2005.feature b/changes/2005.feature new file mode 100644 index 00000000000..77946cf4db6 --- /dev/null +++ b/changes/2005.feature @@ -0,0 +1,3 @@ +- Deprecated BaseRequest.has_body in favour of BaseRequest.can_read_body +- Added BaseRequest.body_exists attribute that stays static for the lifetime +of the request diff --git a/docs/web_reference.rst b/docs/web_reference.rst index 1a25f0b5190..ccbdb1308c6 100644 --- a/docs/web_reference.rst +++ b/docs/web_reference.rst @@ -230,15 +230,33 @@ and :ref:`aiohttp-web-signals` handlers. Read-only property. - .. attribute:: has_body + .. attribute:: body_exists Return ``True`` if request has *HTTP BODY*, ``False`` otherwise. Read-only :class:`bool` property. - .. versionadded:: 0.16 + .. versionadded:: 2.3 + + .. attribute:: can_read_body - .. attribute:: content_type + Return ``True`` if request's *HTTP BODY* can be read, ``False`` otherwise. + + Read-only :class:`bool` property. + + .. versionadded:: 2.3 + + .. attribute:: has_body + + Return ``True`` if request's *HTTP BODY* can be read, ``False`` otherwise. + + Read-only :class:`bool` property. + + .. deprecated:: 2.3 + + Use :meth:`can_read_body` instead. + + .. attribute:: content_type Read-only property with *content* part of *Content-Type* header. diff --git a/tests/test_web_functional.py b/tests/test_web_functional.py index 3f673c2ce9b..d189d80acf8 100644 --- a/tests/test_web_functional.py +++ b/tests/test_web_functional.py @@ -694,7 +694,8 @@ def test_empty_content_for_query_without_body(loop, test_client): @asyncio.coroutine def handler(request): - assert not request.has_body + assert not request.body_exists + assert not request.can_read_body return web.Response() app = web.Application() @@ -710,7 +711,8 @@ def test_empty_content_for_query_with_body(loop, test_client): @asyncio.coroutine def handler(request): - assert request.has_body + assert request.body_exists + assert request.can_read_body body = yield from request.read() return web.Response(body=body) From 171ad0a03e9239742ecb81538374473294800d58 Mon Sep 17 00:00:00 2001 From: David Poirier Date: Sun, 6 Aug 2017 10:10:06 +1000 Subject: [PATCH 2/2] Test obsolete BaseRequest.has_body attr --- tests/test_web_functional.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_web_functional.py b/tests/test_web_functional.py index d189d80acf8..9bb26589119 100644 --- a/tests/test_web_functional.py +++ b/tests/test_web_functional.py @@ -696,6 +696,7 @@ def test_empty_content_for_query_without_body(loop, test_client): def handler(request): assert not request.body_exists assert not request.can_read_body + assert not request.has_body return web.Response() app = web.Application() @@ -713,6 +714,7 @@ def test_empty_content_for_query_with_body(loop, test_client): def handler(request): assert request.body_exists assert request.can_read_body + assert request.has_body body = yield from request.read() return web.Response(body=body)