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

TestClient.request doesn't return a context manager #3852

Closed
nicktimko opened this issue Jun 17, 2019 · 1 comment
Closed

TestClient.request doesn't return a context manager #3852

nicktimko opened this issue Jun 17, 2019 · 1 comment

Comments

@nicktimko
Copy link
Contributor

Long story short

aiohttp.test_utils.TestClient.request seems to be an odd-man out in not returning a context manager.

Expected behaviour

Returns a context manager.

Actual behaviour

When calling the .request() method of TestClient, it doesn't seem to return a _RequestContextManager like the other 3 permutations.

  • ClientSession.request() -> aiohttp.client._RequestContextManager
  • ClientSession.<verb>() -> aiohttp.client._RequestContextManager
  • TestClient.request() -> aiohttp.client._RequestContextManager
  • TestClient.<verb>() -> coroutine
➜ python methodtests.py 
methodtests.py:18: RuntimeWarning: coroutine 'TestClient.request' was never awaited
  async with client.request(method, url) as resp:
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Traceback (most recent call last):
  File "methodtests.py", line 43, in <module>
    loop.run_until_complete(main())
  File "/home/nick/.pyenv/versions/3.7.2/lib/python3.7/asyncio/base_events.py", line 584, in run_until_complete
    return future.result()
  File "methodtests.py", line 40, in main
    await check_client(client, "/")  # XXX dies
  File "methodtests.py", line 18, in check_client
    async with client.request(method, url) as resp:  # YYY dies
AttributeError: __aexit__

Steps to reproduce

import asyncio
import aiohttp.test_utils
import aiohttp.web

async def handleall(request):
    return aiohttp.web.Response()

async def check_client(client, base_url):
    for method in ["GET", "POST", "PUT", "DELETE"]:
        url = base_url + method.lower()

        # slightly awkward, but works with both client types
        clientmethod = getattr(client, method.lower())
        async with clientmethod(url) as resp:
            assert resp.status == 200

        # fails with test clients
        async with client.request(method, url) as resp:  # YYY dies
            assert resp.status == 200

async def main():
    # a "regular" ClientSession
    base_url = "https://httpbin.org/"
    async with aiohttp.ClientSession() as client:
        await check_client(client, base_url)

    app = aiohttp.web.Application()
    app.router.add_route("*", "/{verb}", handleall)

    async with aiohttp.test_utils.TestServer(app) as server:
        async with aiohttp.test_utils.TestClient(server) as test_client:
            base_url = str(test_client.make_url("/"))
            # a "regular" client created from the test client
            client = test_client.session
            await check_client(client, base_url)

    async with aiohttp.test_utils.TestServer(app) as server:
        async with aiohttp.test_utils.TestClient(server) as client:
            # just the test client
            await check_client(client, "/")  # XXX dies

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

Your environment

Python 3.7.2
aiohttp 3.5.4

@webknjaz
Copy link
Member

Thanks, feel free to send a PR. You may start digging @ https://github.com/aio-libs/aiohttp/blob/master/aiohttp/pytest_plugin.py

Transfusion added a commit to Transfusion/aiohttp that referenced this issue Aug 17, 2019
asvetlov pushed a commit that referenced this issue Aug 30, 2019
…questContextManager (#3989)

(cherry picked from commit a2a9d0a)

Co-authored-by: Bryan Kok <Transfusion@users.noreply.github.com>
asvetlov added a commit that referenced this issue Aug 30, 2019
…questContextManager (#3989) (#4022)

(cherry picked from commit a2a9d0a)

Co-authored-by: Bryan Kok <Transfusion@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants