/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_urlutils.py

Merge test-run support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
import os
20
20
import sys
21
21
 
22
 
from .. import osutils, urlutils
 
22
from .. import osutils, urlutils, win32utils
23
23
from ..errors import (
24
24
    PathNotChild,
25
25
    )
26
 
from ..sixish import (
27
 
    text_type,
28
 
    PY3,
29
 
    )
 
26
from ..sixish import text_type
30
27
from . import features, TestCaseInTempDir, TestCase, TestSkipped
31
28
 
32
29
 
38
35
        basename = urlutils.basename
39
36
        if sys.platform == 'win32':
40
37
            self.assertRaises(urlutils.InvalidURL, basename,
41
 
                              'file:///path/to/foo')
 
38
                    'file:///path/to/foo')
42
39
            self.assertEqual('foo', basename('file:///C|/foo'))
43
40
            self.assertEqual('foo', basename('file:///C:/foo'))
44
41
            self.assertEqual('', basename('file:///C:/'))
48
45
 
49
46
        self.assertEqual('foo', basename('http://host/path/to/foo'))
50
47
        self.assertEqual('foo', basename('http://host/path/to/foo/'))
51
 
        self.assertEqual(
52
 
            '', basename('http://host/path/to/foo/',
53
 
                         exclude_trailing_slash=False))
 
48
        self.assertEqual('',
 
49
            basename('http://host/path/to/foo/', exclude_trailing_slash=False))
54
50
        self.assertEqual('path', basename('http://host/path'))
55
51
        self.assertEqual('', basename('http://host/'))
56
52
        self.assertEqual('', basename('http://host'))
57
53
        self.assertEqual('path', basename('http:///nohost/path'))
58
54
 
59
 
        self.assertEqual('path', basename(
60
 
            'random+scheme://user:pass@ahost:port/path'))
61
 
        self.assertEqual('path', basename(
62
 
            'random+scheme://user:pass@ahost:port/path/'))
 
55
        self.assertEqual('path', basename('random+scheme://user:pass@ahost:port/path'))
 
56
        self.assertEqual('path', basename('random+scheme://user:pass@ahost:port/path/'))
63
57
        self.assertEqual('', basename('random+scheme://user:pass@ahost:port/'))
64
58
 
65
59
        # relative paths
66
60
        self.assertEqual('foo', basename('path/to/foo'))
67
61
        self.assertEqual('foo', basename('path/to/foo/'))
68
62
        self.assertEqual('', basename('path/to/foo/',
69
 
                                      exclude_trailing_slash=False))
 
63
            exclude_trailing_slash=False))
70
64
        self.assertEqual('foo', basename('path/../foo'))
71
65
        self.assertEqual('foo', basename('../path/foo'))
72
66
 
125
119
 
126
120
        # Unescape characters that don't need to be escaped
127
121
        eq('http://host/~bob%2525-._',
128
 
           normalize_url('http://host/%7Ebob%2525%2D%2E%5F'))
 
122
                normalize_url('http://host/%7Ebob%2525%2D%2E%5F'))
129
123
        eq('http://host/~bob%2525-._',
130
 
           normalize_url(u'http://host/%7Ebob%2525%2D%2E%5F'))
 
124
                normalize_url(u'http://host/%7Ebob%2525%2D%2E%5F'))
131
125
 
132
 
        if not PY3:
133
 
            # On Python 2, normalize verifies URLs when they are not unicode
134
 
            # (indicating they did not come from the user)
135
 
            self.assertRaises(urlutils.InvalidURL, normalize_url,
136
 
                              b'http://host/\xb5')
137
 
            self.assertRaises(urlutils.InvalidURL,
138
 
                              normalize_url, b'http://host/ ')
 
126
        # Normalize verifies URLs when they are not unicode
 
127
        # (indicating they did not come from the user)
 
128
        self.assertRaises(urlutils.InvalidURL, normalize_url,
 
129
                'http://host/\xb5')
 
130
        self.assertRaises(urlutils.InvalidURL, normalize_url, 'http://host/ ')
139
131
 
140
132
    def test_url_scheme_re(self):
141
133
        # Test paths that may be URLs
164
156
        test_one('file:///usr/bin', ('file', '/usr/bin'))
165
157
        test_one('file:///C:/Windows', ('file', '/C:/Windows'))
166
158
        test_one('file:///C|/Windows', ('file', '/C|/Windows'))
167
 
        test_one(u'readonly+sftp://host/path/\xe5',
168
 
                 ('readonly+sftp', u'host/path/\xe5'))
 
159
        test_one(u'readonly+sftp://host/path/\xe5', ('readonly+sftp', u'host/path/\xe5'))
169
160
 
170
161
        # Weird stuff
171
162
        # Can't have slashes or colons in the scheme
180
171
        dirname = urlutils.dirname
181
172
        if sys.platform == 'win32':
182
173
            self.assertRaises(urlutils.InvalidURL, dirname,
183
 
                              'file:///path/to/foo')
 
174
                'file:///path/to/foo')
184
175
            self.assertEqual('file:///C|/', dirname('file:///C|/foo'))
185
176
            self.assertEqual('file:///C|/', dirname('file:///C|/'))
186
177
        else:
187
178
            self.assertEqual('file:///', dirname('file:///foo'))
188
179
            self.assertEqual('file:///', dirname('file:///'))
189
180
 
190
 
        self.assertEqual('http://host/path/to',
191
 
                         dirname('http://host/path/to/foo'))
192
 
        self.assertEqual('http://host/path/to',
193
 
                         dirname('http://host/path/to/foo/'))
 
181
        self.assertEqual('http://host/path/to', dirname('http://host/path/to/foo'))
 
182
        self.assertEqual('http://host/path/to', dirname('http://host/path/to/foo/'))
194
183
        self.assertEqual('http://host/path/to/foo',
195
 
                         dirname('http://host/path/to/foo/',
196
 
                                 exclude_trailing_slash=False))
 
184
            dirname('http://host/path/to/foo/', exclude_trailing_slash=False))
197
185
        self.assertEqual('http://host/', dirname('http://host/path'))
198
186
        self.assertEqual('http://host/', dirname('http://host/'))
199
187
        self.assertEqual('http://host', dirname('http://host'))
200
188
        self.assertEqual('http:///nohost', dirname('http:///nohost/path'))
201
189
 
202
190
        self.assertEqual('random+scheme://user:pass@ahost:port/',
203
 
                         dirname('random+scheme://user:pass@ahost:port/path'))
204
 
        self.assertEqual('random+scheme://user:pass@ahost:port/',
205
 
                         dirname('random+scheme://user:pass@ahost:port/path/'))
206
 
        self.assertEqual('random+scheme://user:pass@ahost:port/',
207
 
                         dirname('random+scheme://user:pass@ahost:port/'))
 
