def test_request_strategy_sets_session_on_parent_when_not_already_set( mock_client): mock_client.client.get_session.return_value = None strategy = RequestStrategy() strategy.set_client(mock_client.client) mock_client.client.get_session.assert_called_once_with() mock_client.client.set_session.assert_called_once()
def test_request_strategy_get_method_delegates_to_parent_handlers(mock_requests, mock_client): mock_requests.get("mock://testserver.com", json={"active": True}, status_code=200) strategy = RequestStrategy() strategy.set_client(mock_client.client) response = strategy.get("mock://testserver.com", params={"foo": sentinel.params}) assert response == sentinel.result assert_request_called_once(mock_requests, "mock://testserver.com", "GET") assert_mock_client_called_once(mock_client, None)
def __init__( self, authentication_method: Optional[BaseAuthenticationMethod] = None, response_handler: Type[BaseResponseHandler] = RequestsResponseHandler, request_formatter: Type[BaseRequestFormatter] = NoOpRequestFormatter, error_handler: Type[BaseErrorHandler] = ErrorHandler, request_strategy: Optional[BaseRequestStrategy] = None, ): # Set default values self._default_headers = {} self._default_query_params = {} self._default_username_password_authentication = None # A session needs to live at this client level so that all # request strategies have access to the same session. self._session = None # Set client strategies self.set_authentication_method(authentication_method or NoAuthentication()) self.set_response_handler(response_handler) self.set_error_handler(error_handler) self.set_request_formatter(request_formatter) self.set_request_strategy(request_strategy or RequestStrategy()) # Perform any one time authentication required by api self._authentication_method.perform_initial_auth(self)
def test_request_strategy_does_not_set_session_if_already_set(mock_client): mock_client.client.get_session.return_value = sentinel.session strategy = RequestStrategy() strategy.set_client(mock_client.client) mock_client.client.get_session.assert_called_once_with() mock_client.client.set_session.assert_not_called()