def test_secure_by_default(self):
     """
     Test that the connection class will default to secure (https)
     """
     conn = Connection(host='localhost', port=8081)
     conn.connect()
     self.assertEqual(conn.connection.host, 'https://localhost:8081')
Esempio n. 2
0
def get_response_object(url, method='GET', headers=None):
    """
    Utility function which uses libcloud's connection class to issue an HTTP
    request.

    :param url: URL to send the request to.
    :type url: ``str``

    :param method: HTTP method.
    :type method: ``str``

    :param headers: Optional request headers.
    :type headers: ``dict``

    :return: Response object.
    :rtype: :class:`Response`.
    """
    parsed_url = urlparse.urlparse(url)
    parsed_qs = parse_qs(parsed_url.query)
    secure = parsed_url.scheme == 'https'

    headers = headers or {}
    method = method.upper()

    con = Connection(secure=secure, host=parsed_url.netloc)
    response = con.request(action=parsed_url.path, params=parsed_qs,
                           headers=headers, method=method)
    return response
    def test_RawResponse_class_read_method(self):
        """
        Test that the RawResponse class includes a response
        property which exhibits the same properties and methods
        as httplib.HTTPResponse for backward compat <1.5.0
        """
        TEST_DATA = '1234abcd'

        conn = Connection(host='mock.com', port=80, secure=False)
        conn.connect()

        with requests_mock.Mocker() as m:
            m.register_uri('GET', 'http://mock.com/raw_data', text=TEST_DATA,
                           headers={'test': 'value'})
            response = conn.request('/raw_data', raw=True)
        data = response.response.read()
        self.assertEqual(data, TEST_DATA)

        header_value = response.response.getheader('test')
        self.assertEqual(header_value, 'value')

        headers = response.response.getheaders()
        self.assertEqual(headers, [('test', 'value')])

        self.assertEqual(response.response.status, 200)
Esempio n. 4
0
    def test_raw_request_without_proxy(self, patched_putrequest, *unused_args):
        host = '1.2.3.4'
        port = '31337'
        method = 'FAKEMETHOD'
        action = '/fakeendpoint'

        conn = Connection(secure=True, host=host, port=port)
        conn.request(action, params={}, data=None, headers=None, method=method,
                     raw=True)
        patched_putrequest.assert_called_once_with(method, action)
Esempio n. 5
0
    def test_retry_with_sleep(self, mock_connect):
        con = Connection()
        con.connection = Mock()

        mock_connect.side_effect = socket.gaierror("")
        retry_request = Retry(timeout=0.2, retry_delay=0.1, backoff=1)
        self.assertRaises(socket.gaierror,
                          retry_request(con.request),
                          action="/")

        self.assertGreater(mock_connect.call_count, 1, "Retry logic failed")
Esempio n. 6
0
 def test_connection_url_merging(self):
     """
     Test that the connection class will parse URLs correctly
     """
     conn = Connection(url="http://test.com/")
     conn.connect()
     self.assertEqual(conn.connection.host, "http://test.com")
     with requests_mock.mock() as m:
         m.get("http://test.com/test", text="data")
         response = conn.request("/test")
     self.assertEqual(response.body, "data")
 def test_connection_url_merging(self):
     """
     Test that the connection class will parse URLs correctly
     """
     conn = Connection(url='http://test.com/')
     conn.connect()
     self.assertEqual(conn.connection.host, 'http://test.com')
     with requests_mock.mock() as m:
         m.get('http://test.com/test', text='data')
         response = conn.request('/test')
     self.assertEqual(response.body, 'data')
Esempio n. 8
0
 def test_connection_url_merging(self):
     """
     Test that the connection class will parse URLs correctly
     """
     conn = Connection(url='http://test.com/')
     conn.connect()
     self.assertEqual(conn.connection.host, 'http://test.com')
     with requests_mock.mock() as m:
         m.get('http://test.com/test', text='data')
         response = conn.request('/test')
     self.assertEqual(response.body, 'data')
Esempio n. 9
0
    def test_retry_connection(self):
        con = Connection(timeout=0.2, retry_delay=0.1)
        con.connection = Mock()
        connect_method = "libcloud.common.base.Connection.request"

        with patch(connect_method) as mock_connect:
            try:
                mock_connect.side_effect = socket.gaierror("")
                con.request("/")
            except socket.gaierror:
                pass