191
            dirname('random+scheme://user:pass@ahost:port/path'))
 
192
        self.assertEqual('random+scheme://user:pass@ahost:port/',
 
193
            dirname('random+scheme://user:pass@ahost:port/path/'))
 
194
        self.assertEqual('random+scheme://user:pass@ahost:port/',
 
195
            dirname('random+scheme://user:pass@ahost:port/'))
208
196
 
209
197
        # relative paths
210
198
        self.assertEqual('path/to', dirname('path/to/foo'))
211
199
        self.assertEqual('path/to', dirname('path/to/foo/'))
212
200
        self.assertEqual('path/to/foo',
213
 
                         dirname('path/to/foo/', exclude_trailing_slash=False))
 
201
            dirname('path/to/foo/', exclude_trailing_slash=False))
214
202
        self.assertEqual('path/..', dirname('path/../foo'))
215
203
        self.assertEqual('../path', dirname('../path/foo'))
216
 
 
 
204
    
217
205
    def test_is_url(self):
218
206
        self.assertTrue(urlutils.is_url('http://foo/bar'))
219
207
        self.assertTrue(urlutils.is_url('bzr+ssh://foo/bar'))
234
222
            self.assertEqual(expected, joined)
235
223
 
236
224
        # Test relative path joining
237
 
        test('foo', 'foo')  # relative fragment with nothing is preserved.
 
225
        test('foo', 'foo') # relative fragment with nothing is preserved.
238
226
        test('foo/bar', 'foo', 'bar')
239
227
        test('http://foo/bar', 'http://foo', 'bar')
240
228
        test('http://foo/bar', 'http://foo', '.', 'bar')
246
234
        test('lp:foo/bar/baz', 'lp:foo', 'bar/baz')
247
235
 
248
236
        # Absolute paths
249
 
        test('http://foo', 'http://foo')  # abs url with nothing is preserved.
 
237
        test('http://foo', 'http://foo') # abs url with nothing is preserved.
250
238
        test('http://bar', 'http://foo', 'http://bar')
251
239
        test('sftp://bzr/foo', 'http://foo', 'bar', 'sftp://bzr/foo')
252
240
        test('file:///bar', 'foo', 'file:///bar')
267
255
        # Cannot go above root
268
256
        # Implicitly at root:
269
257
        self.assertRaises(urlutils.InvalidURLJoin, urlutils.join,
270
 
                          'http://foo', '../baz')
 
258
                'http://foo', '../baz')
271
259
        self.assertRaises(urlutils.InvalidURLJoin, urlutils.join,
272
 
                          'http://foo', '/..')
 
260
                'http://foo', '/..')
273
261
        # Joining from a path explicitly under the root.
274
262
        self.assertRaises(urlutils.InvalidURLJoin, urlutils.join,
275
 
                          'http://foo/a', '../../b')
 
263
                'http://foo/a', '../../b')
276
264
 
277
265
    def test_joinpath(self):
278
266
        def test(expected, *args):
304
292
        # Invalid joinings
305
293
        # Cannot go above root
306
294
        self.assertRaises(urlutils.InvalidURLJoin, urlutils.joinpath, '/',
307
 
                          '../baz')
308
 
        self.assertRaises(urlutils.InvalidURLJoin, urlutils.joinpath, '/',
309
 
                          '..')
310
 
        self.assertRaises(urlutils.InvalidURLJoin, urlutils.joinpath, '/',
311
 
                          '/..')
 
295
                '../baz')
 
296
        self.assertRaises(urlutils.InvalidURLJoin, urlutils.joinpath, '/',
 
297
                '..')
 
298
        self.assertRaises(urlutils.InvalidURLJoin, urlutils.joinpath, '/',
 
299
                '/..')
312
300
 
313
301
    def test_join_segment_parameters_raw(self):
314
302
        join_segment_parameters_raw = urlutils.join_segment_parameters_raw
315
 
        self.assertEqual("/somedir/path",
316
 
                         join_segment_parameters_raw("/somedir/path"))
317
 
        self.assertEqual("/somedir/path,rawdata",
318
 
                         join_segment_parameters_raw(
319
 
                             "/somedir/path", "rawdata"))
 
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"))
320
307
        self.assertRaises(urlutils.InvalidURLJoin,
321
 
                          join_segment_parameters_raw, "/somedir/path",
322
 
                          "rawdata1,rawdata2,rawdata3")
 
308
            join_segment_parameters_raw, "/somedir/path",
 
309
                "rawdata1,rawdata2,rawdata3")
323
310
        self.assertEqual("/somedir/path,bla,bar",
324
 
                         join_segment_parameters_raw(
325
 
                             "/somedir/path", "bla", "bar"))
326
 
        self.assertEqual(
327
 
            "/somedir,exist=some/path,bla,bar",
 
311
            join_segment_parameters_raw("/somedir/path", "bla", "bar"))
 
312
        self.assertEqual("/somedir,exist=some/path,bla,bar",
328
313
            join_segment_parameters_raw("/somedir,exist=some/path",
329
 
                                        "bla", "bar"))
330
 
        self.assertRaises(TypeError, join_segment_parameters_raw,
331
 
                          "/somepath", 42)
 
314
                "bla", "bar"))
 
315
        self.assertRaises(TypeError, join_segment_parameters_raw, 
 
316
            "/somepath", 42)
332
317
 
333
318
    def test_join_segment_parameters(self):
334
319
        join_segment_parameters = urlutils.join_segment_parameters
335
 
        self.assertEqual("/somedir/path",
336
 
                         join_segment_parameters("/somedir/path", {}))
337
 
        self.assertEqual(
338
 
            "/somedir/path,key1=val1",
 
320
        self.assertEqual("/somedir/path", 
 
321
            join_segment_parameters("/somedir/path", {}))
 
322
        self.assertEqual("/somedir/path,key1=val1", 
339
323
            join_segment_parameters("/somedir/path", {"key1": "val1"}))
340
324
        self.assertRaises(urlutils.InvalidURLJoin,
341
 
                          join_segment_parameters, "/somedir/path",
342
 
                          {"branch": "brr,brr,brr"})
343
 
        self.assertRaises(
344
 
            urlutils.InvalidURLJoin,
 
325
            join_segment_parameters, "/somedir/path",
 
326
            {"branch": "brr,brr,brr"})
 
327
        self.assertRaises(urlutils.InvalidURLJoin,
345
328
            join_segment_parameters, "/somedir/path", {"key1=val1": "val2"})
346
329
        self.assertEqual("/somedir/path,key1=val1,key2=val2",
347
 
                         join_segment_parameters("/somedir/path", {
348
 
                             "key1": "val1", "key2": "val2"}))
 
330
            join_segment_parameters("/somedir/path", {
 
331
                "key1": "val1", "key2": "val2"}))
