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
24
import bzrlib.urlutils as urlutils
25
from bzrlib.tests import TestCaseInTempDir, TestCase
28
class TestUrlToPath(TestCase):
30
def test_function_type(self):
31
if sys.platform == 'win32':
32
self.assertEqual(urlutils._win32_local_path_to_url, urlutils.local_path_to_url)
33
self.assertEqual(urlutils._win32_local_path_from_url, urlutils.local_path_from_url)
35
self.assertEqual(urlutils._posix_local_path_to_url, urlutils.local_path_to_url)
36
self.assertEqual(urlutils._posix_local_path_from_url, urlutils.local_path_from_url)
38
def test_posix_local_path_to_url(self):
39
to_url = urlutils._posix_local_path_to_url
40
self.assertEqual('file:///path/to/foo',
41
to_url('/path/to/foo'))
42
self.assertEqual('file:///path/to/r%C3%A4ksm%C3%B6rg%C3%A5s',
43
to_url(u'/path/to/r\xe4ksm\xf6rg\xe5s'))
45
def test_posix_local_path_from_url(self):
46
from_url = urlutils._posix_local_path_from_url
47
self.assertEqual('/path/to/foo',
48
from_url('file:///path/to/foo'))
49
self.assertEqual(u'/path/to/r\xe4ksm\xf6rg\xe5s',
50
from_url('file:///path/to/r%C3%A4ksm%C3%B6rg%C3%A5s'))
51
self.assertEqual(u'/path/to/r\xe4ksm\xf6rg\xe5s',
52
from_url('file:///path/to/r%c3%a4ksm%c3%b6rg%c3%a5s'))
54
self.assertRaises(InvalidURL, from_url, '/path/to/foo')
56
def test_win32_local_path_to_url(self):
57
to_url = urlutils._win32_local_path_to_url
58
self.assertEqual('file:///C|/path/to/foo',
59
to_url('C:/path/to/foo'))
60
self.assertEqual('file:///d|/path/to/r%C3%A4ksm%C3%B6rg%C3%A5s',
61
to_url(u'd:/path/to/r\xe4ksm\xf6rg\xe5s'))
63
def test_win32_local_path_from_url(self):
64
from_url = urlutils._win32_local_path_from_url
65
self.assertEqual('C:/path/to/foo',
66
from_url('file:///C|/path/to/foo'))
67
self.assertEqual(u'd:/path/to/r\xe4ksm\xf6rg\xe5s',
68
from_url('file:///d|/path/to/r%C3%A4ksm%C3%B6rg%C3%A5s'))
69
self.assertEqual(u'd:/path/to/r\xe4ksm\xf6rg\xe5s',
70
from_url('file:///d|/path/to/r%c3%a4ksm%c3%b6rg%c3%a5s'))
72
self.assertRaises(InvalidURL, from_url, '/path/to/foo')
73
# Not a valid _win32 url, no drive letter
74
self.assertRaises(InvalidURL, from_url, 'file:///path/to/foo')
76
def test_unescape_for_display(self):
77
# Test that URLs are converted to nice unicode strings for display
78
disp = urlutils.unescape_for_display
80
eq('http://foo', disp('http://foo'))
81
if sys.platform == 'win32':
82
eq('C:/foo/path', disp('file:///C|foo/path'))
84
eq('/foo/path', disp('file:///foo/path'))
86
eq('http://foo/%2Fbaz', disp('http://foo/%2Fbaz'))
87
eq(u'http://host/r\xe4ksm\xf6rg\xe5s', disp('http://host/r%C3%A4ksm%C3%B6rg%C3%A5s'))
89
# Make sure special escaped characters stay escaped
90
eq(u'http://host/%3B%2F%3F%3A%40%26%3D%2B%24%2C%23',
91
disp('http://host/%3B%2F%3F%3A%40%26%3D%2B%24%2C%23'))
93
# Can we handle sections that don't have utf-8 encoding?
94
eq(u'http://host/%EE%EE%EE/r\xe4ksm\xf6rg\xe5s',
95
disp('http://host/%EE%EE%EE/r%C3%A4ksm%C3%B6rg%C3%A5s'))
97
def test_escape(self):
98
self.assertEqual('%25', urlutils.escape('%'))
99
self.assertEqual('%C3%A5', urlutils.escape(u'\xe5'))
101
def test_unescape(self):
102
self.assertEqual('%', urlutils.unescape('%25'))
103
self.assertEqual(u'\xe5', urlutils.unescape('%C3%A5'))
105
self.assertRaises(InvalidURL, urlutils.unescape, u'\xe5')
106
self.assertRaises(InvalidURL, urlutils.unescape, '\xe5')
107
self.assertRaises(InvalidURL, urlutils.unescape, '%E5')
109
def test_escape_unescape(self):
110
self.assertEqual(u'\xe5', urlutils.unescape(urlutils.escape(u'\xe5')))
111
self.assertEqual('%', urlutils.unescape(urlutils.escape('%')))