Esempio n. 10
0
    def test_retry_rate_limit_error_timeout(self, mock_connect):
        con = Connection()
        con.connection = Mock()

        mock_connect.__name__ = 'mock_connect'
        headers = {'retry-after': 0.2}
        mock_connect.side_effect = RateLimitReachedError(headers=headers)
        retry_request = Retry(timeout=0.4, retry_delay=0.1, backoff=1)
        self.assertRaises(RateLimitReachedError,
                          retry_request(con.request),
                          action='/')
        self.assertEqual(mock_connect.call_count, 2, 'Retry logic failed')
Esempio n. 11
0
 def test_connect_with_prefix(self):
     """
     Test that a connection with a base path (e.g. /v1/) will
     add the base path to requests
     """
     conn = Connection(url='http://test.com/')
     conn.connect()
     conn.request_path = '/v1'
     self.assertEqual(conn.connection.host, 'http://test.com')
     with requests_mock.mock() as m:
         m.get('http://test.com/v1/test', text='data')
         response = conn.request('/test')
     self.assertEqual(response.body, 'data')
Esempio n. 12
0
    def test_retry_connection(self):
        con = Connection(timeout=1, retry_delay=0.1)
        con.connection = Mock()
        connect_method = 'libcloud.common.base.Connection.request'

        with patch(connect_method) as mock_connect:
            try:
                mock_connect.side_effect = socket.gaierror('')
                con.request('/')
            except socket.gaierror:
                pass
            except Exception:
                self.fail('Failed to raise socket exception')
Esempio n. 13
0
    def test_retry_connection(self):
        con = Connection(timeout=1, retry_delay=0.1)
        con.connection = Mock()
        connect_method = 'libcloud.common.base.Connection.request'

        with patch(connect_method) as mock_connect:
            try:
                mock_connect.side_effect = socket.gaierror('')
                con.request('/')
            except socket.gaierror:
                pass
            except Exception:
                self.fail('Failed to raise socket exception')
Esempio n. 14
0
    def test_retry_with_backoff(self):
        con = Connection()
        con.connection = Mock()
        connect_method = "libcloud.common.base.Connection.request"

        with patch(connect_method) as mock_connect:
            mock_connect.__name__ = "mock_connect"
            with self.assertRaises(socket.gaierror):
                mock_connect.side_effect = socket.gaierror("")
                retry_request = retry(timeout=2, retry_delay=0.1, backoff=1)
                retry_request(con.request)(action="/")

            self.assertGreater(mock_connect.call_count, 1, "Retry logic failed")
 def test_debug_log_class_handles_request(self):
     with StringIO() as fh:
         libcloud.enable_debug(fh)
         conn = Connection(url='http://test.com/')
         conn.connect()
         self.assertEqual(conn.connection.host, 'http://test.com')
         with requests_mock.mock() as m:
             m.get('http://test.com/test', text='data')
             conn.request('/test')
         log = fh.getvalue()
     self.assertTrue(isinstance(conn.connection, LoggingConnection))
     self.assertIn('-i -X GET', log)
     self.assertIn('data', log)
Esempio n. 16
0
    def test_retry_with_backoff(self):
        con = Connection()
        con.connection = Mock()
        connect_method = 'libcloud.common.base.Connection.request'

        with patch(connect_method) as mock_connect:
            mock_connect.__name__ = 'mock_connect'
            with self.assertRaises(socket.gaierror):
                mock_connect.side_effect = socket.gaierror('')
                retry_request = retry(timeout=0.2, retry_delay=0.1, backoff=1)
                retry_request(con.request)(action='/')

            self.assertGreater(mock_connect.call_count, 1,
                               'Retry logic failed')
Esempio n. 17
0
    def test_retry_with_timeout(self):
        con = Connection()
        con.connection = Mock()
        connect_method = 'libcloud.common.base.Connection.request'

        with patch(connect_method) as mock_connect:
            mock_connect.__name__ = 'mock_connect'
            with self.assertRaises(socket.gaierror):
                mock_connect.side_effect = socket.gaierror('')
                retry_request = retry(timeout=2, retry_delay=.1,
                                      backoff=1)
                retry_request(con.request)(action='/')

            self.assertGreater(mock_connect.call_count, 1,
                               'Retry logic failed')