349
332
        self.assertEqual("/somedir/path,key1=val1,key2=val2",
350
 
                         join_segment_parameters("/somedir/path,key1=val1", {
351
 
                             "key2": "val2"}))
 
333
            join_segment_parameters("/somedir/path,key1=val1", {
 
334
                "key2": "val2"}))
352
335
        self.assertEqual("/somedir/path,key1=val2",
353
 
                         join_segment_parameters("/somedir/path,key1=val1", {
354
 
                             "key1": "val2"}))
 
336
            join_segment_parameters("/somedir/path,key1=val1", {
 
337
                "key1": "val2"}))
355
338
        self.assertEqual("/somedir,exist=some/path,key1=val1",
356
 
                         join_segment_parameters("/somedir,exist=some/path",
357
 
                                                 {"key1": "val1"}))
358
 
        self.assertEqual(
359
 
            "/,key1=val1,key2=val2",
 
339
            join_segment_parameters("/somedir,exist=some/path",
 
340
                {"key1": "val1"}))
 
341
        self.assertEqual("/,key1=val1,key2=val2",
360
342
            join_segment_parameters("/,key1=val1", {"key2": "val2"}))
361
343
        self.assertRaises(TypeError,
362
 
                          join_segment_parameters, "/,key1=val1", {"foo": 42})
 
344
            join_segment_parameters, "/,key1=val1", {"foo": 42})
363
345
 
364
346
    def test_function_type(self):
365
347
        if sys.platform == 'win32':
366
348
            self.assertEqual(urlutils._win32_local_path_to_url,
367
 
                             urlutils.local_path_to_url)
 
349
                urlutils.local_path_to_url)
368
350
            self.assertEqual(urlutils._win32_local_path_from_url,
369
 
                             urlutils.local_path_from_url)
 
351
                urlutils.local_path_from_url)
370
352
        else:
371
353
            self.assertEqual(urlutils._posix_local_path_to_url,
372
 
                             urlutils.local_path_to_url)
 
354
                urlutils.local_path_to_url)
373
355
            self.assertEqual(urlutils._posix_local_path_from_url,
374
 
                             urlutils.local_path_from_url)
 
356
                urlutils.local_path_from_url)
375
357
 
376
358
    def test_posix_local_path_to_url(self):
377
359
        to_url = urlutils._posix_local_path_to_url
378
360
        self.assertEqual('file:///path/to/foo',
379
 
                         to_url('/path/to/foo'))
 
361
            to_url('/path/to/foo'))
380
362
 
381
363
        self.assertEqual('file:///path/to/foo%2Cbar',
382
 
                         to_url('/path/to/foo,bar'))
 
364
            to_url('/path/to/foo,bar'))
383
365
 
384
366
        try:
385
367
            result = to_url(u'/path/to/r\xe4ksm\xf6rg\xe5s')
392
374
    def test_posix_local_path_from_url(self):
393
375
        from_url = urlutils._posix_local_path_from_url
394
376
        self.assertEqual('/path/to/foo',
395
 
                         from_url('file:///path/to/foo'))
 
377
            from_url('file:///path/to/foo'))
396
378
        self.assertEqual('/path/to/foo',
397
 
                         from_url('file:///path/to/foo,branch=foo'))
398
 
        self.assertEqual(u'/path/to/r\xe4ksm\xf6rg\xe5s',
399
 
                         from_url('file:///path/to/r%C3%A4ksm%C3%B6rg%C3%A5s'))
400
 
        self.assertEqual(u'/path/to/r\xe4ksm\xf6rg\xe5s',
401
 
                         from_url('file:///path/to/r%c3%a4ksm%c3%b6rg%c3%a5s'))
402
 
        self.assertEqual(
403
 
            u'/path/to/r\xe4ksm\xf6rg\xe5s',
 
379
            from_url('file:///path/to/foo,branch=foo'))
 
380
        self.assertEqual(u'/path/to/r\xe4ksm\xf6rg\xe5s',
 
381
            from_url('file:///path/to/r%C3%A4ksm%C3%B6rg%C3%A5s'))
 
382
        self.assertEqual(u'/path/to/r\xe4ksm\xf6rg\xe5s',
 
383
            from_url('file:///path/to/r%c3%a4ksm%c3%b6rg%c3%a5s'))
 
384
        self.assertEqual(u'/path/to/r\xe4ksm\xf6rg\xe5s',
404
385
            from_url('file://localhost/path/to/r%c3%a4ksm%c3%b6rg%c3%a5s'))
405
386
 
406
387
        self.assertRaises(urlutils.InvalidURL, from_url, '/path/to/foo')
411
392
    def test_win32_local_path_to_url(self):
412
393
        to_url = urlutils._win32_local_path_to_url
413
394
        self.assertEqual('file:///C:/path/to/foo',
414
 
                         to_url('C:/path/to/foo'))
 
395
            to_url('C:/path/to/foo'))
415
396
        # BOGUS: on win32, ntpath.abspath will strip trailing
416
397
        #       whitespace, so this will always fail
417
398
        #       Though under linux, it fakes abspath support
419
400
        # self.assertEqual('file:///C:/path/to/foo%20',
420
401
        #     to_url('C:/path/to/foo '))
421
402
        self.assertEqual('file:///C:/path/to/f%20oo',
422
 
                         to_url('C:/path/to/f oo'))
 
403
            to_url('C:/path/to/f oo'))
423
404
 
424
405
        self.assertEqual('file:///', to_url('/'))
425
406
 
426
407
        self.assertEqual('file:///C:/path/to/foo%2Cbar',
427
 
                         to_url('C:/path/to/foo,bar'))
 
408
            to_url('C:/path/to/foo,bar'))
428
409
        try:
429
410
            result = to_url(u'd:/path/to/r\xe4ksm\xf6rg\xe5s')
430
411
        except UnicodeError:
431
412
            raise TestSkipped("local encoding cannot handle unicode")
432
413
 
433
 
        self.assertEqual(
434
 
            'file:///D:/path/to/r%C3%A4ksm%C3%B6rg%C3%A5s', result)
435
 
        self.assertIsInstance(result, str)
 
414
        self.assertEqual('file:///D:/path/to/r%C3%A4ksm%C3%B6rg%C3%A5s', result)
 
415
        self.assertFalse(isinstance(result, text_type))
436
416
 
437
417
    def test_win32_unc_path_to_url(self):
438
418
        self.requireFeature(features.win32_feature)
439
419
        to_url = urlutils._win32_local_path_to_url
440
420
        self.assertEqual('file://HOST/path',
441
 
                         to_url(r'\\HOST\path'))
 
421
            to_url(r'\\HOST\path'))
442
422
        self.assertEqual('file://HOST/path',
443
 
                         to_url('//HOST/path'))
 
423
            to_url('//HOST/path'))
444
424
 
445
425
        try:
446
426
            result = to_url(u'//HOST/path/to/r\xe4ksm\xf6rg\xe5s')
