325
325
# Test that URLs are converted to nice unicode strings for display
326
326
def test(expected, url, encoding='utf-8'):
327
327
disp_url = urlutils.unescape_for_display(url, encoding=encoding)
328
self.assertIsInstance(disp_url, unicode)
328
329
self.assertEqual(expected, disp_url)
329
330
test('http://foo', 'http://foo')
330
331
if sys.platform == 'win32':
333
334
test('/foo/path', 'file:///foo/path')
335
336
test('http://foo/%2Fbaz', 'http://foo/%2Fbaz')
336
test(u'http://host/r\xe4ksm\xf6rg\xe5s'.encode('utf-8'),
337
'http://host/r%C3%A4ksm%C3%B6rg%C3%A5s')
337
test(u'http://host/r\xe4ksm\xf6rg\xe5s',
338
'http://host/r%C3%A4ksm%C3%B6rg%C3%A5s')
339
340
# Make sure special escaped characters stay escaped
340
341
test(u'http://host/%3B%2F%3F%3A%40%26%3D%2B%24%2C%23',
341
'http://host/%3B%2F%3F%3A%40%26%3D%2B%24%2C%23')
342
'http://host/%3B%2F%3F%3A%40%26%3D%2B%24%2C%23')
343
344
# Can we handle sections that don't have utf-8 encoding?
344
test('http://host/%EE%EE%EE/r\xc3\xa4ksm\xc3\xb6rg\xc3\xa5s',
345
'http://host/%EE%EE%EE/r%C3%A4ksm%C3%B6rg%C3%A5s')
345
test(u'http://host/%EE%EE%EE/r\xe4ksm\xf6rg\xe5s',
346
'http://host/%EE%EE%EE/r%C3%A4ksm%C3%B6rg%C3%A5s')
347
348
# Test encoding into output that can handle some characters
348
test('http://host/%EE%EE%EE/r\xe4ksm\xf6rg\xe5s',
349
'http://host/%EE%EE%EE/r%C3%A4ksm%C3%B6rg%C3%A5s',
350
encoding='iso-8859-1')
351
# Test utf-8 encoding, when some characters aren't handled
352
test('http://host/\xd8\xac\xd9\x88\xd8\xac\xd9\x88',
353
'http://host/%d8%ac%d9%88%d8%ac%d9%88',
355
test('http://host/%d8%ac%d9%88%d8%ac%d9%88',
356
'http://host/%d8%ac%d9%88%d8%ac%d9%88',
357
encoding='iso-8859-1')
349
test(u'http://host/%EE%EE%EE/r\xe4ksm\xf6rg\xe5s',
350
'http://host/%EE%EE%EE/r%C3%A4ksm%C3%B6rg%C3%A5s',
351
encoding='iso-8859-1')
353
# This one can be encoded into utf8
354
test(u'http://host/\u062c\u0648\u062c\u0648',
355
'http://host/%d8%ac%d9%88%d8%ac%d9%88',
358
# This can't be put into 8859-1 and so stays as escapes
359
test(u'http://host/%d8%ac%d9%88%d8%ac%d9%88',
360
'http://host/%d8%ac%d9%88%d8%ac%d9%88',
361
encoding='iso-8859-1')
359
363
def test_escape(self):
360
364
self.assertEqual('%25', urlutils.escape('%'))