PageRenderTime 61ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/Lib/test/test_cookielib.py

http://unladen-swallow.googlecode.com/
Python | 1736 lines | 1696 code | 24 blank | 16 comment | 16 complexity | 2418aefed04ea32a2658b10d9c9f4ee3 MD5 | raw file
Possible License(s): 0BSD, BSD-3-Clause
  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://www.acme.com")
  1024. c.extract_cookies(res, req)
  1025. req = Request("http://www.acme.com/")
  1026. c.add_cookie_header(req)
  1027. h = req.get_header("Cookie")
  1028. self.assert_("PART_NUMBER=ROCKET_LAUNCHER_0001" in h and
  1029. "CUSTOMER=WILE_E_COYOTE" in h and
  1030. "SHIPPING=FEDEX" not in h)
  1031. req = Request("http://www.acme.com/foo/")
  1032. c.add_cookie_header(req)
  1033. h = req.get_header("Cookie")
  1034. self.assert_(("PART_NUMBER=ROCKET_LAUNCHER_0001" in h and
  1035. "CUSTOMER=WILE_E_COYOTE" in h and
  1036. h.startswith("SHIPPING=FEDEX;")))
  1037. def test_netscape_example_2(self):
  1038. from cookielib import CookieJar
  1039. from urllib2 import Request
  1040. # Second Example transaction sequence:
  1041. #
  1042. # Assume all mappings from above have been cleared.
  1043. #
  1044. # Client receives:
  1045. #
  1046. # Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/
  1047. #
  1048. # When client requests a URL in path "/" on this server, it sends:
  1049. #
  1050. # Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001
  1051. #
  1052. # Client receives:
  1053. #
  1054. # Set-Cookie: PART_NUMBER=RIDING_ROCKET_0023; path=/ammo
  1055. #
  1056. # When client requests a URL in path "/ammo" on this server, it sends:
  1057. #
  1058. # Cookie: PART_NUMBER=RIDING_ROCKET_0023; PART_NUMBER=ROCKET_LAUNCHER_0001
  1059. #
  1060. # NOTE: There are two name/value pairs named "PART_NUMBER" due to
  1061. # the inheritance of the "/" mapping in addition to the "/ammo" mapping.
  1062. c = CookieJar()
  1063. headers = []
  1064. req = Request("http://www.acme.com/")
  1065. headers.append("Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/")
  1066. res = FakeResponse(headers, "http://www.acme.com/")
  1067. c.extract_cookies(res, req)
  1068. req = Request("http://www.acme.com/")
  1069. c.add_cookie_header(req)
  1070. self.assertEquals(req.get_header("Cookie"),
  1071. "PART_NUMBER=ROCKET_LAUNCHER_0001")
  1072. headers.append(
  1073. "Set-Cookie: PART_NUMBER=RIDING_ROCKET_0023; path=/ammo")
  1074. res = FakeResponse(headers, "http://www.acme.com/")
  1075. c.extract_cookies(res, req)
  1076. req = Request("http://www.acme.com/ammo")
  1077. c.add_cookie_header(req)
  1078. self.assert_(re.search(r"PART_NUMBER=RIDING_ROCKET_0023;\s*"
  1079. "PART_NUMBER=ROCKET_LAUNCHER_0001",
  1080. req.get_header("Cookie")))
  1081. def test_ietf_example_1(self):
  1082. from cookielib import CookieJar, DefaultCookiePolicy
  1083. #-------------------------------------------------------------------
  1084. # Then we test with the examples from draft-ietf-http-state-man-mec-03.txt
  1085. #
  1086. # 5. EXAMPLES
  1087. c = CookieJar(DefaultCookiePolicy(rfc2965=True))
  1088. #
  1089. # 5.1 Example 1
  1090. #
  1091. # Most detail of request and response headers has been omitted. Assume
  1092. # the user agent has no stored cookies.
  1093. #
  1094. # 1. User Agent -> Server
  1095. #
  1096. # POST /acme/login HTTP/1.1
  1097. # [form data]
  1098. #
  1099. # User identifies self via a form.
  1100. #
  1101. # 2. Server -> User Agent
  1102. #
  1103. # HTTP/1.1 200 OK
  1104. # Set-Cookie2: Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"
  1105. #
  1106. # Cookie reflects user's identity.
  1107. cookie = interact_2965(
  1108. c, 'http://www.acme.com/acme/login',
  1109. 'Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"')
  1110. self.assert_(not cookie)
  1111. #
  1112. # 3. User Agent -> Server
  1113. #
  1114. # POST /acme/pickitem HTTP/1.1
  1115. # Cookie: $Version="1"; Customer="WILE_E_COYOTE"; $Path="/acme"
  1116. # [form data]
  1117. #
  1118. # User selects an item for ``shopping basket.''
  1119. #
  1120. # 4. Server -> User Agent
  1121. #
  1122. # HTTP/1.1 200 OK
  1123. # Set-Cookie2: Part_Number="Rocket_Launcher_0001"; Version="1";
  1124. # Path="/acme"
  1125. #
  1126. # Shopping basket contains an item.
  1127. cookie = interact_2965(c, 'http://www.acme.com/acme/pickitem',
  1128. 'Part_Number="Rocket_Launcher_0001"; '
  1129. 'Version="1"; Path="/acme"');
  1130. self.assert_(re.search(
  1131. r'^\$Version="?1"?; Customer="?WILE_E_COYOTE"?; \$Path="/acme"$',
  1132. cookie))
  1133. #
  1134. # 5. User Agent -> Server
  1135. #
  1136. # POST /acme/shipping HTTP/1.1
  1137. # Cookie: $Version="1";
  1138. # Customer="WILE_E_COYOTE"; $Path="/acme";
  1139. # Part_Number="Rocket_Launcher_0001"; $Path="/acme"
  1140. # [form data]
  1141. #
  1142. # User selects shipping method from form.
  1143. #
  1144. # 6. Server -> User Agent
  1145. #
  1146. # HTTP/1.1 200 OK
  1147. # Set-Cookie2: Shipping="FedEx"; Version="1"; Path="/acme"
  1148. #
  1149. # New cookie reflects shipping method.
  1150. cookie = interact_2965(c, "http://www.acme.com/acme/shipping",
  1151. 'Shipping="FedEx"; Version="1"; Path="/acme"')
  1152. self.assert_(re.search(r'^\$Version="?1"?;', cookie))
  1153. self.assert_(re.search(r'Part_Number="?Rocket_Launcher_0001"?;'
  1154. '\s*\$Path="\/acme"', cookie))
  1155. self.assert_(re.search(r'Customer="?WILE_E_COYOTE"?;\s*\$Path="\/acme"',
  1156. cookie))
  1157. #
  1158. # 7. User Agent -> Server
  1159. #
  1160. # POST /acme/process HTTP/1.1
  1161. # Cookie: $Version="1";
  1162. # Customer="WILE_E_COYOTE"; $Path="/acme";
  1163. # Part_Number="Rocket_Launcher_0001"; $Path="/acme";
  1164. # Shipping="FedEx"; $Path="/acme"
  1165. # [form data]
  1166. #
  1167. # User chooses to process order.
  1168. #
  1169. # 8. Server -> User Agent
  1170. #
  1171. # HTTP/1.1 200 OK
  1172. #
  1173. # Transaction is complete.
  1174. cookie = interact_2965(c, "http://www.acme.com/acme/process")
  1175. self.assert_(
  1176. re.search(r'Shipping="?FedEx"?;\s*\$Path="\/acme"', cookie) and
  1177. "WILE_E_COYOTE" in cookie)
  1178. #
  1179. # The user agent makes a series of requests on the origin server, after
  1180. # each of which it receives a new cookie. All the cookies have the same
  1181. # Path attribute and (default) domain. Because the request URLs all have
  1182. # /acme as a prefix, and that matches the Path attribute, each request
  1183. # contains all the cookies received so far.
  1184. def test_ietf_example_2(self):
  1185. from cookielib import CookieJar, DefaultCookiePolicy
  1186. # 5.2 Example 2
  1187. #
  1188. # This example illustrates the effect of the Path attribute. All detail
  1189. # of request and response headers has been omitted. Assume the user agent
  1190. # has no stored cookies.
  1191. c = CookieJar(DefaultCookiePolicy(rfc2965=True))
  1192. # Imagine the user agent has received, in response to earlier requests,
  1193. # the response headers
  1194. #
  1195. # Set-Cookie2: Part_Number="Rocket_Launcher_0001"; Version="1";
  1196. # Path="/acme"
  1197. #
  1198. # and
  1199. #
  1200. # Set-Cookie2: Part_Number="Riding_Rocket_0023"; Version="1";
  1201. # Path="/acme/ammo"
  1202. interact_2965(
  1203. c, "http://www.acme.com/acme/ammo/specific",
  1204. 'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"',
  1205. 'Part_Number="Riding_Rocket_0023"; Version="1"; Path="/acme/ammo"')
  1206. # A subsequent request by the user agent to the (same) server for URLs of
  1207. # the form /acme/ammo/... would include the following request header:
  1208. #
  1209. # Cookie: $Version="1";
  1210. # Part_Number="Riding_Rocket_0023"; $Path="/acme/ammo";
  1211. # Part_Number="Rocket_Launcher_0001"; $Path="/acme"
  1212. #
  1213. # Note that the NAME=VALUE pair for the cookie with the more specific Path
  1214. # attribute, /acme/ammo, comes before the one with the less specific Path
  1215. # attribute, /acme. Further note that the same cookie name appears more
  1216. # than once.
  1217. cookie = interact_2965(c, "http://www.acme.com/acme/ammo/...")
  1218. self.assert_(
  1219. re.search(r"Riding_Rocket_0023.*Rocket_Launcher_0001", cookie))
  1220. # A subsequent request by the user agent to the (same) server for a URL of
  1221. # the form /acme/parts/ would include the following request header:
  1222. #
  1223. # Cookie: $Version="1"; Part_Number="Rocket_Launcher_0001"; $Path="/acme"
  1224. #
  1225. # Here, the second cookie's Path attribute /acme/ammo is not a prefix of
  1226. # the request URL, /acme/parts/, so the cookie does not get forwarded to
  1227. # the server.
  1228. cookie = interact_2965(c, "http://www.acme.com/acme/parts/")
  1229. self.assert_("Rocket_Launcher_0001" in cookie and
  1230. "Riding_Rocket_0023" not in cookie)
  1231. def test_rejection(self):
  1232. # Test rejection of Set-Cookie2 responses based on domain, path, port.
  1233. from cookielib import DefaultCookiePolicy, LWPCookieJar
  1234. pol = DefaultCookiePolicy(rfc2965=True)
  1235. c = LWPCookieJar(policy=pol)
  1236. max_age = "max-age=3600"
  1237. # illegal domain (no embedded dots)
  1238. cookie = interact_2965(c, "http://www.acme.com",
  1239. 'foo=bar; domain=".com"; version=1')
  1240. self.assert_(not c)
  1241. # legal domain
  1242. cookie = interact_2965(c, "http://www.acme.com",
  1243. 'ping=pong; domain="acme.com"; version=1')
  1244. self.assertEquals(len(c), 1)
  1245. # illegal domain (host prefix "www.a" contains a dot)
  1246. cookie = interact_2965(c, "http://www.a.acme.com",
  1247. 'whiz=bang; domain="acme.com"; version=1')
  1248. self.assertEquals(len(c), 1)
  1249. # legal domain
  1250. cookie = interact_2965(c, "http://www.a.acme.com",
  1251. 'wow=flutter; domain=".a.acme.com"; version=1')
  1252. self.assertEquals(len(c), 2)
  1253. # can't partially match an IP-address
  1254. cookie = interact_2965(c, "http://125.125.125.125",
  1255. 'zzzz=ping; domain="125.125.125"; version=1')
  1256. self.assertEquals(len(c), 2)
  1257. # illegal path (must be prefix of request path)
  1258. cookie = interact_2965(c, "http://www.sol.no",
  1259. 'blah=rhubarb; domain=".sol.no"; path="/foo"; '
  1260. 'version=1')
  1261. self.assertEquals(len(c), 2)
  1262. # legal path
  1263. cookie = interact_2965(c, "http://www.sol.no/foo/bar",
  1264. 'bing=bong; domain=".sol.no"; path="/foo"; '
  1265. 'version=1')
  1266. self.assertEquals(len(c), 3)
  1267. # illegal port (request-port not in list)
  1268. cookie = interact_2965(c, "http://www.sol.no",
  1269. 'whiz=ffft; domain=".sol.no"; port="90,100"; '
  1270. 'version=1')
  1271. self.assertEquals(len(c), 3)
  1272. # legal port
  1273. cookie = interact_2965(
  1274. c, "http://www.sol.no",
  1275. r'bang=wallop; version=1; domain=".sol.no"; '
  1276. r'port="90,100, 80,8080"; '
  1277. r'max-age=100; Comment = "Just kidding! (\"|\\\\) "')
  1278. self.assertEquals(len(c), 4)
  1279. # port attribute without any value (current port)
  1280. cookie = interact_2965(c, "http://www.sol.no",
  1281. 'foo9=bar; version=1; domain=".sol.no"; port; '
  1282. 'max-age=100;')
  1283. self.assertEquals(len(c), 5)
  1284. # encoded path
  1285. # LWP has this test, but unescaping allowed path characters seems
  1286. # like a bad idea, so I think this should fail:
  1287. ## cookie = interact_2965(c, "http://www.sol.no/foo/",
  1288. ## r'foo8=bar; version=1; path="/%66oo"')
  1289. # but this is OK, because '<' is not an allowed HTTP URL path
  1290. # character:
  1291. cookie = interact_2965(c, "http://www.sol.no/<oo/",
  1292. r'foo8=bar; version=1; path="/%3coo"')
  1293. self.assertEquals(len(c), 6)
  1294. # save and restore
  1295. filename = test_support.TESTFN
  1296. try:
  1297. c.save(filename, ignore_discard=True)
  1298. old = repr(c)
  1299. c = LWPCookieJar(policy=pol)
  1300. c.load(filename, ignore_discard=True)
  1301. finally:
  1302. try: os.unlink(filename)
  1303. except OSError: pass
  1304. self.assertEquals(old, repr(c))
  1305. def test_url_encoding(self):
  1306. # Try some URL encodings of the PATHs.
  1307. # (the behaviour here has changed from libwww-perl)
  1308. from cookielib import CookieJar, DefaultCookiePolicy
  1309. c = CookieJar(DefaultCookiePolicy(rfc2965=True))
  1310. interact_2965(c, "http://www.acme.com/foo%2f%25/%3c%3c%0Anew%E5/%E5",
  1311. "foo = bar; version = 1")
  1312. cookie = interact_2965(
  1313. c, "http://www.acme.com/foo%2f%25/<<%0anewו/זרו",
  1314. 'bar=baz; path="/foo/"; version=1');
  1315. version_re = re.compile(r'^\$version=\"?1\"?', re.I)
  1316. self.assert_("foo=bar" in cookie and version_re.search(cookie))
  1317. cookie = interact_2965(
  1318. c, "http://www.acme.com/foo/%25/<<%0anewו/זרו")
  1319. self.assert_(not cookie)
  1320. # unicode URL doesn't raise exception
  1321. cookie = interact_2965(c, u"http://www.acme.com/\xfc")
  1322. def test_mozilla(self):
  1323. # Save / load Mozilla/Netscape cookie file format.
  1324. from cookielib import MozillaCookieJar, DefaultCookiePolicy
  1325. year_plus_one = time.localtime()[0] + 1
  1326. filename = test_support.TESTFN
  1327. c = MozillaCookieJar(filename,
  1328. policy=DefaultCookiePolicy(rfc2965=True))
  1329. interact_2965(c, "http://www.acme.com/",
  1330. "foo1=bar; max-age=100; Version=1")
  1331. interact_2965(c, "http://www.acme.com/",
  1332. 'foo2=bar; port="80"; max-age=100; Discard; Version=1')
  1333. interact_2965(c, "http://www.acme.com/", "foo3=bar; secure; Version=1")
  1334. expires = "expires=09-Nov-%d 23:12:40 GMT" % (year_plus_one,)
  1335. interact_netscape(c, "http://www.foo.com/",
  1336. "fooa=bar; %s" % expires)
  1337. interact_netscape(c, "http://www.foo.com/",
  1338. "foob=bar; Domain=.foo.com; %s" % expires)
  1339. interact_netscape(c, "http://www.foo.com/",
  1340. "fooc=bar; Domain=www.foo.com; %s" % expires)
  1341. def save_and_restore(cj, ignore_discard):
  1342. try:
  1343. cj.save(ignore_discard=ignore_discard)
  1344. new_c = MozillaCookieJar(filename,
  1345. DefaultCookiePolicy(rfc2965=True))
  1346. new_c.load(ignore_discard=ignore_discard)
  1347. finally:
  1348. try: os.unlink(filename)
  1349. except OSError: pass
  1350. return new_c
  1351. new_c = save_and_restore(c, True)
  1352. self.assertEquals(len(new_c), 6) # none discarded
  1353. self.assert_("name='foo1', value='bar'" in repr(new_c))
  1354. new_c = save_and_restore(c, False)
  1355. self.assertEquals(len(new_c), 4) # 2 of them discarded on save
  1356. self.assert_("name='foo1', value='bar'" in repr(new_c))
  1357. def test_netscape_misc(self):
  1358. # Some additional Netscape cookies tests.
  1359. from cookielib import CookieJar
  1360. from urllib2 import Request
  1361. c = CookieJar()
  1362. headers = []
  1363. req = Request("http://foo.bar.acme.com/foo")
  1364. # Netscape allows a host part that contains dots
  1365. headers.append("Set-Cookie: Customer=WILE_E_COYOTE; domain=.acme.com")
  1366. res = FakeResponse(headers, "http://www.acme.com/foo")
  1367. c.extract_cookies(res, req)
  1368. # and that the domain is the same as the host without adding a leading
  1369. # dot to the domain. Should not quote even if strange chars are used
  1370. # in the cookie value.
  1371. headers.append("Set-Cookie: PART_NUMBER=3,4; domain=foo.bar.acme.com")
  1372. res = FakeResponse(headers, "http://www.acme.com/foo")
  1373. c.extract_cookies(res, req)
  1374. req = Request("http://foo.bar.acme.com/foo")
  1375. c.add_cookie_header(req)
  1376. self.assert_(
  1377. "PART_NUMBER=3,4" in req.get_header("Cookie") and
  1378. "Customer=WILE_E_COYOTE" in req.get_header("Cookie"))
  1379. def test_intranet_domains_2965(self):
  1380. # Test handling of local intranet hostnames without a dot.
  1381. from cookielib import CookieJar, DefaultCookiePolicy
  1382. c = CookieJar(DefaultCookiePolicy(rfc2965=True))
  1383. interact_2965(c, "http://example/",
  1384. "foo1=bar; PORT; Discard; Version=1;")
  1385. cookie = interact_2965(c, "http://example/",
  1386. 'foo2=bar; domain=".local"; Version=1')
  1387. self.assert_("foo1=bar" in cookie)
  1388. interact_2965(c, "http://example/", 'foo3=bar; Version=1')
  1389. cookie = interact_2965(c, "http://example/")
  1390. self.assert_("foo2=bar" in cookie and len(c) == 3)
  1391. def test_intranet_domains_ns(self):
  1392. from cookielib import CookieJar, DefaultCookiePolicy
  1393. c = CookieJar(DefaultCookiePolicy(rfc2965 = False))
  1394. interact_netscape(c, "http://example/", "foo1=bar")
  1395. cookie = interact_netscape(c, "http://example/",
  1396. 'foo2=bar; domain=.local')
  1397. self.assertEquals(len(c), 2)
  1398. self.assert_("foo1=bar" in cookie)
  1399. cookie = interact_netscape(c, "http://example/")
  1400. self.assert_("foo2=bar" in cookie)
  1401. self.assertEquals(len(c), 2)
  1402. def test_empty_path(self):
  1403. from cookielib import CookieJar, DefaultCookiePolicy
  1404. from urllib2 import Request
  1405. # Test for empty path
  1406. # Broken web-server ORION/1.3.38 returns to the client response like
  1407. #
  1408. # Set-Cookie: JSESSIONID=ABCDERANDOM123; Path=
  1409. #
  1410. # ie. with Path set to nothing.
  1411. # In this case, extract_cookies() must set cookie to / (root)
  1412. c = CookieJar(DefaultCookiePolicy(rfc2965 = True))
  1413. headers = []
  1414. req = Request("http://www.ants.com/")
  1415. headers.append("Set-Cookie: JSESSIONID=ABCDERANDOM123; Path=")
  1416. res = FakeResponse(headers, "http://www.ants.com/")
  1417. c.extract_cookies(res, req)
  1418. req = Request("http://www.ants.com/")
  1419. c.add_cookie_header(req)
  1420. self.assertEquals(req.get_header("Cookie"),
  1421. "JSESSIONID=ABCDERANDOM123")
  1422. self.assertEquals(req.get_header("Cookie2"), '$Version="1"')
  1423. # missing path in the request URI
  1424. req = Request("http://www.ants.com:8080")
  1425. c.add_cookie_header(req)
  1426. self.assertEquals(req.get_header("Cookie"),
  1427. "JSESSIONID=ABCDERANDOM123")
  1428. self.assertEquals(req.get_header("Cookie2"), '$Version="1"')
  1429. def test_session_cookies(self):
  1430. from cookielib import CookieJar
  1431. from urllib2 import Request
  1432. year_plus_one = time.localtime()[0] + 1
  1433. # Check session cookies are deleted properly by
  1434. # CookieJar.clear_session_cookies method
  1435. req = Request('http://www.perlmeister.com/scripts')
  1436. headers = []
  1437. headers.append("Set-Cookie: s1=session;Path=/scripts")
  1438. headers.append("Set-Cookie: p1=perm; Domain=.perlmeister.com;"
  1439. "Path=/;expires=Fri, 02-Feb-%d 23:24:20 GMT" %
  1440. year_plus_one)
  1441. headers.append("Set-Cookie: p2=perm;Path=/;expires=Fri, "
  1442. "02-Feb-%d 23:24:20 GMT" % year_plus_one)
  1443. headers.append("Set-Cookie: s2=session;Path=/scripts;"
  1444. "Domain=.perlmeister.com")
  1445. headers.append('Set-Cookie2: s3=session;Version=1;Discard;Path="/"')
  1446. res = FakeResponse(headers, 'http://www.perlmeister.com/scripts')
  1447. c = CookieJar()
  1448. c.extract_cookies(res, req)
  1449. # How many session/permanent cookies do we have?
  1450. counter = {"session_after": 0,
  1451. "perm_after": 0,
  1452. "session_before": 0,
  1453. "perm_before": 0}
  1454. for cookie in c:
  1455. key = "%s_before" % cookie.value
  1456. counter[key] = counter[key] + 1
  1457. c.clear_session_cookies()
  1458. # How many now?
  1459. for cookie in c:
  1460. key = "%s_after" % cookie.value
  1461. counter[key] = counter[key] + 1
  1462. self.assert_(not (
  1463. # a permanent cookie got lost accidentally
  1464. counter["perm_after"] != counter["perm_before"] or
  1465. # a session cookie hasn't been cleared
  1466. counter["session_after"] != 0 or
  1467. # we didn't have session cookies in the first place
  1468. counter["session_before"] == 0))
  1469. def test_main(verbose=None):
  1470. from test import test_sets
  1471. test_support.run_unittest(
  1472. DateTimeTests,
  1473. HeaderTests,
  1474. CookieTests,
  1475. FileCookieJarTests,
  1476. LWPCookieTests,
  1477. )
  1478. if __name__ == "__main__":
  1479. test_main(verbose=True)