447
427
        except UnicodeError:
448
428
            raise TestSkipped("local encoding cannot handle unicode")
449
429
 
450
 
        self.assertEqual(
451
 
            'file://HOST/path/to/r%C3%A4ksm%C3%B6rg%C3%A5s', result)
 
430
        self.assertEqual('file://HOST/path/to/r%C3%A4ksm%C3%B6rg%C3%A5s', result)
452
431
        self.assertFalse(isinstance(result, text_type))
453
432
 
454
433
    def test_win32_local_path_from_url(self):
455
434
        from_url = urlutils._win32_local_path_from_url
456
435
        self.assertEqual('C:/path/to/foo',
457
 
                         from_url('file:///C|/path/to/foo'))
458
 
        self.assertEqual(
459
 
            u'D:/path/to/r\xe4ksm\xf6rg\xe5s',
 
436
            from_url('file:///C|/path/to/foo'))
 
437
        self.assertEqual(u'D:/path/to/r\xe4ksm\xf6rg\xe5s',
460
438
            from_url('file:///d|/path/to/r%C3%A4ksm%C3%B6rg%C3%A5s'))
461
 
        self.assertEqual(
462
 
            u'D:/path/to/r\xe4ksm\xf6rg\xe5s',
 
439
        self.assertEqual(u'D:/path/to/r\xe4ksm\xf6rg\xe5s',
463
440
            from_url('file:///d:/path/to/r%c3%a4ksm%c3%b6rg%c3%a5s'))
464
441
        self.assertEqual('/', from_url('file:///'))
465
442
        self.assertEqual('C:/path/to/foo',
466
 
                         from_url('file:///C|/path/to/foo,branch=foo'))
 
443
            from_url('file:///C|/path/to/foo,branch=foo'))
467
444
 
468
445
        self.assertRaises(urlutils.InvalidURL, from_url, 'file:///C:')
469
446
        self.assertRaises(urlutils.InvalidURL, from_url, 'file:///c')
475
452
        from_url = urlutils._win32_local_path_from_url
476
453
        self.assertEqual('//HOST/path', from_url('file://HOST/path'))
477
454
        self.assertEqual('//HOST/path',
478
 
                         from_url('file://HOST/path,branch=foo'))
 
455
            from_url('file://HOST/path,branch=foo'))
479
456
        # despite IE allows 2, 4, 5 and 6 slashes in URL to another machine
480
457
        # we want to use only 2 slashes
481
458
        # Firefox understand only 5 slashes in URL, but it's ugly
482
459
        self.assertRaises(urlutils.InvalidURL, from_url, 'file:////HOST/path')
483
460
        self.assertRaises(urlutils.InvalidURL, from_url, 'file://///HOST/path')
484
 
        self.assertRaises(urlutils.InvalidURL, from_url,
485
 
                          'file://////HOST/path')
 
461
        self.assertRaises(urlutils.InvalidURL, from_url, 'file://////HOST/path')
486
462
        # check for file://C:/ instead of file:///C:/
487
463
        self.assertRaises(urlutils.InvalidURL, from_url, 'file://C:/path')
488
464
 
489
465
    def test_win32_extract_drive_letter(self):
490
466
        extract = urlutils._win32_extract_drive_letter
491
467
        self.assertEqual(('file:///C:', '/foo'), extract('file://', '/C:/foo'))
492
 
        self.assertEqual(('file:///d|', '/path'),
493
 
                         extract('file://', '/d|/path'))
 
468
        self.assertEqual(('file:///d|', '/path'), extract('file://', '/d|/path'))
494
469
        self.assertRaises(urlutils.InvalidURL, extract, 'file://', '/path')
495
470
        # Root drives without slash treated as invalid, see bug #841322
496
471
        self.assertEqual(('file:///C:', '/'), extract('file://', '/C:/'))
503
478
        # Test breezy.urlutils.split()
504
479
        split = urlutils.split
505
480
        if sys.platform == 'win32':
506
 
            self.assertRaises(urlutils.InvalidURL, split,
507
 
                              'file:///path/to/foo')
 
481
            self.assertRaises(urlutils.InvalidURL, split, 'file:///path/to/foo')
508
482
            self.assertEqual(('file:///C|/', 'foo'), split('file:///C|/foo'))
509
483
            self.assertEqual(('file:///C:/', ''), split('file:///C:/'))
510
484
        else:
511
485
            self.assertEqual(('file:///', 'foo'), split('file:///foo'))
512
486
            self.assertEqual(('file:///', ''), split('file:///'))
513
487
 
514
 
        self.assertEqual(('http://host/path/to', 'foo'),
515
 
                         split('http://host/path/to/foo'))
516
 
        self.assertEqual(('http://host/path/to', 'foo'),
517
 
                         split('http://host/path/to/foo/'))
518
 
        self.assertEqual(
519
 
            ('http://host/path/to/foo', ''),
 
488
        self.assertEqual(('http://host/path/to', 'foo'), split('http://host/path/to/foo'))
 
489
        self.assertEqual(('http://host/path/to', 'foo'), split('http://host/path/to/foo/'))
 
490
        self.assertEqual(('http://host/path/to/foo', ''),
520
491
            split('http://host/path/to/foo/', exclude_trailing_slash=False))
521
492
        self.assertEqual(('http://host/', 'path'), split('http://host/path'))
522
493
        self.assertEqual(('http://host/', ''), split('http://host/'))
523
494
        self.assertEqual(('http://host', ''), split('http://host'))
524
 
        self.assertEqual(('http:///nohost', 'path'),
525
 
                         split('http:///nohost/path'))
 
495
        self.assertEqual(('http:///nohost', 'path'), split('http:///nohost/path'))
526
496
 
527
497
        self.assertEqual(('random+scheme://user:pass@ahost:port/', 'path'),
528
 
                         split('random+scheme://user:pass@ahost:port/path'))
 
498
            split('random+scheme://user:pass@ahost:port/path'))
529
499
        self.assertEqual(('random+scheme://user:pass@ahost:port/', 'path'),
530
 
                         split('random+scheme://user:pass@ahost:port/path/'))
 
500
            split('random+scheme://user:pass@ahost:port/path/'))
531
501
        self.assertEqual(('random+scheme://user:pass@ahost:port/', ''),
532
 
                         split('random+scheme://user:pass@ahost:port/'))
 
502
            split('random+scheme://user:pass@ahost:port/'))
533
503
 
534
504
        # relative paths
535
505
        self.assertEqual(('path/to', 'foo'), split('path/to/foo'))
536
506
        self.assertEqual(('path/to', 'foo'), split('path/to/foo/'))
537
507
        self.assertEqual(('path/to/foo', ''),
538
 
                         split('path/to/foo/', exclude_trailing_slash=False))
 
508
            split('path/to/foo/', exclude_trailing_slash=False))
