115
115
eq('http://host/ab/%C2%B5/%C2%B5',
116
116
normalize_url(u'http://host/ab/%C2%B5/\xb5'))
118
# Unescape characters that don't need to be escaped
119
eq('http://host/~bob%2525-._',
120
normalize_url('http://host/%7Ebob%2525%2D%2E%5F'))
121
eq('http://host/~bob%2525-._',
122
normalize_url(u'http://host/%7Ebob%2525%2D%2E%5F'))
124
118
# Normalize verifies URLs when they are not unicode
125
119
# (indicating they did not come from the user)
126
120
self.assertRaises(InvalidURL, normalize_url, 'http://host/\xb5')
203
197
joined = urlutils.join(*args)
204
198
self.assertEqual(expected, joined)
200
# Test a single element
206
203
# Test relative path joining
207
test('foo', 'foo') # relative fragment with nothing is preserved.
208
204
test('foo/bar', 'foo', 'bar')
209
205
test('http://foo/bar', 'http://foo', 'bar')
210
206
test('http://foo/bar', 'http://foo', '.', 'bar')
211
207
test('http://foo/baz', 'http://foo', 'bar', '../baz')
212
208
test('http://foo/bar/baz', 'http://foo', 'bar/baz')
213
209
test('http://foo/baz', 'http://foo', 'bar/../baz')
214
test('http://foo/baz', 'http://foo/bar/', '../baz')
217
test('http://foo', 'http://foo') # abs url with nothing is preserved.
218
212
test('http://bar', 'http://foo', 'http://bar')
219
213
test('sftp://bzr/foo', 'http://foo', 'bar', 'sftp://bzr/foo')
220
214
test('file:///bar', 'foo', 'file:///bar')
221
test('http://bar/', 'http://foo', 'http://bar/')
222
test('http://bar/a', 'http://foo', 'http://bar/a')
223
test('http://bar/a/', 'http://foo', 'http://bar/a/')
225
216
# From a base path
226
217
test('file:///foo', 'file:///', 'foo')
231
222
# Invalid joinings
232
223
# Cannot go above root
233
# Implicitly at root:
234
224
self.assertRaises(InvalidURLJoin, urlutils.join,
235
225
'http://foo', '../baz')
236
self.assertRaises(InvalidURLJoin, urlutils.join,
238
# Joining from a path explicitly under the root.
239
self.assertRaises(InvalidURLJoin, urlutils.join,
240
'http://foo/a', '../../b')
242
def test_joinpath(self):
243
def test(expected, *args):
244
joined = urlutils.joinpath(*args)
245
self.assertEqual(expected, joined)
247
# Test a single element
250
# Test relative path joining
251
test('foo/bar', 'foo', 'bar')
252
test('foo/bar', 'foo', '.', 'bar')
253
test('foo/baz', 'foo', 'bar', '../baz')
254
test('foo/bar/baz', 'foo', 'bar/baz')
255
test('foo/baz', 'foo', 'bar/../baz')
257
# Test joining to an absolute path
259
test('/foo', '/foo', '.')
260
test('/foo/bar', '/foo', 'bar')
261
test('/', '/foo', '..')
263
# Test joining with an absolute path
264
test('/bar', 'foo', '/bar')
266
# Test joining to a path with a trailing slash
267
test('foo/bar', 'foo/', 'bar')
270
# Cannot go above root
271
self.assertRaises(InvalidURLJoin, urlutils.joinpath, '/', '../baz')
272
self.assertRaises(InvalidURLJoin, urlutils.joinpath, '/', '..')
273
self.assertRaises(InvalidURLJoin, urlutils.joinpath, '/', '/..')
275
227
def test_function_type(self):
276
228
if sys.platform == 'win32':
547
493
#test('.', 'http://host/', 'http://host')
548
494
test('http://host', 'http://host/', 'http://host')
550
# On Windows file:///C:/path/to and file:///D:/other/path
551
# should not use relative url over the non-existent '/' directory.
552
if sys.platform == 'win32':
554
test('../../other/path',
555
'file:///C:/path/to', 'file:///C:/other/path')
556
#~next two tests is failed, i.e. urlutils.relative_url expects
557
#~to see normalized file URLs?
558
#~test('../../other/path',
559
#~ 'file:///C:/path/to', 'file:///c:/other/path')
560
#~test('../../other/path',
561
#~ 'file:///C:/path/to', 'file:///C|/other/path')
563
# check UNC paths too
564
test('../../other/path',
565
'file://HOST/base/path/to', 'file://HOST/base/other/path')
566
# on different drives
567
test('file:///D:/other/path',
568
'file:///C:/path/to', 'file:///D:/other/path')
569
# TODO: strictly saying in UNC path //HOST/base is full analog
570
# of drive letter for hard disk, and this situation is also
571
# should be exception from rules. [bialix 20071221]
574
497
class TestCwdToURL(TestCaseInTempDir):
575
498
"""Test that local_path_to_url works base on the cwd"""
599
522
# u'/dod\xe9' => '/dod\xc3\xa9'
600
523
url = urlutils.local_path_to_url('.')
601
524
self.assertEndsWith(url, '/dod%C3%A9')
604
class TestDeriveToLocation(TestCase):
605
"""Test that the mapping of FROM_LOCATION to TO_LOCATION works."""
607
def test_to_locations_derived_from_paths(self):
608
derive = urlutils.derive_to_location
609
self.assertEqual("bar", derive("bar"))
610
self.assertEqual("bar", derive("../bar"))
611
self.assertEqual("bar", derive("/foo/bar"))
612
self.assertEqual("bar", derive("c:/foo/bar"))
613
self.assertEqual("bar", derive("c:bar"))
615
def test_to_locations_derived_from_urls(self):
616
derive = urlutils.derive_to_location
617
self.assertEqual("bar", derive("http://foo/bar"))
618
self.assertEqual("bar", derive("bzr+ssh://foo/bar"))
619
self.assertEqual("foo-bar", derive("lp:foo-bar"))
622
class TestRebaseURL(TestCase):
623
"""Test the behavior of rebase_url."""
625
def test_non_relative(self):
626
result = urlutils.rebase_url('file://foo', 'file://foo',
628
self.assertEqual('file://foo', result)
629
result = urlutils.rebase_url('/foo', 'file://foo',
631
self.assertEqual('/foo', result)
633
def test_different_ports(self):
634
e = self.assertRaises(InvalidRebaseURLs, urlutils.rebase_url,
635
'foo', 'http://bar:80', 'http://bar:81')
636
self.assertEqual(str(e), "URLs differ by more than path:"
637
" 'http://bar:80' and 'http://bar:81'")
639
def test_different_hosts(self):
640
e = self.assertRaises(InvalidRebaseURLs, urlutils.rebase_url,
641
'foo', 'http://bar', 'http://baz')
642
self.assertEqual(str(e), "URLs differ by more than path: 'http://bar'"
645
def test_different_protocol(self):
646
e = self.assertRaises(InvalidRebaseURLs, urlutils.rebase_url,
647
'foo', 'http://bar', 'ftp://bar')
648
self.assertEqual(str(e), "URLs differ by more than path: 'http://bar'"
651
def test_rebase_success(self):
652
self.assertEqual('../bar', urlutils.rebase_url('bar', 'http://baz/',
654
self.assertEqual('qux/bar', urlutils.rebase_url('bar',
655
'http://baz/qux', 'http://baz/'))
656
self.assertEqual('.', urlutils.rebase_url('foo',
657
'http://bar/', 'http://bar/foo/'))
658
self.assertEqual('qux/bar', urlutils.rebase_url('../bar',
659
'http://baz/qux/foo', 'http://baz/'))
661
def test_determine_relative_path(self):
662
self.assertEqual('../../baz/bar',
663
urlutils.determine_relative_path(
664
'/qux/quxx', '/baz/bar'))
665
self.assertEqual('..',
666
urlutils.determine_relative_path(
668
self.assertEqual('baz',
669
urlutils.determine_relative_path(
671
self.assertEqual('.', urlutils.determine_relative_path(