diff --git a/CHANGES/3852.bugfix b/CHANGES/3852.bugfix new file mode 100644 index 00000000000..126817cda9d --- /dev/null +++ b/CHANGES/3852.bugfix @@ -0,0 +1 @@ +Make the signature of `aiohttp.test_utils.TestClient.request` match `asyncio.ClientSession.request` according to the docs diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 94c04969403..ed64039f5cf 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -46,6 +46,7 @@ Boyi Chen Brett Cannon Brian C. Lane Brian Muller +Bryan Kok Bryce Drennan Carl George Cecile Tonglet diff --git a/aiohttp/test_utils.py b/aiohttp/test_utils.py index 18234b39cba..5d9c1959ec2 100644 --- a/aiohttp/test_utils.py +++ b/aiohttp/test_utils.py @@ -285,8 +285,17 @@ 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, @@ -294,53 +303,50 @@ async def request(self, method: str, path: str, 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: