267
292
# Invalid joinings
268
293
# Cannot go above root
269
self.assertRaises(InvalidURLJoin, urlutils.joinpath, '/', '../baz')
270
self.assertRaises(InvalidURLJoin, urlutils.joinpath, '/', '..')
271
self.assertRaises(InvalidURLJoin, urlutils.joinpath, '/', '/..')
294
self.assertRaises(urlutils.InvalidURLJoin, urlutils.joinpath, '/',
296
self.assertRaises(urlutils.InvalidURLJoin, urlutils.joinpath, '/',
298
self.assertRaises(urlutils.InvalidURLJoin, urlutils.joinpath, '/',
301
def test_join_segment_parameters_raw(self):
302
join_segment_parameters_raw = urlutils.join_segment_parameters_raw
303
self.assertEqual("/somedir/path",
304
join_segment_parameters_raw("/somedir/path"))
305
self.assertEqual("/somedir/path,rawdata",
306
join_segment_parameters_raw("/somedir/path", "rawdata"))
307
self.assertRaises(urlutils.InvalidURLJoin,
308
join_segment_parameters_raw, "/somedir/path",
309
"rawdata1,rawdata2,rawdata3")
310
self.assertEqual("/somedir/path,bla,bar",
311
join_segment_parameters_raw("/somedir/path", "bla", "bar"))
312
self.assertEqual("/somedir,exist=some/path,bla,bar",
313
join_segment_parameters_raw("/somedir,exist=some/path",
315
self.assertRaises(TypeError, join_segment_parameters_raw,
318
def test_join_segment_parameters(self):
319
join_segment_parameters = urlutils.join_segment_parameters
320
self.assertEqual("/somedir/path",
321
join_segment_parameters("/somedir/path", {}))
322
self.assertEqual("/somedir/path,key1=val1",
323
join_segment_parameters("/somedir/path", {"key1": "val1"}))
324
self.assertRaises(urlutils.InvalidURLJoin,
325
join_segment_parameters, "/somedir/path",
326
{"branch": "brr,brr,brr"})
327
self.assertRaises(urlutils.InvalidURLJoin,
328
join_segment_parameters, "/somedir/path", {"key1=val1": "val2"})
329
self.assertEqual("/somedir/path,key1=val1,key2=val2",
330
join_segment_parameters("/somedir/path", {
331
"key1": "val1", "key2": "val2"}))
332
self.assertEqual("/somedir/path,key1=val1,key2=val2",
333
join_segment_parameters("/somedir/path,key1=val1", {
335
self.assertEqual("/somedir/path,key1=val2",
336
join_segment_parameters("/somedir/path,key1=val1", {
338
self.assertEqual("/somedir,exist=some/path,key1=val1",
339
join_segment_parameters("/somedir,exist=some/path",
341
self.assertEqual("/,key1=val1,key2=val2",
342
join_segment_parameters("/,key1=val1", {"key2": "val2"}))
343
self.assertRaises(TypeError,
344
join_segment_parameters, "/,key1=val1", {"foo": 42})
273
346
def test_function_type(self):
274
347
if sys.platform == 'win32':
275
self.assertEqual(urlutils._win32_local_path_to_url, urlutils.local_path_to_url)
276
self.assertEqual(urlutils._win32_local_path_from_url, urlutils.local_path_from_url)
348
self.assertEqual(urlutils._win32_local_path_to_url,
349
urlutils.local_path_to_url)
350
self.assertEqual(urlutils._win32_local_path_from_url,
351
urlutils.local_path_from_url)
278
self.assertEqual(urlutils._posix_local_path_to_url, urlutils.local_path_to_url)
279
self.assertEqual(urlutils._posix_local_path_from_url, urlutils.local_path_from_url)
353
self.assertEqual(urlutils._posix_local_path_to_url,
354
urlutils.local_path_to_url)
355
self.assertEqual(urlutils._posix_local_path_from_url,
356
urlutils.local_path_from_url)
281
358
def test_posix_local_path_to_url(self):
282
359
to_url = urlutils._posix_local_path_to_url
283
360
self.assertEqual('file:///path/to/foo',
284
361
to_url('/path/to/foo'))
363
self.assertEqual('file:///path/to/foo%2Cbar',
364
to_url('/path/to/foo,bar'))
287
367
result = to_url(u'/path/to/r\xe4ksm\xf6rg\xe5s')
288
368
except UnicodeError:
289
369
raise TestSkipped("local encoding cannot handle unicode")
291
371
self.assertEqual('file:///path/to/r%C3%A4ksm%C3%B6rg%C3%A5s', result)
292
self.assertFalse(isinstance(result, unicode))
372
self.assertTrue(isinstance(result, str))
294
374
def test_posix_local_path_from_url(self):
295
375
from_url = urlutils._posix_local_path_from_url
296
376
self.assertEqual('/path/to/foo',
297
377
from_url('file:///path/to/foo'))
378
self.assertEqual('/path/to/foo',
379
from_url('file:///path/to/foo,branch=foo'))
298
380
self.assertEqual(u'/path/to/r\xe4ksm\xf6rg\xe5s',
299
381
from_url('file:///path/to/r%C3%A4ksm%C3%B6rg%C3%A5s'))
300
382
self.assertEqual(u'/path/to/r\xe4ksm\xf6rg\xe5s',
354
439
self.assertEqual(u'D:/path/to/r\xe4ksm\xf6rg\xe5s',
355
440
from_url('file:///d:/path/to/r%c3%a4ksm%c3%b6rg%c3%a5s'))
356
441
self.assertEqual('/', from_url('file:///'))
442
self.assertEqual('C:/path/to/foo',
443
from_url('file:///C|/path/to/foo,branch=foo'))
358
self.assertRaises(InvalidURL, from_url, '/path/to/foo')
445
self.assertRaises(urlutils.InvalidURL, from_url, 'file:///C:')
446
self.assertRaises(urlutils.InvalidURL, from_url, 'file:///c')
447
self.assertRaises(urlutils.InvalidURL, from_url, '/path/to/foo')
359
448
# Not a valid _win32 url, no drive letter
360
self.assertRaises(InvalidURL, from_url, 'file:///path/to/foo')
449
self.assertRaises(urlutils.InvalidURL, from_url, 'file:///path/to/foo')
362
451
def test_win32_unc_path_from_url(self):
363
452
from_url = urlutils._win32_local_path_from_url
364
453
self.assertEqual('//HOST/path', from_url('file://HOST/path'))
454
self.assertEqual('//HOST/path',
455
from_url('file://HOST/path,branch=foo'))
365
456
# despite IE allows 2, 4, 5 and 6 slashes in URL to another machine
366
457
# we want to use only 2 slashes
367
458
# Firefox understand only 5 slashes in URL, but it's ugly
368
self.assertRaises(InvalidURL, from_url, 'file:////HOST/path')
369
self.assertRaises(InvalidURL, from_url, 'file://///HOST/path')
370
self.assertRaises(InvalidURL, from_url, 'file://////HOST/path')
459
self.assertRaises(urlutils.InvalidURL, from_url, 'file:////HOST/path')
460
self.assertRaises(urlutils.InvalidURL, from_url, 'file://///HOST/path')
461
self.assertRaises(urlutils.InvalidURL, from_url, 'file://////HOST/path')
371
462
# check for file://C:/ instead of file:///C:/
372
self.assertRaises(InvalidURL, from_url, 'file://C:/path')
463
self.assertRaises(urlutils.InvalidURL, from_url, 'file://C:/path')
374
465
def test_win32_extract_drive_letter(self):
375
466
extract = urlutils._win32_extract_drive_letter
376
467
self.assertEqual(('file:///C:', '/foo'), extract('file://', '/C:/foo'))
377
468
self.assertEqual(('file:///d|', '/path'), extract('file://', '/d|/path'))
378
self.assertRaises(InvalidURL, extract, 'file://', '/path')
469
self.assertRaises(urlutils.InvalidURL, extract, 'file://', '/path')
470
# Root drives without slash treated as invalid, see bug #841322
471
self.assertEqual(('file:///C:', '/'), extract('file://', '/C:/'))
472
self.assertRaises(urlutils.InvalidURL, extract, 'file://', '/C:')
473
# Invalid without drive separator or following forward slash
474
self.assertRaises(urlutils.InvalidURL, extract, 'file://', '/C')
475
self.assertRaises(urlutils.InvalidURL, extract, 'file://', '/C:ool')
380
477
def test_split(self):
381
# Test bzrlib.urlutils.split()
478
# Test breezy.urlutils.split()
382
479
split = urlutils.split
383
480
if sys.platform == 'win32':
384
self.assertRaises(InvalidURL, split, 'file:///path/to/foo')
481
self.assertRaises(urlutils.InvalidURL, split, 'file:///path/to/foo')
385
482
self.assertEqual(('file:///C|/', 'foo'), split('file:///C|/foo'))
386
483
self.assertEqual(('file:///C:/', ''), split('file:///C:/'))
412
509
self.assertEqual(('path/..', 'foo'), split('path/../foo'))
413
510
self.assertEqual(('../path', 'foo'), split('../path/foo'))
512
def test_split_segment_parameters_raw(self):
513
split_segment_parameters_raw = urlutils.split_segment_parameters_raw
514
# Check relative references with absolute paths
515
self.assertEqual(("/some/path", []),
516
split_segment_parameters_raw("/some/path"))
517
self.assertEqual(("/some/path", ["tip"]),
518
split_segment_parameters_raw("/some/path,tip"))
519
self.assertEqual(("/some,dir/path", ["tip"]),
520
split_segment_parameters_raw("/some,dir/path,tip"))
521
self.assertEqual(("/somedir/path", ["heads%2Ftip"]),
522
split_segment_parameters_raw("/somedir/path,heads%2Ftip"))
523
self.assertEqual(("/somedir/path", ["heads%2Ftip", "bar"]),
524
split_segment_parameters_raw("/somedir/path,heads%2Ftip,bar"))
525
# Check relative references with relative paths
526
self.assertEqual(("", ["key1=val1"]),
527
split_segment_parameters_raw(",key1=val1"))
528
self.assertEqual(("foo/", ["key1=val1"]),
529
split_segment_parameters_raw("foo/,key1=val1"))
530
self.assertEqual(("foo", ["key1=val1"]),
531
split_segment_parameters_raw("foo,key1=val1"))
532
self.assertEqual(("foo/base,la=bla/other/elements", []),
533
split_segment_parameters_raw("foo/base,la=bla/other/elements"))
534
self.assertEqual(("foo/base,la=bla/other/elements", ["a=b"]),
535
split_segment_parameters_raw("foo/base,la=bla/other/elements,a=b"))
536
# TODO: Check full URLs as well as relative references
538
def test_split_segment_parameters(self):
539
split_segment_parameters = urlutils.split_segment_parameters
540
# Check relative references with absolute paths
541
self.assertEqual(("/some/path", {}),
542
split_segment_parameters("/some/path"))
543
self.assertEqual(("/some/path", {"branch": "tip"}),
544
split_segment_parameters("/some/path,branch=tip"))
545
self.assertEqual(("/some,dir/path", {"branch": "tip"}),
546
split_segment_parameters("/some,dir/path,branch=tip"))
547
self.assertEqual(("/somedir/path", {"ref": "heads%2Ftip"}),
548
split_segment_parameters("/somedir/path,ref=heads%2Ftip"))
549
self.assertEqual(("/somedir/path",
550
{"ref": "heads%2Ftip", "key1": "val1"}),
551
split_segment_parameters(
552
"/somedir/path,ref=heads%2Ftip,key1=val1"))
553
self.assertEqual(("/somedir/path", {"ref": "heads%2F=tip"}),
554
split_segment_parameters("/somedir/path,ref=heads%2F=tip"))
555
# Check relative references with relative paths
556
self.assertEqual(("", {"key1": "val1"}),
557
split_segment_parameters(",key1=val1"))
558
self.assertEqual(("foo/", {"key1": "val1"}),
559
split_segment_parameters("foo/,key1=val1"))
560
self.assertEqual(("foo/base,key1=val1/other/elements", {}),
561
split_segment_parameters("foo/base,key1=val1/other/elements"))
562
self.assertEqual(("foo/base,key1=val1/other/elements",
563
{"key2": "val2"}), split_segment_parameters(
564
"foo/base,key1=val1/other/elements,key2=val2"))
565
# TODO: Check full URLs as well as relative references
415
567
def test_win32_strip_local_trailing_slash(self):
416
568
strip = urlutils._win32_strip_local_trailing_slash
417
569
self.assertEqual('file://', strip('file://'))
681
830
class TestParseURL(TestCase):
683
def test_parse_url(self):
684
self.assertEqual(urlutils.parse_url('http://example.com:80/one'),
685
('http', None, None, 'example.com', 80, '/one'))
686
self.assertEqual(urlutils.parse_url('http://[1:2:3::40]/one'),
687
('http', None, None, '1:2:3::40', None, '/one'))
688
self.assertEqual(urlutils.parse_url('http://[1:2:3::40]:80/one'),
689
('http', None, None, '1:2:3::40', 80, '/one'))
832
def test_parse_simple(self):
833
parsed = urlutils.parse_url('http://example.com:80/one')
834
self.assertEqual(('http', None, None, 'example.com', 80, '/one'),
838
parsed = urlutils.parse_url('http://[1:2:3::40]/one')
839
self.assertEqual(('http', None, None, '1:2:3::40', None, '/one'),
842
def test_ipv6_port(self):
843
parsed = urlutils.parse_url('http://[1:2:3::40]:80/one')
844
self.assertEqual(('http', None, None, '1:2:3::40', 80, '/one'),
848
class TestURL(TestCase):
850
def test_parse_simple(self):
851
parsed = urlutils.URL.from_string('http://example.com:80/one')
852
self.assertEqual('http', parsed.scheme)
853
self.assertIs(None, parsed.user)
854
self.assertIs(None, parsed.password)
855
self.assertEqual('example.com', parsed.host)
856
self.assertEqual(80, parsed.port)
857
self.assertEqual('/one', parsed.path)
860
parsed = urlutils.URL.from_string('http://[1:2:3::40]/one')
861
self.assertEqual('http', parsed.scheme)
862
self.assertIs(None, parsed.port)
863
self.assertIs(None, parsed.user)
864
self.assertIs(None, parsed.password)
865
self.assertEqual('1:2:3::40', parsed.host)
866
self.assertEqual('/one', parsed.path)
868
def test_ipv6_port(self):
869
parsed = urlutils.URL.from_string('http://[1:2:3::40]:80/one')
870
self.assertEqual('http', parsed.scheme)
871
self.assertEqual('1:2:3::40', parsed.host)
872
self.assertIs(None, parsed.user)
873
self.assertIs(None, parsed.password)
874
self.assertEqual(80, parsed.port)
875
self.assertEqual('/one', parsed.path)
877
def test_quoted(self):
878
parsed = urlutils.URL.from_string(
879
'http://ro%62ey:h%40t@ex%41mple.com:2222/path')
880
self.assertEqual(parsed.quoted_host, 'ex%41mple.com')
881
self.assertEqual(parsed.host, 'exAmple.com')
882
self.assertEqual(parsed.port, 2222)
883
self.assertEqual(parsed.quoted_user, 'ro%62ey')
884
self.assertEqual(parsed.user, 'robey')
885
self.assertEqual(parsed.quoted_password, 'h%40t')
886
self.assertEqual(parsed.password, 'h@t')
887
self.assertEqual(parsed.path, '/path')
890
parsed1 = urlutils.URL.from_string('http://[1:2:3::40]:80/one')
891
parsed2 = urlutils.URL.from_string('http://[1:2:3::40]:80/one')
892
self.assertEqual(parsed1, parsed2)
893
self.assertEqual(parsed1, parsed1)
894
parsed2.path = '/two'
895
self.assertNotEqual(parsed1, parsed2)
898
parsed = urlutils.URL.from_string('http://[1:2:3::40]:80/one')
900
"<URL('http', None, None, '1:2:3::40', 80, '/one')>",
904
parsed = urlutils.URL.from_string('http://[1:2:3::40]:80/one')
905
self.assertEqual('http://[1:2:3::40]:80/one', str(parsed))
907
def test__combine_paths(self):
908
combine = urlutils.URL._combine_paths
909
self.assertEqual('/home/sarah/project/foo',
910
combine('/home/sarah', 'project/foo'))
911
self.assertEqual('/etc',
912
combine('/home/sarah', '../../etc'))
913
self.assertEqual('/etc',
914
combine('/home/sarah', '../../../etc'))
915
self.assertEqual('/etc',
916
combine('/home/sarah', '/etc'))
918
def test_clone(self):
919
url = urlutils.URL.from_string('http://[1:2:3::40]:80/one')
920
url1 = url.clone("two")
921
self.assertEqual("/one/two", url1.path)
922
url2 = url.clone("/two")
923
self.assertEqual("/two", url2.path)
925
self.assertIsNot(url, url3)
926
self.assertEqual(url, url3)
929
class TestFileRelpath(TestCase):
931
# GZ 2011-11-18: A way to override all path handling functions to one
932
# platform or another for testing would be nice.
934
def _with_posix_paths(self):
935
self.overrideAttr(urlutils, "local_path_from_url",
936
urlutils._posix_local_path_from_url)
937
self.overrideAttr(urlutils, "MIN_ABS_FILEURL_LENGTH", len("file:///"))
938
self.overrideAttr(osutils, "normpath", osutils._posix_normpath)
939
self.overrideAttr(osutils, "abspath", osutils._posix_abspath)
940
self.overrideAttr(osutils, "normpath", osutils._posix_normpath)
941
self.overrideAttr(osutils, "pathjoin", osutils.posixpath.join)
942
self.overrideAttr(osutils, "split", osutils.posixpath.split)
943
self.overrideAttr(osutils, "MIN_ABS_PATHLENGTH", 1)
945
def _with_win32_paths(self):
946
self.overrideAttr(urlutils, "local_path_from_url",
947
urlutils._win32_local_path_from_url)
948
self.overrideAttr(urlutils, "MIN_ABS_FILEURL_LENGTH",
949
urlutils.WIN32_MIN_ABS_FILEURL_LENGTH)
950
self.overrideAttr(osutils, "abspath", osutils._win32_abspath)
951
self.overrideAttr(osutils, "normpath", osutils._win32_normpath)
952
self.overrideAttr(osutils, "pathjoin", osutils._win32_pathjoin)
953
self.overrideAttr(osutils, "split", osutils.ntpath.split)
954
self.overrideAttr(osutils, "MIN_ABS_PATHLENGTH", 3)
956
def test_same_url_posix(self):
957
self._with_posix_paths()
959
urlutils.file_relpath("file:///a", "file:///a"))
961
urlutils.file_relpath("file:///a", "file:///a/"))
963
urlutils.file_relpath("file:///a/", "file:///a"))
965
def test_same_url_win32(self):
966
self._with_win32_paths()
968
urlutils.file_relpath("file:///A:/", "file:///A:/"))
970
urlutils.file_relpath("file:///A|/", "file:///A:/"))
972
urlutils.file_relpath("file:///A:/b/", "file:///A:/b/"))
974
urlutils.file_relpath("file:///A:/b", "file:///A:/b/"))
976
urlutils.file_relpath("file:///A:/b/", "file:///A:/b"))
978
def test_child_posix(self):
979
self._with_posix_paths()
980
self.assertEqual("b",
981
urlutils.file_relpath("file:///a", "file:///a/b"))
982
self.assertEqual("b",
983
urlutils.file_relpath("file:///a/", "file:///a/b"))
984
self.assertEqual("b/c",
985
urlutils.file_relpath("file:///a", "file:///a/b/c"))
987
def test_child_win32(self):
988
self._with_win32_paths()
989
self.assertEqual("b",
990
urlutils.file_relpath("file:///A:/", "file:///A:/b"))
991
self.assertEqual("b",
992
urlutils.file_relpath("file:///A|/", "file:///A:/b"))
993
self.assertEqual("c",
994
urlutils.file_relpath("file:///A:/b", "file:///A:/b/c"))
995
self.assertEqual("c",
996
urlutils.file_relpath("file:///A:/b/", "file:///A:/b/c"))
997
self.assertEqual("c/d",
998
urlutils.file_relpath("file:///A:/b", "file:///A:/b/c/d"))
1000
def test_sibling_posix(self):
1001
self._with_posix_paths()
1002
self.assertRaises(PathNotChild,
1003
urlutils.file_relpath, "file:///a/b", "file:///a/c")
1004
self.assertRaises(PathNotChild,
1005
urlutils.file_relpath, "file:///a/b/", "file:///a/c")
1006
self.assertRaises(PathNotChild,
1007
urlutils.file_relpath, "file:///a/b/", "file:///a/c/")
1009
def test_sibling_win32(self):
1010
self._with_win32_paths()
1011
self.assertRaises(PathNotChild,
1012
urlutils.file_relpath, "file:///A:/b", "file:///A:/c")
1013
self.assertRaises(PathNotChild,
1014
urlutils.file_relpath, "file:///A:/b/", "file:///A:/c")
1015
self.assertRaises(PathNotChild,
1016
urlutils.file_relpath, "file:///A:/b/", "file:///A:/c/")
1018
def test_parent_posix(self):
1019
self._with_posix_paths()
1020
self.assertRaises(PathNotChild,
1021
urlutils.file_relpath, "file:///a/b", "file:///a")
1022
self.assertRaises(PathNotChild,
1023
urlutils.file_relpath, "file:///a/b", "file:///a/")
1025
def test_parent_win32(self):
1026
self._with_win32_paths()
1027
self.assertRaises(PathNotChild,
1028
urlutils.file_relpath, "file:///A:/b", "file:///A:/")
1029
self.assertRaises(PathNotChild,
1030
urlutils.file_relpath, "file:///A:/b/c", "file:///A:/b")
1033
class QuoteTests(TestCase):
1035
def test_quote(self):
1036
self.assertEqual('abc%20def', urlutils.quote('abc def'))
1037
self.assertEqual('abc%2Fdef', urlutils.quote('abc/def', safe=''))
1038
self.assertEqual('abc/def', urlutils.quote('abc/def', safe='/'))
1040
def test_quote_tildes(self):
1041
self.assertEqual('%7Efoo', urlutils.quote('~foo'))
1042
self.assertEqual('~foo', urlutils.quote('~foo', safe='/~'))
1044
def test_unquote(self):
1045
self.assertEqual('%', urlutils.unquote('%25'))
1046
self.assertEqual('\xc3\xa5', urlutils.unquote('%C3%A5'))
1047
self.assertEqual(u"\xe5", urlutils.unquote(u'\xe5'))