539
509
        self.assertEqual(('path/..', 'foo'), split('path/../foo'))
540
510
        self.assertEqual(('../path', 'foo'), split('../path/foo'))
541
511
 
542
 
    def test_strip_segment_parameters(self):
543
 
        strip_segment_parameters = urlutils.strip_segment_parameters
544
 
        # Check relative references with absolute paths
545
 
        self.assertEqual("/some/path",
546
 
                         strip_segment_parameters("/some/path"))
547
 
        self.assertEqual("/some/path",
548
 
                         strip_segment_parameters("/some/path,tip"))
549
 
        self.assertEqual("/some,dir/path",
550
 
                         strip_segment_parameters("/some,dir/path,tip"))
551
 
        self.assertEqual(
552
 
            "/somedir/path",
553
 
            strip_segment_parameters("/somedir/path,heads%2Ftip"))
554
 
        self.assertEqual(
555
 
            "/somedir/path",
556
 
            strip_segment_parameters("/somedir/path,heads%2Ftip,bar"))
557
 
        # Check relative references with relative paths
558
 
        self.assertEqual("", strip_segment_parameters(",key1=val1"))
559
 
        self.assertEqual("foo/", strip_segment_parameters("foo/,key1=val1"))
560
 
        self.assertEqual("foo", strip_segment_parameters("foo,key1=val1"))
561
 
        self.assertEqual(
562
 
            "foo/base,la=bla/other/elements",
563
 
            strip_segment_parameters("foo/base,la=bla/other/elements"))
564
 
        self.assertEqual(
565
 
            "foo/base,la=bla/other/elements",
566
 
            strip_segment_parameters("foo/base,la=bla/other/elements,a=b"))
567
 
        # TODO: Check full URLs as well as relative references
568
 
 
569
512
    def test_split_segment_parameters_raw(self):
570
513
        split_segment_parameters_raw = urlutils.split_segment_parameters_raw
571
514
        # Check relative references with absolute paths
572
515
        self.assertEqual(("/some/path", []),
573
 
                         split_segment_parameters_raw("/some/path"))
 
516
            split_segment_parameters_raw("/some/path"))
574
517
        self.assertEqual(("/some/path", ["tip"]),
575
 
                         split_segment_parameters_raw("/some/path,tip"))
 
518
            split_segment_parameters_raw("/some/path,tip"))
576
519
        self.assertEqual(("/some,dir/path", ["tip"]),
577
 
                         split_segment_parameters_raw("/some,dir/path,tip"))
578
 
        self.assertEqual(
579
 
            ("/somedir/path", ["heads%2Ftip"]),
 
520
            split_segment_parameters_raw("/some,dir/path,tip"))
 
521
        self.assertEqual(("/somedir/path", ["heads%2Ftip"]),
580
522
            split_segment_parameters_raw("/somedir/path,heads%2Ftip"))
581
 
        self.assertEqual(
582
 
            ("/somedir/path", ["heads%2Ftip", "bar"]),
 
523
        self.assertEqual(("/somedir/path", ["heads%2Ftip", "bar"]),
583
524
            split_segment_parameters_raw("/somedir/path,heads%2Ftip,bar"))
584
525
        # Check relative references with relative paths
585
526
        self.assertEqual(("", ["key1=val1"]),
586
 
                         split_segment_parameters_raw(",key1=val1"))
 
527
            split_segment_parameters_raw(",key1=val1"))
587
528
        self.assertEqual(("foo/", ["key1=val1"]),
588
 
                         split_segment_parameters_raw("foo/,key1=val1"))
 
529
            split_segment_parameters_raw("foo/,key1=val1"))
589
530
        self.assertEqual(("foo", ["key1=val1"]),
590
 
                         split_segment_parameters_raw("foo,key1=val1"))
591
 
        self.assertEqual(
592
 
            ("foo/base,la=bla/other/elements", []),
 
531
            split_segment_parameters_raw("foo,key1=val1"))
 
532
        self.assertEqual(("foo/base,la=bla/other/elements", []),
593
533
            split_segment_parameters_raw("foo/base,la=bla/other/elements"))
594
 
        self.assertEqual(
595
 
            ("foo/base,la=bla/other/elements", ["a=b"]),
 
534
        self.assertEqual(("foo/base,la=bla/other/elements", ["a=b"]),
596
535
            split_segment_parameters_raw("foo/base,la=bla/other/elements,a=b"))
597
536
        # TODO: Check full URLs as well as relative references
598
537
 
600
539
        split_segment_parameters = urlutils.split_segment_parameters
601
540
        # Check relative references with absolute paths
602
541
        self.assertEqual(("/some/path", {}),
603
 
                         split_segment_parameters("/some/path"))
 
542
            split_segment_parameters("/some/path"))
604
543
        self.assertEqual(("/some/path", {"branch": "tip"}),
605
 
                         split_segment_parameters("/some/path,branch=tip"))
 
544
            split_segment_parameters("/some/path,branch=tip"))
606
545
        self.assertEqual(("/some,dir/path", {"branch": "tip"}),
607
 
                         split_segment_parameters("/some,dir/path,branch=tip"))
608
 
        self.assertEqual(
609
 
            ("/somedir/path", {"ref": "heads%2Ftip"}),
 
546
            split_segment_parameters("/some,dir/path,branch=tip"))
 
547
        self.assertEqual(("/somedir/path", {"ref": "heads%2Ftip"}),
610
548
            split_segment_parameters("/somedir/path,ref=heads%2Ftip"))
611
549
        self.assertEqual(("/somedir/path",
612
 
                          {"ref": "heads%2Ftip", "key1": "val1"}),
613
 
                         split_segment_parameters(
614
 
            "/somedir/path,ref=heads%2Ftip,key1=val1"))
615
 
        self.assertEqual(
616
 
            ("/somedir/path", {"ref": "heads%2F=tip"}),
 
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"}),
617
554
            split_segment_parameters("/somedir/path,ref=heads%2F=tip"))
618
555
        # Check relative references with relative paths
619
556
        self.assertEqual(("", {"key1": "val1"}),
620
 
                         split_segment_parameters(",key1=val1"))
 
557
            split_segment_parameters(",key1=val1"))
621
558
        self.assertEqual(("foo/", {"key1": "val1"}),
622
 
                         split_segment_parameters("foo/,key1=val1"))
623
 
        self.assertEqual(
624
 
            ("foo/base,key1=val1/other/elements", {}),
 
559
            split_segment_parameters("foo/,key1=val1"))
 
560
        self.assertEqual(("foo/base,key1=val1/other/elements", {}),
625
561
            split_segment_parameters("foo/base,key1=val1/other/elements"))
626
562
        self.assertEqual(("foo/base,key1=val1/other/elements",
627
 
                          {"key2": "val2"}), split_segment_parameters(
628
 
            "foo/base,key1=val1/other/elements,key2=val2"))
