Skip to content

Commit

Permalink
[3.6] Fixes #3852 - aiohttp.test_utils.TestClient should return a _Re…
Browse files Browse the repository at this point in the history
…questContextManager (#3989)

(cherry picked from commit a2a9d0a)

Co-authored-by: Bryan Kok <[email protected]>
  • Loading branch information
Transfusion authored and asvetlov committed Aug 30, 2019
1 parent 57c8c78 commit 6ed27b8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGES/3852.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make the signature of `aiohttp.test_utils.TestClient.request` match `asyncio.ClientSession.request` according to the docs
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Boyi Chen
Brett Cannon
Brian C. Lane
Brian Muller
Bryan Kok
Bryce Drennan
Carl George
Cecile Tonglet
Expand Down
34 changes: 20 additions & 14 deletions aiohttp/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,62 +285,68 @@ def session(self) -> ClientSession:
def make_url(self, path: str) -> URL:
return self._server.make_url(path)

async def request(self, method: str, path: str,
**kwargs: Any) -> ClientResponse:
async def _request(self, method: str, path: str,
**kwargs: Any) -> ClientResponse:
resp = await self._session.request(
method, self.make_url(path), **kwargs
)
# save it to close later
self._responses.append(resp)
return resp

def request(self, method: str, path: str,
**kwargs: Any) -> _RequestContextManager:
"""Routes a request to tested http server.
The interface is identical to aiohttp.ClientSession.request,
except the loop kwarg is overridden by the instance used by the
test server.
"""
resp = await self._session.request(
method, self.make_url(path), **kwargs
return _RequestContextManager(
self._request(method, path, **kwargs)
)
# save it to close later
self._responses.append(resp)
return resp

def get(self, path: str, **kwargs: Any) -> _RequestContextManager:
"""Perform an HTTP GET request."""
return _RequestContextManager(
self.request(hdrs.METH_GET, path, **kwargs)
self._request(hdrs.METH_GET, path, **kwargs)
)

def post(self, path: str, **kwargs: Any) -> _RequestContextManager:
"""Perform an HTTP POST request."""
return _RequestContextManager(
self.request(hdrs.METH_POST, path, **kwargs)
self._request(hdrs.METH_POST, path, **kwargs)
)

def options(self, path: str, **kwargs: Any) -> _RequestContextManager:
"""Perform an HTTP OPTIONS request."""
return _RequestContextManager(
self.request(hdrs.METH_OPTIONS, path, **kwargs)
self._request(hdrs.METH_OPTIONS, path, **kwargs)
)

def head(self, path: str, **kwargs: Any) -> _RequestContextManager:
"""Perform an HTTP HEAD request."""
return _RequestContextManager(
self.request(hdrs.METH_HEAD, path, **kwargs)
self._request(hdrs.METH_HEAD, path, **kwargs)
)

def put(self, path: str, **kwargs: Any) -> _RequestContextManager:
"""Perform an HTTP PUT request."""
return _RequestContextManager(
self.request(hdrs.METH_PUT, path, **kwargs)
self._request(hdrs.METH_PUT, path, **kwargs)
)

def patch(self, path: str, **kwargs: Any) -> _RequestContextManager:
"""Perform an HTTP PATCH request."""
return _RequestContextManager(
self.request(hdrs.METH_PATCH, path, **kwargs)
self._request(hdrs.METH_PATCH, path, **kwargs)
)

def delete(self, path: str, **kwargs: Any) -> _RequestContextManager:
"""Perform an HTTP PATCH request."""
return _RequestContextManager(
self.request(hdrs.METH_DELETE, path, **kwargs)
self._request(hdrs.METH_DELETE, path, **kwargs)
)

def ws_connect(self, path: str, **kwargs: Any) -> _WSRequestContextManager:
Expand Down

0 comments on commit 6ed27b8

Please sign in to comment.