/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
1
# Copyright (C) 2005 by Canonical Ltd
2
#
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.
7
#
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.
12
#
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
16
17
"""Tests for the urlutils wrapper."""
18
19
import os
20
import sys
21
22
import bzrlib
1685.1.55 by John Arbash Meinel
Adding bzrlib.urlutils.join() to handle joining URLs
23
from bzrlib.errors import InvalidURL, InvalidURLJoin
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
24
import bzrlib.urlutils as urlutils
1685.1.75 by Wouter van Heyst
more tests handle LANG=C
25
from bzrlib.tests import TestCaseInTempDir, TestCase, TestSkipped
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
26
27
28
class TestUrlToPath(TestCase):
29
    
1685.1.49 by John Arbash Meinel
Added bzrlib.urlutils.split and basename + dirname
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'))
1685.1.78 by Wouter van Heyst
more code cleanup
37
            self.assertEqual('foo', basename('file:///C:/foo'))
38
            self.assertEqual('', basename('file:///C:/'))
1685.1.49 by John Arbash Meinel
Added bzrlib.urlutils.split and basename + dirname
39
        else:
40
            self.assertEqual('foo', basename('file:///foo'))
41
            self.assertEqual('', basename('file:///'))
42
43
        self.assertEqual('foo', basename('http://host/path/to/foo'))
44
        self.assertEqual('foo', basename('http://host/path/to/foo/'))