Esempio n. 18
0
    def test_retry_connection_backoff(self):
        connection = Connection(timeout=10, retry_delay=0.1, backoff=2)
        connection.connection = Mock(request=Mock(
            side_effect=socket.gaierror('')))

        with patch('time.sleep') as sleep_fn:
            sleep_fn.side_effect = [
                None, None, None, None, None, StopIteration
            ]

            self.assertRaises(StopIteration, connection.request, '/')
            self.assertEqual(connection.connection.request.call_count, 6)
            self.assertEqual(sleep_fn.call_count, 6)
            self.assertEqual(
                sleep_fn.call_args_list,
                [mock.call(i) for i in (0.1, 0.2, 0.4, 0.8, 1.6, 3.2)])
Esempio n. 19
0
    def test_dont_allow_insecure(self):
        Connection.allow_insecure = True
        Connection(secure=False)

        Connection.allow_insecure = False

        expected_msg = r"Non https connections are not allowed \(use " r"secure=True\)"
        assertRaisesRegex(self, ValueError, expected_msg, Connection, secure=False)
Esempio n. 20
0
    def test_retry_should_not_retry_on_non_defined_exception(self, mock_connect):
        con = Connection()
        con.connection = Mock()

        self.retry_counter = 0

        mock_connect.__name__ = "mock_connect"
        mock_connect.side_effect = ValueError("should not retry this " "error")
        retry_request = Retry(timeout=5, retry_delay=0.1, backoff=1)

        self.assertRaisesRegex(
            ValueError,
            "should not retry this error",
            retry_request(con.request),
            action="/",
        )
        self.assertEqual(mock_connect.call_count, 1, "Retry logic failed")
Esempio n. 21
0
    def test_retry_connection_timeout(self, datetime_obj):
        connection = Connection(timeout=65, retry_delay=20)
        connection.connection = Mock(request=Mock(
            side_effect=socket.gaierror('')))
        datetime_obj.now.side_effect = [
            datetime(2017, 7, 28, 0, 26, 10, 0),
            datetime(2017, 7, 28, 0, 26, 10, 0),
            datetime(2017, 7, 28, 0, 26, 30, 0),
            datetime(2017, 7, 28, 0, 26, 50, 0),
            datetime(2017, 7, 28, 0, 27, 10, 0),
            datetime(2017, 7, 28, 0, 27, 30, 0),
        ]

        with patch('time.sleep') as sleep_fn:
            self.assertRaises(socket.gaierror, connection.request, '/')
            self.assertEqual(sleep_fn.call_args_list,
                             [mock.call(i) for i in (20, 20, 20, 5)])
Esempio n. 22
0
    def test_retry_connection_with_iterable_retry_delay(self):
        connection = Connection(timeout=20,
                                retry_delay=(1, 1, 3, 5),
                                backoff=1)
        connection.connection = Mock(request=Mock(
            side_effect=socket.gaierror('')))

        with patch('time.sleep') as sleep_fn:
            sleep_fn.side_effect = [
                None, None, None, None, None, StopIteration
            ]

            self.assertRaises(StopIteration, connection.request, '/')
            self.assertEqual(connection.connection.request.call_count, 6)
            self.assertEqual(sleep_fn.call_count, 6)
            self.assertEqual(sleep_fn.call_args_list,
                             [mock.call(i) for i in (1, 1, 3, 5, 5, 5)])
Esempio n. 23
0
    def test_retry_should_not_retry_on_non_defined_exception(
            self, mock_connect):
        con = Connection()
        con.connection = Mock()
        connect_method = 'libcloud.common.base.Connection.request'

        self.retry_counter = 0

        mock_connect.__name__ = 'mock_connect'
        headers = {'retry-after': 0.2}
        mock_connect.side_effect = ValueError('should not retry this ' 'error')
        retry_request = Retry(timeout=5, retry_delay=0.1, backoff=1)

        self.assertRaisesRegex(ValueError,
                               'should not retry this error',
                               retry_request(con.request),
                               action='/')
        self.assertEqual(mock_connect.call_count, 1, 'Retry logic failed')
Esempio n. 24
0
    def test_implicit_port(self):
        """
        Test that the port is not included in the URL if the protocol implies
        the port, e.g. http implies 80
        """
        conn = Connection(secure=True, host='localhost', port=443)
        conn.connect()
        self.assertEqual(conn.connection.host, 'https://localhost')

        conn2 = Connection(secure=False, host='localhost', port=80)
        conn2.connect()
        self.assertEqual(conn2.connection.host, 'http://localhost')
