/Lib/test/test_cookielib.py

http://unladen-swallow.googlecode.com/ · Python · 1736 lines · 1120 code · 251 blank · 365 comment · 49 complexity · 2418aefed04ea32a2658b10d9c9f4ee3 MD5 · raw file

Large files are truncated click here to view the full file

  1. # -*- coding: latin-1 -*-
  2. """Tests for cookielib.py."""
  3. import re, os, time
  4. from unittest import TestCase
  5. from test import test_support
  6. class DateTimeTests(TestCase):
  7. def test_time2isoz(self):
  8. from cookielib import time2isoz
  9. base = 1019227000
  10. day = 24*3600
  11. self.assertEquals(time2isoz(base), "2002-04-19 14:36:40Z")
  12. self.assertEquals(time2isoz(base+day), "2002-04-20 14:36:40Z")
  13. self.assertEquals(time2isoz(base+2*day), "2002-04-21 14:36:40Z")
  14. self.assertEquals(time2isoz(base+3*day), "2002-04-22 14:36:40Z")
  15. az = time2isoz()
  16. bz = time2isoz(500000)
  17. for text in (az, bz):
  18. self.assert_(re.search(r"^\d{4}-\d\d-\d\d \d\d:\d\d:\d\dZ$", text),
  19. "bad time2isoz format: %s %s" % (az, bz))
  20. def test_http2time(self):
  21. from cookielib import http2time
  22. def parse_date(text):
  23. return time.gmtime(http2time(text))[:6]
  24. self.assertEquals(parse_date("01 Jan 2001"), (2001, 1, 1, 0, 0, 0.0))
  25. # this test will break around year 2070
  26. self.assertEquals(parse_date("03-Feb-20"), (2020, 2, 3, 0, 0, 0.0))
  27. # this test will break around year 2048
  28. self.assertEquals(parse_date("03-Feb-98"), (1998, 2, 3, 0, 0, 0.0))
  29. def test_http2time_formats(self):
  30. from cookielib import http2time, time2isoz
  31. # test http2time for supported dates. Test cases with 2 digit year
  32. # will probably break in year 2044.
  33. tests = [
  34. 'Thu, 03 Feb 1994 00:00:00 GMT', # proposed new HTTP format
  35. 'Thursday, 03-Feb-94 00:00:00 GMT', # old rfc850 HTTP format
  36. 'Thursday, 03-Feb-1994 00:00:00 GMT', # broken rfc850 HTTP format
  37. '03 Feb 1994 00:00:00 GMT', # HTTP format (no weekday)
  38. '03-Feb-94 00:00:00 GMT', # old rfc850 (no weekday)
  39. '03-Feb-1994 00:00:00 GMT', # broken rfc850 (no weekday)
  40. '03-Feb-1994 00:00 GMT', # broken rfc850 (no weekday, no seconds)
  41. '03-Feb-1994 00:00', # broken rfc850 (no weekday, no seconds, no tz)
  42. '03-Feb-94', # old rfc850 HTTP format (no weekday, no time)
  43. '03-Feb-1994', # broken rfc850 HTTP format (no weekday, no time)
  44. '03 Feb 1994', # proposed new HTTP format (no weekday, no time)
  45. # A few tests with extra space at various places
  46. ' 03 Feb 1994 0:00 ',
  47. ' 03-Feb-1994 ',
  48. ]
  49. test_t = 760233600 # assume broken POSIX counting of seconds
  50. result = time2isoz(test_t)
  51. expected = "1994-02-03 00:00:00Z"
  52. self.assertEquals(result, expected,
  53. "%s => '%s' (%s)" % (test_t, result, expected))
  54. for s in tests:
  55. t = http2time(s)
  56. t2 = http2time(s.lower())
  57. t3 = http2time(s.upper())
  58. self.assert_(t == t2 == t3 == test_t,
  59. "'%s' => %s, %s, %s (%s)" % (s, t, t2, t3, test_t))
  60. def test_http2time_garbage(self):
  61. from cookielib import http2time
  62. for test in [
  63. '',
  64. 'Garbage',
  65. 'Mandag 16. September 1996',
  66. '01-00-1980',
  67. '01-13-1980',
  68. '00-01-1980',
  69. '32-01-1980',
  70. '01-01-1980 25:00:00',
  71. '01-01-1980 00:61:00',
  72. '01-01-1980 00:00:62',
  73. ]:
  74. self.assert_(http2time(test) is None,
  75. "http2time(%s) is not None\n"
  76. "http2time(test) %s" % (test, http2time(test))
  77. )
  78. class HeaderTests(TestCase):
  79. def test_parse_ns_headers(self):
  80. from cookielib import parse_ns_headers
  81. # quotes should be stripped
  82. expected = [[('foo', 'bar'), ('expires', 2209069412L), ('version', '0')]]
  83. for hdr in [
  84. 'foo=bar; expires=01 Jan 2040 22:23:32 GMT',
  85. 'foo=bar; expires="01 Jan 2040 22:23:32 GMT"',
  86. ]:
  87. self.assertEquals(parse_ns_headers([hdr]), expected)
  88. def test_parse_ns_headers_special_names(self):
  89. # names such as 'expires' are not special in first name=value pair
  90. # of Set-Cookie: header
  91. from cookielib import parse_ns_headers
  92. # Cookie with name 'expires'
  93. hdr = 'expires=01 Jan 2040 22:23:32 GMT'
  94. expected = [[("expires", "01 Jan 2040 22:23:32 GMT"), ("version", "0")]]
  95. self.assertEquals(parse_ns_headers([hdr]), expected)
  96. def test_join_header_words(self):
  97. from cookielib import join_header_words
  98. joined = join_header_words([[("foo", None), ("bar", "baz")]])
  99. self.assertEquals(joined, "foo; bar=baz")
  100. self.assertEquals(join_header_words([[]]), "")
  101. def test_split_header_words(self):
  102. from cookielib import split_header_words
  103. tests = [
  104. ("foo", [[("foo", None)]]),
  105. ("foo=bar", [[("foo", "bar")]]),
  106. (" foo ", [[("foo", None)]]),
  107. (" foo= ", [[("foo", "")]]),
  108. (" foo=", [[("foo", "")]]),
  109. (" foo= ; ", [[("foo", "")]]),
  110. (" foo= ; bar= baz ", [[("foo", ""), ("bar", "baz")]]),
  111. ("foo=bar bar=baz", [[("foo", "bar"), ("bar", "baz")]]),
  112. # doesn't really matter if this next fails, but it works ATM
  113. ("foo= bar=baz", [[("foo", "bar=baz")]]),
  114. ("foo=bar;bar=baz", [[("foo", "bar"), ("bar", "baz")]]),
  115. ('foo bar baz', [[("foo", None), ("bar", None), ("baz", None)]]),
  116. ("a, b, c", [[("a", None)], [("b", None)], [("c", None)]]),
  117. (r'foo; bar=baz, spam=, foo="\,\;\"", bar= ',
  118. [[("foo", None), ("bar", "baz")],
  119. [("spam", "")], [("foo", ',;"')], [("bar", "")]]),
  120. ]
  121. for arg, expect in tests:
  122. try:
  123. result = split_header_words([arg])
  124. except:
  125. import traceback, StringIO
  126. f = StringIO.StringIO()
  127. traceback.print_exc(None, f)
  128. result = "(error -- traceback follows)\n\n%s" % f.getvalue()
  129. self.assertEquals(result, expect, """
  130. When parsing: '%s'
  131. Expected: '%s'
  132. Got: '%s'
  133. """ % (arg, expect, result))
  134. def test_roundtrip(self):
  135. from cookielib import split_header_words, join_header_words
  136. tests = [
  137. ("foo", "foo"),
  138. ("foo=bar", "foo=bar"),
  139. (" foo ", "foo"),
  140. ("foo=", 'foo=""'),
  141. ("foo=bar bar=baz", "foo=bar; bar=baz"),
  142. ("foo=bar;bar=baz", "foo=bar; bar=baz"),
  143. ('foo bar baz', "foo; bar; baz"),
  144. (r'foo="\"" bar="\\"', r'foo="\""; bar="\\"'),
  145. ('foo,,,bar', 'foo, bar'),
  146. ('foo=bar,bar=baz', 'foo=bar, bar=baz'),
  147. ('text/html; charset=iso-8859-1',
  148. 'text/html; charset="iso-8859-1"'),
  149. ('foo="bar"; port="80,81"; discard, bar=baz',
  150. 'foo=bar; port="80,81"; discard, bar=baz'),
  151. (r'Basic realm="\"foo\\\\bar\""',
  152. r'Basic; realm="\"foo\\\\bar\""')
  153. ]
  154. for arg, expect in tests:
  155. input = split_header_words([arg])
  156. res = join_header_words(input)
  157. self.assertEquals(res, expect, """
  158. When parsing: '%s'
  159. Expected: '%s'
  160. Got: '%s'
  161. Input was: '%s'
  162. """ % (arg, expect, res, input))
  163. class FakeResponse:
  164. def __init__(self, headers=[], url=None):
  165. """
  166. headers: list of RFC822-style 'Key: value' strings
  167. """
  168. import mimetools, StringIO
  169. f = StringIO.StringIO("\n".join(headers))
  170. self._headers = mimetools.Message(f)
  171. self._url = url
  172. def info(self): return self._headers
  173. def interact_2965(cookiejar, url, *set_cookie_hdrs):
  174. return _interact(cookiejar, url, set_cookie_hdrs, "Set-Cookie2")
  175. def interact_netscape(cookiejar, url, *set_cookie_hdrs):
  176. return _interact(cookiejar, url, set_cookie_hdrs, "Set-Cookie")
  177. def _interact(cookiejar, url, set_cookie_hdrs, hdr_name):
  178. """Perform a single request / response cycle, returning Cookie: header."""
  179. from urllib2 import Request
  180. req = Request(url)
  181. cookiejar.add_cookie_header(req)
  182. cookie_hdr = req.get_header("Cookie", "")
  183. headers = []
  184. for hdr in set_cookie_hdrs:
  185. headers.append("%s: %s" % (hdr_name, hdr))
  186. res = FakeResponse(headers, url)
  187. cookiejar.extract_cookies(res, req)
  188. return cookie_hdr
  189. class FileCookieJarTests(TestCase):
  190. def test_lwp_valueless_cookie(self):
  191. # cookies with no value should be saved and loaded consistently
  192. from cookielib import LWPCookieJar
  193. filename = test_support.TESTFN
  194. c = LWPCookieJar()
  195. interact_netscape(c, "http://www.acme.com/", 'boo')
  196. self.assertEqual(c._cookies["www.acme.com"]["/"]["boo"].value, None)
  197. try:
  198. c.save(filename, ignore_discard=True)
  199. c = LWPCookieJar()
  200. c.load(filename, ignore_discard=True)
  201. finally:
  202. try: os.unlink(filename)
  203. except OSError: pass
  204. self.assertEqual(c._cookies["www.acme.com"]["/"]["boo"].value, None)
  205. def test_bad_magic(self):
  206. from cookielib import LWPCookieJar, MozillaCookieJar, LoadError
  207. # IOErrors (eg. file doesn't exist) are allowed to propagate
  208. filename = test_support.TESTFN
  209. for cookiejar_class in LWPCookieJar, MozillaCookieJar:
  210. c = cookiejar_class()
  211. try:
  212. c.load(filename="for this test to work, a file with this "
  213. "filename should not exist")
  214. except IOError, exc:
  215. # exactly IOError, not LoadError
  216. self.assertEqual(exc.__class__, IOError)
  217. else:
  218. self.fail("expected IOError for invalid filename")
  219. # Invalid contents of cookies file (eg. bad magic string)
  220. # causes a LoadError.
  221. try:
  222. f = open(filename, "w")
  223. f.write("oops\n")
  224. for cookiejar_class in LWPCookieJar, MozillaCookieJar:
  225. c = cookiejar_class()
  226. self.assertRaises(LoadError, c.load, filename)
  227. finally:
  228. try: os.unlink(filename)
  229. except OSError: pass
  230. class CookieTests(TestCase):
  231. # XXX
  232. # Get rid of string comparisons where not actually testing str / repr.
  233. # .clear() etc.
  234. # IP addresses like 50 (single number, no dot) and domain-matching
  235. # functions (and is_HDN)? See draft RFC 2965 errata.
  236. # Strictness switches
  237. # is_third_party()
  238. # unverifiability / third-party blocking
  239. # Netscape cookies work the same as RFC 2965 with regard to port.
  240. # Set-Cookie with negative max age.
  241. # If turn RFC 2965 handling off, Set-Cookie2 cookies should not clobber
  242. # Set-Cookie cookies.
  243. # Cookie2 should be sent if *any* cookies are not V1 (ie. V0 OR V2 etc.).
  244. # Cookies (V1 and V0) with no expiry date should be set to be discarded.
  245. # RFC 2965 Quoting:
  246. # Should accept unquoted cookie-attribute values? check errata draft.
  247. # Which are required on the way in and out?
  248. # Should always return quoted cookie-attribute values?
  249. # Proper testing of when RFC 2965 clobbers Netscape (waiting for errata).
  250. # Path-match on return (same for V0 and V1).
  251. # RFC 2965 acceptance and returning rules
  252. # Set-Cookie2 without version attribute is rejected.
  253. # Netscape peculiarities list from Ronald Tschalar.
  254. # The first two still need tests, the rest are covered.
  255. ## - Quoting: only quotes around the expires value are recognized as such
  256. ## (and yes, some folks quote the expires value); quotes around any other
  257. ## value are treated as part of the value.
  258. ## - White space: white space around names and values is ignored
  259. ## - Default path: if no path parameter is given, the path defaults to the
  260. ## path in the request-uri up to, but not including, the last '/'. Note
  261. ## that this is entirely different from what the spec says.
  262. ## - Commas and other delimiters: Netscape just parses until the next ';'.
  263. ## This means it will allow commas etc inside values (and yes, both
  264. ## commas and equals are commonly appear in the cookie value). This also
  265. ## means that if you fold multiple Set-Cookie header fields into one,
  266. ## comma-separated list, it'll be a headache to parse (at least my head
  267. ## starts hurting everytime I think of that code).
  268. ## - Expires: You'll get all sorts of date formats in the expires,
  269. ## including emtpy expires attributes ("expires="). Be as flexible as you
  270. ## can, and certainly don't expect the weekday to be there; if you can't
  271. ## parse it, just ignore it and pretend it's a session cookie.
  272. ## - Domain-matching: Netscape uses the 2-dot rule for _all_ domains, not
  273. ## just the 7 special TLD's listed in their spec. And folks rely on
  274. ## that...
  275. def test_domain_return_ok(self):
  276. # test optimization: .domain_return_ok() should filter out most
  277. # domains in the CookieJar before we try to access them (because that
  278. # may require disk access -- in particular, with MSIECookieJar)
  279. # This is only a rough check for performance reasons, so it's not too
  280. # critical as long as it's sufficiently liberal.
  281. import cookielib, urllib2
  282. pol = cookielib.DefaultCookiePolicy()
  283. for url, domain, ok in [
  284. ("http://foo.bar.com/", "blah.com", False),
  285. ("http://foo.bar.com/", "rhubarb.blah.com", False),
  286. ("http://foo.bar.com/", "rhubarb.foo.bar.com", False),
  287. ("http://foo.bar.com/", ".foo.bar.com", True),
  288. ("http://foo.bar.com/", "foo.bar.com", True),
  289. ("http://foo.bar.com/", ".bar.com", True),
  290. ("http://foo.bar.com/", "com", True),
  291. ("http://foo.com/", "rhubarb.foo.com", False),
  292. ("http://foo.com/", ".foo.com", True),
  293. ("http://foo.com/", "foo.com", True),
  294. ("http://foo.com/", "com", True),
  295. ("http://foo/", "rhubarb.foo", False),
  296. ("http://foo/", ".foo", True),
  297. ("http://foo/", "foo", True),
  298. ("http://foo/", "foo.local", True),
  299. ("http://foo/", ".local", True),
  300. ]:
  301. request = urllib2.Request(url)
  302. r = pol.domain_return_ok(domain, request)
  303. if ok: self.assert_(r)
  304. else: self.assert_(not r)
  305. def test_missing_value(self):
  306. from cookielib import MozillaCookieJar, lwp_cookie_str
  307. # missing = sign in Cookie: header is regarded by Mozilla as a missing
  308. # name, and by cookielib as a missing value
  309. filename = test_support.TESTFN
  310. c = MozillaCookieJar(filename)
  311. interact_netscape(c, "http://www.acme.com/", 'eggs')
  312. interact_netscape(c, "http://www.acme.com/", '"spam"; path=/foo/')
  313. cookie = c._cookies["www.acme.com"]["/"]["eggs"]
  314. self.assert_(cookie.value is None)
  315. self.assertEquals(cookie.name, "eggs")
  316. cookie = c._cookies["www.acme.com"]['/foo/']['"spam"']
  317. self.assert_(cookie.value is None)
  318. self.assertEquals(cookie.name, '"spam"')
  319. self.assertEquals(lwp_cookie_str(cookie), (
  320. r'"spam"; path="/foo/"; domain="www.acme.com"; '
  321. 'path_spec; discard; version=0'))
  322. old_str = repr(c)
  323. c.save(ignore_expires=True, ignore_discard=True)
  324. try:
  325. c = MozillaCookieJar(filename)
  326. c.revert(ignore_expires=True, ignore_discard=True)
  327. finally:
  328. os.unlink(c.filename)
  329. # cookies unchanged apart from lost info re. whether path was specified
  330. self.assertEquals(
  331. repr(c),
  332. re.sub("path_specified=%s" % True, "path_specified=%s" % False,
  333. old_str)
  334. )
  335. self.assertEquals(interact_netscape(c, "http://www.acme.com/foo/"),
  336. '"spam"; eggs')
  337. def test_rfc2109_handling(self):
  338. # RFC 2109 cookies are handled as RFC 2965 or Netscape cookies,
  339. # dependent on policy settings
  340. from cookielib import CookieJar, DefaultCookiePolicy
  341. for rfc2109_as_netscape, rfc2965, version in [
  342. # default according to rfc2965 if not explicitly specified
  343. (None, False, 0),
  344. (None, True, 1),
  345. # explicit rfc2109_as_netscape
  346. (False, False, None), # version None here means no cookie stored
  347. (False, True, 1),
  348. (True, False, 0),
  349. (True, True, 0),
  350. ]:
  351. policy = DefaultCookiePolicy(
  352. rfc2109_as_netscape=rfc2109_as_netscape,
  353. rfc2965=rfc2965)
  354. c = CookieJar(policy)
  355. interact_netscape(c, "http://www.example.com/", "ni=ni; Version=1")
  356. try:
  357. cookie = c._cookies["www.example.com"]["/"]["ni"]
  358. except KeyError:
  359. self.assert_(version is None) # didn't expect a stored cookie
  360. else:
  361. self.assertEqual(cookie.version, version)
  362. # 2965 cookies are unaffected
  363. interact_2965(c, "http://www.example.com/",
  364. "foo=bar; Version=1")
  365. if rfc2965:
  366. cookie2965 = c._cookies["www.example.com"]["/"]["foo"]
  367. self.assertEqual(cookie2965.version, 1)
  368. def test_ns_parser(self):
  369. from cookielib import CookieJar, DEFAULT_HTTP_PORT
  370. c = CookieJar()
  371. interact_netscape(c, "http://www.acme.com/",
  372. 'spam=eggs; DoMain=.acme.com; port; blArgh="feep"')
  373. interact_netscape(c, "http://www.acme.com/", 'ni=ni; port=80,8080')
  374. interact_netscape(c, "http://www.acme.com:80/", 'nini=ni')
  375. interact_netscape(c, "http://www.acme.com:80/", 'foo=bar; expires=')
  376. interact_netscape(c, "http://www.acme.com:80/", 'spam=eggs; '
  377. 'expires="Foo Bar 25 33:22:11 3022"')
  378. cookie = c._cookies[".acme.com"]["/"]["spam"]
  379. self.assertEquals(cookie.domain, ".acme.com")
  380. self.assert_(cookie.domain_specified)
  381. self.assertEquals(cookie.port, DEFAULT_HTTP_PORT)
  382. self.assert_(not cookie.port_specified)
  383. # case is preserved
  384. self.assert_(cookie.has_nonstandard_attr("blArgh") and
  385. not cookie.has_nonstandard_attr("blargh"))
  386. cookie = c._cookies["www.acme.com"]["/"]["ni"]
  387. self.assertEquals(cookie.domain, "www.acme.com")
  388. self.assert_(not cookie.domain_specified)
  389. self.assertEquals(cookie.port, "80,8080")
  390. self.assert_(cookie.port_specified)
  391. cookie = c._cookies["www.acme.com"]["/"]["nini"]
  392. self.assert_(cookie.port is None)
  393. self.assert_(not cookie.port_specified)
  394. # invalid expires should not cause cookie to be dropped
  395. foo = c._cookies["www.acme.com"]["/"]["foo"]
  396. spam = c._cookies["www.acme.com"]["/"]["foo"]
  397. self.assert_(foo.expires is None)
  398. self.assert_(spam.expires is None)
  399. def test_ns_parser_special_names(self):
  400. # names such as 'expires' are not special in first name=value pair
  401. # of Set-Cookie: header
  402. from cookielib import CookieJar
  403. c = CookieJar()
  404. interact_netscape(c, "http://www.acme.com/", 'expires=eggs')
  405. interact_netscape(c, "http://www.acme.com/", 'version=eggs; spam=eggs')
  406. cookies = c._cookies["www.acme.com"]["/"]
  407. self.assert_('expires' in cookies)
  408. self.assert_('version' in cookies)
  409. def test_expires(self):
  410. from cookielib import time2netscape, CookieJar
  411. # if expires is in future, keep cookie...
  412. c = CookieJar()
  413. future = time2netscape(time.time()+3600)
  414. interact_netscape(c, "http://www.acme.com/", 'spam="bar"; expires=%s' %
  415. future)
  416. self.assertEquals(len(c), 1)
  417. now = time2netscape(time.time()-1)
  418. # ... and if in past or present, discard it
  419. interact_netscape(c, "http://www.acme.com/", 'foo="eggs"; expires=%s' %
  420. now)
  421. h = interact_netscape(c, "http://www.acme.com/")
  422. self.assertEquals(len(c), 1)
  423. self.assert_('spam="bar"' in h and "foo" not in h)
  424. # max-age takes precedence over expires, and zero max-age is request to
  425. # delete both new cookie and any old matching cookie
  426. interact_netscape(c, "http://www.acme.com/", 'eggs="bar"; expires=%s' %
  427. future)
  428. interact_netscape(c, "http://www.acme.com/", 'bar="bar"; expires=%s' %
  429. future)
  430. self.assertEquals(len(c), 3)
  431. interact_netscape(c, "http://www.acme.com/", 'eggs="bar"; '
  432. 'expires=%s; max-age=0' % future)
  433. interact_netscape(c, "http://www.acme.com/", 'bar="bar"; '
  434. 'max-age=0; expires=%s' % future)
  435. h = interact_netscape(c, "http://www.acme.com/")
  436. self.assertEquals(len(c), 1)
  437. # test expiry at end of session for cookies with no expires attribute
  438. interact_netscape(c, "http://www.rhubarb.net/", 'whum="fizz"')
  439. self.assertEquals(len(c), 2)
  440. c.clear_session_cookies()
  441. self.assertEquals(len(c), 1)
  442. self.assert_('spam="bar"' in h)
  443. # XXX RFC 2965 expiry rules (some apply to V0 too)
  444. def test_default_path(self):
  445. from cookielib import CookieJar, DefaultCookiePolicy
  446. # RFC 2965
  447. pol = DefaultCookiePolicy(rfc2965=True)
  448. c = CookieJar(pol)
  449. interact_2965(c, "http://www.acme.com/", 'spam="bar"; Version="1"')
  450. self.assert_("/" in c._cookies["www.acme.com"])
  451. c = CookieJar(pol)
  452. interact_2965(c, "http://www.acme.com/blah", 'eggs="bar"; Version="1"')
  453. self.assert_("/" in c._cookies["www.acme.com"])
  454. c = CookieJar(pol)
  455. interact_2965(c, "http://www.acme.com/blah/rhubarb",
  456. 'eggs="bar"; Version="1"')
  457. self.assert_("/blah/" in c._cookies["www.acme.com"])
  458. c = CookieJar(pol)
  459. interact_2965(c, "http://www.acme.com/blah/rhubarb/",
  460. 'eggs="bar"; Version="1"')
  461. self.assert_("/blah/rhubarb/" in c._cookies["www.acme.com"])
  462. # Netscape
  463. c = CookieJar()
  464. interact_netscape(c, "http://www.acme.com/", 'spam="bar"')
  465. self.assert_("/" in c._cookies["www.acme.com"])
  466. c = CookieJar()
  467. interact_netscape(c, "http://www.acme.com/blah", 'eggs="bar"')
  468. self.assert_("/" in c._cookies["www.acme.com"])
  469. c = CookieJar()
  470. interact_netscape(c, "http://www.acme.com/blah/rhubarb", 'eggs="bar"')
  471. self.assert_("/blah" in c._cookies["www.acme.com"])
  472. c = CookieJar()
  473. interact_netscape(c, "http://www.acme.com/blah/rhubarb/", 'eggs="bar"')
  474. self.assert_("/blah/rhubarb" in c._cookies["www.acme.com"])
  475. def test_escape_path(self):
  476. from cookielib import escape_path
  477. cases = [
  478. # quoted safe
  479. ("/foo%2f/bar", "/foo%2F/bar"),
  480. ("/foo%2F/bar", "/foo%2F/bar"),
  481. # quoted %
  482. ("/foo%%/bar", "/foo%%/bar"),
  483. # quoted unsafe
  484. ("/fo%19o/bar", "/fo%19o/bar"),
  485. ("/fo%7do/bar", "/fo%7Do/bar"),
  486. # unquoted safe
  487. ("/foo/bar&", "/foo/bar&"),
  488. ("/foo//bar", "/foo//bar"),
  489. ("\176/foo/bar", "\176/foo/bar"),
  490. # unquoted unsafe
  491. ("/foo\031/bar", "/foo%19/bar"),
  492. ("/\175foo/bar", "/%7Dfoo/bar"),
  493. # unicode
  494. (u"/foo/bar\uabcd", "/foo/bar%EA%AF%8D"), # UTF-8 encoded
  495. ]
  496. for arg, result in cases:
  497. self.assertEquals(escape_path(arg), result)
  498. def test_request_path(self):
  499. from urllib2 import Request
  500. from cookielib import request_path
  501. # with parameters
  502. req = Request("http://www.example.com/rheum/rhaponicum;"
  503. "foo=bar;sing=song?apples=pears&spam=eggs#ni")
  504. self.assertEquals(request_path(req), "/rheum/rhaponicum;"
  505. "foo=bar;sing=song?apples=pears&spam=eggs#ni")
  506. # without parameters
  507. req = Request("http://www.example.com/rheum/rhaponicum?"
  508. "apples=pears&spam=eggs#ni")
  509. self.assertEquals(request_path(req), "/rheum/rhaponicum?"
  510. "apples=pears&spam=eggs#ni")
  511. # missing final slash
  512. req = Request("http://www.example.com")
  513. self.assertEquals(request_path(req), "/")
  514. def test_request_port(self):
  515. from urllib2 import Request
  516. from cookielib import request_port, DEFAULT_HTTP_PORT
  517. req = Request("http://www.acme.com:1234/",
  518. headers={"Host": "www.acme.com:4321"})
  519. self.assertEquals(request_port(req), "1234")
  520. req = Request("http://www.acme.com/",
  521. headers={"Host": "www.acme.com:4321"})
  522. self.assertEquals(request_port(req), DEFAULT_HTTP_PORT)
  523. def test_request_host(self):
  524. from urllib2 import Request
  525. from cookielib import request_host
  526. # this request is illegal (RFC2616, 14.2.3)
  527. req = Request("http://1.1.1.1/",
  528. headers={"Host": "www.acme.com:80"})
  529. # libwww-perl wants this response, but that seems wrong (RFC 2616,
  530. # section 5.2, point 1., and RFC 2965 section 1, paragraph 3)
  531. #self.assertEquals(request_host(req), "www.acme.com")
  532. self.assertEquals(request_host(req), "1.1.1.1")
  533. req = Request("http://www.acme.com/",
  534. headers={"Host": "irrelevant.com"})
  535. self.assertEquals(request_host(req), "www.acme.com")
  536. # not actually sure this one is valid Request object, so maybe should
  537. # remove test for no host in url in request_host function?
  538. req = Request("/resource.html",
  539. headers={"Host": "www.acme.com"})
  540. self.assertEquals(request_host(req), "www.acme.com")
  541. # port shouldn't be in request-host
  542. req = Request("http://www.acme.com:2345/resource.html",
  543. headers={"Host": "www.acme.com:5432"})
  544. self.assertEquals(request_host(req), "www.acme.com")
  545. def test_is_HDN(self):
  546. from cookielib import is_HDN
  547. self.assert_(is_HDN("foo.bar.com"))
  548. self.assert_(is_HDN("1foo2.3bar4.5com"))
  549. self.assert_(not is_HDN("192.168.1.1"))
  550. self.assert_(not is_HDN(""))
  551. self.assert_(not is_HDN("."))
  552. self.assert_(not is_HDN(".foo.bar.com"))
  553. self.assert_(not is_HDN("..foo"))
  554. self.assert_(not is_HDN("foo."))
  555. def test_reach(self):
  556. from cookielib import reach
  557. self.assertEquals(reach("www.acme.com"), ".acme.com")
  558. self.assertEquals(reach("acme.com"), "acme.com")
  559. self.assertEquals(reach("acme.local"), ".local")
  560. self.assertEquals(reach(".local"), ".local")
  561. self.assertEquals(reach(".com"), ".com")
  562. self.assertEquals(reach("."), ".")
  563. self.assertEquals(reach(""), "")
  564. self.assertEquals(reach("192.168.0.1"), "192.168.0.1")
  565. def test_domain_match(self):
  566. from cookielib import domain_match, user_domain_match
  567. self.assert_(domain_match("192.168.1.1", "192.168.1.1"))
  568. self.assert_(not domain_match("192.168.1.1", ".168.1.1"))
  569. self.assert_(domain_match("x.y.com", "x.Y.com"))
  570. self.assert_(domain_match("x.y.com", ".Y.com"))
  571. self.assert_(not domain_match("x.y.com", "Y.com"))
  572. self.assert_(domain_match("a.b.c.com", ".c.com"))
  573. self.assert_(not domain_match(".c.com", "a.b.c.com"))
  574. self.assert_(domain_match("example.local", ".local"))
  575. self.assert_(not domain_match("blah.blah", ""))
  576. self.assert_(not domain_match("", ".rhubarb.rhubarb"))
  577. self.assert_(domain_match("", ""))
  578. self.assert_(user_domain_match("acme.com", "acme.com"))
  579. self.assert_(not user_domain_match("acme.com", ".acme.com"))
  580. self.assert_(user_domain_match("rhubarb.acme.com", ".acme.com"))
  581. self.assert_(user_domain_match("www.rhubarb.acme.com", ".acme.com"))
  582. self.assert_(user_domain_match("x.y.com", "x.Y.com"))
  583. self.assert_(user_domain_match("x.y.com", ".Y.com"))
  584. self.assert_(not user_domain_match("x.y.com", "Y.com"))
  585. self.assert_(user_domain_match("y.com", "Y.com"))
  586. self.assert_(not user_domain_match(".y.com", "Y.com"))
  587. self.assert_(user_domain_match(".y.com", ".Y.com"))
  588. self.assert_(user_domain_match("x.y.com", ".com"))
  589. self.assert_(not user_domain_match("x.y.com", "com"))
  590. self.assert_(not user_domain_match("x.y.com", "m"))
  591. self.assert_(not user_domain_match("x.y.com", ".m"))
  592. self.assert_(not user_domain_match("x.y.com", ""))
  593. self.assert_(not user_domain_match("x.y.com", "."))
  594. self.assert_(user_domain_match("192.168.1.1", "192.168.1.1"))
  595. # not both HDNs, so must string-compare equal to match
  596. self.assert_(not user_domain_match("192.168.1.1", ".168.1.1"))
  597. self.assert_(not user_domain_match("192.168.1.1", "."))
  598. # empty string is a special case
  599. self.assert_(not user_domain_match("192.168.1.1", ""))
  600. def test_wrong_domain(self):
  601. # Cookies whose effective request-host name does not domain-match the
  602. # domain are rejected.
  603. # XXX far from complete
  604. from cookielib import CookieJar
  605. c = CookieJar()
  606. interact_2965(c, "http://www.nasty.com/",
  607. 'foo=bar; domain=friendly.org; Version="1"')
  608. self.assertEquals(len(c), 0)
  609. def test_strict_domain(self):
  610. # Cookies whose domain is a country-code tld like .co.uk should
  611. # not be set if CookiePolicy.strict_domain is true.
  612. from cookielib import CookieJar, DefaultCookiePolicy
  613. cp = DefaultCookiePolicy(strict_domain=True)
  614. cj = CookieJar(policy=cp)
  615. interact_netscape(cj, "http://example.co.uk/", 'no=problemo')
  616. interact_netscape(cj, "http://example.co.uk/",
  617. 'okey=dokey; Domain=.example.co.uk')
  618. self.assertEquals(len(cj), 2)
  619. for pseudo_tld in [".co.uk", ".org.za", ".tx.us", ".name.us"]:
  620. interact_netscape(cj, "http://example.%s/" % pseudo_tld,
  621. 'spam=eggs; Domain=.co.uk')
  622. self.assertEquals(len(cj), 2)
  623. def test_two_component_domain_ns(self):
  624. # Netscape: .www.bar.com, www.bar.com, .bar.com, bar.com, no domain
  625. # should all get accepted, as should .acme.com, acme.com and no domain
  626. # for 2-component domains like acme.com.
  627. from cookielib import CookieJar, DefaultCookiePolicy
  628. c = CookieJar()
  629. # two-component V0 domain is OK
  630. interact_netscape(c, "http://foo.net/", 'ns=bar')
  631. self.assertEquals(len(c), 1)
  632. self.assertEquals(c._cookies["foo.net"]["/"]["ns"].value, "bar")
  633. self.assertEquals(interact_netscape(c, "http://foo.net/"), "ns=bar")
  634. # *will* be returned to any other domain (unlike RFC 2965)...
  635. self.assertEquals(interact_netscape(c, "http://www.foo.net/"),
  636. "ns=bar")
  637. # ...unless requested otherwise
  638. pol = DefaultCookiePolicy(
  639. strict_ns_domain=DefaultCookiePolicy.DomainStrictNonDomain)
  640. c.set_policy(pol)
  641. self.assertEquals(interact_netscape(c, "http://www.foo.net/"), "")
  642. # unlike RFC 2965, even explicit two-component domain is OK,
  643. # because .foo.net matches foo.net
  644. interact_netscape(c, "http://foo.net/foo/",
  645. 'spam1=eggs; domain=foo.net')
  646. # even if starts with a dot -- in NS rules, .foo.net matches foo.net!
  647. interact_netscape(c, "http://foo.net/foo/bar/",
  648. 'spam2=eggs; domain=.foo.net')
  649. self.assertEquals(len(c), 3)
  650. self.assertEquals(c._cookies[".foo.net"]["/foo"]["spam1"].value,
  651. "eggs")
  652. self.assertEquals(c._cookies[".foo.net"]["/foo/bar"]["spam2"].value,
  653. "eggs")
  654. self.assertEquals(interact_netscape(c, "http://foo.net/foo/bar/"),
  655. "spam2=eggs; spam1=eggs; ns=bar")
  656. # top-level domain is too general
  657. interact_netscape(c, "http://foo.net/", 'nini="ni"; domain=.net')
  658. self.assertEquals(len(c), 3)
  659. ## # Netscape protocol doesn't allow non-special top level domains (such
  660. ## # as co.uk) in the domain attribute unless there are at least three
  661. ## # dots in it.
  662. # Oh yes it does! Real implementations don't check this, and real
  663. # cookies (of course) rely on that behaviour.
  664. interact_netscape(c, "http://foo.co.uk", 'nasty=trick; domain=.co.uk')
  665. ## self.assertEquals(len(c), 2)
  666. self.assertEquals(len(c), 4)
  667. def test_two_component_domain_rfc2965(self):
  668. from cookielib import CookieJar, DefaultCookiePolicy
  669. pol = DefaultCookiePolicy(rfc2965=True)
  670. c = CookieJar(pol)
  671. # two-component V1 domain is OK
  672. interact_2965(c, "http://foo.net/", 'foo=bar; Version="1"')
  673. self.assertEquals(len(c), 1)
  674. self.assertEquals(c._cookies["foo.net"]["/"]["foo"].value, "bar")
  675. self.assertEquals(interact_2965(c, "http://foo.net/"),
  676. "$Version=1; foo=bar")
  677. # won't be returned to any other domain (because domain was implied)
  678. self.assertEquals(interact_2965(c, "http://www.foo.net/"), "")
  679. # unless domain is given explicitly, because then it must be
  680. # rewritten to start with a dot: foo.net --> .foo.net, which does
  681. # not domain-match foo.net
  682. interact_2965(c, "http://foo.net/foo",
  683. 'spam=eggs; domain=foo.net; path=/foo; Version="1"')
  684. self.assertEquals(len(c), 1)
  685. self.assertEquals(interact_2965(c, "http://foo.net/foo"),
  686. "$Version=1; foo=bar")
  687. # explicit foo.net from three-component domain www.foo.net *does* get
  688. # set, because .foo.net domain-matches .foo.net
  689. interact_2965(c, "http://www.foo.net/foo/",
  690. 'spam=eggs; domain=foo.net; Version="1"')
  691. self.assertEquals(c._cookies[".foo.net"]["/foo/"]["spam"].value,
  692. "eggs")
  693. self.assertEquals(len(c), 2)
  694. self.assertEquals(interact_2965(c, "http://foo.net/foo/"),
  695. "$Version=1; foo=bar")
  696. self.assertEquals(interact_2965(c, "http://www.foo.net/foo/"),
  697. '$Version=1; spam=eggs; $Domain="foo.net"')
  698. # top-level domain is too general
  699. interact_2965(c, "http://foo.net/",
  700. 'ni="ni"; domain=".net"; Version="1"')
  701. self.assertEquals(len(c), 2)
  702. # RFC 2965 doesn't require blocking this
  703. interact_2965(c, "http://foo.co.uk/",
  704. 'nasty=trick; domain=.co.uk; Version="1"')
  705. self.assertEquals(len(c), 3)
  706. def test_domain_allow(self):
  707. from cookielib import CookieJar, DefaultCookiePolicy
  708. from urllib2 import Request
  709. c = CookieJar(policy=DefaultCookiePolicy(
  710. blocked_domains=["acme.com"],
  711. allowed_domains=["www.acme.com"]))
  712. req = Request("http://acme.com/")
  713. headers = ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/"]
  714. res = FakeResponse(headers, "http://acme.com/")
  715. c.extract_cookies(res, req)
  716. self.assertEquals(len(c), 0)
  717. req = Request("http://www.acme.com/")
  718. res = FakeResponse(headers, "http://www.acme.com/")
  719. c.extract_cookies(res, req)
  720. self.assertEquals(len(c), 1)
  721. req = Request("http://www.coyote.com/")
  722. res = FakeResponse(headers, "http://www.coyote.com/")
  723. c.extract_cookies(res, req)
  724. self.assertEquals(len(c), 1)
  725. # set a cookie with non-allowed domain...
  726. req = Request("http://www.coyote.com/")
  727. res = FakeResponse(headers, "http://www.coyote.com/")
  728. cookies = c.make_cookies(res, req)
  729. c.set_cookie(cookies[0])
  730. self.assertEquals(len(c), 2)
  731. # ... and check is doesn't get returned
  732. c.add_cookie_header(req)
  733. self.assert_(not req.has_header("Cookie"))
  734. def test_domain_block(self):
  735. from cookielib import CookieJar, DefaultCookiePolicy
  736. from urllib2 import Request
  737. pol = DefaultCookiePolicy(
  738. rfc2965=True, blocked_domains=[".acme.com"])
  739. c = CookieJar(policy=pol)
  740. headers = ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/"]
  741. req = Request("http://www.acme.com/")
  742. res = FakeResponse(headers, "http://www.acme.com/")
  743. c.extract_cookies(res, req)
  744. self.assertEquals(len(c), 0)
  745. p = pol.set_blocked_domains(["acme.com"])
  746. c.extract_cookies(res, req)
  747. self.assertEquals(len(c), 1)
  748. c.clear()
  749. req = Request("http://www.roadrunner.net/")
  750. res = FakeResponse(headers, "http://www.roadrunner.net/")
  751. c.extract_cookies(res, req)
  752. self.assertEquals(len(c), 1)
  753. req = Request("http://www.roadrunner.net/")
  754. c.add_cookie_header(req)
  755. self.assert_((req.has_header("Cookie") and
  756. req.has_header("Cookie2")))
  757. c.clear()
  758. pol.set_blocked_domains([".acme.com"])
  759. c.extract_cookies(res, req)
  760. self.assertEquals(len(c), 1)
  761. # set a cookie with blocked domain...
  762. req = Request("http://www.acme.com/")
  763. res = FakeResponse(headers, "http://www.acme.com/")
  764. cookies = c.make_cookies(res, req)
  765. c.set_cookie(cookies[0])
  766. self.assertEquals(len(c), 2)
  767. # ... and check is doesn't get returned
  768. c.add_cookie_header(req)
  769. self.assert_(not req.has_header("Cookie"))
  770. def test_secure(self):
  771. from cookielib import CookieJar, DefaultCookiePolicy
  772. for ns in True, False:
  773. for whitespace in " ", "":
  774. c = CookieJar()
  775. if ns:
  776. pol = DefaultCookiePolicy(rfc2965=False)
  777. int = interact_netscape
  778. vs = ""
  779. else:
  780. pol = DefaultCookiePolicy(rfc2965=True)
  781. int = interact_2965
  782. vs = "; Version=1"
  783. c.set_policy(pol)
  784. url = "http://www.acme.com/"
  785. int(c, url, "foo1=bar%s%s" % (vs, whitespace))
  786. int(c, url, "foo2=bar%s; secure%s" % (vs, whitespace))
  787. self.assert_(
  788. not c._cookies["www.acme.com"]["/"]["foo1"].secure,
  789. "non-secure cookie registered secure")
  790. self.assert_(
  791. c._cookies["www.acme.com"]["/"]["foo2"].secure,
  792. "secure cookie registered non-secure")
  793. def test_quote_cookie_value(self):
  794. from cookielib import CookieJar, DefaultCookiePolicy
  795. c = CookieJar(policy=DefaultCookiePolicy(rfc2965=True))
  796. interact_2965(c, "http://www.acme.com/", r'foo=\b"a"r; Version=1')
  797. h = interact_2965(c, "http://www.acme.com/")
  798. self.assertEquals(h, r'$Version=1; foo=\\b\"a\"r')
  799. def test_missing_final_slash(self):
  800. # Missing slash from request URL's abs_path should be assumed present.
  801. from cookielib import CookieJar, DefaultCookiePolicy
  802. from urllib2 import Request
  803. url = "http://www.acme.com"
  804. c = CookieJar(DefaultCookiePolicy(rfc2965=True))
  805. interact_2965(c, url, "foo=bar; Version=1")
  806. req = Request(url)
  807. self.assertEquals(len(c), 1)
  808. c.add_cookie_header(req)
  809. self.assert_(req.has_header("Cookie"))
  810. def test_domain_mirror(self):
  811. from cookielib import CookieJar, DefaultCookiePolicy
  812. pol = DefaultCookiePolicy(rfc2965=True)
  813. c = CookieJar(pol)
  814. url = "http://foo.bar.com/"
  815. interact_2965(c, url, "spam=eggs; Version=1")
  816. h = interact_2965(c, url)
  817. self.assert_("Domain" not in h,
  818. "absent domain returned with domain present")
  819. c = CookieJar(pol)
  820. url = "http://foo.bar.com/"
  821. interact_2965(c, url, 'spam=eggs; Version=1; Domain=.bar.com')
  822. h = interact_2965(c, url)
  823. self.assert_('$Domain=".bar.com"' in h, "domain not returned")
  824. c = CookieJar(pol)
  825. url = "http://foo.bar.com/"
  826. # note missing initial dot in Domain
  827. interact_2965(c, url, 'spam=eggs; Version=1; Domain=bar.com')
  828. h = interact_2965(c, url)
  829. self.assert_('$Domain="bar.com"' in h, "domain not returned")
  830. def test_path_mirror(self):
  831. from cookielib import CookieJar, DefaultCookiePolicy
  832. pol = DefaultCookiePolicy(rfc2965=True)
  833. c = CookieJar(pol)
  834. url = "http://foo.bar.com/"
  835. interact_2965(c, url, "spam=eggs; Version=1")
  836. h = interact_2965(c, url)
  837. self.assert_("Path" not in h,
  838. "absent path returned with path present")
  839. c = CookieJar(pol)
  840. url = "http://foo.bar.com/"
  841. interact_2965(c, url, 'spam=eggs; Version=1; Path=/')
  842. h = interact_2965(c, url)
  843. self.assert_('$Path="/"' in h, "path not returned")
  844. def test_port_mirror(self):
  845. from cookielib import CookieJar, DefaultCookiePolicy
  846. pol = DefaultCookiePolicy(rfc2965=True)
  847. c = CookieJar(pol)
  848. url = "http://foo.bar.com/"
  849. interact_2965(c, url, "spam=eggs; Version=1")
  850. h = interact_2965(c, url)
  851. self.assert_("Port" not in h,
  852. "absent port returned with port present")
  853. c = CookieJar(pol)
  854. url = "http://foo.bar.com/"
  855. interact_2965(c, url, "spam=eggs; Version=1; Port")
  856. h = interact_2965(c, url)
  857. self.assert_(re.search("\$Port([^=]|$)", h),
  858. "port with no value not returned with no value")
  859. c = CookieJar(pol)
  860. url = "http://foo.bar.com/"
  861. interact_2965(c, url, 'spam=eggs; Version=1; Port="80"')
  862. h = interact_2965(c, url)
  863. self.assert_('$Port="80"' in h,
  864. "port with single value not returned with single value")
  865. c = CookieJar(pol)
  866. url = "http://foo.bar.com/"
  867. interact_2965(c, url, 'spam=eggs; Version=1; Port="80,8080"')
  868. h = interact_2965(c, url)
  869. self.assert_('$Port="80,8080"' in h,
  870. "port with multiple values not returned with multiple "
  871. "values")
  872. def test_no_return_comment(self):
  873. from cookielib import CookieJar, DefaultCookiePolicy
  874. c = CookieJar(DefaultCookiePolicy(rfc2965=True))
  875. url = "http://foo.bar.com/"
  876. interact_2965(c, url, 'spam=eggs; Version=1; '
  877. 'Comment="does anybody read these?"; '
  878. 'CommentURL="http://foo.bar.net/comment.html"')
  879. h = interact_2965(c, url)
  880. self.assert_(
  881. "Comment" not in h,
  882. "Comment or CommentURL cookie-attributes returned to server")
  883. def test_Cookie_iterator(self):
  884. from cookielib import CookieJar, Cookie, DefaultCookiePolicy
  885. cs = CookieJar(DefaultCookiePolicy(rfc2965=True))
  886. # add some random cookies
  887. interact_2965(cs, "http://blah.spam.org/", 'foo=eggs; Version=1; '
  888. 'Comment="does anybody read these?"; '
  889. 'CommentURL="http://foo.bar.net/comment.html"')
  890. interact_netscape(cs, "http://www.acme.com/blah/", "spam=bar; secure")
  891. interact_2965(cs, "http://www.acme.com/blah/",
  892. "foo=bar; secure; Version=1")
  893. interact_2965(cs, "http://www.acme.com/blah/",
  894. "foo=bar; path=/; Version=1")
  895. interact_2965(cs, "http://www.sol.no",
  896. r'bang=wallop; version=1; domain=".sol.no"; '
  897. r'port="90,100, 80,8080"; '
  898. r'max-age=100; Comment = "Just kidding! (\"|\\\\) "')
  899. versions = [1, 1, 1, 0, 1]
  900. names = ["bang", "foo", "foo", "spam", "foo"]
  901. domains = [".sol.no", "blah.spam.org", "www.acme.com",
  902. "www.acme.com", "www.acme.com"]
  903. paths = ["/", "/", "/", "/blah", "/blah/"]
  904. for i in range(4):
  905. i = 0
  906. for c in cs:
  907. self.assert_(isinstance(c, Cookie))
  908. self.assertEquals(c.version, versions[i])
  909. self.assertEquals(c.name, names[i])
  910. self.assertEquals(c.domain, domains[i])
  911. self.assertEquals(c.path, paths[i])
  912. i = i + 1
  913. def test_parse_ns_headers(self):
  914. from cookielib import parse_ns_headers
  915. # missing domain value (invalid cookie)
  916. self.assertEquals(
  917. parse_ns_headers(["foo=bar; path=/; domain"]),
  918. [[("foo", "bar"),
  919. ("path", "/"), ("domain", None), ("version", "0")]]
  920. )
  921. # invalid expires value
  922. self.assertEquals(
  923. parse_ns_headers(["foo=bar; expires=Foo Bar 12 33:22:11 2000"]),
  924. [[("foo", "bar"), ("expires", None), ("version", "0")]]
  925. )
  926. # missing cookie value (valid cookie)
  927. self.assertEquals(
  928. parse_ns_headers(["foo"]),
  929. [[("foo", None), ("version", "0")]]
  930. )
  931. # shouldn't add version if header is empty
  932. self.assertEquals(parse_ns_headers([""]), [])
  933. def test_bad_cookie_header(self):
  934. def cookiejar_from_cookie_headers(headers):
  935. from cookielib import CookieJar
  936. from urllib2 import Request
  937. c = CookieJar()
  938. req = Request("http://www.example.com/")
  939. r = FakeResponse(headers, "http://www.example.com/")
  940. c.extract_cookies(r, req)
  941. return c
  942. # none of these bad headers should cause an exception to be raised
  943. for headers in [
  944. ["Set-Cookie: "], # actually, nothing wrong with this
  945. ["Set-Cookie2: "], # ditto
  946. # missing domain value
  947. ["Set-Cookie2: a=foo; path=/; Version=1; domain"],
  948. # bad max-age
  949. ["Set-Cookie: b=foo; max-age=oops"],
  950. ]:
  951. c = cookiejar_from_cookie_headers(headers)
  952. # these bad cookies shouldn't be set
  953. self.assertEquals(len(c), 0)
  954. # cookie with invalid expires is treated as session cookie
  955. headers = ["Set-Cookie: c=foo; expires=Foo Bar 12 33:22:11 2000"]
  956. c = cookiejar_from_cookie_headers(headers)
  957. cookie = c._cookies["www.example.com"]["/"]["c"]
  958. self.assert_(cookie.expires is None)
  959. class LWPCookieTests(TestCase):
  960. # Tests taken from libwww-perl, with a few modifications and additions.
  961. def test_netscape_example_1(self):
  962. from cookielib import CookieJar, DefaultCookiePolicy
  963. from urllib2 import Request
  964. #-------------------------------------------------------------------
  965. # First we check that it works for the original example at
  966. # http://www.netscape.com/newsref/std/cookie_spec.html
  967. # Client requests a document, and receives in the response:
  968. #
  969. # Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT
  970. #
  971. # When client requests a URL in path "/" on this server, it sends:
  972. #
  973. # Cookie: CUSTOMER=WILE_E_COYOTE
  974. #
  975. # Client requests a document, and receives in the response:
  976. #
  977. # Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/
  978. #
  979. # When client requests a URL in path "/" on this server, it sends:
  980. #
  981. # Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001
  982. #
  983. # Client receives:
  984. #
  985. # Set-Cookie: SHIPPING=FEDEX; path=/fo
  986. #
  987. # When client requests a URL in path "/" on this server, it sends:
  988. #
  989. # Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001
  990. #
  991. # When client requests a URL in path "/foo" on this server, it sends:
  992. #
  993. # Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001; SHIPPING=FEDEX
  994. #
  995. # The last Cookie is buggy, because both specifications say that the
  996. # most specific cookie must be sent first. SHIPPING=FEDEX is the
  997. # most specific and should thus be first.
  998. year_plus_one = time.localtime()[0] + 1
  999. headers = []
  1000. c = CookieJar(DefaultCookiePolicy(rfc2965 = True))
  1001. #req = Request("http://1.1.1.1/",
  1002. # headers={"Host": "www.acme.com:80"})
  1003. req = Request("http://www.acme.com:80/",
  1004. headers={"Host": "www.acme.com:80"})
  1005. headers.append(
  1006. "Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/ ; "
  1007. "expires=Wednesday, 09-Nov-%d 23:12:40 GMT" % year_plus_one)
  1008. res = FakeResponse(headers, "http://www.acme.com/")
  1009. c.extract_cookies(res, req)
  1010. req = Request("http://www.acme.com/")
  1011. c.add_cookie_header(req)
  1012. self.assertEqual(req.get_header("Cookie"), "CUSTOMER=WILE_E_COYOTE")
  1013. self.assertEqual(req.get_header("Cookie2"), '$Version="1"')
  1014. headers.append("Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/")
  1015. res = FakeResponse(headers, "http://www.acme.com/")
  1016. c.extract_cookies(res, req)
  1017. req = Request("http://www.acme.com/foo/bar")
  1018. c.add_cookie_header(req)
  1019. h = req.get_header("Cookie")
  1020. self.assert_("PART_NUMBER=ROCKET_LAUNCHER_0001" in h and
  1021. "CUSTOMER=WILE_E_COYOTE" in h)
  1022. headers.append('Set-Cookie: SHIPPING=FEDEX; path=/foo')
  1023. res = FakeResponse(headers, "http:/…