Esempio n. 1
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()
        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. 2
0
    def test_context_is_reset_after_request_has_finished(self):
        context = {"foo": "bar"}

        def responseCls(connection, response) -> mock.MagicMock:
            connection.called = True
            self.assertEqual(connection.context, context)
            return mock.MagicMock(spec=Response)

        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. 3
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()
        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. 4
0
    def test_parse_errors_can_be_retried(self):
        class RetryableThrowingError(Response):
            parse_error_counter: int = 0
            success_counter: int = 0

            def __init__(self, *_, **__):
                super().__init__(mock.MagicMock(), mock.MagicMock())

            def parse_body(self):
                return super().parse_body()

            def parse_error(self):
                RetryableThrowingError.parse_error_counter += 1
                if RetryableThrowingError.parse_error_counter > 1:
                    return "success"
                else:
                    raise RateLimitReachedError()

            def success(self):
                RetryableThrowingError.success_counter += 1
                if RetryableThrowingError.success_counter > 1:
                    return True
                else:
                    return False

        con = Connection()
        con.connection = Mock()
        con.responseCls = RetryableThrowingError
        result = con.request(action="/", retry_failed=True)

        self.assertEqual(result.success(), True)
Esempio n. 5
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. 6
0
    def test_request_parses_errors(self):
        class ThrowingResponse(Response):
            def __init__(self, *_, **__):
                super().__init__(mock.MagicMock(), mock.MagicMock())

            def parse_body(self):
                return super().parse_body()

            def parse_error(self):
                raise RateLimitReachedError()

            def success(self):
                return False

        con = Connection()
        con.connection = Mock()
        con.responseCls = ThrowingResponse

        with self.assertRaises(RateLimitReachedError):
            con.request(action="/")