Esempio n. 25
0
    def test_dont_allow_insecure(self):
        Connection.allow_insecure = True
        Connection(secure=False)

        Connection.allow_insecure = False

        expected_msg = (r'Non https connections are not allowed \(use '
                        'secure=True\)')
        self.assertRaisesRegexp(ValueError, expected_msg, Connection,
                                secure=False)
Esempio n. 26
0
    def test_secure_connection_unusual_port(self):
        """
        Test that the connection class will default to secure (https) even
        when the port is an unusual (non 443, 80) number
        """
        conn = Connection(secure=True, host='localhost', port=8081)
        conn.connect()
        self.assertEqual(conn.connection.host, 'https://localhost:8081')

        conn2 = Connection(url='https://localhost:8081')
        conn2.connect()
        self.assertEqual(conn2.connection.host, 'https://localhost:8081')
Esempio n. 27
0
    def test_insecure_connection_unusual_port(self):
        """
        Test that the connection will allow unusual ports and insecure
        schemes
        """
        conn = Connection(secure=False, host='localhost', port=8081)
        conn.connect()
        self.assertEqual(conn.connection.host, 'http://localhost:8081')

        conn2 = Connection(url='http://localhost:8081')
        conn2.connect()
        self.assertEqual(conn2.connection.host, 'http://localhost:8081')
Esempio n. 28
0
def get_response_object(url):
    """
    Utility function which uses libcloud's connection class to issue an HTTP
    request.

    :param url: URL to send the request to.
    :type url: ``str``

    :return: Response object.
    :rtype: :class:`Response`.
    """
    parsed_url = urlparse.urlparse(url)
    parsed_qs = parse_qs(parsed_url.query)
    secure = parsed_url.scheme == 'https'

    con = Connection(secure=secure, host=parsed_url.netloc)
    response = con.request(method='GET', action=parsed_url.path,
                           params=parsed_qs)
    return response
Esempio n. 29
0
def get_response_object(url):
    """
    Utility function which uses libcloud's connection class to issue an HTTP
    request.

    :param url: URL to send the request to.
    :type url: ``str``

    :return: Response object.
    :rtype: :class:`Response`.
    """
    parsed_url = urlparse.urlparse(url)
    parsed_qs = parse_qs(parsed_url.query)
    secure = parsed_url.scheme == 'https'

    con = Connection(secure=secure, host=parsed_url.netloc)
    response = con.request(method='GET',
                           action=parsed_url.path,
                           params=parsed_qs)
    return response
Esempio n. 30
0
    def test_retry_connection_ssl_error(self):
        conn = Connection(timeout=1, retry_delay=0.1)

        with patch.object(conn, 'connect', Mock()):
            with patch.object(conn, 'connection') as connection:
                connection.request = MagicMock(
                    __name__='request',
                    side_effect=ssl.SSLError(TRANSIENT_SSL_ERROR))

                self.assertRaises(ssl.SSLError, conn.request, '/')
                self.assertGreater(connection.request.call_count, 1)
Esempio n. 31
0
    def test_retry_rate_limit_error_success_on_second_attempt(self, mock_connect):
        con = Connection()
        con.connection = Mock()

        self.retry_counter = 0

        def mock_connect_side_effect(*args, **kwargs):
            self.retry_counter += 1

            if self.retry_counter < 2:
                headers = {"retry-after": 0.2}
                raise RateLimitReachedError(headers=headers)

            return "success"

        mock_connect.__name__ = "mock_connect"
        mock_connect.side_effect = mock_connect_side_effect
        retry_request = Retry(timeout=1, retry_delay=0.1, backoff=1)
        result = retry_request(con.request)(action="/")
        self.assertEqual(result, "success")

        self.assertEqual(mock_connect.call_count, 2, "Retry logic failed")
Esempio n. 32
0
    def test_retry_on_all_default_retry_exception_classes(self, mock_connect):
        con = Connection()
        con.connection = Mock()

        self.retry_counter = 0

        def mock_connect_side_effect(*args, **kwargs):
            self.retry_counter += 1

            if self.retry_counter < len(RETRY_EXCEPTIONS):
                raise RETRY_EXCEPTIONS[self.retry_counter]

            return "success"

        mock_connect.__name__ = "mock_connect"
        mock_connect.side_effect = mock_connect_side_effect
        retry_request = Retry(timeout=0.6, retry_delay=0.1, backoff=1)
        result = retry_request(con.request)(action="/")
        self.assertEqual(result, "success")

        self.assertEqual(mock_connect.call_count, len(RETRY_EXCEPTIONS),
                         "Retry logic failed")
