From 45c059707f59a377639b075fa2f6afdca2e44bc5 Mon Sep 17 00:00:00 2001 From: Alex Hayes Date: Thu, 6 Oct 2016 07:53:20 +1100 Subject: [PATCH] Ensure TestClient HTTP method returns a context manager like ClientSession does. --- CHANGES.rst | 2 ++ CONTRIBUTORS.txt | 3 ++- aiohttp/test_utils.py | 29 ++++++++++++++++++++------- tests/test_py35/test_test_utils_35.py | 12 ++++++++--- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 0b8dd008c21..a7529da8988 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -41,6 +41,8 @@ CHANGES - Fix bug when ClientRequest send payload file with opened as open('filename', 'r+b') #1306 +- Ensure TestClient HTTP methods return a context manager #1318 + - - diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 03405eae177..0cfc584a980 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -139,4 +139,5 @@ Yusuke Tsutsumi Pau Freixes Alexey Firsov Vikas Kawadia -Dmitry Shamov \ No newline at end of file +Dmitry Shamov +Alex Hayes \ No newline at end of file diff --git a/aiohttp/test_utils.py b/aiohttp/test_utils.py index 70ac6f4f5ab..5806a93af08 100644 --- a/aiohttp/test_utils.py +++ b/aiohttp/test_utils.py @@ -13,6 +13,7 @@ from yarl import URL import aiohttp +from aiohttp.client import _RequestContextManager from . import ClientSession, hdrs from .helpers import sentinel @@ -214,31 +215,45 @@ def request(self, method, path, *args, **kwargs): def get(self, path, *args, **kwargs): """Perform an HTTP GET request.""" - return self.request(hdrs.METH_GET, path, *args, **kwargs) + return _RequestContextManager( + self.request(hdrs.METH_GET, path, *args, **kwargs) + ) def post(self, path, *args, **kwargs): """Perform an HTTP POST request.""" - return self.request(hdrs.METH_POST, path, *args, **kwargs) + return _RequestContextManager( + self.request(hdrs.METH_POST, path, *args, **kwargs) + ) def options(self, path, *args, **kwargs): """Perform an HTTP OPTIONS request.""" - return self.request(hdrs.METH_OPTIONS, path, *args, **kwargs) + return _RequestContextManager( + self.request(hdrs.METH_OPTIONS, path, *args, **kwargs) + ) def head(self, path, *args, **kwargs): """Perform an HTTP HEAD request.""" - return self.request(hdrs.METH_HEAD, path, *args, **kwargs) + return _RequestContextManager( + self.request(hdrs.METH_HEAD, path, *args, **kwargs) + ) def put(self, path, *args, **kwargs): """Perform an HTTP PUT request.""" - return self.request(hdrs.METH_PUT, path, *args, **kwargs) + return _RequestContextManager( + self.request(hdrs.METH_PUT, path, *args, **kwargs) + ) def patch(self, path, *args, **kwargs): """Perform an HTTP PATCH request.""" - return self.request(hdrs.METH_PATCH, path, *args, **kwargs) + return _RequestContextManager( + self.request(hdrs.METH_PATCH, path, *args, **kwargs) + ) def delete(self, path, *args, **kwargs): """Perform an HTTP PATCH request.""" - return self.request(hdrs.METH_DELETE, path, *args, **kwargs) + return _RequestContextManager( + self.request(hdrs.METH_DELETE, path, *args, **kwargs) + ) @asyncio.coroutine def ws_connect(self, path, *args, **kwargs): diff --git a/tests/test_py35/test_test_utils_35.py b/tests/test_py35/test_test_utils_35.py index 49c1572ec64..4e2b625d2c6 100644 --- a/tests/test_py35/test_test_utils_35.py +++ b/tests/test_py35/test_test_utils_35.py @@ -23,7 +23,13 @@ async def test_server_context_manager(app, loop): assert resp.status == 200 -async def test_client_context_manager(app, loop): +@pytest.mark.parametrize("method", [ + "head", "get", "post", "options", "post", "put", "patch", "delete" +]) +async def test_client_context_manager_response(method, app, loop): async with _TestClient(app) as client: - resp = await client.head('/') - assert resp.status == 200 + async with getattr(client, method)('/') as resp: + assert resp.status == 200 + if method != 'head': + text = await resp.text() + assert "OK" in text