549
535
#test('.', 'http://host/', 'http://host')
550
536
test('http://host', 'http://host/', 'http://host')
552
# On Windows file:///C:/path/to and file:///D:/other/path
553
# should not use relative url over the non-existent '/' directory.
554
if sys.platform == 'win32':
556
test('../../other/path',
557
'file:///C:/path/to', 'file:///C:/other/path')
558
#~next two tests is failed, i.e. urlutils.relative_url expects
559
#~to see normalized file URLs?
560
#~test('../../other/path',
561
#~ 'file:///C:/path/to', 'file:///c:/other/path')
562
#~test('../../other/path',
563
#~ 'file:///C:/path/to', 'file:///C|/other/path')
565
# check UNC paths too
566
test('../../other/path',
567
'file://HOST/base/path/to', 'file://HOST/base/other/path')
568
# on different drives
569
test('file:///D:/other/path',
570
'file:///C:/path/to', 'file:///D:/other/path')
571
# TODO: strictly saying in UNC path //HOST/base is full analog
572
# of drive letter for hard disk, and this situation is also
573
# should be exception from rules. [bialix 20071221]
576
539
class TestCwdToURL(TestCaseInTempDir):
577
540
"""Test that local_path_to_url works base on the cwd"""
596
559
os.chdir(u'dod\xe9')
598
# On Mac OSX this directory is actually:
561
# On Mac OSX this directory is actually:
599
562
# u'/dode\u0301' => '/dode\xcc\x81
600
# but we should normalize it back to
563
# but we should normalize it back to
601
564
# u'/dod\xe9' => '/dod\xc3\xa9'
602
565
url = urlutils.local_path_to_url('.')
603
566
self.assertEndsWith(url, '/dod%C3%A9')
606
class TestDeriveToLocation(TestCase):
607
"""Test that the mapping of FROM_LOCATION to TO_LOCATION works."""
609
def test_to_locations_derived_from_paths(self):
610
derive = urlutils.derive_to_location
611
self.assertEqual("bar", derive("bar"))
612
self.assertEqual("bar", derive("../bar"))
613
self.assertEqual("bar", derive("/foo/bar"))
614
self.assertEqual("bar", derive("c:/foo/bar"))
615
self.assertEqual("bar", derive("c:bar"))
617
def test_to_locations_derived_from_urls(self):
618
derive = urlutils.derive_to_location
619
self.assertEqual("bar", derive("http://foo/bar"))
620
self.assertEqual("bar", derive("bzr+ssh://foo/bar"))
621
self.assertEqual("foo-bar", derive("lp:foo-bar"))
624
class TestRebaseURL(TestCase):
625
"""Test the behavior of rebase_url."""
627
def test_non_relative(self):
628
result = urlutils.rebase_url('file://foo', 'file://foo',
630
self.assertEqual('file://foo', result)
631
result = urlutils.rebase_url('/foo', 'file://foo',
633
self.assertEqual('/foo', result)
635
def test_different_ports(self):
636
e = self.assertRaises(InvalidRebaseURLs, urlutils.rebase_url,
637
'foo', 'http://bar:80', 'http://bar:81')
638
self.assertEqual(str(e), "URLs differ by more than path:"
639
" 'http://bar:80' and 'http://bar:81'")
641
def test_different_hosts(self):
642
e = self.assertRaises(InvalidRebaseURLs, urlutils.rebase_url,
643
'foo', 'http://bar', 'http://baz')
644
self.assertEqual(str(e), "URLs differ by more than path: 'http://bar'"
647
def test_different_protocol(self):
648
e = self.assertRaises(InvalidRebaseURLs, urlutils.rebase_url,
649
'foo', 'http://bar', 'ftp://bar')
650
self.assertEqual(str(e), "URLs differ by more than path: 'http://bar'"
653
def test_rebase_success(self):
654
self.assertEqual('../bar', urlutils.rebase_url('bar', 'http://baz/',
656
self.assertEqual('qux/bar', urlutils.rebase_url('bar',
657
'http://baz/qux', 'http://baz/'))
658
self.assertEqual('.', urlutils.rebase_url('foo',
659
'http://bar/', 'http://bar/foo/'))
660
self.assertEqual('qux/bar', urlutils.rebase_url('../bar',
661
'http://baz/qux/foo', 'http://baz/'))
663
def test_determine_relative_path(self):
664
self.assertEqual('../../baz/bar',
665
urlutils.determine_relative_path(
666
'/qux/quxx', '/baz/bar'))
667
self.assertEqual('..',
668
urlutils.determine_relative_path(
670
self.assertEqual('baz',
671
urlutils.determine_relative_path(
673
self.assertEqual('.', urlutils.determine_relative_path(
677
class TestParseURL(TestCase):
679
def test_parse_url(self):
680
self.assertEqual(urlutils.parse_url('http://example.com:80/one'),
681
('http', None, None, 'example.com', 80, '/one'))
682
self.assertEqual(urlutils.parse_url('http://[1:2:3::40]/one'),
683
('http', None, None, '1:2:3::40', None, '/one'))
684
self.assertEqual(urlutils.parse_url('http://[1:2:3::40]:80/one'),
685
('http', None, None, '1:2:3::40', 80, '/one'))