test_urlparse.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. from test import test_support
  2. import unittest
  3. import urlparse
  4. RFC1808_BASE = "http://a/b/c/d;p?q#f"
  5. RFC2396_BASE = "http://a/b/c/d;p?q"
  6. RFC3986_BASE = 'http://a/b/c/d;p?q'
  7. SIMPLE_BASE = 'http://a/b/c/d'
  8. # A list of test cases. Each test case is a two-tuple that contains
  9. # a string with the query and a dictionary with the expected result.
  10. parse_qsl_test_cases = [
  11. ("", []),
  12. ("&", []),
  13. ("&&", []),
  14. ("=", [('', '')]),
  15. ("=a", [('', 'a')]),
  16. ("a", [('a', '')]),
  17. ("a=", [('a', '')]),
  18. ("a=", [('a', '')]),
  19. ("&a=b", [('a', 'b')]),
  20. ("a=a+b&b=b+c", [('a', 'a b'), ('b', 'b c')]),
  21. ("a=1&a=2", [('a', '1'), ('a', '2')]),
  22. (";", []),
  23. (";;", []),
  24. (";a=b", [('a', 'b')]),
  25. ("a=a+b;b=b+c", [('a', 'a b'), ('b', 'b c')]),
  26. ("a=1;a=2", [('a', '1'), ('a', '2')]),
  27. (b";", []),
  28. (b";;", []),
  29. (b";a=b", [(b'a', b'b')]),
  30. (b"a=a+b;b=b+c", [(b'a', b'a b'), (b'b', b'b c')]),
  31. (b"a=1;a=2", [(b'a', b'1'), (b'a', b'2')]),
  32. ]
  33. parse_qs_test_cases = [
  34. ("", {}),
  35. ("&", {}),
  36. ("&&", {}),
  37. ("=", {'': ['']}),
  38. ("=a", {'': ['a']}),
  39. ("a", {'a': ['']}),
  40. ("a=", {'a': ['']}),
  41. ("&a=b", {'a': ['b']}),
  42. ("a=a+b&b=b+c", {'a': ['a b'], 'b': ['b c']}),
  43. ("a=1&a=2", {'a': ['1', '2']}),
  44. (b"", {}),
  45. (b"&", {}),
  46. (b"&&", {}),
  47. (b"=", {b'': [b'']}),
  48. (b"=a", {b'': [b'a']}),
  49. (b"a", {b'a': [b'']}),
  50. (b"a=", {b'a': [b'']}),
  51. (b"&a=b", {b'a': [b'b']}),
  52. (b"a=a+b&b=b+c", {b'a': [b'a b'], b'b': [b'b c']}),
  53. (b"a=1&a=2", {b'a': [b'1', b'2']}),
  54. (";", {}),
  55. (";;", {}),
  56. (";a=b", {'a': ['b']}),
  57. ("a=a+b;b=b+c", {'a': ['a b'], 'b': ['b c']}),
  58. ("a=1;a=2", {'a': ['1', '2']}),
  59. (b";", {}),
  60. (b";;", {}),
  61. (b";a=b", {b'a': [b'b']}),
  62. (b"a=a+b;b=b+c", {b'a': [b'a b'], b'b': [b'b c']}),
  63. (b"a=1;a=2", {b'a': [b'1', b'2']}),
  64. ]
  65. class UrlParseTestCase(unittest.TestCase):
  66. def checkRoundtrips(self, url, parsed, split):
  67. result = urlparse.urlparse(url)
  68. self.assertEqual(result, parsed)
  69. t = (result.scheme, result.netloc, result.path,
  70. result.params, result.query, result.fragment)
  71. self.assertEqual(t, parsed)
  72. # put it back together and it should be the same
  73. result2 = urlparse.urlunparse(result)
  74. self.assertEqual(result2, url)
  75. self.assertEqual(result2, result.geturl())
  76. # the result of geturl() is a fixpoint; we can always parse it
  77. # again to get the same result:
  78. result3 = urlparse.urlparse(result.geturl())
  79. self.assertEqual(result3.geturl(), result.geturl())
  80. self.assertEqual(result3, result)
  81. self.assertEqual(result3.scheme, result.scheme)
  82. self.assertEqual(result3.netloc, result.netloc)
  83. self.assertEqual(result3.path, result.path)
  84. self.assertEqual(result3.params, result.params)
  85. self.assertEqual(result3.query, result.query)
  86. self.assertEqual(result3.fragment, result.fragment)
  87. self.assertEqual(result3.username, result.username)
  88. self.assertEqual(result3.password, result.password)
  89. self.assertEqual(result3.hostname, result.hostname)
  90. self.assertEqual(result3.port, result.port)
  91. # check the roundtrip using urlsplit() as well
  92. result = urlparse.urlsplit(url)
  93. self.assertEqual(result, split)
  94. t = (result.scheme, result.netloc, result.path,
  95. result.query, result.fragment)
  96. self.assertEqual(t, split)
  97. result2 = urlparse.urlunsplit(result)
  98. self.assertEqual(result2, url)
  99. self.assertEqual(result2, result.geturl())
  100. # check the fixpoint property of re-parsing the result of geturl()
  101. result3 = urlparse.urlsplit(result.geturl())
  102. self.assertEqual(result3.geturl(), result.geturl())
  103. self.assertEqual(result3, result)
  104. self.assertEqual(result3.scheme, result.scheme)
  105. self.assertEqual(result3.netloc, result.netloc)
  106. self.assertEqual(result3.path, result.path)
  107. self.assertEqual(result3.query, result.query)
  108. self.assertEqual(result3.fragment, result.fragment)
  109. self.assertEqual(result3.username, result.username)
  110. self.assertEqual(result3.password, result.password)
  111. self.assertEqual(result3.hostname, result.hostname)
  112. self.assertEqual(result3.port, result.port)
  113. def test_qsl(self):
  114. for orig, expect in parse_qsl_test_cases:
  115. result = urlparse.parse_qsl(orig, keep_blank_values=True)
  116. self.assertEqual(result, expect, "Error parsing %r" % orig)
  117. expect_without_blanks = [v for v in expect if len(v[1])]
  118. result = urlparse.parse_qsl(orig, keep_blank_values=False)
  119. self.assertEqual(result, expect_without_blanks,
  120. "Error parsing %r" % orig)
  121. def test_qs(self):
  122. for orig, expect in parse_qs_test_cases:
  123. result = urlparse.parse_qs(orig, keep_blank_values=True)
  124. self.assertEqual(result, expect, "Error parsing %r" % orig)
  125. expect_without_blanks = dict(
  126. [(v, expect[v]) for v in expect if len(expect[v][0])])
  127. result = urlparse.parse_qs(orig, keep_blank_values=False)
  128. self.assertEqual(result, expect_without_blanks,
  129. "Error parsing %r" % orig)
  130. def test_roundtrips(self):
  131. testcases = [
  132. ('file:///tmp/junk.txt',
  133. ('file', '', '/tmp/junk.txt', '', '', ''),
  134. ('file', '', '/tmp/junk.txt', '', '')),
  135. ('imap://mail.python.org/mbox1',
  136. ('imap', 'mail.python.org', '/mbox1', '', '', ''),
  137. ('imap', 'mail.python.org', '/mbox1', '', '')),
  138. ('mms://wms.sys.hinet.net/cts/Drama/09006251100.asf',
  139. ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf',
  140. '', '', ''),
  141. ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf',
  142. '', '')),
  143. ('nfs://server/path/to/file.txt',
  144. ('nfs', 'server', '/path/to/file.txt', '', '', ''),
  145. ('nfs', 'server', '/path/to/file.txt', '', '')),
  146. ('svn+ssh://svn.zope.org/repos/main/ZConfig/trunk/',
  147. ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/',
  148. '', '', ''),
  149. ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/',
  150. '', '')),
  151. ('git+ssh://git@github.com/user/project.git',
  152. ('git+ssh', 'git@github.com','/user/project.git',
  153. '','',''),
  154. ('git+ssh', 'git@github.com','/user/project.git',
  155. '', ''))
  156. ]
  157. for url, parsed, split in testcases:
  158. self.checkRoundtrips(url, parsed, split)
  159. def test_http_roundtrips(self):
  160. # urlparse.urlsplit treats 'http:' as an optimized special case,
  161. # so we test both 'http:' and 'https:' in all the following.
  162. # Three cheers for white box knowledge!
  163. testcases = [
  164. ('://www.python.org',
  165. ('www.python.org', '', '', '', ''),
  166. ('www.python.org', '', '', '')),
  167. ('://www.python.org#abc',
  168. ('www.python.org', '', '', '', 'abc'),
  169. ('www.python.org', '', '', 'abc')),
  170. ('://www.python.org?q=abc',
  171. ('www.python.org', '', '', 'q=abc', ''),
  172. ('www.python.org', '', 'q=abc', '')),
  173. ('://www.python.org/#abc',
  174. ('www.python.org', '/', '', '', 'abc'),
  175. ('www.python.org', '/', '', 'abc')),
  176. ('://a/b/c/d;p?q#f',
  177. ('a', '/b/c/d', 'p', 'q', 'f'),
  178. ('a', '/b/c/d;p', 'q', 'f')),
  179. ]
  180. for scheme in ('http', 'https'):
  181. for url, parsed, split in testcases:
  182. url = scheme + url
  183. parsed = (scheme,) + parsed
  184. split = (scheme,) + split
  185. self.checkRoundtrips(url, parsed, split)
  186. def checkJoin(self, base, relurl, expected):
  187. self.assertEqual(urlparse.urljoin(base, relurl), expected,
  188. (base, relurl, expected))
  189. def test_unparse_parse(self):
  190. for u in ['Python', './Python','x-newscheme://foo.com/stuff','x://y','x:/y','x:/','/',]:
  191. self.assertEqual(urlparse.urlunsplit(urlparse.urlsplit(u)), u)
  192. self.assertEqual(urlparse.urlunparse(urlparse.urlparse(u)), u)
  193. def test_RFC1808(self):
  194. # "normal" cases from RFC 1808:
  195. self.checkJoin(RFC1808_BASE, 'g:h', 'g:h')
  196. self.checkJoin(RFC1808_BASE, 'g', 'http://a/b/c/g')
  197. self.checkJoin(RFC1808_BASE, './g', 'http://a/b/c/g')
  198. self.checkJoin(RFC1808_BASE, 'g/', 'http://a/b/c/g/')
  199. self.checkJoin(RFC1808_BASE, '/g', 'http://a/g')
  200. self.checkJoin(RFC1808_BASE, '//g', 'http://g')
  201. self.checkJoin(RFC1808_BASE, 'g?y', 'http://a/b/c/g?y')
  202. self.checkJoin(RFC1808_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
  203. self.checkJoin(RFC1808_BASE, '#s', 'http://a/b/c/d;p?q#s')
  204. self.checkJoin(RFC1808_BASE, 'g#s', 'http://a/b/c/g#s')
  205. self.checkJoin(RFC1808_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
  206. self.checkJoin(RFC1808_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
  207. self.checkJoin(RFC1808_BASE, 'g;x', 'http://a/b/c/g;x')
  208. self.checkJoin(RFC1808_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
  209. self.checkJoin(RFC1808_BASE, '.', 'http://a/b/c/')
  210. self.checkJoin(RFC1808_BASE, './', 'http://a/b/c/')
  211. self.checkJoin(RFC1808_BASE, '..', 'http://a/b/')
  212. self.checkJoin(RFC1808_BASE, '../', 'http://a/b/')
  213. self.checkJoin(RFC1808_BASE, '../g', 'http://a/b/g')
  214. self.checkJoin(RFC1808_BASE, '../..', 'http://a/')
  215. self.checkJoin(RFC1808_BASE, '../../', 'http://a/')
  216. self.checkJoin(RFC1808_BASE, '../../g', 'http://a/g')
  217. # "abnormal" cases from RFC 1808:
  218. self.checkJoin(RFC1808_BASE, '', 'http://a/b/c/d;p?q#f')
  219. self.checkJoin(RFC1808_BASE, '../../../g', 'http://a/../g')
  220. self.checkJoin(RFC1808_BASE, '../../../../g', 'http://a/../../g')
  221. self.checkJoin(RFC1808_BASE, '/./g', 'http://a/./g')
  222. self.checkJoin(RFC1808_BASE, '/../g', 'http://a/../g')
  223. self.checkJoin(RFC1808_BASE, 'g.', 'http://a/b/c/g.')
  224. self.checkJoin(RFC1808_BASE, '.g', 'http://a/b/c/.g')
  225. self.checkJoin(RFC1808_BASE, 'g..', 'http://a/b/c/g..')
  226. self.checkJoin(RFC1808_BASE, '..g', 'http://a/b/c/..g')
  227. self.checkJoin(RFC1808_BASE, './../g', 'http://a/b/g')
  228. self.checkJoin(RFC1808_BASE, './g/.', 'http://a/b/c/g/')
  229. self.checkJoin(RFC1808_BASE, 'g/./h', 'http://a/b/c/g/h')
  230. self.checkJoin(RFC1808_BASE, 'g/../h', 'http://a/b/c/h')
  231. # RFC 1808 and RFC 1630 disagree on these (according to RFC 1808),
  232. # so we'll not actually run these tests (which expect 1808 behavior).
  233. #self.checkJoin(RFC1808_BASE, 'http:g', 'http:g')
  234. #self.checkJoin(RFC1808_BASE, 'http:', 'http:')
  235. def test_RFC2368(self):
  236. # Issue 11467: path that starts with a number is not parsed correctly
  237. self.assertEqual(urlparse.urlparse('mailto:1337@example.org'),
  238. ('mailto', '', '1337@example.org', '', '', ''))
  239. def test_RFC2396(self):
  240. # cases from RFC 2396
  241. self.checkJoin(RFC2396_BASE, 'g:h', 'g:h')
  242. self.checkJoin(RFC2396_BASE, 'g', 'http://a/b/c/g')
  243. self.checkJoin(RFC2396_BASE, './g', 'http://a/b/c/g')
  244. self.checkJoin(RFC2396_BASE, 'g/', 'http://a/b/c/g/')
  245. self.checkJoin(RFC2396_BASE, '/g', 'http://a/g')
  246. self.checkJoin(RFC2396_BASE, '//g', 'http://g')
  247. self.checkJoin(RFC2396_BASE, 'g?y', 'http://a/b/c/g?y')
  248. self.checkJoin(RFC2396_BASE, '#s', 'http://a/b/c/d;p?q#s')
  249. self.checkJoin(RFC2396_BASE, 'g#s', 'http://a/b/c/g#s')
  250. self.checkJoin(RFC2396_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
  251. self.checkJoin(RFC2396_BASE, 'g;x', 'http://a/b/c/g;x')
  252. self.checkJoin(RFC2396_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
  253. self.checkJoin(RFC2396_BASE, '.', 'http://a/b/c/')
  254. self.checkJoin(RFC2396_BASE, './', 'http://a/b/c/')
  255. self.checkJoin(RFC2396_BASE, '..', 'http://a/b/')
  256. self.checkJoin(RFC2396_BASE, '../', 'http://a/b/')
  257. self.checkJoin(RFC2396_BASE, '../g', 'http://a/b/g')
  258. self.checkJoin(RFC2396_BASE, '../..', 'http://a/')
  259. self.checkJoin(RFC2396_BASE, '../../', 'http://a/')
  260. self.checkJoin(RFC2396_BASE, '../../g', 'http://a/g')
  261. self.checkJoin(RFC2396_BASE, '', RFC2396_BASE)
  262. self.checkJoin(RFC2396_BASE, '../../../g', 'http://a/../g')
  263. self.checkJoin(RFC2396_BASE, '../../../../g', 'http://a/../../g')
  264. self.checkJoin(RFC2396_BASE, '/./g', 'http://a/./g')
  265. self.checkJoin(RFC2396_BASE, '/../g', 'http://a/../g')
  266. self.checkJoin(RFC2396_BASE, 'g.', 'http://a/b/c/g.')
  267. self.checkJoin(RFC2396_BASE, '.g', 'http://a/b/c/.g')
  268. self.checkJoin(RFC2396_BASE, 'g..', 'http://a/b/c/g..')
  269. self.checkJoin(RFC2396_BASE, '..g', 'http://a/b/c/..g')
  270. self.checkJoin(RFC2396_BASE, './../g', 'http://a/b/g')
  271. self.checkJoin(RFC2396_BASE, './g/.', 'http://a/b/c/g/')
  272. self.checkJoin(RFC2396_BASE, 'g/./h', 'http://a/b/c/g/h')
  273. self.checkJoin(RFC2396_BASE, 'g/../h', 'http://a/b/c/h')
  274. self.checkJoin(RFC2396_BASE, 'g;x=1/./y', 'http://a/b/c/g;x=1/y')
  275. self.checkJoin(RFC2396_BASE, 'g;x=1/../y', 'http://a/b/c/y')
  276. self.checkJoin(RFC2396_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
  277. self.checkJoin(RFC2396_BASE, 'g?y/../x', 'http://a/b/c/g?y/../x')
  278. self.checkJoin(RFC2396_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
  279. self.checkJoin(RFC2396_BASE, 'g#s/../x', 'http://a/b/c/g#s/../x')
  280. def test_RFC3986(self):
  281. # Test cases from RFC3986
  282. self.checkJoin(RFC3986_BASE, '?y','http://a/b/c/d;p?y')
  283. self.checkJoin(RFC2396_BASE, ';x', 'http://a/b/c/;x')
  284. self.checkJoin(RFC3986_BASE, 'g:h','g:h')
  285. self.checkJoin(RFC3986_BASE, 'g','http://a/b/c/g')
  286. self.checkJoin(RFC3986_BASE, './g','http://a/b/c/g')
  287. self.checkJoin(RFC3986_BASE, 'g/','http://a/b/c/g/')
  288. self.checkJoin(RFC3986_BASE, '/g','http://a/g')
  289. self.checkJoin(RFC3986_BASE, '//g','http://g')
  290. self.checkJoin(RFC3986_BASE, '?y','http://a/b/c/d;p?y')
  291. self.checkJoin(RFC3986_BASE, 'g?y','http://a/b/c/g?y')
  292. self.checkJoin(RFC3986_BASE, '#s','http://a/b/c/d;p?q#s')
  293. self.checkJoin(RFC3986_BASE, 'g#s','http://a/b/c/g#s')
  294. self.checkJoin(RFC3986_BASE, 'g?y#s','http://a/b/c/g?y#s')
  295. self.checkJoin(RFC3986_BASE, ';x','http://a/b/c/;x')
  296. self.checkJoin(RFC3986_BASE, 'g;x','http://a/b/c/g;x')
  297. self.checkJoin(RFC3986_BASE, 'g;x?y#s','http://a/b/c/g;x?y#s')
  298. self.checkJoin(RFC3986_BASE, '','http://a/b/c/d;p?q')
  299. self.checkJoin(RFC3986_BASE, '.','http://a/b/c/')
  300. self.checkJoin(RFC3986_BASE, './','http://a/b/c/')
  301. self.checkJoin(RFC3986_BASE, '..','http://a/b/')
  302. self.checkJoin(RFC3986_BASE, '../','http://a/b/')
  303. self.checkJoin(RFC3986_BASE, '../g','http://a/b/g')
  304. self.checkJoin(RFC3986_BASE, '../..','http://a/')
  305. self.checkJoin(RFC3986_BASE, '../../','http://a/')
  306. self.checkJoin(RFC3986_BASE, '../../g','http://a/g')
  307. #Abnormal Examples
  308. # The 'abnormal scenarios' are incompatible with RFC2986 parsing
  309. # Tests are here for reference.
  310. #self.checkJoin(RFC3986_BASE, '../../../g','http://a/g')
  311. #self.checkJoin(RFC3986_BASE, '../../../../g','http://a/g')
  312. #self.checkJoin(RFC3986_BASE, '/./g','http://a/g')
  313. #self.checkJoin(RFC3986_BASE, '/../g','http://a/g')
  314. self.checkJoin(RFC3986_BASE, 'g.','http://a/b/c/g.')
  315. self.checkJoin(RFC3986_BASE, '.g','http://a/b/c/.g')
  316. self.checkJoin(RFC3986_BASE, 'g..','http://a/b/c/g..')
  317. self.checkJoin(RFC3986_BASE, '..g','http://a/b/c/..g')
  318. self.checkJoin(RFC3986_BASE, './../g','http://a/b/g')
  319. self.checkJoin(RFC3986_BASE, './g/.','http://a/b/c/g/')
  320. self.checkJoin(RFC3986_BASE, 'g/./h','http://a/b/c/g/h')
  321. self.checkJoin(RFC3986_BASE, 'g/../h','http://a/b/c/h')
  322. self.checkJoin(RFC3986_BASE, 'g;x=1/./y','http://a/b/c/g;x=1/y')
  323. self.checkJoin(RFC3986_BASE, 'g;x=1/../y','http://a/b/c/y')
  324. self.checkJoin(RFC3986_BASE, 'g?y/./x','http://a/b/c/g?y/./x')
  325. self.checkJoin(RFC3986_BASE, 'g?y/../x','http://a/b/c/g?y/../x')
  326. self.checkJoin(RFC3986_BASE, 'g#s/./x','http://a/b/c/g#s/./x')
  327. self.checkJoin(RFC3986_BASE, 'g#s/../x','http://a/b/c/g#s/../x')
  328. #self.checkJoin(RFC3986_BASE, 'http:g','http:g') # strict parser
  329. self.checkJoin(RFC3986_BASE, 'http:g','http://a/b/c/g') # relaxed parser
  330. # Test for issue9721
  331. self.checkJoin('http://a/b/c/de', ';x','http://a/b/c/;x')
  332. def test_urljoins(self):
  333. self.checkJoin(SIMPLE_BASE, 'g:h','g:h')
  334. self.checkJoin(SIMPLE_BASE, 'http:g','http://a/b/c/g')
  335. self.checkJoin(SIMPLE_BASE, 'http:','http://a/b/c/d')
  336. self.checkJoin(SIMPLE_BASE, 'g','http://a/b/c/g')
  337. self.checkJoin(SIMPLE_BASE, './g','http://a/b/c/g')
  338. self.checkJoin(SIMPLE_BASE, 'g/','http://a/b/c/g/')
  339. self.checkJoin(SIMPLE_BASE, '/g','http://a/g')
  340. self.checkJoin(SIMPLE_BASE, '//g','http://g')
  341. self.checkJoin(SIMPLE_BASE, '?y','http://a/b/c/d?y')
  342. self.checkJoin(SIMPLE_BASE, 'g?y','http://a/b/c/g?y')
  343. self.checkJoin(SIMPLE_BASE, 'g?y/./x','http://a/b/c/g?y/./x')
  344. self.checkJoin(SIMPLE_BASE, '.','http://a/b/c/')
  345. self.checkJoin(SIMPLE_BASE, './','http://a/b/c/')
  346. self.checkJoin(SIMPLE_BASE, '..','http://a/b/')
  347. self.checkJoin(SIMPLE_BASE, '../','http://a/b/')
  348. self.checkJoin(SIMPLE_BASE, '../g','http://a/b/g')
  349. self.checkJoin(SIMPLE_BASE, '../..','http://a/')
  350. self.checkJoin(SIMPLE_BASE, '../../g','http://a/g')
  351. self.checkJoin(SIMPLE_BASE, '../../../g','http://a/../g')
  352. self.checkJoin(SIMPLE_BASE, './../g','http://a/b/g')
  353. self.checkJoin(SIMPLE_BASE, './g/.','http://a/b/c/g/')
  354. self.checkJoin(SIMPLE_BASE, '/./g','http://a/./g')
  355. self.checkJoin(SIMPLE_BASE, 'g/./h','http://a/b/c/g/h')
  356. self.checkJoin(SIMPLE_BASE, 'g/../h','http://a/b/c/h')
  357. self.checkJoin(SIMPLE_BASE, 'http:g','http://a/b/c/g')
  358. self.checkJoin(SIMPLE_BASE, 'http:','http://a/b/c/d')
  359. self.checkJoin(SIMPLE_BASE, 'http:?y','http://a/b/c/d?y')
  360. self.checkJoin(SIMPLE_BASE, 'http:g?y','http://a/b/c/g?y')
  361. self.checkJoin(SIMPLE_BASE, 'http:g?y/./x','http://a/b/c/g?y/./x')
  362. self.checkJoin('http:///', '..','http:///')
  363. self.checkJoin('', 'http://a/b/c/g?y/./x','http://a/b/c/g?y/./x')
  364. self.checkJoin('', 'http://a/./g', 'http://a/./g')
  365. self.checkJoin('svn://pathtorepo/dir1','dir2','svn://pathtorepo/dir2')
  366. self.checkJoin('svn+ssh://pathtorepo/dir1','dir2','svn+ssh://pathtorepo/dir2')
  367. def test_RFC2732(self):
  368. for url, hostname, port in [
  369. ('http://Test.python.org:5432/foo/', 'test.python.org', 5432),
  370. ('http://12.34.56.78:5432/foo/', '12.34.56.78', 5432),
  371. ('http://[::1]:5432/foo/', '::1', 5432),
  372. ('http://[dead:beef::1]:5432/foo/', 'dead:beef::1', 5432),
  373. ('http://[dead:beef::]:5432/foo/', 'dead:beef::', 5432),
  374. ('http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]:5432/foo/',
  375. 'dead:beef:cafe:5417:affe:8fa3:deaf:feed', 5432),
  376. ('http://[::12.34.56.78]:5432/foo/', '::12.34.56.78', 5432),
  377. ('http://[::ffff:12.34.56.78]:5432/foo/',
  378. '::ffff:12.34.56.78', 5432),
  379. ('http://Test.python.org/foo/', 'test.python.org', None),
  380. ('http://12.34.56.78/foo/', '12.34.56.78', None),
  381. ('http://[::1]/foo/', '::1', None),
  382. ('http://[dead:beef::1]/foo/', 'dead:beef::1', None),
  383. ('http://[dead:beef::]/foo/', 'dead:beef::', None),
  384. ('http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]/foo/',
  385. 'dead:beef:cafe:5417:affe:8fa3:deaf:feed', None),
  386. ('http://[::12.34.56.78]/foo/', '::12.34.56.78', None),
  387. ('http://[::ffff:12.34.56.78]/foo/',
  388. '::ffff:12.34.56.78', None),
  389. ('http://Test.python.org:/foo/', 'test.python.org', None),
  390. ('http://12.34.56.78:/foo/', '12.34.56.78', None),
  391. ('http://[::1]:/foo/', '::1', None),
  392. ('http://[dead:beef::1]:/foo/', 'dead:beef::1', None),
  393. ('http://[dead:beef::]:/foo/', 'dead:beef::', None),
  394. ('http://[dead:beef:cafe:5417:affe:8FA3:deaf:feed]:/foo/',
  395. 'dead:beef:cafe:5417:affe:8fa3:deaf:feed', None),
  396. ('http://[::12.34.56.78]:/foo/', '::12.34.56.78', None),
  397. ('http://[::ffff:12.34.56.78]:/foo/',
  398. '::ffff:12.34.56.78', None),
  399. ]:
  400. urlparsed = urlparse.urlparse(url)
  401. self.assertEqual((urlparsed.hostname, urlparsed.port) , (hostname, port))
  402. for invalid_url in [
  403. 'http://::12.34.56.78]/',
  404. 'http://[::1/foo/',
  405. 'ftp://[::1/foo/bad]/bad',
  406. 'http://[::1/foo/bad]/bad',
  407. 'http://[::ffff:12.34.56.78']:
  408. self.assertRaises(ValueError, urlparse.urlparse, invalid_url)
  409. def test_urldefrag(self):
  410. for url, defrag, frag in [
  411. ('http://python.org#frag', 'http://python.org', 'frag'),
  412. ('http://python.org', 'http://python.org', ''),
  413. ('http://python.org/#frag', 'http://python.org/', 'frag'),
  414. ('http://python.org/', 'http://python.org/', ''),
  415. ('http://python.org/?q#frag', 'http://python.org/?q', 'frag'),
  416. ('http://python.org/?q', 'http://python.org/?q', ''),
  417. ('http://python.org/p#frag', 'http://python.org/p', 'frag'),
  418. ('http://python.org/p?q', 'http://python.org/p?q', ''),
  419. (RFC1808_BASE, 'http://a/b/c/d;p?q', 'f'),
  420. (RFC2396_BASE, 'http://a/b/c/d;p?q', ''),
  421. ]:
  422. self.assertEqual(urlparse.urldefrag(url), (defrag, frag))
  423. def test_urlsplit_attributes(self):
  424. url = "HTTP://WWW.PYTHON.ORG/doc/#frag"
  425. p = urlparse.urlsplit(url)
  426. self.assertEqual(p.scheme, "http")
  427. self.assertEqual(p.netloc, "WWW.PYTHON.ORG")
  428. self.assertEqual(p.path, "/doc/")
  429. self.assertEqual(p.query, "")
  430. self.assertEqual(p.fragment, "frag")
  431. self.assertEqual(p.username, None)
  432. self.assertEqual(p.password, None)
  433. self.assertEqual(p.hostname, "www.python.org")
  434. self.assertEqual(p.port, None)
  435. # geturl() won't return exactly the original URL in this case
  436. # since the scheme is always case-normalized
  437. #self.assertEqual(p.geturl(), url)
  438. url = "http://User:Pass@www.python.org:080/doc/?query=yes#frag"
  439. p = urlparse.urlsplit(url)
  440. self.assertEqual(p.scheme, "http")
  441. self.assertEqual(p.netloc, "User:Pass@www.python.org:080")
  442. self.assertEqual(p.path, "/doc/")
  443. self.assertEqual(p.query, "query=yes")
  444. self.assertEqual(p.fragment, "frag")
  445. self.assertEqual(p.username, "User")
  446. self.assertEqual(p.password, "Pass")
  447. self.assertEqual(p.hostname, "www.python.org")
  448. self.assertEqual(p.port, 80)
  449. self.assertEqual(p.geturl(), url)
  450. # Addressing issue1698, which suggests Username can contain
  451. # "@" characters. Though not RFC compliant, many ftp sites allow
  452. # and request email addresses as usernames.
  453. url = "http://User@example.com:Pass@www.python.org:080/doc/?query=yes#frag"
  454. p = urlparse.urlsplit(url)
  455. self.assertEqual(p.scheme, "http")
  456. self.assertEqual(p.netloc, "User@example.com:Pass@www.python.org:080")
  457. self.assertEqual(p.path, "/doc/")
  458. self.assertEqual(p.query, "query=yes")
  459. self.assertEqual(p.fragment, "frag")
  460. self.assertEqual(p.username, "User@example.com")
  461. self.assertEqual(p.password, "Pass")
  462. self.assertEqual(p.hostname, "www.python.org")
  463. self.assertEqual(p.port, 80)
  464. self.assertEqual(p.geturl(), url)
  465. # Verify an illegal port of value greater than 65535 is set as None
  466. url = "http://www.python.org:65536"
  467. p = urlparse.urlsplit(url)
  468. self.assertEqual(p.port, None)
  469. def test_issue14072(self):
  470. p1 = urlparse.urlsplit('tel:+31-641044153')
  471. self.assertEqual(p1.scheme, 'tel')
  472. self.assertEqual(p1.path, '+31-641044153')
  473. p2 = urlparse.urlsplit('tel:+31641044153')
  474. self.assertEqual(p2.scheme, 'tel')
  475. self.assertEqual(p2.path, '+31641044153')
  476. # Assert for urlparse
  477. p1 = urlparse.urlparse('tel:+31-641044153')
  478. self.assertEqual(p1.scheme, 'tel')
  479. self.assertEqual(p1.path, '+31-641044153')
  480. p2 = urlparse.urlparse('tel:+31641044153')
  481. self.assertEqual(p2.scheme, 'tel')
  482. self.assertEqual(p2.path, '+31641044153')
  483. def test_telurl_params(self):
  484. p1 = urlparse.urlparse('tel:123-4;phone-context=+1-650-516')
  485. self.assertEqual(p1.scheme, 'tel')
  486. self.assertEqual(p1.path, '123-4')
  487. self.assertEqual(p1.params, 'phone-context=+1-650-516')
  488. p1 = urlparse.urlparse('tel:+1-201-555-0123')
  489. self.assertEqual(p1.scheme, 'tel')
  490. self.assertEqual(p1.path, '+1-201-555-0123')
  491. self.assertEqual(p1.params, '')
  492. p1 = urlparse.urlparse('tel:7042;phone-context=example.com')
  493. self.assertEqual(p1.scheme, 'tel')
  494. self.assertEqual(p1.path, '7042')
  495. self.assertEqual(p1.params, 'phone-context=example.com')
  496. p1 = urlparse.urlparse('tel:863-1234;phone-context=+1-914-555')
  497. self.assertEqual(p1.scheme, 'tel')
  498. self.assertEqual(p1.path, '863-1234')
  499. self.assertEqual(p1.params, 'phone-context=+1-914-555')
  500. def test_attributes_bad_port(self):
  501. """Check handling of non-integer ports."""
  502. p = urlparse.urlsplit("http://www.example.net:foo")
  503. self.assertEqual(p.netloc, "www.example.net:foo")
  504. self.assertRaises(ValueError, lambda: p.port)
  505. p = urlparse.urlparse("http://www.example.net:foo")
  506. self.assertEqual(p.netloc, "www.example.net:foo")
  507. self.assertRaises(ValueError, lambda: p.port)
  508. def test_attributes_without_netloc(self):
  509. # This example is straight from RFC 3261. It looks like it
  510. # should allow the username, hostname, and port to be filled
  511. # in, but doesn't. Since it's a URI and doesn't use the
  512. # scheme://netloc syntax, the netloc and related attributes
  513. # should be left empty.
  514. uri = "sip:alice@atlanta.com;maddr=239.255.255.1;ttl=15"
  515. p = urlparse.urlsplit(uri)
  516. self.assertEqual(p.netloc, "")
  517. self.assertEqual(p.username, None)
  518. self.assertEqual(p.password, None)
  519. self.assertEqual(p.hostname, None)
  520. self.assertEqual(p.port, None)
  521. self.assertEqual(p.geturl(), uri)
  522. p = urlparse.urlparse(uri)
  523. self.assertEqual(p.netloc, "")
  524. self.assertEqual(p.username, None)
  525. self.assertEqual(p.password, None)
  526. self.assertEqual(p.hostname, None)
  527. self.assertEqual(p.port, None)
  528. self.assertEqual(p.geturl(), uri)
  529. def test_caching(self):
  530. # Test case for bug #1313119
  531. uri = "http://example.com/doc/"
  532. unicode_uri = unicode(uri)
  533. urlparse.urlparse(unicode_uri)
  534. p = urlparse.urlparse(uri)
  535. self.assertEqual(type(p.scheme), type(uri))
  536. self.assertEqual(type(p.hostname), type(uri))
  537. self.assertEqual(type(p.path), type(uri))
  538. def test_noslash(self):
  539. # Issue 1637: http://foo.com?query is legal
  540. self.assertEqual(urlparse.urlparse("http://example.com?blahblah=/foo"),
  541. ('http', 'example.com', '', '', 'blahblah=/foo', ''))
  542. def test_anyscheme(self):
  543. # Issue 7904: s3://foo.com/stuff has netloc "foo.com".
  544. self.assertEqual(urlparse.urlparse("s3://foo.com/stuff"),
  545. ('s3','foo.com','/stuff','','',''))
  546. self.assertEqual(urlparse.urlparse("x-newscheme://foo.com/stuff"),
  547. ('x-newscheme','foo.com','/stuff','','',''))
  548. self.assertEqual(urlparse.urlparse("x-newscheme://foo.com/stuff?query#fragment"),
  549. ('x-newscheme','foo.com','/stuff','','query','fragment'))
  550. self.assertEqual(urlparse.urlparse("x-newscheme://foo.com/stuff?query"),
  551. ('x-newscheme','foo.com','/stuff','','query',''))
  552. def test_withoutscheme(self):
  553. # Test urlparse without scheme
  554. # Issue 754016: urlparse goes wrong with IP:port without scheme
  555. # RFC 1808 specifies that netloc should start with //, urlparse expects
  556. # the same, otherwise it classifies the portion of url as path.
  557. self.assertEqual(urlparse.urlparse("path"),
  558. ('','','path','','',''))
  559. self.assertEqual(urlparse.urlparse("//www.python.org:80"),
  560. ('','www.python.org:80','','','',''))
  561. self.assertEqual(urlparse.urlparse("http://www.python.org:80"),
  562. ('http','www.python.org:80','','','',''))
  563. def test_portseparator(self):
  564. # Issue 754016 makes changes for port separator ':' from scheme separator
  565. self.assertEqual(urlparse.urlparse("path:80"),
  566. ('','','path:80','','',''))
  567. self.assertEqual(urlparse.urlparse("http:"),('http','','','','',''))
  568. self.assertEqual(urlparse.urlparse("https:"),('https','','','','',''))
  569. self.assertEqual(urlparse.urlparse("http://www.python.org:80"),
  570. ('http','www.python.org:80','','','',''))
  571. def test_main():
  572. test_support.run_unittest(UrlParseTestCase)
  573. if __name__ == "__main__":
  574. test_main()