def test_generic_urlparse():
    url = 'https://ansible.com/blog'
    parts = urlparse(url)
    generic_parts = generic_urlparse(parts)
    assert generic_parts.as_list() == list(parts)

    assert urlunparse(generic_parts.as_list()) == url
Example #2
0
    def __init__(self, url, key_file=None, cert_file=None, debug=False):
        """LXD Client.

        :param url: The URL of the LXD server. (e.g. unix:/var/lib/lxd/unix.socket or https://127.0.0.1)
        :type url: ``str``
        :param key_file: The path of the client certificate key file.
        :type key_file: ``str``
        :param cert_file: The path of the client certificate file.
        :type cert_file: ``str``
        :param debug: The debug flag. The request and response are stored in logs when debug is true.
        :type debug: ``bool``
        """
        self.url = url
        self.debug = debug
        self.logs = []
        if url.startswith('https:'):
            self.cert_file = cert_file
            self.key_file = key_file
            parts = generic_urlparse(urlparse(self.url))
            ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
            ctx.load_cert_chain(cert_file, keyfile=key_file)
            self.connection = HTTPSConnection(parts.get('netloc'), context=ctx)
        elif url.startswith('unix:'):
            unix_socket_path = url[len('unix:'):]
            self.connection = UnixHTTPConnection(unix_socket_path)
        else:
            raise LXDClientException('URL scheme must be unix: or https:')
def test_generic_urlparse_no_netloc_no_host():
    url = '/blog'
    parts = list(urlparse(url))
    generic_parts = generic_urlparse(parts)
    assert generic_parts.username is None
    assert generic_parts.password is None
    assert generic_parts.port is None
    assert generic_parts.hostname == ''
def test_generic_urlparse_netloc():
    url = 'https://ansible.com:443/blog'
    parts = urlparse(url)
    generic_parts = generic_urlparse(parts)
    assert generic_parts.hostname == parts.hostname
    assert generic_parts.hostname == 'ansible.com'
    assert generic_parts.port == 443
    assert urlunparse(generic_parts.as_list()) == url
def test_generic_urlparse_no_netloc():
    url = 'https://*****:*****@ansible.com:443/blog'
    parts = list(urlparse(url))
    generic_parts = generic_urlparse(parts)
    assert generic_parts.hostname == 'ansible.com'
    assert generic_parts.port == 443
    assert generic_parts.username == 'user'
    assert generic_parts.password == 'passwd'
    assert urlunparse(generic_parts.as_list()) == url
Example #6
0
    def _normalize_url(self, url):
        '''
        The hostname in URLs from vmware may be ``*`` update it accordingly
        '''
        url_parts = generic_urlparse(urlparse(url))
        if url_parts.hostname == '*':
            if url_parts.port:
                url_parts.netloc = '%s:%d' % (self.params['hostname'],
                                              url_parts.port)
            else:
                url_parts.netloc = self.params['hostname']

        return urlunparse(url_parts.as_list())
 def __del__(self):
     if self.last_url is None:
         return
     token = self.request.headers.get('X-F5-Auth-Token', None)
     if not token:
         return
     try:
         p = generic_urlparse(urlparse(self.last_url))
         uri = "https://{0}:{1}/mgmt/shared/authz/tokens/{2}".format(
             p['hostname'], p['port'], token
         )
         self.delete(uri)
     except ValueError:
         pass
def test_generic_urlparse_no_netloc_no_auth():
    url = 'https://ansible.com:443/blog'
    parts = list(urlparse(url))
    generic_parts = generic_urlparse(parts)
    assert generic_parts.username is None
    assert generic_parts.password is None