def test_url_property(get_http_host):
    http_host = get_http_host()
    http_host.port = None

    security = http_logging.HttpSecurity(ssl_enable=False)
    config = http_logging.ConfigLog(security=security)

    transport = AsyncHttpTransport(
        http_host=http_host,
        config=config,
    )

    assert transport.url == \
        f'http://{http_host.name}/{http_host.path}'

    security.ssl_enable = True

    transport = AsyncHttpTransport(
        http_host=http_host,
        config=config,
    )

    assert transport.url == \
        f'https://{http_host.name}/{http_host.path}'

    http_host.port = 1234

    transport = AsyncHttpTransport(
        http_host=http_host,
        config=config,
    )

    assert transport.url == \
        f'https://{http_host.name}:{http_host.port}/{http_host.path}'
    def __init__(
            self,
            *,  # Prevent usage of positional args
            http_host: Optional[http_logging.HttpHost] = None,
            config: Optional[http_logging.ConfigLog] = None,
            **kwargs):
        if not http_host:
            http_host = HttpHost(name=None)

        self.http_host = http_host
        self.config = config

        # Set default ConfigLog params
        if self.config is None:
            self.config = http_logging.ConfigLog()

        super().__init__(
            host=self.http_host.name,
            port=self.http_host.port,
            timeout=self.http_host.timeout,
            ssl_enable=self.config.security.ssl_enable,
            ssl_verify=self.config.security.ssl_verify,
            use_logging=self.config.use_logging,
            **kwargs,
        )

        self.__batches = super()._HttpTransport__batches

        self._path = self.http_host.path
        self._custom_headers = self.config.custom_headers
def test_send_success_request(
    mock_requests,
    mock_logger,
    mock_json,
    get_http_host,
):
    http_host = get_http_host()
    security = http_logging.HttpSecurity(ssl_enable=True)
    config = http_logging.ConfigLog(
        use_logging=True,
        security=security,
    )

    mock_response = mock.Mock()
    mock_response.ok = False
    mock_post = mock.Mock(return_value=mock_response)
    mock_requests.Session().post = mock_post

    mock_json.dumps().encoding.return_value = '{"foo": "bar"}'

    transport = AsyncHttpTransport(
        http_host=http_host,
        config=config,
    )

    events = mock.Mock()
    batches = [mock.MagicMock(), mock.MagicMock(), mock.MagicMock()]
    for batch in batches:
        batch.__len__.return_value = 10

    transport._AsyncHttpTransport__batches = mock.Mock(return_value=batches)

    transport.send(events=events)

    transport._AsyncHttpTransport__batches.assert_called_with(events)
    mock_logger.debug.assert_called()
    mock_logger.exception.assert_not_called()
    mock_requests.Session.assert_called()

    post_requests = mock_post.mock_calls

    assert len(post_requests) == 3

    for batch in batches:
        expected_request = mock.call(
            transport.url,
            headers=transport.headers,
            json=batch,
            verify=transport._ssl_verify,
            timeout=transport._timeout,
        )

        assert expected_request in post_requests
示例#4
0
    def __init__(
        self,
        *,  # Prevent usage of positional args
        http_host: Optional[http_logging.HttpHost] = None,
        support_class: Optional[http_logging.SupportClass] = None,
        transport_class: Optional[Transport] = None,
        formatter_class: Optional[logging.Formatter] = None,
        config: Optional[http_logging.ConfigLog] = None,
        **kwargs,
    ):
        if not http_host:
            http_host = HttpHost(name=None)

        self.http_host = http_host
        self.support_class = support_class
        self.config = config

        # Register this Handler as the HttpHost parent
        self.http_host.register_parent_handler(handler=self)

        # Set default ConfigLog params
        if self.config is None:
            self.config = http_logging.ConfigLog()

        # Set default support class
        if self.support_class is None:
            self.support_class = http_logging.SupportClass(
                http_host=http_host,
                config=config,
                _transport=transport_class,
                _formatter=formatter_class,
            )

        super().__init__(
            host=self.http_host.name,
            port=self.http_host.port,
            database_path=self.config.database_path,
            transport=self.support_class.transport,
            ssl_enable=self.config.security.ssl_enable,
            ssl_verify=self.config.security.ssl_verify,
            keyfile=self.config.security.keyfile,
            certfile=self.config.security.certfile,
            ca_certs=self.config.security.ca_certs,
            enable=self.config.enable,
            event_ttl=self.config.event_ttl,
            encoding=self.config.encoding,
            **kwargs,
        )

        self.formatter = self.support_class.formatter
def test_custom_formatter_and_transport(http_host):
    config = http_logging.ConfigLog()
    transport = AsyncHttpTransport(http_host=http_host, config=config)
    formatter = HttpLogFormatter()

    handler = AsyncHttpHandler(
        http_host=http_host,
        support_class=http_logging.SupportClass(
            http_host=http_host,
            config=config,
            _transport=transport,
            _formatter=formatter,
        ),
    )

    handler._setup_transport()

    assert handler._transport == transport
    assert handler.formatter == formatter
def test_send_failed_request(
    mock_requests,
    mock_logger,
    mock_json,
    get_http_host,
):
    http_host = get_http_host()
    security = http_logging.HttpSecurity(ssl_enable=True)
    config = http_logging.ConfigLog(
        use_logging=True,
        security=security,
    )

    req_exception = requests.exceptions.RequestException('HTTP Error')
    mock_response = mock.Mock()
    mock_response.ok = False  # Simulate failed request
    mock_response.raise_for_status.side_effect = req_exception
    mock_post = mock.Mock(return_value=mock_response)
    mock_requests.Session().post = mock_post

    mock_json.dumps().encoding.return_value = '{"foo": "bar"}'

    transport = AsyncHttpTransport(
        http_host=http_host,
        config=config,
    )

    events = mock.Mock()
    batches = [mock.MagicMock()]

    batches[0].__len__.return_value = 10

    transport._AsyncHttpTransport__batches = mock.Mock(return_value=batches)

    transport.send(events=events)

    assert len(mock_post.mock_calls) == 1

    mock_requests.Session().close.assert_called()
    mock_response.raise_for_status.assert_called()
    mock_logger.debug.assert_called()
    mock_logger.exception.assert_called_with(req_exception)
def test_get_custom_headers(get_http_host):
    http_host = get_http_host()

    transport = AsyncHttpTransport(
        http_host=http_host,
    )

    assert transport.get_custom_headers() == {}

    dummy_headers = {'foo': 'bar'}
    mock_custom_headers = mock.Mock(return_value=dummy_headers)

    config = http_logging.ConfigLog(custom_headers=mock_custom_headers)

    transport = AsyncHttpTransport(
        http_host=http_host,
        config=config,
    )

    assert transport.get_custom_headers() == dummy_headers