Ticket #2093: test_paths.4.diff

File test_paths.4.diff, 5.3 KB (added by oubiwann, 10 years ago)

completely redone tests based on suggestions/comments

  • twisted/test/test_paths.py

    old new  
    425425        self.assertEquals(str(self.path.here()), 'http://example.com/foo/')
    426426        self.assertEquals(str(self.path.child('').here()), 'http://example.com/foo/bar/')
    427427
     428class URLPathAuthoritySectionTestCase(unittest.TestCase):
     429
     430    def testWithAllAuthorityValues(self):
     431        """
     432        Check that URLPath is properly using URIAuthority.
     433
     434        Exhaustive tests against URIAuthority are in their own test cases
     435        below.
     436        """
     437        url = "http://alice:asecret@example.com:8080/foo/bar?yes=no&no=yes#footer"
     438        path = urlpath.URLPath.fromString(url)
     439        self.assertEquals(path.user, 'alice')
     440        self.assertEquals(path.password, 'asecret')
     441        self.assertEquals(path.host, 'example.com')
     442        self.assertEquals(int(path.port), 8080)
     443
     444class URIAuthorityClassTestCase(unittest.TestCase):
     445
     446    def testInstantiatedAuthority(self):
     447        """
     448        Check that the URIAuthority class instantiates properly and parses
     449        values as expected.
     450        """
     451        auth = urlpath.URIAuthority(user='bob', password='asecret',
     452            host='example.com', port=80)
     453        self.assertEquals(auth.user, 'bob')
     454        self.assertEquals(auth.password, 'asecret')
     455        self.assertEquals(auth.host, 'example.com')
     456        self.assertEquals(int(auth.port), 80)
     457        self.assertEquals(str(auth), 'bob:asecret@example.com:80')
     458        self.assertEquals(repr(auth),
     459            "URIAuthority(user='bob', password='asecret', host='example.com', port=80)")
     460
     461    def testNoPassword(self):
     462        """
     463        Make sure that without a password supplied, the authority section gets
     464        parsed properly.
     465        """
     466        auth = urlpath.URIAuthority(user='bob', password='',
     467            host='example.com', port=80)
     468        self.assertEquals(auth.user, 'bob')
     469        self.assertEquals(auth.password, '')
     470        self.assertEquals(auth.host, 'example.com')
     471        self.assertEquals(int(auth.port), 80)
     472
     473    def testNoUsername(self):
     474        """
     475        Make sure that without a user name supplied, the authority section gets
     476        parsed properly.
     477        """
     478        # not really sure of a use case for this one, but oh well...
     479        auth = urlpath.URIAuthority(user='', password='asecret',
     480            host='example.com', port=80)
     481        self.assertEquals(auth.user, '')
     482        self.assertEquals(auth.password, 'asecret')
     483        self.assertEquals(auth.host, 'example.com')
     484        self.assertEquals(int(auth.port), 80)
     485
     486    def testUnparse(self):
     487        auth = urlpath.URIAuthority(host='example.com')
     488        self.assertEquals(auth.unparse(), 'example.com')
     489        auth = urlpath.URIAuthority(user='alice', host='example.com')
     490        self.assertEquals(auth.unparse(), 'alice@example.com')
     491        auth = urlpath.URIAuthority(password='asecret', host='example.com')
     492        self.assertEquals(auth.unparse(), ':asecret@example.com')
     493        auth = urlpath.URIAuthority(host='example.com', port=8080)
     494        self.assertEquals(auth.unparse(), 'example.com:8080')
     495        auth = urlpath.URIAuthority(user='bob', host='example.com', port=8080)
     496        self.assertEquals(auth.unparse(), 'bob@example.com:8080')
     497        auth = urlpath.URIAuthority(password='asecret', host='example.com', port=8080)
     498        self.assertEquals(auth.unparse(), ':asecret@example.com:8080')
     499        auth = urlpath.URIAuthority(user='alice', password='asecret', host='example.com', port=8080)
     500        self.assertEquals(auth.unparse(), 'alice:asecret@example.com:8080')
     501
     502    def testBadPortValue(self):
     503        """
     504        Confirm that a port value with a string in it will not be handled.
     505        """
     506        host = 'example.com'
     507        port = 'z80'
     508        self.assertRaises(ValueError, urlpath.URIAuthority,
     509            None, None, host, port)
     510
     511
     512class URIAuthorityClassMethodTestCase(unittest.TestCase):
     513    """
     514    Test URIAuthority using the fromString() class method.
     515    """
     516       
     517    def testFullAuthority(self):
     518        """
     519        Check that the full authority is properly parsed.
     520        """
     521        url = 'bob:asecret@example.com:80'
     522        auth = urlpath.URIAuthority.fromString(url)
     523        user, password, host, port = auth
     524        self.assertEquals(user, 'bob')
     525        self.assertEquals(password, 'asecret')
     526        self.assertEquals(host, 'example.com')
     527        self.assertEquals(int(port), 80)
     528
     529    def testBadPortValue(self):
     530        """
     531        Confirm that a port value with a string in it will not be handled.
     532        """
     533        self.assertRaises(ValueError, urlpath.URIAuthority.fromString,
     534            "example.com:z80")
     535
     536    def testURLPassedAsAuthority(self):
     537        """
     538        Attempt to pass a URL to fromString().
     539        """
     540        self.assertRaises(ValueError, urlpath.URIAuthority.fromString,
     541            "http://example.com:8080")
     542       
     543    def testSwappedSeparators(self):
     544        """
     545        Test for swapping the user/password separator and the user-pass/host
     546        separator.
     547        """
     548        self.assertRaises(ValueError, urlpath.URIAuthority.fromString,
     549            "alice@asecret:example.com:8080")
     550