Beispiel #1
0
 def test_non_200_result(self):
     curl = CurlStub(b"result", {pycurl.HTTP_CODE: 404})
     try:
         fetch("http://example.com", curl=curl)
     except HTTPCodeError as error:
         self.assertEqual(error.http_code, 404)
         self.assertEqual(error.body, b"result")
     else:
         self.fail("HTTPCodeError not raised")
Beispiel #2
0
 def test_pycurl_error(self):
     curl = CurlStub(error=pycurl.error(60, "pycurl error"))
     try:
         fetch("http://example.com", curl=curl)
     except PyCurlError as error:
         self.assertEqual(error.error_code, 60)
         self.assertEqual(error.message, "pycurl error")
     else:
         self.fail("PyCurlError not raised")
Beispiel #3
0
    def test_create_curl(self):
        curls = []

        def pycurl_Curl():
            curl = CurlStub(b"result")
            curls.append(curl)
            return curl

        Curl = pycurl.Curl
        try:
            pycurl.Curl = pycurl_Curl
            result = fetch("http://example.com")
            curl = curls[0]
            self.assertEqual(result, b"result")
            self.assertEqual(
                curl.options, {
                    pycurl.URL: b"http://example.com",
                    pycurl.FOLLOWLOCATION: 1,
                    pycurl.MAXREDIRS: 5,
                    pycurl.CONNECTTIMEOUT: 30,
                    pycurl.LOW_SPEED_LIMIT: 1,
                    pycurl.LOW_SPEED_TIME: 600,
                    pycurl.NOSIGNAL: 1,
                    pycurl.WRITEFUNCTION: Any(),
                    pycurl.DNS_CACHE_TIMEOUT: 0,
                    pycurl.ENCODING: b"gzip,deflate"
                })
        finally:
            pycurl.Curl = Curl
Beispiel #4
0
 def test_pycurl_proxy(self):
     """If provided, the proxy is set in the request."""
     curl = CurlStub(b"result")
     proxy = "http://my.little.proxy"
     result = fetch("http://example.com", curl=curl, proxy=proxy)
     self.assertEqual(b"result", result)
     self.assertEqual(proxy.encode('ascii'), curl.options[pycurl.PROXY])
 def _curl(self, payload, computer_id, exchange_token, message_api):
     # There are a few "if _PY3" checks below, because for Python 3 we
     # want to convert a number of values from bytes to string, before
     # assigning them to the headers.
     if _PY3 and isinstance(message_api, bytes):
         message_api = message_api.decode("ascii")
     headers = {
         "X-Message-API": message_api,
         "User-Agent": "landscape-client/%s" % VERSION,
         "Content-Type": "application/octet-stream"
     }
     if computer_id:
         if _PY3 and isinstance(computer_id, bytes):
             computer_id = computer_id.decode("ascii")
         headers["X-Computer-ID"] = computer_id
     if exchange_token:
         if _PY3 and isinstance(exchange_token, bytes):
             exchange_token = exchange_token.decode("ascii")
         headers["X-Exchange-Token"] = str(exchange_token)
     curl = pycurl.Curl()
     return (curl,
             fetch(self._url,
                   post=True,
                   data=payload,
                   headers=headers,
                   cainfo=self._pubkey,
                   curl=curl))
Beispiel #6
0
 def test_pycurl_user_agent(self):
     """If provided, the user-agent is set in the request."""
     curl = CurlStub(b"result")
     result = fetch("http://example.com",
                    curl=curl,
                    user_agent="user-agent")
     self.assertEqual(result, b"result")
     self.assertEqual(b"user-agent", curl.options[pycurl.USERAGENT])
Beispiel #7
0
    def fetch_import_url(self, url):
        """Handle fetching of URLs passed to --url."""

        print_text("Fetching configuration from %s..." % url)
        error_message = None
        try:
            content = fetch(url)
        except FetchError, error:
            error_message = str(error)
Beispiel #8
0
 def test_unicode(self):
     """
     The L{fetch} function converts the C{url} parameter to C{bytes} before
     passing it to curl.
     """
     curl = CurlStub(b"result")
     result = fetch(u"http://example.com", curl=curl)
     self.assertEqual(result, b"result")
     self.assertEqual(curl.options[pycurl.URL], b"http://example.com")
     self.assertTrue(isinstance(curl.options[pycurl.URL], bytes))
