@@ -209,6 +209,16 @@ def test_headers_property_is_dict_reference():
209209 h ['X-Ref' ] = 'ref'
210210 assert apim .headers ['X-Ref' ] == 'ref'
211211
212+
213+ def test_subscription_key_setter_updates_and_clears_header ():
214+ apim = ApimRequests (DEFAULT_URL , DEFAULT_KEY )
215+
216+ apim .subscriptionKey = 'new-key'
217+ assert apim .headers [SUBSCRIPTION_KEY_PARAMETER_NAME ] == 'new-key'
218+
219+ apim .subscriptionKey = None
220+ assert SUBSCRIPTION_KEY_PARAMETER_NAME not in apim .headers
221+
212222# ------------------------------
213223# ADDITIONAL COVERAGE TESTS FOR APIMREQUESTS
214224# ------------------------------
@@ -432,6 +442,49 @@ def test_multi_request_non_json_response(mock_print_info, mock_session_class, ap
432442 assert result [0 ]['response' ] == 'Plain text response'
433443
434444
445+ @pytest .mark .unit
446+ @patch ('apimrequests.time.sleep' )
447+ @patch ('apimrequests.requests.Session' )
448+ def test_multi_request_sleep_zero (mock_session_class , mock_sleep , apim ):
449+ """Test _multiRequest respects sleepMs=0 without sleeping."""
450+ mock_session = MagicMock ()
451+ mock_session_class .return_value = mock_session
452+
453+ mock_response = MagicMock ()
454+ mock_response .status_code = 200
455+ mock_response .headers = {'Content-Type' : 'application/json' }
456+ mock_response .json .return_value = {'ok' : True }
457+ mock_response .text = '{"ok": true}'
458+ mock_session .request .return_value = mock_response
459+
460+ with patch .object (apim , '_print_response_code' ):
461+ result = apim ._multiRequest (HTTP_VERB .GET , '/sleep' , 1 , sleepMs = 0 )
462+
463+ assert result [0 ]['status_code' ] == 200
464+ mock_sleep .assert_not_called ()
465+
466+
467+ @pytest .mark .unit
468+ @patch ('apimrequests.time.sleep' )
469+ @patch ('apimrequests.requests.Session' )
470+ def test_multi_request_sleep_positive (mock_session_class , mock_sleep , apim ):
471+ """Test _multiRequest sleeps when sleepMs is positive."""
472+ mock_session = MagicMock ()
473+ mock_session_class .return_value = mock_session
474+
475+ mock_response = MagicMock ()
476+ mock_response .status_code = 200
477+ mock_response .headers = {'Content-Type' : 'application/json' }
478+ mock_response .json .return_value = {'ok' : True }
479+ mock_response .text = '{"ok": true}'
480+ mock_session .request .return_value = mock_response
481+
482+ with patch .object (apim , '_print_response_code' ):
483+ apim ._multiRequest (HTTP_VERB .GET , '/sleep' , 2 , sleepMs = 150 )
484+
485+ mock_sleep .assert_called_once_with (0.15 )
486+
487+
435488@pytest .mark .unit
436489@patch ('apimrequests.print_val' )
437490def test_print_response_non_200_status (mock_print_val , apim ):
@@ -449,6 +502,60 @@ def test_print_response_non_200_status(mock_print_val, apim):
449502 mock_print_val .assert_any_call ('Response body' , '{"error": "not found"}' , True )
450503
451504
505+ @pytest .mark .unit
506+ @patch ('apimrequests.print_val' )
507+ def test_print_response_200_invalid_json (mock_print_val , apim ):
508+ """Test _print_response handles invalid JSON body for 200 responses."""
509+ mock_response = MagicMock ()
510+ mock_response .status_code = 200
511+ mock_response .reason = 'OK'
512+ mock_response .headers = {'Content-Type' : 'application/json' }
513+ mock_response .text = 'not valid json'
514+
515+ with patch .object (apim , '_print_response_code' ):
516+ apim ._print_response (mock_response )
517+
518+ mock_print_val .assert_any_call ('Response body' , 'not valid json' , True )
519+
520+
521+ @pytest .mark .unit
522+ @patch ('apimrequests.print_val' )
523+ def test_print_response_200_valid_json (mock_print_val , apim ):
524+ """Test _print_response prints formatted JSON when parse succeeds."""
525+ mock_response = MagicMock ()
526+ mock_response .status_code = 200
527+ mock_response .reason = 'OK'
528+ mock_response .headers = {'Content-Type' : 'application/json' }
529+ mock_response .text = '{"alpha": 1}'
530+
531+ with patch .object (apim , '_print_response_code' ):
532+ apim ._print_response (mock_response )
533+
534+ mock_print_val .assert_any_call ('Response body' , '{\n "alpha": 1\n }' , True )
535+
536+
537+ @pytest .mark .unit
538+ @patch ('apimrequests.print_val' )
539+ def test_print_response_code_success_and_error (mock_print_val , apim ):
540+ """Test _print_response_code color formatting for success and error codes."""
541+ class DummyResponse :
542+ status_code = 200
543+ reason = 'OK'
544+
545+ apim ._print_response_code (DummyResponse ())
546+
547+ class ErrorResponse :
548+ status_code = 500
549+ reason = 'Server Error'
550+
551+ apim ._print_response_code (ErrorResponse ())
552+
553+ messages = [record .args [1 ] for record in mock_print_val .call_args_list ]
554+
555+ assert any ('200 - OK' in msg for msg in messages )
556+ assert any ('500 - Server Error' in msg for msg in messages )
557+
558+
452559@pytest .mark .unit
453560@patch ('apimrequests.requests.get' )
454561@patch ('apimrequests.print_info' )
0 commit comments