def test_request_dry_run( mock_send, mock_getenv, mock_format, enabled_env, write_only_env, method, expected_real_request, ): # Mock any environment variables specified env_vars = {} if enabled_env is not None: env_vars['DRY_RUN_ENABLED'] = enabled_env if write_only_env is not None: env_vars['DRY_RUN_WRITE_ONLY'] = write_only_env mock_getenv.side_effect = env_vars.__getitem__ # Mock constants and run request response = request(method, 'http://url') # Verify that the request was or wasn't mocked based on settings if expected_real_request: assert mock_send.call_count == 1 assert response == mock_send() else: assert response == MOCK_RESPONSE assert mock_send.call_count == 0
def test_request_dry_run_disabled(requests_mock): real_response = {'results': ['response object']} requests_mock.get('http://url', json={'results': ['response object']}, status_code=200) assert request('GET', 'http://url').json() == real_response
def test_request_session(mock_ClientSession, mock_format): mock_session = MagicMock() request('GET', 'https://url', session=mock_session) mock_session.send.assert_called() mock_ClientSession.assert_not_called()
def test_request_headers(mock_send, mock_format): """Test that the request() wrapper passes along expected headers""" request('GET', 'https://url', access_token='token') request_obj = mock_send.call_args[0][0] assert request_obj.headers['Authorization'] == 'Bearer token'
def test_request_dry_run_kwarg(mock_request): response = request('GET', 'http://url', dry_run=True) assert response == MOCK_RESPONSE assert mock_request.call_count == 0