Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure TestClient HTTP methods return a context manager #1318

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ CHANGES

- Enhancement to AccessLogger (pass *extra* dict) #1303

- Ensure TestClient HTTP methods return a context manager #1318

-

-

-
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ A. Jesse Jiryu Davis
Alejandro Gómez
Aleksandr Danshyn
Aleksey Kutepov
Alex Hayes
Alexander Bayandin
Alexander Karpinsky
Alexander Koshevoy
Expand Down
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