Esempio n. 33
0
class MockHttpFileFixturesTests(unittest.TestCase):
    """
    Test the behaviour of MockHttp
    """
    def setUp(self):
        Connection.conn_class = TestMockHttp
        Connection.responseCls = Response
        self.connection = Connection()

    def test_unicode_response(self):
        r = self.connection.request("/unicode")
        self.assertEqual(r.parse_body(), "Ś")

    def test_json_unicode_response(self):
        self.connection.responseCls = JsonResponse
        r = self.connection.request("/unicode/json")
        self.assertEqual(r.object, {"test": "Ś"})

    def test_xml_unicode_response(self):
        self.connection.responseCls = XmlResponse
        response = self.connection.request("/unicode/xml")
        self.assertEqual(response.object.text, "Ś")
Esempio n. 34
0
    def test_retry_rate_limit_error_success_on_second_attempt(
            self, mock_connect):
        con = Connection()
        con.connection = Mock()
        connect_method = 'libcloud.common.base.Connection.request'

        self.retry_counter = 0

        def mock_connect_side_effect(*args, **kwargs):
            self.retry_counter += 1

            if self.retry_counter < 2:
                headers = {'retry-after': 0.2}
                raise RateLimitReachedError(headers=headers)

            return 'success'

        mock_connect.__name__ = 'mock_connect'
        mock_connect.side_effect = mock_connect_side_effect
        retry_request = Retry(timeout=0.6, retry_delay=0.1, backoff=1)
        result = retry_request(con.request)(action='/')
        self.assertEqual(result, "success")

        self.assertEqual(mock_connect.call_count, 2, 'Retry logic failed')
Esempio n. 35
0
    def test_implicit_port(self):
        """
        Test that the port is not included in the URL if the protocol implies
        the port, e.g. http implies 80
        """
        conn = Connection(secure=True, host='localhost', port=443)
        conn.connect()
        self.assertEqual(conn.connection.host, 'https://localhost')

        conn2 = Connection(secure=False, host='localhost', port=80)
        conn2.connect()
        self.assertEqual(conn2.connection.host, 'http://localhost')
Esempio n. 36
0
    def test_secure_connection_unusual_port(self):
        """
        Test that the connection class will default to secure (https) even
        when the port is an unusual (non 443, 80) number
        """
        conn = Connection(secure=True, host='localhost', port=8081)
        conn.connect()
        self.assertEqual(conn.connection.host, 'https://localhost:8081')

        conn2 = Connection(url='https://localhost:8081')
        conn2.connect()
        self.assertEqual(conn2.connection.host, 'https://localhost:8081')
Esempio n. 37
0
    def test_insecure_connection_unusual_port(self):
        """
        Test that the connection will allow unusual ports and insecure
        schemes
        """
        conn = Connection(secure=False, host='localhost', port=8081)
        conn.connect()
        self.assertEqual(conn.connection.host, 'http://localhost:8081')

        conn2 = Connection(url='http://localhost:8081')
        conn2.connect()
        self.assertEqual(conn2.connection.host, 'http://localhost:8081')
Esempio n. 38
0
 def test_debug_log_class_handles_request(self):
     with StringIO() as fh:
         libcloud.enable_debug(fh)
         conn = Connection(url="http://test.com/")
         conn.connect()
         self.assertEqual(conn.connection.host, "http://test.com")
         with requests_mock.mock() as m:
             m.get("http://test.com/test", text="data")
             conn.request("/test")
         log = fh.getvalue()
     self.assertTrue(isinstance(conn.connection, LoggingConnection))
     self.assertIn("-i -X GET", log)
     self.assertIn("data", log)
Esempio n. 39
0
 def test_connect_with_prefix(self):
     """
     Test that a connection with a base path (e.g. /v1/) will
     add the base path to requests
     """
     conn = Connection(url="http://test.com/")
     conn.connect()
     conn.request_path = "/v1"
     self.assertEqual(conn.connection.host, "http://test.com")
     with requests_mock.mock() as m:
         m.get("http://test.com/v1/test", text="data")
         response = conn.request("/test")
     self.assertEqual(response.body, "data")
 def test_debug_log_class_handles_request(self):
     with StringIO() as fh:
         libcloud.enable_debug(fh)
         conn = Connection(url='http://test.com/')
         conn.connect()
         self.assertEqual(conn.connection.host, 'http://test.com')
         with requests_mock.mock() as m:
             m.get('http://test.com/test', text='data')
             conn.request('/test')
         log = fh.getvalue()
     self.assertTrue(isinstance(conn.connection, LoggingConnection))
     self.assertIn('-i -X GET', log)
     self.assertIn('data', log)
