Beispiel #1
0
 def testToString(self):
     for url, expected in urls:
         result = Url.from_string(url)
         error = 'URL:      ' + url + '\n'
         error += 'Result:   ' + str(result) + '\n'
         error += 'Expected: ' + expected
         self.assertIsInstance(result, Url, error)
         self.assertEqual(result.to_string(), expected, error)
Beispiel #2
0
 def testFromString(self):
     for url, expected in urls:
         result = Url.from_string(url)
         error = 'URL:      ' + url + '\n'
         error += 'Result:   ' + str(result) + '\n'
         error += 'Expected: ' + expected
         self.assert_(isinstance(result, Url))
         self.assert_(str(result) == expected, error)
Beispiel #3
0
 def testFromString(self):
     for url, expected in urls:
         result = Url.from_string(url)
         error = 'URL:      ' + url + '\n'
         error += 'Result:   ' + str(result) + '\n'
         error += 'Expected: ' + expected
         self.assertIsInstance(result, Url)
         self.assertTrue(str(result) == expected, error)
Beispiel #4
0
    def testConstructor(self):
        host = Host('localhost')
        self.assertEqual(host.get_protocol(), 'telnet')
        host = Host('localhost', default_protocol = 'foo')
        self.assertEqual(host.get_protocol(), 'foo')

        for url, result in urls:
            host = Host(url)
            uri  = Url.from_string(url)
            self.assertEqual(host.get_name(),    uri.hostname)
            self.assertEqual(host.get_address(), uri.hostname)
            self.assertEqual(host.get_uri(), str(uri))
Beispiel #5
0
    def testConstructor(self):
        host = Host('localhost')
        self.assertEqual(host.get_protocol(), 'telnet')
        host = Host('localhost', default_protocol='foo')
        self.assertEqual(host.get_protocol(), 'foo')

        for url, result in urls:
            host = Host(url)
            uri = Url.from_string(url)
            self.assertEqual(host.get_name(), uri.hostname)
            self.assertEqual(host.get_address(), uri.hostname)
            self.assertEqual(host.get_uri(), str(uri))
Beispiel #6
0
    def get_uri(self):
        """
        Returns a URI formatted representation of the host, including all
        of it's attributes except for the name. Uses the
        address, not the name of the host to build the URI.

        @rtype:  str
        @return: A URI.
        """
        url = Url()
        url.protocol = self.get_protocol()
        url.hostname = self.get_address()
        url.port = self.get_tcp_port()
        url.vars = dict((k, to_list(v))
                        for (k, v) in self.get_all().iteritems()
                        if isinstance(v, str) or isinstance(v, list))

        if self.account:
            url.username = self.account.get_name()
            url.password1 = self.account.get_password()
            url.password2 = self.account.authorization_password

        return str(url)
Beispiel #7
0
    def set_uri(self, uri):
        """
        Defines the protocol, hostname/address, TCP port number, username,
        and password from the given URL. The hostname may be URL formatted,
        so the following formats are all valid:

            - myhostname
            - myhostname.domain
            - ssh:hostname
            - ssh:hostname.domain
            - ssh://hostname
            - ssh://user@hostname
            - ssh://user:password@hostname
            - ssh://user:password@hostname:21

        For a list of supported protocols please see set_protocol().

        @type  uri: string
        @param uri: An URL formatted hostname.
        """
        try:
            uri = Url.from_string(uri, self.protocol)
        except ValueError:
            raise ValueError('Hostname parse error: ' + repr(uri))
        hostname = uri.hostname or ''
        name = uri.path and hostname + uri.path or hostname
        self.set_protocol(uri.protocol)
        self.set_tcp_port(uri.port)
        self.set_name(name)
        self.set_address(name)

        if uri.username is not None \
           or uri.password1 is not None \
           or uri.password2:
            account = Account(uri.username, uri.password1, uri.password2)
            self.set_account(account)

        for key, val in uri.vars.iteritems():
            self.set(key, val)
Beispiel #8
0
 def testConstructor(self):
     self.assert_(isinstance(Url(), Url))
Beispiel #9
0
 def testGetUri(self):
     for url, result in urls:
         host = Host(url)
         uri = Url.from_string(url)
         self.assertEqual(host.get_uri().split('&').sort(),
                          str(uri).split('&').sort())
Beispiel #10
0
 def testSetUri(self):
     for url, result in urls:
         self.host.set_uri(url)
         uri = Url.from_string(url)
         self.assertEqual(self.host.get_name(), uri.hostname)
         self.assertEqual(self.host.get_address(), uri.hostname)
Beispiel #11
0
 def testConstructor(self):
     self.assertIsInstance(Url(), Url)
Beispiel #12
0
 def testGetUri(self):
     for url, result in urls:
         host = Host(url)
         uri  = Url.from_string(url)
         self.assertEqual(host.get_uri(), str(uri))
Beispiel #13
0
 def testSetUri(self):
     for url, result in urls:
         self.host.set_uri(url)
         uri = Url.from_string(url)
         self.assertEqual(self.host.get_name(),    uri.hostname)
         self.assertEqual(self.host.get_address(), uri.hostname)