/Lib/test/test_urlparse.py

http://unladen-swallow.googlecode.com/ · Python · 357 lines · 285 code · 37 blank · 35 comment · 7 complexity · f30cafd6881896d3655be743ea4d72ea MD5 · raw file

  1. #! /usr/bin/env python
  2. from test import test_support
  3. import unittest
  4. import urlparse
  5. RFC1808_BASE = "http://a/b/c/d;p?q#f"
  6. RFC2396_BASE = "http://a/b/c/d;p?q"
  7. RFC3986_BASE = "http://a/b/c/d;p?q"
  8. # A list of test cases. Each test case is a 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. class UrlParseTestCase(unittest.TestCase):
  24. def checkRoundtrips(self, url, parsed, split):
  25. result = urlparse.urlparse(url)
  26. self.assertEqual(result, parsed)
  27. t = (result.scheme, result.netloc, result.path,
  28. result.params, result.query, result.fragment)
  29. self.assertEqual(t, parsed)
  30. # put it back together and it should be the same
  31. result2 = urlparse.urlunparse(result)
  32. self.assertEqual(result2, url)
  33. self.assertEqual(result2, result.geturl())
  34. # the result of geturl() is a fixpoint; we can always parse it
  35. # again to get the same result:
  36. result3 = urlparse.urlparse(result.geturl())
  37. self.assertEqual(result3.geturl(), result.geturl())
  38. self.assertEqual(result3, result)
  39. self.assertEqual(result3.scheme, result.scheme)
  40. self.assertEqual(result3.netloc, result.netloc)
  41. self.assertEqual(result3.path, result.path)
  42. self.assertEqual(result3.params, result.params)
  43. self.assertEqual(result3.query, result.query)
  44. self.assertEqual(result3.fragment, result.fragment)
  45. self.assertEqual(result3.username, result.username)
  46. self.assertEqual(result3.password, result.password)
  47. self.assertEqual(result3.hostname, result.hostname)
  48. self.assertEqual(result3.port, result.port)
  49. # check the roundtrip using urlsplit() as well
  50. result = urlparse.urlsplit(url)
  51. self.assertEqual(result, split)
  52. t = (result.scheme, result.netloc, result.path,
  53. result.query, result.fragment)
  54. self.assertEqual(t, split)
  55. result2 = urlparse.urlunsplit(result)
  56. self.assertEqual(result2, url)
  57. self.assertEqual(result2, result.geturl())
  58. # check the fixpoint property of re-parsing the result of geturl()
  59. result3 = urlparse.urlsplit(result.geturl())
  60. self.assertEqual(result3.geturl(), result.geturl())
  61. self.assertEqual(result3, result)
  62. self.assertEqual(result3.scheme, result.scheme)
  63. self.assertEqual(result3.netloc, result.netloc)
  64. self.assertEqual(result3.path, result.path)
  65. self.assertEqual(result3.query, result.query)
  66. self.assertEqual(result3.fragment, result.fragment)
  67. self.assertEqual(result3.username, result.username)
  68. self.assertEqual(result3.password, result.password)
  69. self.assertEqual(result3.hostname, result.hostname)
  70. self.assertEqual(result3.port, result.port)
  71. def test_qsl(self):
  72. for orig, expect in parse_qsl_test_cases:
  73. result = urlparse.parse_qsl(orig, keep_blank_values=True)
  74. self.assertEqual(result, expect, "Error parsing %s" % repr(orig))
  75. def test_roundtrips(self):
  76. testcases = [
  77. ('file:///tmp/junk.txt',
  78. ('file', '', '/tmp/junk.txt', '', '', ''),
  79. ('file', '', '/tmp/junk.txt', '', '')),
  80. ('imap://mail.python.org/mbox1',
  81. ('imap', 'mail.python.org', '/mbox1', '', '', ''),
  82. ('imap', 'mail.python.org', '/mbox1', '', '')),
  83. ('mms://wms.sys.hinet.net/cts/Drama/09006251100.asf',
  84. ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf',
  85. '', '', ''),
  86. ('mms', 'wms.sys.hinet.net', '/cts/Drama/09006251100.asf',
  87. '', '')),
  88. ('svn+ssh://svn.zope.org/repos/main/ZConfig/trunk/',
  89. ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/',
  90. '', '', ''),
  91. ('svn+ssh', 'svn.zope.org', '/repos/main/ZConfig/trunk/',
  92. '', ''))
  93. ]
  94. for url, parsed, split in testcases:
  95. self.checkRoundtrips(url, parsed, split)
  96. def test_http_roundtrips(self):
  97. # urlparse.urlsplit treats 'http:' as an optimized special case,
  98. # so we test both 'http:' and 'https:' in all the following.
  99. # Three cheers for white box knowledge!
  100. testcases = [
  101. ('://www.python.org',
  102. ('www.python.org', '', '', '', ''),
  103. ('www.python.org', '', '', '')),
  104. ('://www.python.org#abc',
  105. ('www.python.org', '', '', '', 'abc'),
  106. ('www.python.org', '', '', 'abc')),
  107. ('://www.python.org?q=abc',
  108. ('www.python.org', '', '', 'q=abc', ''),
  109. ('www.python.org', '', 'q=abc', '')),
  110. ('://www.python.org/#abc',
  111. ('www.python.org', '/', '', '', 'abc'),
  112. ('www.python.org', '/', '', 'abc')),
  113. ('://a/b/c/d;p?q#f',
  114. ('a', '/b/c/d', 'p', 'q', 'f'),
  115. ('a', '/b/c/d;p', 'q', 'f')),
  116. ]
  117. for scheme in ('http', 'https'):
  118. for url, parsed, split in testcases:
  119. url = scheme + url
  120. parsed = (scheme,) + parsed
  121. split = (scheme,) + split
  122. self.checkRoundtrips(url, parsed, split)
  123. def checkJoin(self, base, relurl, expected):
  124. self.assertEqual(urlparse.urljoin(base, relurl), expected,
  125. (base, relurl, expected))
  126. def test_unparse_parse(self):
  127. for u in ['Python', './Python']:
  128. self.assertEqual(urlparse.urlunsplit(urlparse.urlsplit(u)), u)
  129. self.assertEqual(urlparse.urlunparse(urlparse.urlparse(u)), u)
  130. def test_RFC1808(self):
  131. # "normal" cases from RFC 1808:
  132. self.checkJoin(RFC1808_BASE, 'g:h', 'g:h')
  133. self.checkJoin(RFC1808_BASE, 'g', 'http://a/b/c/g')
  134. self.checkJoin(RFC1808_BASE, './g', 'http://a/b/c/g')
  135. self.checkJoin(RFC1808_BASE, 'g/', 'http://a/b/c/g/')
  136. self.checkJoin(RFC1808_BASE, '/g', 'http://a/g')
  137. self.checkJoin(RFC1808_BASE, '//g', 'http://g')
  138. self.checkJoin(RFC1808_BASE, 'g?y', 'http://a/b/c/g?y')
  139. self.checkJoin(RFC1808_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
  140. self.checkJoin(RFC1808_BASE, '#s', 'http://a/b/c/d;p?q#s')
  141. self.checkJoin(RFC1808_BASE, 'g#s', 'http://a/b/c/g#s')
  142. self.checkJoin(RFC1808_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
  143. self.checkJoin(RFC1808_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
  144. self.checkJoin(RFC1808_BASE, 'g;x', 'http://a/b/c/g;x')
  145. self.checkJoin(RFC1808_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
  146. self.checkJoin(RFC1808_BASE, '.', 'http://a/b/c/')
  147. self.checkJoin(RFC1808_BASE, './', 'http://a/b/c/')
  148. self.checkJoin(RFC1808_BASE, '..', 'http://a/b/')
  149. self.checkJoin(RFC1808_BASE, '../', 'http://a/b/')
  150. self.checkJoin(RFC1808_BASE, '../g', 'http://a/b/g')
  151. self.checkJoin(RFC1808_BASE, '../..', 'http://a/')
  152. self.checkJoin(RFC1808_BASE, '../../', 'http://a/')
  153. self.checkJoin(RFC1808_BASE, '../../g', 'http://a/g')
  154. # "abnormal" cases from RFC 1808:
  155. self.checkJoin(RFC1808_BASE, '', 'http://a/b/c/d;p?q#f')
  156. self.checkJoin(RFC1808_BASE, '../../../g', 'http://a/../g')
  157. self.checkJoin(RFC1808_BASE, '../../../../g', 'http://a/../../g')
  158. self.checkJoin(RFC1808_BASE, '/./g', 'http://a/./g')
  159. self.checkJoin(RFC1808_BASE, '/../g', 'http://a/../g')
  160. self.checkJoin(RFC1808_BASE, 'g.', 'http://a/b/c/g.')
  161. self.checkJoin(RFC1808_BASE, '.g', 'http://a/b/c/.g')
  162. self.checkJoin(RFC1808_BASE, 'g..', 'http://a/b/c/g..')
  163. self.checkJoin(RFC1808_BASE, '..g', 'http://a/b/c/..g')
  164. self.checkJoin(RFC1808_BASE, './../g', 'http://a/b/g')
  165. self.checkJoin(RFC1808_BASE, './g/.', 'http://a/b/c/g/')
  166. self.checkJoin(RFC1808_BASE, 'g/./h', 'http://a/b/c/g/h')
  167. self.checkJoin(RFC1808_BASE, 'g/../h', 'http://a/b/c/h')
  168. # RFC 1808 and RFC 1630 disagree on these (according to RFC 1808),
  169. # so we'll not actually run these tests (which expect 1808 behavior).
  170. #self.checkJoin(RFC1808_BASE, 'http:g', 'http:g')
  171. #self.checkJoin(RFC1808_BASE, 'http:', 'http:')
  172. def test_RFC2396(self):
  173. # cases from RFC 2396
  174. self.checkJoin(RFC2396_BASE, 'g:h', 'g:h')
  175. self.checkJoin(RFC2396_BASE, 'g', 'http://a/b/c/g')
  176. self.checkJoin(RFC2396_BASE, './g', 'http://a/b/c/g')
  177. self.checkJoin(RFC2396_BASE, 'g/', 'http://a/b/c/g/')
  178. self.checkJoin(RFC2396_BASE, '/g', 'http://a/g')
  179. self.checkJoin(RFC2396_BASE, '//g', 'http://g')
  180. self.checkJoin(RFC2396_BASE, 'g?y', 'http://a/b/c/g?y')
  181. self.checkJoin(RFC2396_BASE, '#s', 'http://a/b/c/d;p?q#s')
  182. self.checkJoin(RFC2396_BASE, 'g#s', 'http://a/b/c/g#s')
  183. self.checkJoin(RFC2396_BASE, 'g?y#s', 'http://a/b/c/g?y#s')
  184. self.checkJoin(RFC2396_BASE, 'g;x', 'http://a/b/c/g;x')
  185. self.checkJoin(RFC2396_BASE, 'g;x?y#s', 'http://a/b/c/g;x?y#s')
  186. self.checkJoin(RFC2396_BASE, '.', 'http://a/b/c/')
  187. self.checkJoin(RFC2396_BASE, './', 'http://a/b/c/')
  188. self.checkJoin(RFC2396_BASE, '..', 'http://a/b/')
  189. self.checkJoin(RFC2396_BASE, '../', 'http://a/b/')
  190. self.checkJoin(RFC2396_BASE, '../g', 'http://a/b/g')
  191. self.checkJoin(RFC2396_BASE, '../..', 'http://a/')
  192. self.checkJoin(RFC2396_BASE, '../../', 'http://a/')
  193. self.checkJoin(RFC2396_BASE, '../../g', 'http://a/g')
  194. self.checkJoin(RFC2396_BASE, '', RFC2396_BASE)
  195. self.checkJoin(RFC2396_BASE, '../../../g', 'http://a/../g')
  196. self.checkJoin(RFC2396_BASE, '../../../../g', 'http://a/../../g')
  197. self.checkJoin(RFC2396_BASE, '/./g', 'http://a/./g')
  198. self.checkJoin(RFC2396_BASE, '/../g', 'http://a/../g')
  199. self.checkJoin(RFC2396_BASE, 'g.', 'http://a/b/c/g.')
  200. self.checkJoin(RFC2396_BASE, '.g', 'http://a/b/c/.g')
  201. self.checkJoin(RFC2396_BASE, 'g..', 'http://a/b/c/g..')
  202. self.checkJoin(RFC2396_BASE, '..g', 'http://a/b/c/..g')
  203. self.checkJoin(RFC2396_BASE, './../g', 'http://a/b/g')
  204. self.checkJoin(RFC2396_BASE, './g/.', 'http://a/b/c/g/')
  205. self.checkJoin(RFC2396_BASE, 'g/./h', 'http://a/b/c/g/h')
  206. self.checkJoin(RFC2396_BASE, 'g/../h', 'http://a/b/c/h')
  207. self.checkJoin(RFC2396_BASE, 'g;x=1/./y', 'http://a/b/c/g;x=1/y')
  208. self.checkJoin(RFC2396_BASE, 'g;x=1/../y', 'http://a/b/c/y')
  209. self.checkJoin(RFC2396_BASE, 'g?y/./x', 'http://a/b/c/g?y/./x')
  210. self.checkJoin(RFC2396_BASE, 'g?y/../x', 'http://a/b/c/g?y/../x')
  211. self.checkJoin(RFC2396_BASE, 'g#s/./x', 'http://a/b/c/g#s/./x')
  212. self.checkJoin(RFC2396_BASE, 'g#s/../x', 'http://a/b/c/g#s/../x')
  213. #The following scenarios have been updated in RFC3986
  214. #self.checkJoin(RFC2396_BASE, '?y', 'http://a/b/c/?y')
  215. #self.checkJoin(RFC2396_BASE, ';x', 'http://a/b/c/;x')
  216. def test_RFC3986(self):
  217. self.checkJoin(RFC3986_BASE, '?y','http://a/b/c/d;p?y')
  218. self.checkJoin(RFC2396_BASE, ';x', 'http://a/b/c/;x')
  219. def test_urldefrag(self):
  220. for url, defrag, frag in [
  221. ('http://python.org#frag', 'http://python.org', 'frag'),
  222. ('http://python.org', 'http://python.org', ''),
  223. ('http://python.org/#frag', 'http://python.org/', 'frag'),
  224. ('http://python.org/', 'http://python.org/', ''),
  225. ('http://python.org/?q#frag', 'http://python.org/?q', 'frag'),
  226. ('http://python.org/?q', 'http://python.org/?q', ''),
  227. ('http://python.org/p#frag', 'http://python.org/p', 'frag'),
  228. ('http://python.org/p?q', 'http://python.org/p?q', ''),
  229. (RFC1808_BASE, 'http://a/b/c/d;p?q', 'f'),
  230. (RFC2396_BASE, 'http://a/b/c/d;p?q', ''),
  231. ]:
  232. self.assertEqual(urlparse.urldefrag(url), (defrag, frag))
  233. def test_urlsplit_attributes(self):
  234. url = "HTTP://WWW.PYTHON.ORG/doc/#frag"
  235. p = urlparse.urlsplit(url)
  236. self.assertEqual(p.scheme, "http")
  237. self.assertEqual(p.netloc, "WWW.PYTHON.ORG")
  238. self.assertEqual(p.path, "/doc/")
  239. self.assertEqual(p.query, "")
  240. self.assertEqual(p.fragment, "frag")
  241. self.assertEqual(p.username, None)
  242. self.assertEqual(p.password, None)
  243. self.assertEqual(p.hostname, "www.python.org")
  244. self.assertEqual(p.port, None)
  245. # geturl() won't return exactly the original URL in this case
  246. # since the scheme is always case-normalized
  247. #self.assertEqual(p.geturl(), url)
  248. url = "http://User:Pass@www.python.org:080/doc/?query=yes#frag"
  249. p = urlparse.urlsplit(url)
  250. self.assertEqual(p.scheme, "http")
  251. self.assertEqual(p.netloc, "User:Pass@www.python.org:080")
  252. self.assertEqual(p.path, "/doc/")
  253. self.assertEqual(p.query, "query=yes")
  254. self.assertEqual(p.fragment, "frag")
  255. self.assertEqual(p.username, "User")
  256. self.assertEqual(p.password, "Pass")
  257. self.assertEqual(p.hostname, "www.python.org")
  258. self.assertEqual(p.port, 80)
  259. self.assertEqual(p.geturl(), url)
  260. # Addressing issue1698, which suggests Username can contain
  261. # "@" characters. Though not RFC compliant, many ftp sites allow
  262. # and request email addresses as usernames.
  263. url = "http://User@example.com:Pass@www.python.org:080/doc/?query=yes#frag"
  264. p = urlparse.urlsplit(url)
  265. self.assertEqual(p.scheme, "http")
  266. self.assertEqual(p.netloc, "User@example.com:Pass@www.python.org:080")
  267. self.assertEqual(p.path, "/doc/")
  268. self.assertEqual(p.query, "query=yes")
  269. self.assertEqual(p.fragment, "frag")
  270. self.assertEqual(p.username, "User@example.com")
  271. self.assertEqual(p.password, "Pass")
  272. self.assertEqual(p.hostname, "www.python.org")
  273. self.assertEqual(p.port, 80)
  274. self.assertEqual(p.geturl(), url)
  275. def test_attributes_bad_port(self):
  276. """Check handling of non-integer ports."""
  277. p = urlparse.urlsplit("http://www.example.net:foo")
  278. self.assertEqual(p.netloc, "www.example.net:foo")
  279. self.assertRaises(ValueError, lambda: p.port)
  280. p = urlparse.urlparse("http://www.example.net:foo")
  281. self.assertEqual(p.netloc, "www.example.net:foo")
  282. self.assertRaises(ValueError, lambda: p.port)
  283. def test_attributes_without_netloc(self):
  284. # This example is straight from RFC 3261. It looks like it
  285. # should allow the username, hostname, and port to be filled
  286. # in, but doesn't. Since it's a URI and doesn't use the
  287. # scheme://netloc syntax, the netloc and related attributes
  288. # should be left empty.
  289. uri = "sip:alice@atlanta.com;maddr=239.255.255.1;ttl=15"
  290. p = urlparse.urlsplit(uri)
  291. self.assertEqual(p.netloc, "")
  292. self.assertEqual(p.username, None)
  293. self.assertEqual(p.password, None)
  294. self.assertEqual(p.hostname, None)
  295. self.assertEqual(p.port, None)
  296. self.assertEqual(p.geturl(), uri)
  297. p = urlparse.urlparse(uri)
  298. self.assertEqual(p.netloc, "")
  299. self.assertEqual(p.username, None)
  300. self.assertEqual(p.password, None)
  301. self.assertEqual(p.hostname, None)
  302. self.assertEqual(p.port, None)
  303. self.assertEqual(p.geturl(), uri)
  304. def test_caching(self):
  305. # Test case for bug #1313119
  306. uri = "http://example.com/doc/"
  307. unicode_uri = unicode(uri)
  308. urlparse.urlparse(unicode_uri)
  309. p = urlparse.urlparse(uri)
  310. self.assertEqual(type(p.scheme), type(uri))
  311. self.assertEqual(type(p.hostname), type(uri))
  312. self.assertEqual(type(p.path), type(uri))
  313. def test_noslash(self):
  314. # Issue 1637: http://foo.com?query is legal
  315. self.assertEqual(urlparse.urlparse("http://example.com?blahblah=/foo"),
  316. ('http', 'example.com', '', '', 'blahblah=/foo', ''))
  317. def test_main():
  318. test_support.run_unittest(UrlParseTestCase)
  319. if __name__ == "__main__":
  320. test_main()