Esempio n. 41
0
 def test_connect_with_prefix(self):
     """
     Test that a connection with a base path (e.g. /v1/) will
     add the base path to requests
     """
     conn = Connection(url='http://test.com/')
     conn.connect()
     conn.request_path = '/v1'
     self.assertEqual(conn.connection.host, 'http://test.com')
     with requests_mock.mock() as m:
         m.get('http://test.com/v1/test', text='data')
         response = conn.request('/test')
     self.assertEqual(response.body, 'data')
 def test_debug_log_class_handles_request_with_compression(self):
     request = zlib.compress(b'data')
     with StringIO() as fh:
         libcloud.enable_debug(fh)
         conn = Connection(url='http://test.com/')
         conn.connect()
         self.assertEqual(conn.connection.host, 'http://test.com')
         with requests_mock.mock() as m:
             m.get('http://test.com/test',
                   content=request,
                   headers={'content-encoding': 'zlib'})
             conn.request('/test')
         log = fh.getvalue()
     self.assertTrue(isinstance(conn.connection, LoggingConnection))
     self.assertIn('-i -X GET', log)
Esempio n. 43
0
    def test_raw_request_with_proxy(self, patched_putrequest, *unused_args):
        proxy_url = 'http://127.0.0.1:3128'
        host = '1.2.3.4'
        port = '31337'
        method = 'FAKEMETHOD'
        action = '/fakeendpoint'

        conn = Connection(secure=True, host=host, port=port)
        conn.set_http_proxy(proxy_url)
        conn.responseCls = FakeResponse
        conn.request(action, params={}, data=None, headers=None, method=method,
                     raw=True)

        proxied_action = "https://%s:%s%s" % (host, port, action)
        patched_putrequest.assert_called_once_with(method, proxied_action)
Esempio n. 44
0
    def test_rate_limit_error(self):
        sock = Mock()
        con = Connection()

        try:
            with patch('libcloud.utils.py3.httplib.HTTPResponse.getheaders',
                       MagicMock(return_value=CONFLICT_RESPONSE_STATUS)):
                with patch(
                        'libcloud.utils.py3.httplib.HTTPResponse._read_status',
                        MagicMock(return_value=SIMPLE_RESPONSE_STATUS)):
                    with tempfile.TemporaryFile(mode='w+b') as f:
                        f.write('HTTP/1.1 429 CONFLICT\n'.encode())
                        f.flush()
                        sock.makefile = Mock(return_value=f)
                        mock_obj = httplib.HTTPResponse(sock)
                        mock_obj.begin()
                        Response(mock_obj, con)
        except RateLimitReachedError:
            pass
        except Exception:
            self.fail('Failed to raise Rate Limit exception')
Esempio n. 45
0
 def test_debug_log_class_handles_request_with_compression(self):
     request = zlib.compress(b"data")
     with StringIO() as fh:
         libcloud.enable_debug(fh)
         conn = Connection(url="http://test.com/")
         conn.connect()
         self.assertEqual(conn.connection.host, "http://test.com")
         with requests_mock.mock() as m:
             m.get(
                 "http://test.com/test",
                 content=request,
                 headers={"content-encoding": "zlib"},
             )
             conn.request("/test")
         log = fh.getvalue()
     self.assertTrue(isinstance(conn.connection, LoggingConnection))
     self.assertIn("-i -X GET", log)
 def test_debug_method_uses_log_class(self):
     with StringIO() as fh:
         libcloud.enable_debug(fh)
         conn = Connection(timeout=10)
         conn.connect()
     self.assertTrue(isinstance(conn.connection, LoggingConnection))
