@@ -183,135 +183,3 @@ def test_get_connection_error(client):
183183 client .get ("/conn" )
184184 assert exc .value .error .error_code == "CONNECTION_ERROR"
185185
186-
187-
188-
189-
190- # Test async POST success scenario.
191- @pytest .mark .asyncio
192- async def test_async_post_success (client ):
193- # Patch aiohttp ClientSession.post to mock async POST request.
194- with patch ("mpesakit.http_client.mpesa_http_client.aiohttp.ClientSession.post" ) as mock_post :
195- mock_response = Mock ()
196- mock_response .status = 200 # Simulate HTTP 200 OK.
197- mock_response .json = Mock (return_value = {"foo" : "bar" }) # Mock JSON response.
198- mock_post .return_value .__aenter__ .return_value = mock_response
199-
200- # Call async_post and assert the result.
201- result = await client .async_post ("/test" , json = {"a" : 1 }, headers = {"h" : "v" })
202- assert result == {"foo" : "bar" }
203- mock_post .assert_called_once ()
204-
205-
206- # Test async POST HTTP error scenario.
207- @pytest .mark .asyncio
208- async def test_async_post_http_error (client ):
209- # Patch aiohttp ClientSession.post to simulate HTTP 400 error.
210- with patch ("mpesakit.http_client.mpesa_http_client.aiohttp.ClientSession.post" ) as mock_post :
211- mock_response = Mock ()
212- mock_response .status = 400 # Simulate HTTP 400 error.
213- mock_response .json = Mock (return_value = {"errorMessage" : "Bad Request" })
214- mock_post .return_value .__aenter__ .return_value = mock_response
215-
216- # Assert MpesaApiException is raised with correct error code and message.
217- with pytest .raises (MpesaApiException ) as exc :
218- await client .async_post ("/fail" , json = {}, headers = {})
219- assert exc .value .error .error_code == "HTTP_400"
220- assert "Bad Request" in exc .value .error .error_message
221-
222-
223- # Test async POST JSON decode error scenario.
224- @pytest .mark .asyncio
225- async def test_async_post_json_decode_error (client ):
226- # Patch aiohttp ClientSession.post to simulate JSON decode error.
227- with patch ("mpesakit.http_client.mpesa_http_client.aiohttp.ClientSession.post" ) as mock_post :
228- mock_response = Mock ()
229- mock_response .status = 500 # Simulate HTTP 500 error.
230- mock_response .json = Mock (side_effect = ValueError ()) # Simulate JSON decode error.
231- mock_response .text = "Internal Server Error"
232- mock_post .return_value .__aenter__ .return_value = mock_response
233-
234- # Assert MpesaApiException is raised with correct error code and message.
235- with pytest .raises (MpesaApiException ) as exc :
236- await client .async_post ("/fail" , json = {}, headers = {})
237- assert exc .value .error .error_code == "HTTP_500"
238- assert "Internal Server Error" in exc .value .error .error_message
239-
240-
241- # Test async POST generic exception scenario.
242- @pytest .mark .asyncio
243- async def test_async_post_exception (client ):
244- # Patch aiohttp ClientSession.post to raise a generic exception.
245- with patch (
246- "mpesakit.http_client.mpesa_http_client.aiohttp.ClientSession.post" ,
247- side_effect = Exception ("boom" ),
248- ):
249- # Assert MpesaApiException is raised with REQUEST_FAILED code.
250- with pytest .raises (MpesaApiException ) as exc :
251- await client .async_post ("/fail" , json = {}, headers = {})
252- assert exc .value .error .error_code == "REQUEST_FAILED"
253-
254-
255- # Test async GET success scenario.
256- @pytest .mark .asyncio
257- async def test_async_get_success (client ):
258- # Patch aiohttp ClientSession.get to mock async GET request.
259- with patch ("mpesakit.http_client.mpesa_http_client.aiohttp.ClientSession.get" ) as mock_get :
260- mock_response = Mock ()
261- mock_response .status = 200 # Simulate HTTP 200 OK.
262- mock_response .json = Mock (return_value = {"foo" : "bar" }) # Mock JSON response.
263- mock_get .return_value .__aenter__ .return_value = mock_response
264-
265- # Call async_get and assert the result.
266- result = await client .async_get ("/test" , params = {"a" : 1 }, headers = {"h" : "v" })
267- assert result == {"foo" : "bar" }
268- mock_get .assert_called_once ()
269-
270-
271- # Test async GET HTTP error scenario.
272- @pytest .mark .asyncio
273- async def test_async_get_http_error (client ):
274- # Patch aiohttp ClientSession.get to simulate HTTP 404 error.
275- with patch ("mpesakit.http_client.mpesa_http_client.aiohttp.ClientSession.get" ) as mock_get :
276- mock_response = Mock ()
277- mock_response .status = 404 # Simulate HTTP 404 error.
278- mock_response .json = Mock (return_value = {"errorMessage" : "Not Found" })
279- mock_get .return_value .__aenter__ .return_value = mock_response
280-
281- # Assert MpesaApiException is raised with correct error code and message.
282- with pytest .raises (MpesaApiException ) as exc :
283- await client .async_get ("/fail" )
284- assert exc .value .error .error_code == "HTTP_404"
285- assert "Not Found" in exc .value .error .error_message
286-
287-
288- # Test async GET JSON decode error scenario.
289- @pytest .mark .asyncio
290- async def test_async_get_json_decode_error (client ):
291- # Patch aiohttp ClientSession.get to simulate JSON decode error.
292- with patch ("mpesakit.http_client.mpesa_http_client.aiohttp.ClientSession.get" ) as mock_get :
293- mock_response = Mock ()
294- mock_response .status = 500 # Simulate HTTP 500 error.
295- mock_response .json = Mock (side_effect = ValueError ()) # Simulate JSON decode error.
296- mock_response .text = "Internal Server Error"
297- mock_get .return_value .__aenter__ .return_value = mock_response
298-
299- # Assert MpesaApiException is raised with correct error code and message.
300- with pytest .raises (MpesaApiException ) as exc :
301- await client .async_get ("/fail" )
302- assert exc .value .error .error_code == "HTTP_500"
303- assert "Internal Server Error" in exc .value .error .error_message
304-
305-
306- # Test async GET generic exception scenario.
307- @pytest .mark .asyncio
308- async def test_async_get_exception (client ):
309- # Patch aiohttp ClientSession.get to raise a generic exception.
310- with patch (
311- "mpesakit.http_client.mpesa_http_client.aiohttp.ClientSession.get" ,
312- side_effect = Exception ("boom" ),
313- ):
314- # Assert MpesaApiException is raised with REQUEST_FAILED code.
315- with pytest .raises (MpesaApiException ) as exc :
316- await client .async_get ("/fail" )
317- assert exc .value .error .error_code == "REQUEST_FAILED"
0 commit comments