def test_request_raise_connections_error(self):
     pool = Urllib3Pool()
     http_error = urllib3.exceptions.HTTPError(
         pool.pool, "http://127.0.0.1:8098/stats", "error")
     pool.pool.urlopen.side_effect = http_error
     self.assertRaises(
         exceptions.RiakcachedConnectionError,
         pool.request,
         "GET",
         "http://127.0.0.1:8098/stats",
     )
 def test_request_raise_timeout_error(self):
     pool = Urllib3Pool()
     timeout_error = urllib3.exceptions.TimeoutError(
         pool.pool, "http://127.0.0.1:8098/stats", "timeout")
     pool.pool.urlopen.side_effect = timeout_error
     self.assertRaises(
         exceptions.RiakcachedTimeout,
         pool.request,
         "GET",
         "http://127.0.0.1:8098/stats",
     )
 def test_request_calls_pool_urlopen(self):
     pool = Urllib3Pool()
     result = mock.Mock()
     result.status = 200
     result.data = ""
     result.getheaders = lambda: {}
     pool.pool.urlopen.return_value = result
     self.assertEqual(pool.request("GET", "http://127.0.0.1:8098/stats"),
                      (200, "", {}))
     pool.pool.urlopen.assert_called_with(
         method="GET",
         url="http://127.0.0.1:8098/stats",
         body=None,
         headers=None,
         timeout=2,
         redirect=False,
     )
Exemple #4
0
    def __init__(self, bucket, pool=None):
        """Constructor for a new :class:`riakcached.clients.RiakClient`

        Pool - if no pool is provided then a default :class:`riakcached.pools.Urllib3Pool` is used

        :param bucket: The name of the Riak bucket to use
        :type bucket: str
        :param pool: The :class:`riakcached.pools.Pool` to use for requests
        :type pool: :class:`riakcached.pools.Pool`
        """
        if pool is None:
            self.pool = Urllib3Pool()
        else:
            self.pool = pool

        self.bucket = bucket
        self.base_url = self.pool.url.rstrip("/")
        self._serializers = {
            "application/json": json.dumps,
        }
        self._deserializers = {
            "application/json": json.loads,
        }
 def test_request_urlopen_with_headers_and_body(self):
     pool = Urllib3Pool()
     result = mock.Mock()
     result.status = 200
     result.data = ""
     result.getheaders = lambda: {}
     pool.pool.urlopen.return_value = result
     self.assertEqual(
         pool.request("POST",
                      "http://127.0.0.1:8098/stats",
                      body="test",
                      headers={"Content-Type": "application/test"}),
         (200, "", {}))
     pool.pool.urlopen.assert_called_with(
         method="POST",
         url="http://127.0.0.1:8098/stats",
         body="test",
         headers={
             "Content-Type": "application/test",
         },
         timeout=2,
         redirect=False,
     )
 def test_close_calls_pool_close(self):
     pool = Urllib3Pool()
     pool.close()
     pool.pool.assert_called()
 def test_connect_auto_connect_doesnt_call_connect(self):
     Urllib3Pool(auto_connect=False)
     self.connection_from_url.assert_not_called()
 def test_connect_with_different_url(self):
     pool = Urllib3Pool(base_url="http://example.org:8098")
     self.assertEqual(pool.url, "http://example.org:8098")
     self.connection_from_url.assert_called()
     self.connection_from_url.assert_called_with("http://example.org:8098")
 def test_connect_sets_up_pool(self):
     pool = Urllib3Pool()
     self.assertTrue(pool.pool)
     self.connection_from_url.assert_called()
     self.connection_from_url.assert_called_with("http://127.0.0.1:8098")