Esempio n. 47
0
    def test_content_length(self):
        con = Connection()
        con.connection = Mock()

        # GET method
        # No data, no content length should be present
        con.request("/test", method="GET", data=None)
        call_kwargs = con.connection.request.call_args[1]
        self.assertTrue("Content-Length" not in call_kwargs["headers"])

        # '' as data, no content length should be present
        con.request("/test", method="GET", data="")
        call_kwargs = con.connection.request.call_args[1]
        self.assertTrue("Content-Length" not in call_kwargs["headers"])

        # 'a' as data, content length should be present (data in GET is not
        # correct, but anyways)
        con.request("/test", method="GET", data="a")
        call_kwargs = con.connection.request.call_args[1]
        self.assertEqual(call_kwargs["headers"]["Content-Length"], "1")

        # POST, PUT method
        # No data, content length should be present
        for method in ["POST", "PUT", "post", "put"]:
            con.request("/test", method=method, data=None)
            call_kwargs = con.connection.request.call_args[1]
            self.assertEqual(call_kwargs["headers"]["Content-Length"], "0")

        # '' as data, content length should be present
        for method in ["POST", "PUT", "post", "put"]:
            con.request("/test", method=method, data="")
            call_kwargs = con.connection.request.call_args[1]
            self.assertEqual(call_kwargs["headers"]["Content-Length"], "0")

        # No data, raw request, do not touch Content-Length if present
        for method in ["POST", "PUT", "post", "put"]:
            con.request("/test", method=method, data=None, headers={"Content-Length": "42"}, raw=True)
            putheader_call_list = con.connection.putheader.call_args_list
            self.assertIn(call("Content-Length", "42"), putheader_call_list)

        # '' as data, raw request, do not touch Content-Length if present
        for method in ["POST", "PUT", "post", "put"]:
            con.request("/test", method=method, data=None, headers={"Content-Length": "42"}, raw=True)
            putheader_call_list = con.connection.putheader.call_args_list
            self.assertIn(call("Content-Length", "42"), putheader_call_list)

        # 'a' as data, content length should be present
        for method in ["POST", "PUT", "post", "put"]:
            con.request("/test", method=method, data="a")
            call_kwargs = con.connection.request.call_args[1]
            self.assertEqual(call_kwargs["headers"]["Content-Length"], "1")
Esempio n. 48
0
    def test_cache_busting(self):
        params1 = {"foo1": "bar1", "foo2": "bar2"}
        params2 = [("foo1", "bar1"), ("foo2", "bar2")]

        con = Connection()
        con.connection = Mock()
        con.pre_connect_hook = Mock()
        con.pre_connect_hook.return_value = {}, {}
        con.cache_busting = False

        con.request(action="/path", params=params1)
        args, kwargs = con.pre_connect_hook.call_args
        self.assertFalse("cache-busting" in args[0])
        self.assertEqual(args[0], params1)

        con.request(action="/path", params=params2)
        args, kwargs = con.pre_connect_hook.call_args
        self.assertFalse("cache-busting" in args[0])
        self.assertEqual(args[0], params2)

        con.cache_busting = True

        con.request(action="/path", params=params1)
        args, kwargs = con.pre_connect_hook.call_args
        self.assertTrue("cache-busting" in args[0])

        con.request(action="/path", params=params2)
        args, kwargs = con.pre_connect_hook.call_args
        self.assertTrue("cache-busting" in args[0][len(params2)])
Esempio n. 49
0
    def test_content_length(self):
        con = Connection()
        con.connection = Mock()

        # GET method
        # No data, no content length should be present
        con.request('/test', method='GET', data=None)
        call_kwargs = con.connection.request.call_args[1]
        self.assertTrue('Content-Length' not in call_kwargs['headers'])

        # '' as data, no content length should be present
        con.request('/test', method='GET', data='')
        call_kwargs = con.connection.request.call_args[1]
        self.assertTrue('Content-Length' not in call_kwargs['headers'])

        # 'a' as data, content length should be present (data in GET is not
        # correct, but anyways)
        con.request('/test', method='GET', data='a')
        call_kwargs = con.connection.request.call_args[1]
        self.assertEqual(call_kwargs['headers']['Content-Length'], '1')

        # POST, PUT method
        # No data, content length should be present
        for method in ['POST', 'PUT', 'post', 'put']:
            con.request('/test', method=method, data=None)
            call_kwargs = con.connection.request.call_args[1]
            self.assertEqual(call_kwargs['headers']['Content-Length'], '0')

        # '' as data, content length should be present
        for method in ['POST', 'PUT', 'post', 'put']:
            con.request('/test', method=method, data='')
            call_kwargs = con.connection.request.call_args[1]
            self.assertEqual(call_kwargs['headers']['Content-Length'], '0')

        # No data, raw request, do not touch Content-Length if present
        for method in ['POST', 'PUT', 'post', 'put']:
            con.request('/test', method=method, data=None,
                        headers={'Content-Length': '42'}, raw=True)
            putheader_call_list = con.connection.putheader.call_args_list
            self.assertIn(call('Content-Length', '42'), putheader_call_list)

        # '' as data, raw request, do not touch Content-Length if present
        for method in ['POST', 'PUT', 'post', 'put']:
            con.request('/test', method=method, data=None,
                        headers={'Content-Length': '42'}, raw=True)
            putheader_call_list = con.connection.putheader.call_args_list
            self.assertIn(call('Content-Length', '42'), putheader_call_list)

        # 'a' as data, content length should be present
        for method in ['POST', 'PUT', 'post', 'put']:
            con.request('/test', method=method, data='a')
            call_kwargs = con.connection.request.call_args[1]
            self.assertEqual(call_kwargs['headers']['Content-Length'], '1')