45
        self.assertEqual('',
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'))
51
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/'))
55
56
        # relative paths
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'))
63
1685.1.51 by John Arbash Meinel
Working on getting normalize_url working.
64
    def test_normalize_url_files(self):
65
        # Test that local paths are properly normalized
66
        normalize_url = urlutils.normalize_url
67
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:'):]
73
            else:
74
                url = url[len('file://'):]
75
1685.1.53 by John Arbash Meinel
Updated normalize_url
76
            self.assertEndsWith(url, expected)
1685.1.51 by John Arbash Meinel
Working on getting normalize_url working.
77
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')
81
82
        # Local paths are assumed to *not* be escaped at all
1685.1.75 by Wouter van Heyst
more tests handle LANG=C
83
        try:
84
            u'uni/\xb5'.encode(bzrlib.user_encoding)
85
        except UnicodeError:
86
            # locale cannot handle unicode 
87
            pass
88
        else:
89
            norm_file('uni/%C2%B5', u'uni/\xb5')
90
1685.1.51 by John Arbash Meinel
Working on getting normalize_url working.
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
1685.1.53 by John Arbash Meinel
Updated normalize_url
94
        norm_file('%27%3B/%3F%3A%40%26%3D%2B%24%2C%23%20', "';/?:@&=+$,# ")
1685.1.51 by John Arbash Meinel
Working on getting normalize_url working.
95
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
100
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$'))
110
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'))
114
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/ ')
1685.1.50 by John Arbash Meinel
Added an re for handling scheme paths.
119
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
124
125
            :param scheme_and_path: The (scheme, path) that should be matched
126
                can be None, to indicate it should not match
127
            """
128
            m = urlutils._url_scheme_re.match(url)
129
            if scheme_and_path is None:
130
                self.assertEqual(None, m)
131
            else:
132
                self.assertEqual(scheme_and_path[0], m.group('scheme'))
133
                self.assertEqual(scheme_and_path[1], m.group('path'))
134
135
        # Local paths
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)
140
141
        # Real URLS
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'))
148
149
        # Weird stuff
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'))
156
1685.1.49 by John Arbash Meinel
Added bzrlib.urlutils.split and basename + dirname
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|/'))
164
        else:
165
            self.assertEqual('file:///', dirname('file:///foo'))
166
            self.assertEqual('file:///', dirname('file:///'))
167
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'))
176
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/'))
183
184
        # relative paths
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'))
191
1685.1.55 by John Arbash Meinel
Adding bzrlib.urlutils.join() to handle joining URLs
192
    def test_join(self):
193
        def test(expected, *args):
194
            joined = urlutils.join(*args)
195
            self.assertEqual(expected, joined)
196
197
        # Test a single element
198
        test('foo', 'foo')
199
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')
207
208
        # Absolute paths
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')
1711.2.49 by John Arbash Meinel
urlutils.join should work for root paths.
212
213
        # From a base path
214
        test('file:///foo', 'file:///', 'foo')
215
        test('file:///bar/foo', 'file:///bar/', 'foo')
216
        test('http://host/foo', 'http://host/', 'foo')
217
        test('http://host/', 'http://host', '')
1685.1.55 by John Arbash Meinel
Adding bzrlib.urlutils.join() to handle joining URLs
218
        
219
        # Invalid joinings
220
        # Cannot go above root
221
        self.assertRaises(InvalidURLJoin, urlutils.join,
222
                'http://foo', '../baz')
223
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
224
    def test_function_type(self):
225
        if sys.platform == 'win32':
226
            self.assertEqual(urlutils._win32_local_path_to_url, urlutils.local_path_to_url)
227
            self.assertEqual(urlutils._win32_local_path_from_url, urlutils.local_path_from_url)
228
        else:
229
            self.assertEqual(urlutils._posix_local_path_to_url, urlutils.local_path_to_url)
230
            self.assertEqual(urlutils._posix_local_path_from_url, urlutils.local_path_from_url)
231
232
    def test_posix_local_path_to_url(self):
233
        to_url = urlutils._posix_local_path_to_url
234
        self.assertEqual('file:///path/to/foo',
235
            to_url('/path/to/foo'))
1685.1.75 by Wouter van Heyst
more tests handle LANG=C
236
237
        try:
238
            result = to_url(u'/path/to/r\xe4ksm\xf6rg\xe5s')
239
        except UnicodeError:
240
            raise TestSkipped("local encoding cannot handle unicode")
241
242
        self.assertEqual('file:///path/to/r%C3%A4ksm%C3%B6rg%C3%A5s', result)
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
243
244
    def test_posix_local_path_from_url(self):
245
        from_url = urlutils._posix_local_path_from_url
246
        self.assertEqual('/path/to/foo',
247
            from_url('file:///path/to/foo'))
248
        self.assertEqual(u'/path/to/r\xe4ksm\xf6rg\xe5s',
249
            from_url('file:///path/to/r%C3%A4ksm%C3%B6rg%C3%A5s'))
250
        self.assertEqual(u'/path/to/r\xe4ksm\xf6rg\xe5s',
251
            from_url('file:///path/to/r%c3%a4ksm%c3%b6rg%c3%a5s'))
252
253
        self.assertRaises(InvalidURL, from_url, '/path/to/foo')
254
255
    def test_win32_local_path_to_url(self):
256
        to_url = urlutils._win32_local_path_to_url
1685.1.78 by Wouter van Heyst
more code cleanup
257
        self.assertEqual('file:///C:/path/to/foo',
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
258
            to_url('C:/path/to/foo'))
1685.1.75 by Wouter van Heyst
more tests handle LANG=C
259
260
        try:
261
            result = to_url(u'd:/path/to/r\xe4ksm\xf6rg\xe5s')
262
        except UnicodeError:
263
            raise TestSkipped("local encoding cannot handle unicode")
264
1685.1.78 by Wouter van Heyst
more code cleanup
265
        self.assertEqual('file:///D:/path/to/r%C3%A4ksm%C3%B6rg%C3%A5s', result)
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
266
267
    def test_win32_local_path_from_url(self):
268
        from_url = urlutils._win32_local_path_from_url
269
        self.assertEqual('C:/path/to/foo',
270
            from_url('file:///C|/path/to/foo'))
1685.1.49 by John Arbash Meinel
Added bzrlib.urlutils.split and basename + dirname
271
        self.assertEqual(u'D:/path/to/r\xe4ksm\xf6rg\xe5s',
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
272
            from_url('file:///d|/path/to/r%C3%A4ksm%C3%B6rg%C3%A5s'))
1685.1.49 by John Arbash Meinel
Added bzrlib.urlutils.split and basename + dirname
273
        self.assertEqual(u'D:/path/to/r\xe4ksm\xf6rg\xe5s',
1685.1.79 by Wouter van Heyst
cleanup urlutils
274
            from_url('file:///d:/path/to/r%c3%a4ksm%c3%b6rg%c3%a5s'))
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
275
276
        self.assertRaises(InvalidURL, from_url, '/path/to/foo')
277
        # Not a valid _win32 url, no drive letter
278
        self.assertRaises(InvalidURL, from_url, 'file:///path/to/foo')
279
1711.2.43 by John Arbash Meinel
Split out win32 specific code so that it can be tested on all platforms.
280
    def test__win32_extract_drive_letter(self):
281
        extract = urlutils._win32_extract_drive_letter
282
        self.assertEqual(('file:///C:', '/foo'), extract('file://', '/C:/foo'))
283
        self.assertEqual(('file:///d|', '/path'), extract('file://', '/d|/path'))
284
        self.assertRaises(InvalidURL, extract, 'file://', '/path')
285
1685.1.49 by John Arbash Meinel
Added bzrlib.urlutils.split and basename + dirname
286
    def test_split(self):
287
        # Test bzrlib.urlutils.split()
288
        split = urlutils.split
289
        if sys.platform == 'win32':
290
            self.assertRaises(InvalidURL, split, 'file:///path/to/foo')
291
            self.assertEqual(('file:///C|/', 'foo'), split('file:///C|/foo'))
1685.1.79 by Wouter van Heyst
cleanup urlutils
292
            self.assertEqual(('file:///C:/', ''), split('file:///C:/'))
1685.1.49 by John Arbash Meinel
Added bzrlib.urlutils.split and basename + dirname
293
        else:
294
            self.assertEqual(('file:///', 'foo'), split('file:///foo'))
295
            self.assertEqual(('file:///', ''), split('file:///'))
296
297
        self.assertEqual(('http://host/path/to', 'foo'), split('http://host/path/to/foo'))
298
        self.assertEqual(('http://host/path/to', 'foo'), split('http://host/path/to/foo/'))
299
        self.assertEqual(('http://host/path/to/foo', ''),
300
            split('http://host/path/to/foo/', exclude_trailing_slash=False))
301
        self.assertEqual(('http://host/', 'path'), split('http://host/path'))
302
        self.assertEqual(('http://host/', ''), split('http://host/'))
303
        self.assertEqual(('http://host', ''), split('http://host'))
304
        self.assertEqual(('http:///nohost', 'path'), split('http:///nohost/path'))
305
306
        self.assertEqual(('random+scheme://user:pass@ahost:port/', 'path'),
307
            split('random+scheme://user:pass@ahost:port/path'))
308
        self.assertEqual(('random+scheme://user:pass@ahost:port/', 'path'),
309
            split('random+scheme://user:pass@ahost:port/path/'))
310
        self.assertEqual(('random+scheme://user:pass@ahost:port/', ''),
311
            split('random+scheme://user:pass@ahost:port/'))
312
313
        # relative paths
314
        self.assertEqual(('path/to', 'foo'), split('path/to/foo'))
315
        self.assertEqual(('path/to', 'foo'), split('path/to/foo/'))
316
        self.assertEqual(('path/to/foo', ''),
317
            split('path/to/foo/', exclude_trailing_slash=False))
318
        self.assertEqual(('path/..', 'foo'), split('path/../foo'))
319
        self.assertEqual(('../path', 'foo'), split('../path/foo'))
320
1711.2.44 by John Arbash Meinel
Factor out another win32 special case and add platform independent tests for it.
321
    def test__win32_strip_local_trailing_slash(self):
322
        strip = urlutils._win32_strip_local_trailing_slash
323
        self.assertEqual('file://', strip('file://'))
324
        self.assertEqual('file:///', strip('file:///'))
325
        self.assertEqual('file:///C', strip('file:///C'))
326
        self.assertEqual('file:///C:', strip('file:///C:'))
327
        self.assertEqual('file:///d|', strip('file:///d|'))
328
        self.assertEqual('file:///C:/', strip('file:///C:/'))
329
        self.assertEqual('file:///C:/a', strip('file:///C:/a/'))
330
1685.1.48 by John Arbash Meinel
Updated strip_trailing_slash to support lots more url stuff, added tests
331
    def test_strip_trailing_slash(self):
332
        sts = urlutils.strip_trailing_slash
333
        if sys.platform == 'win32':
334
            self.assertEqual('file:///C|/', sts('file:///C|/'))
1685.1.79 by Wouter van Heyst
cleanup urlutils
335
            self.assertEqual('file:///C:/foo', sts('file:///C:/foo'))
1685.1.48 by John Arbash Meinel
Updated strip_trailing_slash to support lots more url stuff, added tests
336
            self.assertEqual('file:///C|/foo', sts('file:///C|/foo/'))
337
        else:
338
            self.assertEqual('file:///', sts('file:///'))
339
            self.assertEqual('file:///foo', sts('file:///foo'))
340
            self.assertEqual('file:///foo', sts('file:///foo/'))
341
342
        self.assertEqual('http://host/', sts('http://host/'))
343
        self.assertEqual('http://host/foo', sts('http://host/foo'))
344
        self.assertEqual('http://host/foo', sts('http://host/foo/'))
345
346
        # No need to fail just because the slash is missing
347
        self.assertEqual('http://host', sts('http://host'))
348
        # TODO: jam 20060502 Should this raise InvalidURL?
349
        self.assertEqual('file://', sts('file://'))
350
351
        self.assertEqual('random+scheme://user:pass@ahost:port/path',
352
            sts('random+scheme://user:pass@ahost:port/path'))
353
        self.assertEqual('random+scheme://user:pass@ahost:port/path',
354
            sts('random+scheme://user:pass@ahost:port/path/'))
355
        self.assertEqual('random+scheme://user:pass@ahost:port/',
356
            sts('random+scheme://user:pass@ahost:port/'))
357
358
        # Make sure relative paths work too
359
        self.assertEqual('path/to/foo', sts('path/to/foo'))
360
        self.assertEqual('path/to/foo', sts('path/to/foo/'))
361
        self.assertEqual('../to/foo', sts('../to/foo/'))
362
        self.assertEqual('path/../foo', sts('path/../foo/'))
363
1685.1.54 by John Arbash Meinel
url_for_display now makes sure output can be properly encoded.
364
    def test_unescape_for_display_utf8(self):
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
365
        # Test that URLs are converted to nice unicode strings for display
1685.1.54 by John Arbash Meinel
url_for_display now makes sure output can be properly encoded.
366
        def test(expected, url, encoding='utf-8'):
367
            disp_url = urlutils.unescape_for_display(url, encoding=encoding)
1685.1.58 by Martin Pool
urlutils.unescape_for_display should return Unicode
368
            self.assertIsInstance(disp_url, unicode)
1685.1.54 by John Arbash Meinel
url_for_display now makes sure output can be properly encoded.
369
            self.assertEqual(expected, disp_url)
1685.1.79 by Wouter van Heyst
cleanup urlutils
370
1685.1.54 by John Arbash Meinel
url_for_display now makes sure output can be properly encoded.
371
        test('http://foo', 'http://foo')
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
372
        if sys.platform == 'win32':
1685.1.79 by Wouter van Heyst
cleanup urlutils
373
            test('C:/foo/path', 'file:///C|/foo/path')
374
            test('C:/foo/path', 'file:///C:/foo/path')
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
375
        else:
1685.1.54 by John Arbash Meinel
url_for_display now makes sure output can be properly encoded.
376
            test('/foo/path', 'file:///foo/path')
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
377
1685.1.54 by John Arbash Meinel
url_for_display now makes sure output can be properly encoded.
378
        test('http://foo/%2Fbaz', 'http://foo/%2Fbaz')
1685.1.58 by Martin Pool
urlutils.unescape_for_display should return Unicode
379
        test(u'http://host/r\xe4ksm\xf6rg\xe5s',
380
             'http://host/r%C3%A4ksm%C3%B6rg%C3%A5s')
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
381
382
        # Make sure special escaped characters stay escaped
1685.1.54 by John Arbash Meinel
url_for_display now makes sure output can be properly encoded.
383
        test(u'http://host/%3B%2F%3F%3A%40%26%3D%2B%24%2C%23',
1685.1.58 by Martin Pool
urlutils.unescape_for_display should return Unicode
384
             'http://host/%3B%2F%3F%3A%40%26%3D%2B%24%2C%23')
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
385
386
        # Can we handle sections that don't have utf-8 encoding?
1685.1.58 by Martin Pool
urlutils.unescape_for_display should return Unicode
387
        test(u'http://host/%EE%EE%EE/r\xe4ksm\xf6rg\xe5s',
388
             'http://host/%EE%EE%EE/r%C3%A4ksm%C3%B6rg%C3%A5s')
1685.1.54 by John Arbash Meinel
url_for_display now makes sure output can be properly encoded.
389
390
        # Test encoding into output that can handle some characters
1685.1.58 by Martin Pool
urlutils.unescape_for_display should return Unicode
391
        test(u'http://host/%EE%EE%EE/r\xe4ksm\xf6rg\xe5s',
392
             'http://host/%EE%EE%EE/r%C3%A4ksm%C3%B6rg%C3%A5s',
393
             encoding='iso-8859-1')
394
395
        # This one can be encoded into utf8
396
        test(u'http://host/\u062c\u0648\u062c\u0648',
397
             'http://host/%d8%ac%d9%88%d8%ac%d9%88',
398
             encoding='utf-8')
399
400
        # This can't be put into 8859-1 and so stays as escapes
401
        test(u'http://host/%d8%ac%d9%88%d8%ac%d9%88',
402
             'http://host/%d8%ac%d9%88%d8%ac%d9%88',
403
             encoding='iso-8859-1')
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
404
405
    def test_escape(self):
406
        self.assertEqual('%25', urlutils.escape('%'))
407
        self.assertEqual('%C3%A5', urlutils.escape(u'\xe5'))
408
409
    def test_unescape(self):
410
        self.assertEqual('%', urlutils.unescape('%25'))
411
        self.assertEqual(u'\xe5', urlutils.unescape('%C3%A5'))
412
413
        self.assertRaises(InvalidURL, urlutils.unescape, u'\xe5')
414
        self.assertRaises(InvalidURL, urlutils.unescape, '\xe5')
415
        self.assertRaises(InvalidURL, urlutils.unescape, '%E5')
416
417
    def test_escape_unescape(self):
418
        self.assertEqual(u'\xe5', urlutils.unescape(urlutils.escape(u'\xe5')))
419
        self.assertEqual('%', urlutils.unescape(urlutils.escape('%')))
420
1685.1.70 by Wouter van Heyst
working on get_parent, set_parent and relative urls, broken
421
    def test_relative_url(self):
422
        def test(expected, base, other):
423
            result = urlutils.relative_url(base, other)
424
            self.assertEqual(expected, result)
425
            
426
        test('a', 'http://host/', 'http://host/a')
1685.1.71 by Wouter van Heyst
change branch.{get,set}_parent to store a relative path but return full urls
427
        test('http://entirely/different', 'sftp://host/branch',
428
                    'http://entirely/different')
429
        test('../person/feature', 'http://host/branch/mainline',
430
                    'http://host/branch/person/feature')
1685.1.70 by Wouter van Heyst
working on get_parent, set_parent and relative urls, broken
431
        test('..', 'http://host/branch', 'http://host/')
432
        test('http://host2/branch', 'http://host1/branch', 'http://host2/branch')
1685.1.71 by Wouter van Heyst
change branch.{get,set}_parent to store a relative path but return full urls
433
        test('.', 'http://host1/branch', 'http://host1/branch')
434
        test('../../../branch/2b', 'file:///home/jelmer/foo/bar/2b',
435
                    'file:///home/jelmer/branch/2b')
436
        test('../../branch/2b', 'sftp://host/home/jelmer/bar/2b',
437
                    'sftp://host/home/jelmer/branch/2b')
1685.1.79 by Wouter van Heyst
cleanup urlutils
438
        test('../../branch/feature/%2b', 'http://host/home/jelmer/bar/%2b',
439
                    'http://host/home/jelmer/branch/feature/%2b')
1685.1.71 by Wouter van Heyst
change branch.{get,set}_parent to store a relative path but return full urls
440
        test('../../branch/feature/2b', 'http://host/home/jelmer/bar/2b/', 
441
                    'http://host/home/jelmer/branch/feature/2b')
442
        # relative_url should preserve a trailing slash
443
        test('../../branch/feature/2b/', 'http://host/home/jelmer/bar/2b/',
444
                    'http://host/home/jelmer/branch/feature/2b/')
445
        test('../../branch/feature/2b/', 'http://host/home/jelmer/bar/2b',
446
                    'http://host/home/jelmer/branch/feature/2b/')
447
448
        # TODO: treat http://host as http://host/
449
        #       relative_url is typically called from a branch.base or
450
        #       transport.base which always ends with a /
451
        #test('a', 'http://host', 'http://host/a')
452
        test('http://host/a', 'http://host', 'http://host/a')
453
        #test('.', 'http://host', 'http://host/')
454
        test('http://host/', 'http://host', 'http://host/')
455
        #test('.', 'http://host/', 'http://host')
456
        test('http://host', 'http://host/', 'http://host')