629
 
        self.assertRaises(
630
 
            urlutils.InvalidURL, split_segment_parameters,
631
 
            "foo/base,key1")
 
563
            {"key2": "val2"}), split_segment_parameters(
 
564
                "foo/base,key1=val1/other/elements,key2=val2"))
632
565
        # TODO: Check full URLs as well as relative references
633
566
 
634
567
    def test_win32_strip_local_trailing_slash(self):
662
595
        self.assertEqual('file://', sts('file://'))
663
596
 
664
597
        self.assertEqual('random+scheme://user:pass@ahost:port/path',
665
 
                         sts('random+scheme://user:pass@ahost:port/path'))
 
598
            sts('random+scheme://user:pass@ahost:port/path'))
666
599
        self.assertEqual('random+scheme://user:pass@ahost:port/path',
667
 
                         sts('random+scheme://user:pass@ahost:port/path/'))
 
600
            sts('random+scheme://user:pass@ahost:port/path/'))
668
601
        self.assertEqual('random+scheme://user:pass@ahost:port/',
669
 
                         sts('random+scheme://user:pass@ahost:port/'))
 
602
            sts('random+scheme://user:pass@ahost:port/'))
670
603
 
671
604
        # Make sure relative paths work too
672
605
        self.assertEqual('path/to/foo', sts('path/to/foo'))
718
651
    def test_escape(self):
719
652
        self.assertEqual('%25', urlutils.escape('%'))
720
653
        self.assertEqual('%C3%A5', urlutils.escape(u'\xe5'))
721
 
        self.assertIsInstance(urlutils.escape(u'\xe5'), str)
 
654
        self.assertFalse(isinstance(urlutils.escape(u'\xe5'), text_type))
722
655
 
723
656
    def test_escape_tildes(self):
724
657
        self.assertEqual('~foo', urlutils.escape('~foo'))
727
660
        self.assertEqual('%', urlutils.unescape('%25'))
728
661
        self.assertEqual(u'\xe5', urlutils.unescape('%C3%A5'))
729
662
 
730
 
        if not PY3:
731
 
            self.assertRaises(urlutils.InvalidURL, urlutils.unescape, u'\xe5')
732
 
        self.assertRaises((TypeError, urlutils.InvalidURL),
733
 
                          urlutils.unescape, b'\xe5')
734
 
        if not PY3:
735
 
            self.assertRaises(urlutils.InvalidURL, urlutils.unescape, '%E5')
736
 
        else:
737
 
            self.assertEqual('\xe5', urlutils.unescape('%C3%A5'))
 
663
        self.assertRaises(urlutils.InvalidURL, urlutils.unescape, u'\xe5')
 
664
        self.assertRaises(urlutils.InvalidURL, urlutils.unescape, '\xe5')
 
665
        self.assertRaises(urlutils.InvalidURL, urlutils.unescape, '%E5')
738
666
 
739
667
    def test_escape_unescape(self):
740
668
        self.assertEqual(u'\xe5', urlutils.unescape(urlutils.escape(u'\xe5')))
747
675
 
748
676
        test('a', 'http://host/', 'http://host/a')
749
677
        test('http://entirely/different', 'sftp://host/branch',
750
 
             'http://entirely/different')
 
678
                    'http://entirely/different')
751
679
        test('../person/feature', 'http://host/branch/mainline',
752
 
             'http://host/branch/person/feature')
 
680
                    'http://host/branch/person/feature')
753
681
        test('..', 'http://host/branch', 'http://host/')
754
 
        test('http://host2/branch', 'http://host1/branch',
755
 
             'http://host2/branch')
 
682
        test('http://host2/branch', 'http://host1/branch', 'http://host2/branch')
756
683
        test('.', 'http://host1/branch', 'http://host1/branch')
757
684
        test('../../../branch/2b', 'file:///home/jelmer/foo/bar/2b',
758
 
             'file:///home/jelmer/branch/2b')
 
685
                    'file:///home/jelmer/branch/2b')
759
686
        test('../../branch/2b', 'sftp://host/home/jelmer/bar/2b',
760
 
             'sftp://host/home/jelmer/branch/2b')
 
687
                    'sftp://host/home/jelmer/branch/2b')
761
688
        test('../../branch/feature/%2b', 'http://host/home/jelmer/bar/%2b',
762
 
             'http://host/home/jelmer/branch/feature/%2b')
 
689
                    'http://host/home/jelmer/branch/feature/%2b')
763
690
        test('../../branch/feature/2b', 'http://host/home/jelmer/bar/2b/',
764
 
             'http://host/home/jelmer/branch/feature/2b')
 
691
                    'http://host/home/jelmer/branch/feature/2b')
765
692
        # relative_url should preserve a trailing slash
766
693
        test('../../branch/feature/2b/', 'http://host/home/jelmer/bar/2b/',
767
 
             'http://host/home/jelmer/branch/feature/2b/')
 
694
                    'http://host/home/jelmer/branch/feature/2b/')
768
695
        test('../../branch/feature/2b/', 'http://host/home/jelmer/bar/2b',
769
 
             'http://host/home/jelmer/branch/feature/2b/')
 
696
                    'http://host/home/jelmer/branch/feature/2b/')
770
697
 
771
698
        # TODO: treat http://host as http://host/
772
699
        #       relative_url is typically called from a branch.base or
773
700
        #       transport.base which always ends with a /
774
 
        # test('a', 'http://host', 'http://host/a')
 
701
        #test('a', 'http://host', 'http://host/a')
775
702
        test('http://host/a', 'http://host', 'http://host/a')
776
 
        # test('.', 'http://host', 'http://host/')
 
703
        #test('.', 'http://host', 'http://host/')
777
704
        test('http://host/', 'http://host', 'http://host/')
778
 
        # test('.', 'http://host/', 'http://host')
 
705
        #test('.', 'http://host/', 'http://host')
779
706
        test('http://host', 'http://host/', 'http://host')
780
707
 
781
708
        # On Windows file:///C:/path/to and file:///D:/other/path
783
710
        if sys.platform == 'win32':
784
711
            # on the same drive
785
712
            test('../../other/path',
786
 
                 'file:///C:/path/to', 'file:///C:/other/path')
787
 
            # ~next two tests is failed, i.e. urlutils.relative_url expects
788
 
            # ~to see normalized file URLs?
789
 
            # ~test('../../other/path',
790
 
            # ~    'file:///C:/path/to', 'file:///c:/other/path')
791
 
            # ~test('../../other/path',
792
 
            # ~    'file:///C:/path/to', 'file:///C|/other/path')
 
713
                'file:///C:/path/to', 'file:///C:/other/path')
 
714
            #~next two tests is failed, i.e. urlutils.relative_url expects
 
715
            #~to see normalized file URLs?
 
