Skip to content

Commit

Permalink
Ensure TestClient HTTP method returns a context manager like ClientSe…
Browse files Browse the repository at this point in the history
…ssion does.
  • Loading branch information
Alex Hayes committed Oct 17, 2016
1 parent de17c89 commit 45c0597
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

-

-
Expand Down
3 changes: 2 additions & 1 deletion CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,5 @@ Yusuke Tsutsumi
Pau Freixes
Alexey Firsov
Vikas Kawadia
Dmitry Shamov
Dmitry Shamov
Alex Hayes
29 changes: 22 additions & 7 deletions aiohttp/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from yarl import URL

import aiohttp
from aiohttp.client import _RequestContextManager

from . import ClientSession, hdrs
from .helpers import sentinel
Expand Down Expand Up @@ -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):
Expand Down
12 changes: 9 additions & 3 deletions tests/test_py35/test_test_utils_35.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 45c0597

Please sign in to comment.