Skip to content

Commit 0d44d22

Browse files
committed
Add tests for json post requests in http session manager.
1 parent faa08ae commit 0d44d22

File tree

2 files changed

+67
-17
lines changed

2 files changed

+67
-17
lines changed

tests/core/utilities/test_http_session_manager.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,28 @@ def test_session_manager_json_make_get_request(mocker, http_session_manager):
112112
assert adapter._pool_maxsize == DEFAULT_POOLSIZE
113113

114114

115+
def test_session_manager_json_make_post_request(mocker, http_session_manager):
116+
mocker.patch("requests.Session.post", return_value=MockedResponse())
117+
118+
# Submit a first request to create a session with default parameters
119+
assert len(http_session_manager.session_cache) == 0
120+
response = http_session_manager.json_make_post_request(
121+
TEST_URI, json={"data": "request"}
122+
)
123+
assert response == json.dumps({"data": "content"})
124+
assert len(http_session_manager.session_cache) == 1
125+
cache_key = generate_cache_key(f"{threading.get_ident()}:{TEST_URI}")
126+
session = http_session_manager.session_cache.get_cache_entry(cache_key)
127+
session.post.assert_called_once_with(TEST_URI, json={"data": "request"}, timeout=30)
128+
129+
# Ensure the adapter was created with default values
130+
check_adapters_mounted(session)
131+
adapter = session.get_adapter(TEST_URI)
132+
assert isinstance(adapter, HTTPAdapter)
133+
assert adapter._pool_connections == DEFAULT_POOLSIZE
134+
assert adapter._pool_maxsize == DEFAULT_POOLSIZE
135+
136+
115137
def test_session_manager_make_post_request_no_args(mocker, http_session_manager):
116138
mocker.patch("requests.Session.post", return_value=MockedResponse())
117139

@@ -332,6 +354,32 @@ async def test_session_manager_async_json_make_get_request(
332354
await session.close()
333355

334356

357+
@pytest.mark.asyncio
358+
async def test_session_manager_async_json_make_post_request(
359+
mocker, http_session_manager
360+
):
361+
mocker.patch("aiohttp.ClientSession.post", return_value=AsyncMockedResponse())
362+
363+
# Submit a first request to create a session with default parameters
364+
assert len(http_session_manager.session_cache) == 0
365+
response = await http_session_manager.async_json_make_post_request(
366+
TEST_URI, json={"data": "request"}
367+
)
368+
assert response == json.dumps({"data": "content"})
369+
assert len(http_session_manager.session_cache) == 1
370+
cache_key = generate_cache_key(f"{threading.get_ident()}:{TEST_URI}")
371+
session = http_session_manager.session_cache.get_cache_entry(cache_key)
372+
assert isinstance(session, ClientSession)
373+
session.post.assert_called_once_with(
374+
TEST_URI,
375+
json={"data": "request"},
376+
timeout=ClientTimeout(
377+
total=30, connect=None, sock_read=None, sock_connect=None
378+
),
379+
)
380+
await session.close()
381+
382+
335383
@pytest.mark.asyncio
336384
async def test_session_manager_async_make_post_request(mocker, http_session_manager):
337385
mocker.patch("aiohttp.ClientSession.post", return_value=AsyncMockedResponse())

web3/_utils/http_session_manager.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,22 @@ def json_make_get_request(
117117
response.raise_for_status()
118118
return response.json()
119119

120-
def json_make_post_request(
121-
self, endpoint_uri: URI, *args: Any, **kwargs: Any
122-
) -> Dict[str, Any]:
123-
response = self.get_response_from_post_request(endpoint_uri, *args, **kwargs)
124-
response.raise_for_status()
125-
return response.json()
126-
127120
def get_response_from_post_request(
128121
self, endpoint_uri: URI, *args: Any, **kwargs: Any
129122
) -> requests.Response:
123+
kwargs.setdefault("timeout", DEFAULT_HTTP_TIMEOUT)
130124
session = self.cache_and_return_session(
131125
endpoint_uri, request_timeout=kwargs["timeout"]
132126
)
133127
return session.post(endpoint_uri, *args, **kwargs)
134128

129+
def json_make_post_request(
130+
self, endpoint_uri: URI, *args: Any, **kwargs: Any
131+
) -> Dict[str, Any]:
132+
response = self.get_response_from_post_request(endpoint_uri, *args, **kwargs)
133+
response.raise_for_status()
134+
return response.json()
135+
135136
def make_post_request(
136137
self, endpoint_uri: URI, data: Union[bytes, Dict[str, Any]], **kwargs: Any
137138
) -> bytes:
@@ -150,8 +151,9 @@ def make_post_request(
150151
else:
151152
return response.content
152153

154+
@staticmethod
153155
def _handle_streaming_response(
154-
self, response: requests.Response, start: float, timeout: float
156+
response: requests.Response, start: float, timeout: float
155157
) -> bytes:
156158
response_body = b""
157159
for data in response.iter_content():
@@ -296,15 +298,6 @@ async def async_get_response_from_post_request(
296298
response = await session.post(endpoint_uri, *args, **kwargs)
297299
return response
298300

299-
async def async_make_post_request(
300-
self, endpoint_uri: URI, data: Union[bytes, Dict[str, Any]], **kwargs: Any
301-
) -> bytes:
302-
response = await self.async_get_response_from_post_request(
303-
endpoint_uri, data=data, **kwargs
304-
)
305-
response.raise_for_status()
306-
return await response.read()
307-
308301
async def async_json_make_post_request(
309302
self, endpoint_uri: URI, *args: Any, **kwargs: Any
310303
) -> Dict[str, Any]:
@@ -314,6 +307,15 @@ async def async_json_make_post_request(
314307
response.raise_for_status()
315308
return await response.json()
316309

310+
async def async_make_post_request(
311+
self, endpoint_uri: URI, data: Union[bytes, Dict[str, Any]], **kwargs: Any
312+
) -> bytes:
313+
response = await self.async_get_response_from_post_request(
314+
endpoint_uri, data=data, **kwargs
315+
)
316+
response.raise_for_status()
317+
return await response.read()
318+
317319
async def _async_close_evicted_sessions(
318320
self, timeout: float, evicted_sessions: List[ClientSession]
319321
) -> None:

0 commit comments

Comments
 (0)