1
# Copyright (C) 2005 by Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Tests for the urlutils wrapper."""
23
from bzrlib.errors import InvalidURL, InvalidURLJoin
24
import bzrlib.urlutils as urlutils
25
from bzrlib.tests import TestCaseInTempDir, TestCase, TestSkipped
28
class TestUrlToPath(TestCase):
30
def test_basename(self):
31
# bzrlib.urlutils.basename
32
# Test bzrlib.urlutils.split()
33
basename = urlutils.basename
34
if sys.platform == 'win32':
35
self.assertRaises(InvalidURL, basename, 'file:///path/to/foo')
36
self.assertEqual('foo', basename('file:///C|/foo'))
37
self.assertEqual('foo', basename('file:///C:/foo'))
38
self.assertEqual('', basename('file:///C:/'))
40
self.assertEqual('foo', basename('file:///foo'))
41
self.assertEqual('', basename('file:///'))
43
self.assertEqual('foo', basename('http://host/path/to/foo'))
44
self.assertEqual('foo', basename('http://host/path/to/foo/'))
46
basename('http://host/path/to/foo/', exclude_trailing_slash=False))
47
self.assertEqual('path', basename('http://host/path'))
48
self.assertEqual('', basename('http://host/'))
49
self.assertEqual('', basename('http://host'))
50
self.assertEqual('path', basename('http:///nohost/path'))
52
self.assertEqual('path', basename('random+scheme://user:pass@ahost:port/path'))
53
self.assertEqual('path', basename('random+scheme://user:pass@ahost:port/path/'))
54
self.assertEqual('', basename('random+scheme://user:pass@ahost:port/'))
57
self.assertEqual('foo', basename('path/to/foo'))
58
self.assertEqual('foo', basename('path/to/foo/'))
59
self.assertEqual('', basename('path/to/foo/',
60
exclude_trailing_slash=False))
61
self.assertEqual('foo', basename('path/../foo'))
62
self.assertEqual('foo', basename('../path/foo'))
64
def test_normalize_url_files(self):
65
# Test that local paths are properly normalized
66
normalize_url = urlutils.normalize_url
68
def norm_file(expected, path):
69
url = normalize_url(path)
70
self.assertStartsWith(url, 'file:///')
71
if sys.platform == 'win32':
72
url = url[len('file:///C:'):]
74
url = url[len('file://'):]
76
self.assertEndsWith(url, expected)
78
norm_file('path/to/foo', 'path/to/foo')
79
norm_file('/path/to/foo', '/path/to/foo')
80
norm_file('path/to/foo', '../path/to/foo')
82
# Local paths are assumed to *not* be escaped at all
84
u'uni/\xb5'.encode(bzrlib.user_encoding)
86
# locale cannot handle unicode
89
norm_file('uni/%C2%B5', u'uni/\xb5')
91
norm_file('uni/%25C2%25B5', u'uni/%C2%B5')
92
norm_file('uni/%20b', u'uni/ b')
93
# All the crazy characters get escaped in local paths => file:/// urls
94
norm_file('%27%3B/%3F%3A%40%26%3D%2B%24%2C%23%20', "';/?:@&=+$,# ")
96
def test_normalize_url_hybrid(self):
97
# Anything with a scheme:// should be treated as a hybrid url
98
# which changes what characters get escaped.
99
normalize_url = urlutils.normalize_url
101
eq = self.assertEqual
102
eq('file:///foo/', normalize_url(u'file:///foo/'))
103
eq('file:///foo/%20', normalize_url(u'file:///foo/ '))
104
eq('file:///foo/%20', normalize_url(u'file:///foo/%20'))
105
# Don't escape reserved characters
106
eq('file:///ab_c.d-e/%f:?g&h=i+j;k,L#M$',
107
normalize_url('file:///ab_c.d-e/%f:?g&h=i+j;k,L#M$'))
108
eq('http://ab_c.d-e/%f:?g&h=i+j;k,L#M$',
109
normalize_url('http://ab_c.d-e/%f:?g&h=i+j;k,L#M$'))
111
# Escape unicode characters, but not already escaped chars
112
eq('http://host/ab/%C2%B5/%C2%B5',
113
normalize_url(u'http://host/ab/%C2%B5/\xb5'))
115
# Normalize verifies URLs when they are not unicode
116
# (indicating they did not come from the user)
117
self.assertRaises(InvalidURL, normalize_url, 'http://host/\xb5')
118
self.assertRaises(InvalidURL, normalize_url, 'http://host/ ')
120
def test_url_scheme_re(self):
121
# Test paths that may be URLs
122
def test_one(url, scheme_and_path):
123
"""Assert that _url_scheme_re correctly matches
125
:param scheme_and_path: The (scheme, path) that should be matched
126
can be None, to indicate it should not match
128
m = urlutils._url_scheme_re.match(url)
129
if scheme_and_path is None:
130
self.assertEqual(None, m)
132
self.assertEqual(scheme_and_path[0], m.group('scheme'))
133
self.assertEqual(scheme_and_path[1], m.group('path'))
136
test_one('/path', None)
137
test_one('C:/path', None)
138
test_one('../path/to/foo', None)
139
test_one(u'../path/to/fo\xe5', None)
142
test_one('http://host/path/', ('http', 'host/path/'))
143
test_one('sftp://host/path/to/foo', ('sftp', 'host/path/to/foo'))
144
test_one('file:///usr/bin', ('file', '/usr/bin'))
145
test_one('file:///C:/Windows', ('file', '/C:/Windows'))
146
test_one('file:///C|/Windows', ('file', '/C|/Windows'))
147
test_one(u'readonly+sftp://host/path/\xe5', ('readonly+sftp', u'host/path/\xe5'))
150
# Can't have slashes or colons in the scheme
151
test_one('/path/to/://foo', None)
152
test_one('path:path://foo', None)
153
# Must have more than one character for scheme
154
test_one('C://foo', None)
155
test_one('ab://foo', ('ab', 'foo'))
157
def test_dirname(self):
158
# Test bzrlib.urlutils.dirname()
159
dirname = urlutils.dirname
160
if sys.platform == 'win32':
161
self.assertRaises(InvalidURL, dirname, 'file:///path/to/foo')
162
self.assertEqual('file:///C|/', dirname('file:///C|/foo'))
163
self.assertEqual('file:///C|/', dirname('file:///C|/'))
165
self.assertEqual('file:///', dirname('file:///foo'))
166
self.assertEqual('file:///', dirname('file:///'))
168
self.assertEqual('http://host/path/to', dirname('http://host/path/to/foo'))
169
self.assertEqual('http://host/path/to', dirname('http://host/path/to/foo/'))
170
self.assertEqual('http://host/path/to/foo',
171
dirname('http://host/path/to/foo/', exclude_trailing_slash=False))
172
self.assertEqual('http://host/', dirname('http://host/path'))
173
self.assertEqual('http://host/', dirname('http://host/'))
174
self.assertEqual('http://host', dirname('http://host'))
175
self.assertEqual('http:///nohost', dirname('http:///nohost/path'))
177
self.assertEqual('random+scheme://user:pass@ahost:port/',
178
dirname('random+scheme://user:pass@ahost:port/path'))
179
self.assertEqual('random+scheme://user:pass@ahost:port/',
180
dirname('random+scheme://user:pass@ahost:port/path/'))
181
self.assertEqual('random+scheme://user:pass@ahost:port/',
182
dirname('random+scheme://user:pass@ahost:port/'))
185
self.assertEqual('path/to', dirname('path/to/foo'))
186
self.assertEqual('path/to', dirname('path/to/foo/'))
187
self.assertEqual('path/to/foo',
188
dirname('path/to/foo/', exclude_trailing_slash=False))
189
self.assertEqual('path/..', dirname('path/../foo'))
190
self.assertEqual('../path', dirname('../path/foo'))
193
def test(expected, *args):
194
joined = urlutils.join(*args)
195
self.assertEqual(expected, joined)
197
# Test a single element
200
# Test relative path joining
201
test('foo/bar', 'foo', 'bar')
202
test('http://foo/bar', 'http://foo', 'bar')
203
test('http://foo/bar', 'http://foo', '.', 'bar')
204
test('http://foo/baz', 'http://foo', 'bar', '../baz')
205
test('http://foo/bar/baz', 'http://foo', 'bar/baz')
206
test('http://foo/baz', 'http://foo', 'bar/../baz')
209
test('http://bar', 'http://foo', 'http://bar')
210
test('sftp://bzr/foo', 'http://foo', 'bar', 'sftp://bzr/foo')
211
test('file:///bar', 'foo', 'file:///bar')
214
# Cannot go above root
215
self.assertRaises(InvalidURLJoin, urlutils.join,
216
'http://foo', '../baz')
218
def test_function_type(self):
219
if sys.platform == 'win32':
220
self.assertEqual(urlutils._win32_local_path_to_url, urlutils.local_path_to_url)
221
self.assertEqual(urlutils._win32_local_path_from_url, urlutils.local_path_from_url)
223
self.assertEqual(urlutils._posix_local_path_to_url, urlutils.local_path_to_url)
224
self.assertEqual(urlutils._posix_local_path_from_url, urlutils.local_path_from_url)
226
def test_posix_local_path_to_url(self):
227
to_url = urlutils._posix_local_path_to_url
228
self.assertEqual('file:///path/to/foo',
229
to_url('/path/to/foo'))
232
result = to_url(u'/path/to/r\xe4ksm\xf6rg\xe5s')
234
raise TestSkipped("local encoding cannot handle unicode")
236
self.assertEqual('file:///path/to/r%C3%A4ksm%C3%B6rg%C3%A5s', result)
238
def test_posix_local_path_from_url(self):
239
from_url = urlutils._posix_local_path_from_url
240
self.assertEqual('/path/to/foo',
241
from_url('file:///path/to/foo'))
242
self.assertEqual(u'/path/to/r\xe4ksm\xf6rg\xe5s',
243
from_url('file:///path/to/r%C3%A4ksm%C3%B6rg%C3%A5s'))
244
self.assertEqual(u'/path/to/r\xe4ksm\xf6rg\xe5s',
245
from_url('file:///path/to/r%c3%a4ksm%c3%b6rg%c3%a5s'))
247
self.assertRaises(InvalidURL, from_url, '/path/to/foo')
249
def test_win32_local_path_to_url(self):
250
to_url = urlutils._win32_local_path_to_url
251
self.assertEqual('file:///C:/path/to/foo',
252
to_url('C:/path/to/foo'))
255
result = to_url(u'd:/path/to/r\xe4ksm\xf6rg\xe5s')
257
raise TestSkipped("local encoding cannot handle unicode")
259
self.assertEqual('file:///D:/path/to/r%C3%A4ksm%C3%B6rg%C3%A5s', result)
261
def test_win32_local_path_from_url(self):
262
from_url = urlutils._win32_local_path_from_url
263
self.assertEqual('C:/path/to/foo',
264
from_url('file:///C|/path/to/foo'))
265
self.assertEqual(u'D:/path/to/r\xe4ksm\xf6rg\xe5s',
266
from_url('file:///d|/path/to/r%C3%A4ksm%C3%B6rg%C3%A5s'))
267
self.assertEqual(u'D:/path/to/r\xe4ksm\xf6rg\xe5s',
268
from_url('file:///d:/path/to/r%c3%a4ksm%c3%b6rg%c3%a5s'))
270
self.assertRaises(InvalidURL, from_url, '/path/to/foo')
271
# Not a valid _win32 url, no drive letter
272
self.assertRaises(InvalidURL, from_url, 'file:///path/to/foo')
274
def test_split(self):
275
# Test bzrlib.urlutils.split()
276
split = urlutils.split
277
if sys.platform == 'win32':
278
self.assertRaises(InvalidURL, split, 'file:///path/to/foo')
279
self.assertEqual(('file:///C|/', 'foo'), split('file:///C|/foo'))
280
self.assertEqual(('file:///C:/', ''), split('file:///C:/'))
282
self.assertEqual(('file:///', 'foo'), split('file:///foo'))
283
self.assertEqual(('file:///', ''), split('file:///'))
285
self.assertEqual(('http://host/path/to', 'foo'), split('http://host/path/to/foo'))
286
self.assertEqual(('http://host/path/to', 'foo'), split('http://host/path/to/foo/'))
287
self.assertEqual(('http://host/path/to/foo', ''),
288
split('http://host/path/to/foo/', exclude_trailing_slash=False))
289
self.assertEqual(('http://host/', 'path'), split('http://host/path'))
290
self.assertEqual(('http://host/', ''), split('http://host/'))
291
self.assertEqual(('http://host', ''), split('http://host'))
292
self.assertEqual(('http:///nohost', 'path'), split('http:///nohost/path'))
294
self.assertEqual(('random+scheme://user:pass@ahost:port/', 'path'),
295
split('random+scheme://user:pass@ahost:port/path'))
296
self.assertEqual(('random+scheme://user:pass@ahost:port/', 'path'),
297
split('random+scheme://user:pass@ahost:port/path/'))
298
self.assertEqual(('random+scheme://user:pass@ahost:port/', ''),
299
split('random+scheme://user:pass@ahost:port/'))
302
self.assertEqual(('path/to', 'foo'), split('path/to/foo'))
303
self.assertEqual(('path/to', 'foo'), split('path/to/foo/'))
304
self.assertEqual(('path/to/foo', ''),
305
split('path/to/foo/', exclude_trailing_slash=False))
306
self.assertEqual(('path/..', 'foo'), split('path/../foo'))
307
self.assertEqual(('../path', 'foo'), split('../path/foo'))
309
def test_strip_trailing_slash(self):
310
sts = urlutils.strip_trailing_slash
311
if sys.platform == 'win32':
312
self.assertEqual('file:///C|/', sts('file:///C|/'))
313
self.assertEqual('file:///C:/foo', sts('file:///C:/foo'))
314
self.assertEqual('file:///C|/foo', sts('file:///C|/foo/'))
316
self.assertEqual('file:///', sts('file:///'))
317
self.assertEqual('file:///foo', sts('file:///foo'))
318
self.assertEqual('file:///foo', sts('file:///foo/'))
320
self.assertEqual('http://host/', sts('http://host/'))
321
self.assertEqual('http://host/foo', sts('http://host/foo'))
322
self.assertEqual('http://host/foo', sts('http://host/foo/'))
324
# No need to fail just because the slash is missing
325
self.assertEqual('http://host', sts('http://host'))
326
# TODO: jam 20060502 Should this raise InvalidURL?
327
self.assertEqual('file://', sts('file://'))
329
self.assertEqual('random+scheme://user:pass@ahost:port/path',
330
sts('random+scheme://user:pass@ahost:port/path'))
331
self.assertEqual('random+scheme://user:pass@ahost:port/path',
332
sts('random+scheme://user:pass@ahost:port/path/'))
333
self.assertEqual('random+scheme://user:pass@ahost:port/',
334
sts('random+scheme://user:pass@ahost:port/'))
336
# Make sure relative paths work too
337
self.assertEqual('path/to/foo', sts('path/to/foo'))
338
self.assertEqual('path/to/foo', sts('path/to/foo/'))
339
self.assertEqual('../to/foo', sts('../to/foo/'))
340
self.assertEqual('path/../foo', sts('path/../foo/'))
342
def test_unescape_for_display_utf8(self):
343
# Test that URLs are converted to nice unicode strings for display
344
def test(expected, url, encoding='utf-8'):
345
disp_url = urlutils.unescape_for_display(url, encoding=encoding)
346
self.assertIsInstance(disp_url, unicode)
347
self.assertEqual(expected, disp_url)
349
test('http://foo', 'http://foo')
350
if sys.platform == 'win32':
351
test('C:/foo/path', 'file:///C|/foo/path')
352
test('C:/foo/path', 'file:///C:/foo/path')
354
test('/foo/path', 'file:///foo/path')
356
test('http://foo/%2Fbaz', 'http://foo/%2Fbaz')
357
test(u'http://host/r\xe4ksm\xf6rg\xe5s',
358
'http://host/r%C3%A4ksm%C3%B6rg%C3%A5s')
360
# Make sure special escaped characters stay escaped
361
test(u'http://host/%3B%2F%3F%3A%40%26%3D%2B%24%2C%23',
362
'http://host/%3B%2F%3F%3A%40%26%3D%2B%24%2C%23')
364
# Can we handle sections that don't have utf-8 encoding?
365
test(u'http://host/%EE%EE%EE/r\xe4ksm\xf6rg\xe5s',
366
'http://host/%EE%EE%EE/r%C3%A4ksm%C3%B6rg%C3%A5s')
368
# Test encoding into output that can handle some characters
369
test(u'http://host/%EE%EE%EE/r\xe4ksm\xf6rg\xe5s',
370
'http://host/%EE%EE%EE/r%C3%A4ksm%C3%B6rg%C3%A5s',
371
encoding='iso-8859-1')
373
# This one can be encoded into utf8
374
test(u'http://host/\u062c\u0648\u062c\u0648',
375
'http://host/%d8%ac%d9%88%d8%ac%d9%88',
378
# This can't be put into 8859-1 and so stays as escapes
379
test(u'http://host/%d8%ac%d9%88%d8%ac%d9%88',
380
'http://host/%d8%ac%d9%88%d8%ac%d9%88',
381
encoding='iso-8859-1')
383
def test_escape(self):
384
self.assertEqual('%25', urlutils.escape('%'))
385
self.assertEqual('%C3%A5', urlutils.escape(u'\xe5'))
387
def test_unescape(self):
388
self.assertEqual('%', urlutils.unescape('%25'))
389
self.assertEqual(u'\xe5', urlutils.unescape('%C3%A5'))
391
self.assertRaises(InvalidURL, urlutils.unescape, u'\xe5')
392
self.assertRaises(InvalidURL, urlutils.unescape, '\xe5')
393
self.assertRaises(InvalidURL, urlutils.unescape, '%E5')
395
def test_escape_unescape(self):
396
self.assertEqual(u'\xe5', urlutils.unescape(urlutils.escape(u'\xe5')))
397
self.assertEqual('%', urlutils.unescape(urlutils.escape('%')))
399
def test_relative_url(self):
400
def test(expected, base, other):
401
result = urlutils.relative_url(base, other)
402
self.assertEqual(expected, result)
404
test('a', 'http://host/', 'http://host/a')
405
test('http://entirely/different', 'sftp://host/branch',
406
'http://entirely/different')
407
test('../person/feature', 'http://host/branch/mainline',
408
'http://host/branch/person/feature')
409
test('..', 'http://host/branch', 'http://host/')
410
test('http://host2/branch', 'http://host1/branch', 'http://host2/branch')
411
test('.', 'http://host1/branch', 'http://host1/branch')
412
test('../../../branch/2b', 'file:///home/jelmer/foo/bar/2b',
413
'file:///home/jelmer/branch/2b')
414
test('../../branch/2b', 'sftp://host/home/jelmer/bar/2b',
415
'sftp://host/home/jelmer/branch/2b')
416
test('../../branch/feature/%2b', 'http://host/home/jelmer/bar/%2b',
417
'http://host/home/jelmer/branch/feature/%2b')
418
test('../../branch/feature/2b', 'http://host/home/jelmer/bar/2b/',
419
'http://host/home/jelmer/branch/feature/2b')
420
# relative_url should preserve a trailing slash
421
test('../../branch/feature/2b/', 'http://host/home/jelmer/bar/2b/',
422
'http://host/home/jelmer/branch/feature/2b/')
423
test('../../branch/feature/2b/', 'http://host/home/jelmer/bar/2b',
424
'http://host/home/jelmer/branch/feature/2b/')
426
# TODO: treat http://host as http://host/
427
# relative_url is typically called from a branch.base or
428
# transport.base which always ends with a /
429
#test('a', 'http://host', 'http://host/a')
430
test('http://host/a', 'http://host', 'http://host/a')
431
#test('.', 'http://host', 'http://host/')
432
test('http://host/', 'http://host', 'http://host/')
433
#test('.', 'http://host/', 'http://host')
434
test('http://host', 'http://host/', 'http://host')