Esempio n. 50
0
    def test_morph_action_hook(self):
        conn = Connection(url="http://test.com")

        conn.request_path = ''
        self.assertEqual(conn.morph_action_hook('/test'), '/test')
        self.assertEqual(conn.morph_action_hook('test'), '/test')

        conn.request_path = '/v1'
        self.assertEqual(conn.morph_action_hook('/test'), '/v1/test')
        self.assertEqual(conn.morph_action_hook('test'), '/v1/test')

        conn.request_path = '/v1'
        self.assertEqual(conn.morph_action_hook('/test'), '/v1/test')
        self.assertEqual(conn.morph_action_hook('test'), '/v1/test')

        conn.request_path = 'v1'
        self.assertEqual(conn.morph_action_hook('/test'), '/v1/test')
        self.assertEqual(conn.morph_action_hook('test'), '/v1/test')

        conn.request_path = 'v1/'
        self.assertEqual(conn.morph_action_hook('/test'), '/v1/test')
        self.assertEqual(conn.morph_action_hook('test'), '/v1/test')
Esempio n. 51
0
    def test_context_is_reset_after_request_has_finished(self):
        context = {'foo': 'bar'}

        def responseCls(connection, response):
            connection.called = True
            self.assertEqual(connection.context, context)

        con = Connection()
        con.called = False
        con.connection = Mock()
        con.responseCls = responseCls

        con.set_context(context)
        self.assertEqual(con.context, context)

        con.request('/')

        # Context should have been reset
        self.assertTrue(con.called)
        self.assertEqual(con.context, {})

        # Context should also be reset if a method inside request throws
        con = Connection(timeout=1, retry_delay=0.1)
        con.connection = Mock()

        con.set_context(context)
        self.assertEqual(con.context, context)
        con.connection.request = Mock(side_effect=ssl.SSLError())

        try:
            con.request('/')
        except ssl.SSLError:
            pass

        self.assertEqual(con.context, {})

        con.connection = Mock()
        con.set_context(context)
        self.assertEqual(con.context, context)

        con.responseCls = Mock(side_effect=ValueError())

        try:
            con.request('/')
        except ValueError:
            pass

        self.assertEqual(con.context, {})
Esempio n. 52
0
    def test_cache_busting(self):
        params1 = {'foo1': 'bar1', 'foo2': 'bar2'}
        params2 = [('foo1', 'bar1'), ('foo2', 'bar2')]

        con = Connection()
        con.connection = Mock()
        con.pre_connect_hook = Mock()
        con.pre_connect_hook.return_value = {}, {}
        con.cache_busting = False

        con.request(action='/path', params=params1)
        args, kwargs = con.pre_connect_hook.call_args
        self.assertFalse('cache-busting' in args[0])
        self.assertEqual(args[0], params1)

        con.request(action='/path', params=params2)
        args, kwargs = con.pre_connect_hook.call_args
        self.assertFalse('cache-busting' in args[0])
        self.assertEqual(args[0], params2)

        con.cache_busting = True

        con.request(action='/path', params=params1)
        args, kwargs = con.pre_connect_hook.call_args
        self.assertTrue('cache-busting' in args[0])

        con.request(action='/path', params=params2)
        args, kwargs = con.pre_connect_hook.call_args
        self.assertTrue('cache-busting' in args[0][len(params2)])