From bf35e1faad13d2c6f6cb439405bf2e10f29a780b Mon Sep 17 00:00:00 2001 From: Evan Kepner Date: Sat, 25 Apr 2020 12:34:12 -0400 Subject: [PATCH 1/9] add ClientResponse.ok property --- aiohttp/client_reqrep.py | 13 +++++++++++++ tests/test_client_functional.py | 17 +++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/aiohttp/client_reqrep.py b/aiohttp/client_reqrep.py index 70bf1bde6ba..33b3174a3cb 100644 --- a/aiohttp/client_reqrep.py +++ b/aiohttp/client_reqrep.py @@ -876,6 +876,19 @@ def release(self) -> Any: self._cleanup_writer() return noop() + @property + def ok(self) -> bool: + """Returns ``True`` if ``status`` is less than ``400``, ``False`` if not. + + This is **not** a check for ``200 OK`` but a check that the response + status is under 400. + """ + try: + self.raise_for_status() + except ClientResponseError: + return False + return True + def raise_for_status(self) -> None: if 400 <= self.status: # reason should always be not None for a started response diff --git a/tests/test_client_functional.py b/tests/test_client_functional.py index 656a14e2b9a..41a4ac735a6 100644 --- a/tests/test_client_functional.py +++ b/tests/test_client_functional.py @@ -2202,6 +2202,23 @@ async def handler_redirect(request): assert data == body +@pytest.mark.parametrize("status", [200, 201, 301, 400, 403, 500], + ids= lambda x: f"status_code: {x}") +async def test_ok_from_status(aiohttp_client, status) -> None: + + async def handler(request): + return web.Response(status=status, body=b'') + + app = web.Application() + app.router.add_route('GET', '/endpoint', handler) + client = await aiohttp_client(app, raise_for_status=False) + resp = await client.get('/endpoint') + + if resp.status < 400: + assert resp.ok + else: + assert not resp.ok + async def test_raise_for_status(aiohttp_client) -> None: async def handler(request): From eee01c0d0f73575b5623131eefe486e1b7cc6d66 Mon Sep 17 00:00:00 2001 From: Evan Kepner Date: Sat, 25 Apr 2020 12:40:31 -0400 Subject: [PATCH 2/9] add contributor and 4711.feature --- CHANGES/4711.feature | 1 + CONTRIBUTORS.txt | 1 + 2 files changed, 2 insertions(+) create mode 100644 CHANGES/4711.feature diff --git a/CHANGES/4711.feature b/CHANGES/4711.feature new file mode 100644 index 00000000000..eebb65c5cbe --- /dev/null +++ b/CHANGES/4711.feature @@ -0,0 +1 @@ +Add ClientResponse.ok property for checking status code under 400. diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index b8c39b67fc8..041d58fcae8 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -100,6 +100,7 @@ Eugene Ershov Eugene Naydenov Eugene Nikolaiev Eugene Tolmachev +Evan Kepner Evert Lammerts Felix Yan Fernanda GuimarĂ£es From 2c6a7210cc35d729846ecb30300f5d02e28fb365 Mon Sep 17 00:00:00 2001 From: Evan Kepner Date: Sat, 25 Apr 2020 16:16:02 -0400 Subject: [PATCH 3/9] fix flake8 spacing error --- tests/test_client_functional.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_client_functional.py b/tests/test_client_functional.py index 41a4ac735a6..684396477d3 100644 --- a/tests/test_client_functional.py +++ b/tests/test_client_functional.py @@ -2202,8 +2202,9 @@ async def handler_redirect(request): assert data == body -@pytest.mark.parametrize("status", [200, 201, 301, 400, 403, 500], - ids= lambda x: f"status_code: {x}") +@pytest.mark.parametrize( + "status", [200, 201, 301, 400, 403, 500], ids=lambda x: f"status_code: {x}" +) async def test_ok_from_status(aiohttp_client, status) -> None: async def handler(request): From ec9d7fe73281c7be21be6e471ff303de20da2af7 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Sat, 25 Apr 2020 23:23:19 +0200 Subject: [PATCH 4/9] Simplify test_ok_from_status --- tests/test_client_functional.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/tests/test_client_functional.py b/tests/test_client_functional.py index 684396477d3..14b0cedea2e 100644 --- a/tests/test_client_functional.py +++ b/tests/test_client_functional.py @@ -2203,9 +2203,18 @@ async def handler_redirect(request): @pytest.mark.parametrize( - "status", [200, 201, 301, 400, 403, 500], ids=lambda x: f"status_code: {x}" + ("status", "expected_ok"), + ( + (200, True), + (201, True), + (301, True), + (400, False), + (403, False), + (500, False), + ), + ids=lambda c, y: f"HTTP status code {c} is{' ' if o else ' not'} ok", ) -async def test_ok_from_status(aiohttp_client, status) -> None: +async def test_ok_from_status(aiohttp_client, status, expected_ok) -> None: async def handler(request): return web.Response(status=status, body=b'') @@ -2215,10 +2224,7 @@ async def handler(request): client = await aiohttp_client(app, raise_for_status=False) resp = await client.get('/endpoint') - if resp.status < 400: - assert resp.ok - else: - assert not resp.ok + assert resp.ok is expected_ok async def test_raise_for_status(aiohttp_client) -> None: From ada5c1eb5ae41967e06f8e8f9664d1511a070dc6 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Sat, 25 Apr 2020 23:30:20 +0200 Subject: [PATCH 5/9] Fix a typo in the arg name --- tests/test_client_functional.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_client_functional.py b/tests/test_client_functional.py index 14b0cedea2e..5dc6f7d9ae2 100644 --- a/tests/test_client_functional.py +++ b/tests/test_client_functional.py @@ -2212,7 +2212,7 @@ async def handler_redirect(request): (403, False), (500, False), ), - ids=lambda c, y: f"HTTP status code {c} is{' ' if o else ' not'} ok", + ids=lambda c, o: f"HTTP status code {c} is{' ' if o else ' not'} ok", ) async def test_ok_from_status(aiohttp_client, status, expected_ok) -> None: From c9cc622f4fc53d3fd7f6c548ea999304902c4f08 Mon Sep 17 00:00:00 2001 From: Evan Kepner Date: Sat, 25 Apr 2020 18:02:49 -0400 Subject: [PATCH 6/9] add docs for ClientResponse.ok --- docs/client_reference.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/client_reference.rst b/docs/client_reference.rst index 1d17717521a..1fa6c66444d 100644 --- a/docs/client_reference.rst +++ b/docs/client_reference.rst @@ -1091,6 +1091,11 @@ Response object HTTP status reason of response (:class:`str`), e.g. ``"OK"``. + .. attribute:: ok + + Boolean representation of HTTP status code (:class:`bool`). + ``True`` if ``status`` is less than ``400``; otherwise, ``False``. + .. attribute:: method Request's method (:class:`str`). From a2fe2d0ffa0741d97d0984011508e7bc9eefa8ef Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Sun, 26 Apr 2020 01:27:30 +0200 Subject: [PATCH 7/9] Fix lambda in test_ok_from_status --- tests/test_client_functional.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_client_functional.py b/tests/test_client_functional.py index 5dc6f7d9ae2..f302c6a6e8b 100644 --- a/tests/test_client_functional.py +++ b/tests/test_client_functional.py @@ -2212,7 +2212,7 @@ async def handler_redirect(request): (403, False), (500, False), ), - ids=lambda c, o: f"HTTP status code {c} is{' ' if o else ' not'} ok", + ids=lambda tc: f"HTTP status code {tc[0]} is{' ' if tc[1] else ' not'} ok", ) async def test_ok_from_status(aiohttp_client, status, expected_ok) -> None: From 0cdef5e33917af665b600203768c22a86de191e6 Mon Sep 17 00:00:00 2001 From: Evan Kepner Date: Sat, 25 Apr 2020 20:27:52 -0400 Subject: [PATCH 8/9] fix test_ok_from_status arg params --- tests/test_client_functional.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/test_client_functional.py b/tests/test_client_functional.py index f302c6a6e8b..1ba6d258074 100644 --- a/tests/test_client_functional.py +++ b/tests/test_client_functional.py @@ -2203,7 +2203,7 @@ async def handler_redirect(request): @pytest.mark.parametrize( - ("status", "expected_ok"), + "status, expected_ok", ( (200, True), (201, True), @@ -2211,8 +2211,7 @@ async def handler_redirect(request): (400, False), (403, False), (500, False), - ), - ids=lambda tc: f"HTTP status code {tc[0]} is{' ' if tc[1] else ' not'} ok", + ) ) async def test_ok_from_status(aiohttp_client, status, expected_ok) -> None: From 5f5aa534e47bd38c29a0fec6ce1fa3157f0b1939 Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Sun, 26 Apr 2020 10:26:46 +0200 Subject: [PATCH 9/9] Revert to having params name as a tuple --- tests/test_client_functional.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_client_functional.py b/tests/test_client_functional.py index 1ba6d258074..ecebea642d2 100644 --- a/tests/test_client_functional.py +++ b/tests/test_client_functional.py @@ -2203,7 +2203,7 @@ async def handler_redirect(request): @pytest.mark.parametrize( - "status, expected_ok", + ("status", "expected_ok"), ( (200, True), (201, True),