Example #1
0
    def test_pre_send():
        """Test the pre_send method."""
        # backup time methods
        backup_sleep = time.sleep
        backup_time = time.time

        rate_limit_handler = RateLimitHandler()

        sleep = MagicMock(return_value=None)
        time.sleep = sleep

        time.time = MagicMock(return_value=7)

        response: Response = Response()
        type(response).ok = PropertyMock(True)
        response.headers = {
            'X-RateLimit-Remaining': 0,
            'X-RateLimit-Reset': 10
        }

        rate_limit_handler.post_send(response)

        request: PreparedRequest = PreparedRequest()

        rate_limit_handler.pre_send(request)
        sleep.assert_called_once_with(3)

        # restore time methods
        time.sleep = backup_sleep
        time.time = backup_time
Example #2
0
    def test_pre_send_IMF_fixdate():
        """Test the pre_send method."""
        rate_limit_handler = RateLimitHandler()
        response: Response = Response()
        type(response).ok = PropertyMock(True)
        response.headers = {
            'X-RateLimit-Remaining': 0,
            'X-RateLimit-Reset': 'Wed, 16 Sep 2020 19:04:00 GMT',
        }

        rate_limit_handler.post_send(response)
        request: PreparedRequest = PreparedRequest()

        rate_limit_handler.pre_send(request)
        time.sleep.assert_called_once_with(40.0)  # pylint: disable=no-member
Example #3
0
    def test_pre_send_timestamp():
        """Test the pre_send method."""
        rate_limit_handler = RateLimitHandler()
        response: Response = Response()
        type(response).ok = PropertyMock(True)
        response.headers = {
            'X-RateLimit-Remaining': 0,
            'X-RateLimit-Reset': 1600284000
        }

        rate_limit_handler.post_send(response)
        request: PreparedRequest = PreparedRequest()

        rate_limit_handler.pre_send(request)
        time.sleep.assert_called_once_with(1000)  # pylint: disable=no-member
Example #4
0
    def test_post_send():
        """Test the post_send method."""
        rate_limit_handler = RateLimitHandler(
            limit_remaining_header='remaining', limit_reset_header='reset')

        response: Response = Response()
        type(response).ok = PropertyMock(True)
        response.headers = {
            'X-RateLimit-Remaining': 10,
            'X-RateLimit-Reset': 10,
            'remaining': 50,
            'reset': 100000,
        }

        rate_limit_handler.post_send(response)

        assert rate_limit_handler.last_limit_reset_value == 100000, 'Got wrong last reset value'
        assert rate_limit_handler.last_limit_remaining_value == 50, 'Got wrong last remaining value'