diff --git a/CHANGES/9141.misc.rst b/CHANGES/9141.misc.rst new file mode 100644 index 00000000000..6ee9d726d39 --- /dev/null +++ b/CHANGES/9141.misc.rst @@ -0,0 +1,3 @@ +- Modified the `ClientSession` class by adding a private attribute `_test` of type `bool`, with a default value of `False` to the class. +- Modified the default value of `retry_persistent_connection` in `ClientSession` to `False` if `_test` was `True`. +- Also, `TestClient` initialized `ClientSession` with `test=True`. diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index e18aad14f64..c97977fe8ae 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -309,6 +309,7 @@ Sergey Skripnick Serhii Charykov Serhii Kostel Serhiy Storchaka +Shubh Agarwal Simon Kennedy Sin-Woo Bang Stanislas Plum diff --git a/aiohttp/client.py b/aiohttp/client.py index f31ad6bad25..400d2edeb56 100644 --- a/aiohttp/client.py +++ b/aiohttp/client.py @@ -286,6 +286,7 @@ def __init__( max_line_size: int = 8190, max_field_size: int = 8190, fallback_charset_resolver: _CharsetResolver = lambda r, b: "utf-8", + test: bool = False, ) -> None: # We initialise _connector to None immediately, as it's referenced in __del__() # and could cause issues if an exception occurs during initialisation. @@ -367,6 +368,7 @@ def __init__( self._default_proxy = proxy self._default_proxy_auth = proxy_auth + self._test = test def __init_subclass__(cls: Type["ClientSession"]) -> None: raise TypeError( @@ -539,7 +541,9 @@ async def _request( try: with timer: # https://www.rfc-editor.org/rfc/rfc9112.html#name-retrying-requests - retry_persistent_connection = method in IDEMPOTENT_METHODS + retry_persistent_connection = ( + False if self._test else method in IDEMPOTENT_METHODS + ) while True: url, auth_from_url = strip_auth_from_url(url) if not url.raw_host: diff --git a/aiohttp/test_utils.py b/aiohttp/test_utils.py index 88dcf8ebf1b..2c076f39ed9 100644 --- a/aiohttp/test_utils.py +++ b/aiohttp/test_utils.py @@ -293,7 +293,7 @@ def __init__( # type: ignore[misc] self._server = server if cookie_jar is None: cookie_jar = aiohttp.CookieJar(unsafe=True) - self._session = ClientSession(cookie_jar=cookie_jar, **kwargs) + self._session = ClientSession(cookie_jar=cookie_jar, test=True, **kwargs) self._closed = False self._responses: List[ClientResponse] = [] self._websockets: List[ClientWebSocketResponse] = []