716
            #~test('../../other/path',
 
717
            #~    'file:///C:/path/to', 'file:///c:/other/path')
 
718
            #~test('../../other/path',
 
719
            #~    'file:///C:/path/to', 'file:///C|/other/path')
793
720
 
794
721
            # check UNC paths too
795
722
            test('../../other/path',
796
 
                 'file://HOST/base/path/to', 'file://HOST/base/other/path')
 
723
                'file://HOST/base/path/to', 'file://HOST/base/other/path')
797
724
            # on different drives
798
725
            test('file:///D:/other/path',
799
 
                 'file:///C:/path/to', 'file:///D:/other/path')
 
726
                'file:///C:/path/to', 'file:///D:/other/path')
800
727
            # TODO: strictly saying in UNC path //HOST/base is full analog
801
728
            # of drive letter for hard disk, and this situation is also
802
729
            # should be exception from rules. [bialix 20071221]
878
805
 
879
806
    def test_rebase_success(self):
880
807
        self.assertEqual('../bar', urlutils.rebase_url('bar', 'http://baz/',
881
 
                                                       'http://baz/qux'))
882
 
        self.assertEqual(
883
 
            'qux/bar',
884
 
            urlutils.rebase_url('bar', 'http://baz/qux', 'http://baz/'))
885
 
        self.assertEqual(
886
 
            '.', urlutils.rebase_url('foo', 'http://bar/', 'http://bar/foo/'))
887
 
        self.assertEqual(
888
 
            'qux/bar',
889
 
            urlutils.rebase_url('../bar', 'http://baz/qux/foo', 'http://baz/'))
 
808
                         'http://baz/qux'))
 
809
        self.assertEqual('qux/bar', urlutils.rebase_url('bar',
 
810
                         'http://baz/qux', 'http://baz/'))
 
811
        self.assertEqual('.', urlutils.rebase_url('foo',
 
812
                         'http://bar/', 'http://bar/foo/'))
 
813
        self.assertEqual('qux/bar', urlutils.rebase_url('../bar',
 
814
                         'http://baz/qux/foo', 'http://baz/'))
890
815
 
891
816
    def test_determine_relative_path(self):
892
817
        self.assertEqual('../../baz/bar',
893
818
                         urlutils.determine_relative_path(
894
 
                             '/qux/quxx', '/baz/bar'))
 
819
                         '/qux/quxx', '/baz/bar'))
895
820
        self.assertEqual('..',
896
821
                         urlutils.determine_relative_path(
897
 
                             '/bar/baz', '/bar'))
 
822
                         '/bar/baz', '/bar'))
898
823
        self.assertEqual('baz',
899
824
                         urlutils.determine_relative_path(
900
 
                             '/bar', '/bar/baz'))
 
825
                         '/bar', '/bar/baz'))
901
826
        self.assertEqual('.', urlutils.determine_relative_path(
902
827
                         '/bar', '/bar'))
903
828
 
907
832
    def test_parse_simple(self):
908
833
        parsed = urlutils.parse_url('http://example.com:80/one')
909
834
        self.assertEqual(('http', None, None, 'example.com', 80, '/one'),
910
 
                         parsed)
 
835
            parsed)
911
836
 
912
837
    def test_ipv6(self):
913
838
        parsed = urlutils.parse_url('http://[1:2:3::40]/one')
914
839
        self.assertEqual(('http', None, None, '1:2:3::40', None, '/one'),
915
 
                         parsed)
 
840
            parsed)
916
841
 
917
842
    def test_ipv6_port(self):
918
843
        parsed = urlutils.parse_url('http://[1:2:3::40]:80/one')
919
844
        self.assertEqual(('http', None, None, '1:2:3::40', 80, '/one'),
920
 
                         parsed)
 
845
            parsed)
921
846
 
922
847
 
923
848
class TestURL(TestCase):
1000
925
        self.assertIsNot(url, url3)
1001
926
        self.assertEqual(url, url3)
1002
927
 
1003
 
    def test_parse_empty_port(self):
1004
 
        parsed = urlutils.URL.from_string('http://example.com:/one')
1005
 
        self.assertEqual('http', parsed.scheme)
1006
 
        self.assertIs(None, parsed.user)
1007
 
        self.assertIs(None, parsed.password)
1008
 
        self.assertEqual('example.com', parsed.host)
1009
 
        self.assertIs(None, parsed.port)
1010
 
        self.assertEqual('/one', parsed.path)
1011
 
 
1012
928
 
1013
929
class TestFileRelpath(TestCase):
1014
930
 
1017
933
 
1018
934
    def _with_posix_paths(self):
1019
935
        self.overrideAttr(urlutils, "local_path_from_url",
1020
 
                          urlutils._posix_local_path_from_url)
 
936
            urlutils._posix_local_path_from_url)
1021
937
        self.overrideAttr(urlutils, "MIN_ABS_FILEURL_LENGTH", len("file:///"))
1022
938
        self.overrideAttr(osutils, "normpath", osutils._posix_normpath)
1023
939
        self.overrideAttr(osutils, "abspath", osutils._posix_abspath)
1028
944
 
1029
945
    def _with_win32_paths(self):
1030
946
        self.overrideAttr(urlutils, "local_path_from_url",
1031
 
                          urlutils._win32_local_path_from_url)
 
947
            urlutils._win32_local_path_from_url)
1032
948
        self.overrideAttr(urlutils, "MIN_ABS_FILEURL_LENGTH",
1033
 
                          urlutils.WIN32_MIN_ABS_FILEURL_LENGTH)
 
949
            urlutils.WIN32_MIN_ABS_FILEURL_LENGTH)
1034
950
        self.overrideAttr(osutils, "abspath", osutils._win32_abspath)
1035
951
        self.overrideAttr(osutils, "normpath", osutils._win32_normpath)
1036
952
        self.overrideAttr(osutils, "pathjoin", osutils._win32_pathjoin)
1040
956
    def test_same_url_posix(self):
1041
957
        self._with_posix_paths()
1042
958
        self.assertEqual("",
1043
 
                         urlutils.file_relpath("file:///a", "file:///a"))
1044
 
        self.assertEqual("",
1045
 
                         urlutils.file_relpath("file:///a", "file:///a/"))
1046
 
        self.assertEqual("",
1047
 
                         urlutils.file_relpath("file:///a/", "file:///a"))
 
959
            urlutils.file_relpath("file:///a", "file:///a"))
 
960
        self.assertEqual("",
 
961
            urlutils.file_relpath("file:///a", "file:///a/"))
 
962
        self.assertEqual("",
 
963
            urlutils.file_relpath("file:///a/", "file:///a"))
1048
964
 
1049
965
    def test_same_url_win32(self):
1050
966
        self._with_win32_paths()
1051
967
        self.assertEqual("",
1052
 
                         urlutils.file_relpath("file:///A:/", "file:///A:/"))
