diff options
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_urllib.py | 76 |
1 files changed, 58 insertions, 18 deletions
diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index cbe4327..cf57047 100644 --- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -11,10 +11,12 @@ from test import support import os import sys import tempfile +from nturl2path import url2pathname, pathname2url from base64 import b64encode import collections + def hexescape(char): """Escape char as RFC 2396 specifies""" hex_repr = hex(ord(char))[2:].upper() @@ -24,6 +26,8 @@ def hexescape(char): # Shortcut for testing FancyURLopener _urlopener = None + + def urlopen(url, data=None, proxies=None): """urlopen(url [, data]) -> open file-like object""" global _urlopener @@ -1363,6 +1367,7 @@ class URLopener_Tests(unittest.TestCase): # self.assertEqual(ftp.ftp.sock.gettimeout(), 30) # ftp.close() + class RequestTests(unittest.TestCase): """Unit tests for urllib.request.Request.""" @@ -1387,25 +1392,60 @@ class RequestTests(unittest.TestCase): self.assertEqual(request.get_method(), 'HEAD') -def test_main(): - support.run_unittest( - urlopen_FileTests, - urlopen_HttpTests, - urlopen_DataTests, - urlretrieve_FileTests, - urlretrieve_HttpTests, - ProxyTests, - QuotingTests, - UnquotingTests, - urlencode_Tests, - Pathname_Tests, - Utility_Tests, - URLopener_Tests, - #FTPWrapperTests, - RequestTests, - ) +class URL2PathNameTests(unittest.TestCase): + + def test_converting_drive_letter(self): + self.assertEqual(url2pathname("///C|"), 'C:') + self.assertEqual(url2pathname("///C:"), 'C:') + self.assertEqual(url2pathname("///C|/"), 'C:\\') + + def test_converting_when_no_drive_letter(self): + # cannot end a raw string in \ + self.assertEqual(url2pathname("///C/test/"), r'\\\C\test' '\\') + self.assertEqual(url2pathname("////C/test/"), r'\\C\test' '\\') + + def test_simple_compare(self): + self.assertEqual(url2pathname("///C|/foo/bar/spam.foo"), + r'C:\foo\bar\spam.foo') + + def test_non_ascii_drive_letter(self): + self.assertRaises(IOError, url2pathname, "///\u00e8|/") + + def test_roundtrip_url2pathname(self): + list_of_paths = ['C:', + r'\\\C\test\\', + r'C:\foo\bar\spam.foo' + ] + for path in list_of_paths: + self.assertEqual(url2pathname(pathname2url(path)), path) + +class PathName2URLTests(unittest.TestCase): + + def test_converting_drive_letter(self): + self.assertEqual(pathname2url("C:"), '///C:') + self.assertEqual(pathname2url("C:\\"), '///C:') + + def test_converting_when_no_drive_letter(self): + self.assertEqual(pathname2url(r"\\\folder\test" "\\"), + '/////folder/test/') + self.assertEqual(pathname2url(r"\\folder\test" "\\"), + '////folder/test/') + self.assertEqual(pathname2url(r"\folder\test" "\\"), + '/folder/test/') + + def test_simple_compare(self): + self.assertEqual(pathname2url(r'C:\foo\bar\spam.foo'), + "///C:/foo/bar/spam.foo" ) + def test_long_drive_letter(self): + self.assertRaises(IOError, pathname2url, "XX:\\") + def test_roundtrip_pathname2url(self): + list_of_paths = ['///C:', + '/////folder/test/', + '///C:/foo/bar/spam.foo'] + for path in list_of_paths: + self.assertEqual(pathname2url(url2pathname(path)), path) if __name__ == '__main__': - test_main() + unittest.main() |