Beispiel #9
0
 def _curl(self, payload, computer_id, exchange_token, message_api):
     headers = {"X-Message-API": message_api,
                "User-Agent": "landscape-client/%s" % VERSION,
                "Content-Type": "application/octet-stream"}
     if computer_id:
         headers["X-Computer-ID"] = computer_id
     if exchange_token:
         headers["X-Exchange-Token"] = str(exchange_token)
     curl = pycurl.Curl()
     return (curl, fetch(self._url, post=True, data=payload,
                         headers=headers, cainfo=self._pubkey, curl=curl))
Beispiel #10
0
    def fetch_import_url(self, url):
        """Handle fetching of URLs passed to --url."""

        print_text("Fetching configuration from %s..." % url)
        error_message = None
        try:
            content = fetch(url)
        except FetchError as error:
            error_message = str(error)
        if error_message is not None:
            raise ImportOptionError(
                "Couldn't download configuration from %s: %s" %
                (url, error_message))
        return content
Beispiel #11
0
 def test_basic(self):
     curl = CurlStub(b"result")
     result = fetch("http://example.com", curl=curl)
     self.assertEqual(result, b"result")
     self.assertEqual(
         curl.options, {
             pycurl.URL: b"http://example.com",
             pycurl.FOLLOWLOCATION: 1,
             pycurl.MAXREDIRS: 5,
             pycurl.CONNECTTIMEOUT: 30,
             pycurl.LOW_SPEED_LIMIT: 1,
             pycurl.LOW_SPEED_TIME: 600,
             pycurl.NOSIGNAL: 1,
             pycurl.WRITEFUNCTION: Any(),
             pycurl.DNS_CACHE_TIMEOUT: 0,
             pycurl.ENCODING: b"gzip,deflate"
         })
Beispiel #12
0
 def test_pycurl_insecure(self):
     curl = CurlStub(b"result")
     result = fetch("http://example.com/get-ca-cert",
                    curl=curl,
                    insecure=True)
     self.assertEqual(result, b"result")
     self.assertEqual(
         curl.options, {
             pycurl.URL: b"http://example.com/get-ca-cert",
             pycurl.FOLLOWLOCATION: 1,
             pycurl.MAXREDIRS: 5,
             pycurl.CONNECTTIMEOUT: 30,
             pycurl.LOW_SPEED_LIMIT: 1,
             pycurl.LOW_SPEED_TIME: 600,
             pycurl.NOSIGNAL: 1,
             pycurl.WRITEFUNCTION: Any(),
             pycurl.SSL_VERIFYPEER: False,
             pycurl.DNS_CACHE_TIMEOUT: 0,
             pycurl.ENCODING: b"gzip,deflate"
         })
Beispiel #13
0
 def test_post_data(self):
     curl = CurlStub(b"result")
     result = fetch("http://example.com", post=True, data="data", curl=curl)
     self.assertEqual(result, b"result")
     self.assertEqual(curl.options[pycurl.READFUNCTION](), b"data")
     self.assertEqual(
         curl.options, {
             pycurl.URL: b"http://example.com",
             pycurl.FOLLOWLOCATION: 1,
             pycurl.MAXREDIRS: 5,
             pycurl.CONNECTTIMEOUT: 30,
             pycurl.LOW_SPEED_LIMIT: 1,
             pycurl.LOW_SPEED_TIME: 600,
             pycurl.NOSIGNAL: 1,
             pycurl.WRITEFUNCTION: Any(),
             pycurl.POST: True,
             pycurl.POSTFIELDSIZE: 4,
             pycurl.READFUNCTION: Any(),
             pycurl.DNS_CACHE_TIMEOUT: 0,
             pycurl.ENCODING: b"gzip,deflate"
         })
Beispiel #14
0
 def test_pycurl_follow_false(self):
     curl = CurlStub(b"result")
     result = fetch("http://example.com", curl=curl, follow=False)
     self.assertEqual(result, b"result")
     self.assertNotIn(pycurl.FOLLOWLOCATION, curl.options.keys())
Beispiel #15
0
 def test_pycurl_follow_true(self):
     curl = CurlStub(b"result")
     result = fetch("http://example.com", curl=curl, follow=True)
     self.assertEqual(result, b"result")
     self.assertEqual(1, curl.options[pycurl.FOLLOWLOCATION])
Beispiel #16
0
 def test_cainfo_on_http(self):
     curl = CurlStub(b"result")
     result = fetch("http://example.com", cainfo="cainfo", curl=curl)
     self.assertEqual(result, b"result")
     self.assertTrue(pycurl.CAINFO not in curl.options)