1053
 
        self.assertEqual("",
1054
 
                         urlutils.file_relpath("file:///A|/", "file:///A:/"))
1055
 
        self.assertEqual(
1056
 
            "", urlutils.file_relpath("file:///A:/b/", "file:///A:/b/"))
1057
 
        self.assertEqual(
1058
 
            "", urlutils.file_relpath("file:///A:/b", "file:///A:/b/"))
1059
 
        self.assertEqual(
1060
 
            "", urlutils.file_relpath("file:///A:/b/", "file:///A:/b"))
 
968
            urlutils.file_relpath("file:///A:/", "file:///A:/"))
 
969
        self.assertEqual("",
 
970
            urlutils.file_relpath("file:///A|/", "file:///A:/"))
 
971
        self.assertEqual("",
 
972
            urlutils.file_relpath("file:///A:/b/", "file:///A:/b/"))
 
973
        self.assertEqual("",
 
974
            urlutils.file_relpath("file:///A:/b", "file:///A:/b/"))
 
975
        self.assertEqual("",
 
976
            urlutils.file_relpath("file:///A:/b/", "file:///A:/b"))
1061
977
 
1062
978
    def test_child_posix(self):
1063
979
        self._with_posix_paths()
1064
 
        self.assertEqual(
1065
 
            "b", urlutils.file_relpath("file:///a", "file:///a/b"))
1066
 
        self.assertEqual(
1067
 
            "b", urlutils.file_relpath("file:///a/", "file:///a/b"))
1068
 
        self.assertEqual(
1069
 
            "b/c", urlutils.file_relpath("file:///a", "file:///a/b/c"))
 
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"))
1070
986
 
1071
987
    def test_child_win32(self):
1072
988
        self._with_win32_paths()
1073
 
        self.assertEqual(
1074
 
            "b", urlutils.file_relpath("file:///A:/", "file:///A:/b"))
1075
 
        self.assertEqual(
1076
 
            "b", urlutils.file_relpath("file:///A|/", "file:///A:/b"))
1077
 
        self.assertEqual(
1078
 
            "c", urlutils.file_relpath("file:///A:/b", "file:///A:/b/c"))
1079
 
        self.assertEqual(
1080
 
            "c", urlutils.file_relpath("file:///A:/b/", "file:///A:/b/c"))
1081
 
        self.assertEqual(
1082
 
            "c/d", urlutils.file_relpath("file:///A:/b", "file:///A:/b/c/d"))
 
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"))
1083
999
 
1084
1000
    def test_sibling_posix(self):
1085
1001
        self._with_posix_paths()
1086
 
        self.assertRaises(
1087
 
            PathNotChild,
 
1002
        self.assertRaises(PathNotChild,
1088
1003
            urlutils.file_relpath, "file:///a/b", "file:///a/c")
1089
 
        self.assertRaises(
1090
 
            PathNotChild,
 
1004
        self.assertRaises(PathNotChild,
1091
1005
            urlutils.file_relpath, "file:///a/b/", "file:///a/c")
1092
 
        self.assertRaises(
1093
 
            PathNotChild,
 
1006
        self.assertRaises(PathNotChild,
1094
1007
            urlutils.file_relpath, "file:///a/b/", "file:///a/c/")
1095
1008
 
1096
1009
    def test_sibling_win32(self):
1097
1010
        self._with_win32_paths()
1098
 
        self.assertRaises(
1099
 
            PathNotChild,
 
1011
        self.assertRaises(PathNotChild,
1100
1012
            urlutils.file_relpath, "file:///A:/b", "file:///A:/c")
1101
 
        self.assertRaises(
1102
 
            PathNotChild,
 
1013
        self.assertRaises(PathNotChild,
1103
1014
            urlutils.file_relpath, "file:///A:/b/", "file:///A:/c")
1104
 
        self.assertRaises(
1105
 
            PathNotChild,
 
1015
        self.assertRaises(PathNotChild,
1106
1016
            urlutils.file_relpath, "file:///A:/b/", "file:///A:/c/")
1107
1017
 
1108
1018
    def test_parent_posix(self):
1109
1019
        self._with_posix_paths()
1110
1020
        self.assertRaises(PathNotChild,
1111
 
                          urlutils.file_relpath, "file:///a/b", "file:///a")
 
1021
            urlutils.file_relpath, "file:///a/b", "file:///a")
1112
1022
        self.assertRaises(PathNotChild,
1113
 
                          urlutils.file_relpath, "file:///a/b", "file:///a/")
 
1023
            urlutils.file_relpath, "file:///a/b", "file:///a/")
1114
1024
 
1115
1025
    def test_parent_win32(self):
1116
1026
        self._with_win32_paths()
1117
 
        self.assertRaises(
1118
 
            PathNotChild,
 
1027
        self.assertRaises(PathNotChild,
1119
1028
            urlutils.file_relpath, "file:///A:/b", "file:///A:/")
1120
 
        self.assertRaises(
1121
 
            PathNotChild,
 
1029
        self.assertRaises(PathNotChild,
1122
1030
            urlutils.file_relpath, "file:///A:/b/c", "file:///A:/b")
1123
1031
 
1124
1032
 
1130
1038
        self.assertEqual('abc/def', urlutils.quote('abc/def', safe='/'))
1131
1039
 
1132
1040
    def test_quote_tildes(self):
1133
 
        # Whether ~ is quoted by default depends on the python version
1134
 
        if sys.version_info[:2] >= (3, 7):
1135
 
            # https://docs.python.org/3/whatsnew/3.7.html#urllib-parse
1136
 
            self.assertEqual('~foo', urlutils.quote('~foo'))
1137
 
        else:
1138
 
            self.assertEqual('%7Efoo', urlutils.quote('~foo'))
 
1041
        self.assertEqual('%7Efoo', urlutils.quote('~foo'))
1139
1042
        self.assertEqual('~foo', urlutils.quote('~foo', safe='/~'))
1140
1043
 
1141
1044
    def test_unquote(self):
1142
1045
        self.assertEqual('%', urlutils.unquote('%25'))
1143
 
        if PY3:
1144
 
            self.assertEqual('\xe5', urlutils.unquote('%C3%A5'))
1145
 
        else:
1146
 
            self.assertEqual('\xc3\xa5', urlutils.unquote('%C3%A5'))
 
1046
        self.assertEqual('\xc3\xa5', urlutils.unquote('%C3%A5'))
1147
1047
        self.assertEqual(u"\xe5", urlutils.unquote(u'\xe5'))
1148
 
 
1149
 
    def test_unquote_to_bytes(self):
1150
 
        self.assertEqual(b'%', urlutils.unquote_to_bytes('%25'))
1151
 
        self.assertEqual(b'\xc3\xa5', urlutils.unquote_to_bytes('%C3%A5'))