Back to index

python3.2  3.2.2
Classes | Functions | Variables
test.test_http_cookiejar Namespace Reference

Classes

class  DateTimeTests
class  HeaderTests
class  FakeResponse
class  FileCookieJarTests
class  LWPCookieTests

Functions

def interact_2965
def interact_netscape
def _interact
def test_domain_return_ok
def test_missing_value
def test_rfc2109_handling
def test_ns_parser
def test_ns_parser_special_names
def test_expires
def test_default_path
def test_default_path_with_query
def test_escape_path
def test_request_path
def test_request_port
def test_request_host
def test_is_HDN
def test_reach
def test_domain_match
def test_wrong_domain
def test_strict_domain
def test_two_component_domain_ns
def test_two_component_domain_rfc2965
 

Netscape protocol doesn't allow non-special top level domains (such

as co.uk) in the domain attribute unless there are at least three

dots in it.


def test_domain_allow
def test_domain_block
def test_secure
def test_quote_cookie_value
def test_missing_final_slash
def test_domain_mirror
def test_path_mirror
def test_port_mirror
def test_no_return_comment
def test_Cookie_iterator
def test_parse_ns_headers
def test_bad_cookie_header
def test_url_encoding
def test_mozilla
def test_netscape_misc
def test_intranet_domains_2965
def test_intranet_domains_ns
def test_empty_path
def test_session_cookies
def test_main

Variables

tuple cookie
 
    cookie = interact_2965(c, "http://www.sol.no/foo/",
                      r'foo8=bar; version=1; path="/%66oo"')

but this is OK, because '<' is not an allowed HTTP URL path character:

 filename = test.support.TESTFN
tuple old = repr(c)
tuple c = LWPCookieJar(policy=pol)

Detailed Description

Tests for http/cookiejar.py.

Class Documentation

class test::test_http_cookiejar::CookieTests

Definition at line 275 of file test_http_cookiejar.py.


Function Documentation

def test.test_http_cookiejar._interact (   cookiejar,
  url,
  set_cookie_hdrs,
  hdr_name 
) [private]
Perform a single request / response cycle, returning Cookie: header.

Definition at line 221 of file test_http_cookiejar.py.

00221 
00222 def _interact(cookiejar, url, set_cookie_hdrs, hdr_name):
00223     """Perform a single request / response cycle, returning Cookie: header."""
00224     req = urllib.request.Request(url)
00225     cookiejar.add_cookie_header(req)
00226     cookie_hdr = req.get_header("Cookie", "")
00227     headers = []
00228     for hdr in set_cookie_hdrs:
00229         headers.append("%s: %s" % (hdr_name, hdr))
00230     res = FakeResponse(headers, url)
00231     cookiejar.extract_cookies(res, req)
00232     return cookie_hdr
00233 

Here is the caller graph for this function:

def test.test_http_cookiejar.interact_2965 (   cookiejar,
  url,
  set_cookie_hdrs 
)

Definition at line 215 of file test_http_cookiejar.py.

00215 
00216 def interact_2965(cookiejar, url, *set_cookie_hdrs):
00217     return _interact(cookiejar, url, set_cookie_hdrs, "Set-Cookie2")

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_http_cookiejar.interact_netscape (   cookiejar,
  url,
  set_cookie_hdrs 
)

Definition at line 218 of file test_http_cookiejar.py.

00218 
00219 def interact_netscape(cookiejar, url, *set_cookie_hdrs):
00220     return _interact(cookiejar, url, set_cookie_hdrs, "Set-Cookie")

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 1029 of file test_http_cookiejar.py.

01029 
01030     def test_bad_cookie_header(self):
01031 
01032         def cookiejar_from_cookie_headers(headers):
01033             c = CookieJar()
01034             req = urllib.request.Request("http://www.example.com/")
01035             r = FakeResponse(headers, "http://www.example.com/")
01036             c.extract_cookies(r, req)
01037             return c
01038 
01039         # none of these bad headers should cause an exception to be raised
01040         for headers in [
01041             ["Set-Cookie: "],  # actually, nothing wrong with this
01042             ["Set-Cookie2: "],  # ditto
01043             # missing domain value
01044             ["Set-Cookie2: a=foo; path=/; Version=1; domain"],
01045             # bad max-age
01046             ["Set-Cookie: b=foo; max-age=oops"],
01047             # bad version
01048             ["Set-Cookie: b=foo; version=spam"],
01049             ]:
01050             c = cookiejar_from_cookie_headers(headers)
01051             # these bad cookies shouldn't be set
01052             self.assertEqual(len(c), 0)
01053 
01054         # cookie with invalid expires is treated as session cookie
01055         headers = ["Set-Cookie: c=foo; expires=Foo Bar 12 33:22:11 2000"]
01056         c = cookiejar_from_cookie_headers(headers)
01057         cookie = c._cookies["www.example.com"]["/"]["c"]
01058         self.assertTrue(cookie.expires is None)
01059 

Definition at line 977 of file test_http_cookiejar.py.

00977 
00978     def test_Cookie_iterator(self):
00979         cs = CookieJar(DefaultCookiePolicy(rfc2965=True))
00980         # add some random cookies
00981         interact_2965(cs, "http://blah.spam.org/", 'foo=eggs; Version=1; '
00982                       'Comment="does anybody read these?"; '
00983                       'CommentURL="http://foo.bar.net/comment.html"')
00984         interact_netscape(cs, "http://www.acme.com/blah/", "spam=bar; secure")
00985         interact_2965(cs, "http://www.acme.com/blah/",
00986                       "foo=bar; secure; Version=1")
00987         interact_2965(cs, "http://www.acme.com/blah/",
00988                       "foo=bar; path=/; Version=1")
00989         interact_2965(cs, "http://www.sol.no",
00990                       r'bang=wallop; version=1; domain=".sol.no"; '
00991                       r'port="90,100, 80,8080"; '
00992                       r'max-age=100; Comment = "Just kidding! (\"|\\\\) "')
00993 
00994         versions = [1, 1, 1, 0, 1]
00995         names = ["bang", "foo", "foo", "spam", "foo"]
00996         domains = [".sol.no", "blah.spam.org", "www.acme.com",
00997                    "www.acme.com", "www.acme.com"]
00998         paths = ["/", "/", "/", "/blah", "/blah/"]
00999 
01000         for i in range(4):
01001             i = 0
01002             for c in cs:
01003                 self.assertTrue(isinstance(c, Cookie))
01004                 self.assertEqual(c.version, versions[i])
01005                 self.assertEqual(c.name, names[i])
01006                 self.assertEqual(c.domain, domains[i])
01007                 self.assertEqual(c.path, paths[i])
01008                 i = i + 1

Here is the call graph for this function:

Definition at line 500 of file test_http_cookiejar.py.

00500 
00501     def test_default_path(self):
00502         # RFC 2965
00503         pol = DefaultCookiePolicy(rfc2965=True)
00504 
00505         c = CookieJar(pol)
00506         interact_2965(c, "http://www.acme.com/", 'spam="bar"; Version="1"')
00507         self.assertIn("/", c._cookies["www.acme.com"])
00508 
00509         c = CookieJar(pol)
00510         interact_2965(c, "http://www.acme.com/blah", 'eggs="bar"; Version="1"')
00511         self.assertIn("/", c._cookies["www.acme.com"])
00512 
00513         c = CookieJar(pol)
00514         interact_2965(c, "http://www.acme.com/blah/rhubarb",
00515                       'eggs="bar"; Version="1"')
00516         self.assertIn("/blah/", c._cookies["www.acme.com"])
00517 
00518         c = CookieJar(pol)
00519         interact_2965(c, "http://www.acme.com/blah/rhubarb/",
00520                       'eggs="bar"; Version="1"')
00521         self.assertIn("/blah/rhubarb/", c._cookies["www.acme.com"])
00522 
00523         # Netscape
00524 
00525         c = CookieJar()
00526         interact_netscape(c, "http://www.acme.com/", 'spam="bar"')
00527         self.assertIn("/", c._cookies["www.acme.com"])
00528 
00529         c = CookieJar()
00530         interact_netscape(c, "http://www.acme.com/blah", 'eggs="bar"')
00531         self.assertIn("/", c._cookies["www.acme.com"])
00532 
00533         c = CookieJar()
00534         interact_netscape(c, "http://www.acme.com/blah/rhubarb", 'eggs="bar"')
00535         self.assertIn("/blah", c._cookies["www.acme.com"])
00536 
00537         c = CookieJar()
00538         interact_netscape(c, "http://www.acme.com/blah/rhubarb/", 'eggs="bar"')
00539         self.assertIn("/blah/rhubarb", c._cookies["www.acme.com"])

Here is the call graph for this function:

Definition at line 540 of file test_http_cookiejar.py.

00540 
00541     def test_default_path_with_query(self):
00542         cj = CookieJar()
00543         uri = "http://example.com/?spam/eggs"
00544         value = 'eggs="bar"'
00545         interact_netscape(cj, uri, value)
00546         # Default path does not include query, so is "/", not "/?spam".
00547         self.assertIn("/", cj._cookies["example.com"])
00548         # Cookie is sent back to the same URI.
00549         self.assertEqual(interact_netscape(cj, uri), value)

Here is the call graph for this function:

Definition at line 787 of file test_http_cookiejar.py.

00787 
00788     def test_domain_allow(self):
00789         c = CookieJar(policy=DefaultCookiePolicy(
00790             blocked_domains=["acme.com"],
00791             allowed_domains=["www.acme.com"]))
00792 
00793         req = urllib.request.Request("http://acme.com/")
00794         headers = ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/"]
00795         res = FakeResponse(headers, "http://acme.com/")
00796         c.extract_cookies(res, req)
00797         self.assertEqual(len(c), 0)
00798 
00799         req = urllib.request.Request("http://www.acme.com/")
00800         res = FakeResponse(headers, "http://www.acme.com/")
00801         c.extract_cookies(res, req)
00802         self.assertEqual(len(c), 1)
00803 
00804         req = urllib.request.Request("http://www.coyote.com/")
00805         res = FakeResponse(headers, "http://www.coyote.com/")
00806         c.extract_cookies(res, req)
00807         self.assertEqual(len(c), 1)
00808 
00809         # set a cookie with non-allowed domain...
00810         req = urllib.request.Request("http://www.coyote.com/")
00811         res = FakeResponse(headers, "http://www.coyote.com/")
00812         cookies = c.make_cookies(res, req)
00813         c.set_cookie(cookies[0])
00814         self.assertEqual(len(c), 2)
00815         # ... and check is doesn't get returned
00816         c.add_cookie_header(req)
00817         self.assertTrue(not req.has_header("Cookie"))

Definition at line 818 of file test_http_cookiejar.py.

00818 
00819     def test_domain_block(self):
00820         pol = DefaultCookiePolicy(
00821             rfc2965=True, blocked_domains=[".acme.com"])
00822         c = CookieJar(policy=pol)
00823         headers = ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/"]
00824 
00825         req = urllib.request.Request("http://www.acme.com/")
00826         res = FakeResponse(headers, "http://www.acme.com/")
00827         c.extract_cookies(res, req)
00828         self.assertEqual(len(c), 0)
00829 
00830         p = pol.set_blocked_domains(["acme.com"])
00831         c.extract_cookies(res, req)
00832         self.assertEqual(len(c), 1)
00833 
00834         c.clear()
00835         req = urllib.request.Request("http://www.roadrunner.net/")
00836         res = FakeResponse(headers, "http://www.roadrunner.net/")
00837         c.extract_cookies(res, req)
00838         self.assertEqual(len(c), 1)
00839         req = urllib.request.Request("http://www.roadrunner.net/")
00840         c.add_cookie_header(req)
00841         self.assertTrue((req.has_header("Cookie") and
00842                       req.has_header("Cookie2")))
00843 
00844         c.clear()
00845         pol.set_blocked_domains([".acme.com"])
00846         c.extract_cookies(res, req)
00847         self.assertEqual(len(c), 1)
00848 
00849         # set a cookie with blocked domain...
00850         req = urllib.request.Request("http://www.acme.com/")
00851         res = FakeResponse(headers, "http://www.acme.com/")
00852         cookies = c.make_cookies(res, req)
00853         c.set_cookie(cookies[0])
00854         self.assertEqual(len(c), 2)
00855         # ... and check is doesn't get returned
00856         c.add_cookie_header(req)
00857         self.assertTrue(not req.has_header("Cookie"))

Definition at line 635 of file test_http_cookiejar.py.

00635 
00636     def test_domain_match(self):
00637         self.assertTrue(domain_match("192.168.1.1", "192.168.1.1"))
00638         self.assertTrue(not domain_match("192.168.1.1", ".168.1.1"))
00639         self.assertTrue(domain_match("x.y.com", "x.Y.com"))
00640         self.assertTrue(domain_match("x.y.com", ".Y.com"))
00641         self.assertTrue(not domain_match("x.y.com", "Y.com"))
00642         self.assertTrue(domain_match("a.b.c.com", ".c.com"))
00643         self.assertTrue(not domain_match(".c.com", "a.b.c.com"))
00644         self.assertTrue(domain_match("example.local", ".local"))
00645         self.assertTrue(not domain_match("blah.blah", ""))
00646         self.assertTrue(not domain_match("", ".rhubarb.rhubarb"))
00647         self.assertTrue(domain_match("", ""))
00648 
00649         self.assertTrue(user_domain_match("acme.com", "acme.com"))
00650         self.assertTrue(not user_domain_match("acme.com", ".acme.com"))
00651         self.assertTrue(user_domain_match("rhubarb.acme.com", ".acme.com"))
00652         self.assertTrue(user_domain_match("www.rhubarb.acme.com", ".acme.com"))
00653         self.assertTrue(user_domain_match("x.y.com", "x.Y.com"))
00654         self.assertTrue(user_domain_match("x.y.com", ".Y.com"))
00655         self.assertTrue(not user_domain_match("x.y.com", "Y.com"))
00656         self.assertTrue(user_domain_match("y.com", "Y.com"))
00657         self.assertTrue(not user_domain_match(".y.com", "Y.com"))
00658         self.assertTrue(user_domain_match(".y.com", ".Y.com"))
00659         self.assertTrue(user_domain_match("x.y.com", ".com"))
00660         self.assertTrue(not user_domain_match("x.y.com", "com"))
00661         self.assertTrue(not user_domain_match("x.y.com", "m"))
00662         self.assertTrue(not user_domain_match("x.y.com", ".m"))
00663         self.assertTrue(not user_domain_match("x.y.com", ""))
00664         self.assertTrue(not user_domain_match("x.y.com", "."))
00665         self.assertTrue(user_domain_match("192.168.1.1", "192.168.1.1"))
00666         # not both HDNs, so must string-compare equal to match
00667         self.assertTrue(not user_domain_match("192.168.1.1", ".168.1.1"))
00668         self.assertTrue(not user_domain_match("192.168.1.1", "."))
00669         # empty string is a special case
00670         self.assertTrue(not user_domain_match("192.168.1.1", ""))

Here is the call graph for this function:

Definition at line 897 of file test_http_cookiejar.py.

00897 
00898     def test_domain_mirror(self):
00899         pol = DefaultCookiePolicy(rfc2965=True)
00900 
00901         c = CookieJar(pol)
00902         url = "http://foo.bar.com/"
00903         interact_2965(c, url, "spam=eggs; Version=1")
00904         h = interact_2965(c, url)
00905         self.assertNotIn("Domain", h,
00906                      "absent domain returned with domain present")
00907 
00908         c = CookieJar(pol)
00909         url = "http://foo.bar.com/"
00910         interact_2965(c, url, 'spam=eggs; Version=1; Domain=.bar.com')
00911         h = interact_2965(c, url)
00912         self.assertIn('$Domain=".bar.com"', h, "domain not returned")
00913 
00914         c = CookieJar(pol)
00915         url = "http://foo.bar.com/"
00916         # note missing initial dot in Domain
00917         interact_2965(c, url, 'spam=eggs; Version=1; Domain=bar.com')
00918         h = interact_2965(c, url)
00919         self.assertIn('$Domain="bar.com"', h, "domain not returned")

Here is the call graph for this function:

  • Quoting: only quotes around the expires value are recognized as such (and yes, some folks quote the expires value); quotes around any other value are treated as part of the value.
  • White space: white space around names and values is ignored
  • Default path: if no path parameter is given, the path defaults to the path in the request-uri up to, but not including, the last '/'. Note that this is entirely different from what the spec says.
  • Commas and other delimiters: Netscape just parses until the next ';'. This means it will allow commas etc inside values (and yes, both commas and equals are commonly appear in the cookie value). This also means that if you fold multiple Set-Cookie header fields into one, comma-separated list, it'll be a headache to parse (at least my head starts hurting everytime I think of that code).
  • Expires: You'll get all sorts of date formats in the expires, including emtpy expires attributes ("expires="). Be as flexible as you can, and certainly don't expect the weekday to be there; if you can't parse it, just ignore it and pretend it's a session cookie.
  • Domain-matching: Netscape uses the 2-dot rule for all domains, not just the 7 special TLD's listed in their spec. And folks rely on that...

Definition at line 322 of file test_http_cookiejar.py.

00322 
00323     def test_domain_return_ok(self):
00324         # test optimization: .domain_return_ok() should filter out most
00325         # domains in the CookieJar before we try to access them (because that
00326         # may require disk access -- in particular, with MSIECookieJar)
00327         # This is only a rough check for performance reasons, so it's not too
00328         # critical as long as it's sufficiently liberal.
00329         pol = DefaultCookiePolicy()
00330         for url, domain, ok in [
00331             ("http://foo.bar.com/", "blah.com", False),
00332             ("http://foo.bar.com/", "rhubarb.blah.com", False),
00333             ("http://foo.bar.com/", "rhubarb.foo.bar.com", False),
00334             ("http://foo.bar.com/", ".foo.bar.com", True),
00335             ("http://foo.bar.com/", "foo.bar.com", True),
00336             ("http://foo.bar.com/", ".bar.com", True),
00337             ("http://foo.bar.com/", "com", True),
00338             ("http://foo.com/", "rhubarb.foo.com", False),
00339             ("http://foo.com/", ".foo.com", True),
00340             ("http://foo.com/", "foo.com", True),
00341             ("http://foo.com/", "com", True),
00342             ("http://foo/", "rhubarb.foo", False),
00343             ("http://foo/", ".foo", True),
00344             ("http://foo/", "foo", True),
00345             ("http://foo/", "foo.local", True),
00346             ("http://foo/", ".local", True),
00347             ]:
00348             request = urllib.request.Request(url)
00349             r = pol.domain_return_ok(domain, request)
00350             if ok: self.assertTrue(r)
00351             else: self.assertTrue(not r)

Definition at line 1576 of file test_http_cookiejar.py.

01576 
01577     def test_empty_path(self):
01578         # Test for empty path
01579         # Broken web-server ORION/1.3.38 returns to the client response like
01580         #
01581         #       Set-Cookie: JSESSIONID=ABCDERANDOM123; Path=
01582         #
01583         # ie. with Path set to nothing.
01584         # In this case, extract_cookies() must set cookie to / (root)
01585         c = CookieJar(DefaultCookiePolicy(rfc2965 = True))
01586         headers = []
01587 
01588         req = urllib.request.Request("http://www.ants.com/")
01589         headers.append("Set-Cookie: JSESSIONID=ABCDERANDOM123; Path=")
01590         res = FakeResponse(headers, "http://www.ants.com/")
01591         c.extract_cookies(res, req)
01592 
01593         req = urllib.request.Request("http://www.ants.com/")
01594         c.add_cookie_header(req)
01595 
01596         self.assertEqual(req.get_header("Cookie"),
01597                          "JSESSIONID=ABCDERANDOM123")
01598         self.assertEqual(req.get_header("Cookie2"), '$Version="1"')
01599 
01600         # missing path in the request URI
01601         req = urllib.request.Request("http://www.ants.com:8080")
01602         c.add_cookie_header(req)
01603 
01604         self.assertEqual(req.get_header("Cookie"),
01605                          "JSESSIONID=ABCDERANDOM123")
01606         self.assertEqual(req.get_header("Cookie2"), '$Version="1"')

Definition at line 550 of file test_http_cookiejar.py.

00550 
00551     def test_escape_path(self):
00552         cases = [
00553             # quoted safe
00554             ("/foo%2f/bar", "/foo%2F/bar"),
00555             ("/foo%2F/bar", "/foo%2F/bar"),
00556             # quoted %
00557             ("/foo%%/bar", "/foo%%/bar"),
00558             # quoted unsafe
00559             ("/fo%19o/bar", "/fo%19o/bar"),
00560             ("/fo%7do/bar", "/fo%7Do/bar"),
00561             # unquoted safe
00562             ("/foo/bar&", "/foo/bar&"),
00563             ("/foo//bar", "/foo//bar"),
00564             ("\176/foo/bar", "\176/foo/bar"),
00565             # unquoted unsafe
00566             ("/foo\031/bar", "/foo%19/bar"),
00567             ("/\175foo/bar", "/%7Dfoo/bar"),
00568             # unicode, latin-1 range
00569             ("/foo/bar\u00fc", "/foo/bar%C3%BC"),     # UTF-8 encoded
00570             # unicode
00571             ("/foo/bar\uabcd", "/foo/bar%EA%AF%8D"),  # UTF-8 encoded
00572             ]
00573         for arg, result in cases:
00574             self.assertEqual(escape_path(arg), result)

Here is the call graph for this function:

Definition at line 461 of file test_http_cookiejar.py.

00461 
00462     def test_expires(self):
00463         # if expires is in future, keep cookie...
00464         c = CookieJar()
00465         future = time2netscape(time.time()+3600)
00466         interact_netscape(c, "http://www.acme.com/", 'spam="bar"; expires=%s' %
00467                           future)
00468         self.assertEqual(len(c), 1)
00469         now = time2netscape(time.time()-1)
00470         # ... and if in past or present, discard it
00471         interact_netscape(c, "http://www.acme.com/", 'foo="eggs"; expires=%s' %
00472                           now)
00473         h = interact_netscape(c, "http://www.acme.com/")
00474         self.assertEqual(len(c), 1)
00475         self.assertIn('spam="bar"', h)
00476         self.assertNotIn("foo", h)
00477 
00478         # max-age takes precedence over expires, and zero max-age is request to
00479         # delete both new cookie and any old matching cookie
00480         interact_netscape(c, "http://www.acme.com/", 'eggs="bar"; expires=%s' %
00481                           future)
00482         interact_netscape(c, "http://www.acme.com/", 'bar="bar"; expires=%s' %
00483                           future)
00484         self.assertEqual(len(c), 3)
00485         interact_netscape(c, "http://www.acme.com/", 'eggs="bar"; '
00486                           'expires=%s; max-age=0' % future)
00487         interact_netscape(c, "http://www.acme.com/", 'bar="bar"; '
00488                           'max-age=0; expires=%s' % future)
00489         h = interact_netscape(c, "http://www.acme.com/")
00490         self.assertEqual(len(c), 1)
00491 
00492         # test expiry at end of session for cookies with no expires attribute
00493         interact_netscape(c, "http://www.rhubarb.net/", 'whum="fizz"')
00494         self.assertEqual(len(c), 2)
00495         c.clear_session_cookies()
00496         self.assertEqual(len(c), 1)
00497         self.assertIn('spam="bar"', h)
00498 
00499         # XXX RFC 2965 expiry rules (some apply to V0 too)

Here is the call graph for this function:

Definition at line 1550 of file test_http_cookiejar.py.

01550 
01551     def test_intranet_domains_2965(self):
01552         # Test handling of local intranet hostnames without a dot.
01553         c = CookieJar(DefaultCookiePolicy(rfc2965=True))
01554         interact_2965(c, "http://example/",
01555                       "foo1=bar; PORT; Discard; Version=1;")
01556         cookie = interact_2965(c, "http://example/",
01557                                'foo2=bar; domain=".local"; Version=1')
01558         self.assertIn("foo1=bar", cookie)
01559 
01560         interact_2965(c, "http://example/", 'foo3=bar; Version=1')
01561         cookie = interact_2965(c, "http://example/")
01562         self.assertIn("foo2=bar", cookie)
01563         self.assertEqual(len(c), 3)

Here is the call graph for this function:

Definition at line 1564 of file test_http_cookiejar.py.

01564 
01565     def test_intranet_domains_ns(self):
01566         c = CookieJar(DefaultCookiePolicy(rfc2965 = False))
01567         interact_netscape(c, "http://example/", "foo1=bar")
01568         cookie = interact_netscape(c, "http://example/",
01569                                    'foo2=bar; domain=.local')
01570         self.assertEqual(len(c), 2)
01571         self.assertIn("foo1=bar", cookie)
01572 
01573         cookie = interact_netscape(c, "http://example/")
01574         self.assertIn("foo2=bar", cookie)
01575         self.assertEqual(len(c), 2)

Here is the call graph for this function:

Definition at line 615 of file test_http_cookiejar.py.

00615 
00616     def test_is_HDN(self):
00617         self.assertTrue(is_HDN("foo.bar.com"))
00618         self.assertTrue(is_HDN("1foo2.3bar4.5com"))
00619         self.assertTrue(not is_HDN("192.168.1.1"))
00620         self.assertTrue(not is_HDN(""))
00621         self.assertTrue(not is_HDN("."))
00622         self.assertTrue(not is_HDN(".foo.bar.com"))
00623         self.assertTrue(not is_HDN("..foo"))
00624         self.assertTrue(not is_HDN("foo."))

Here is the call graph for this function:

def test.test_http_cookiejar.test_main (   verbose = None)

Definition at line 1651 of file test_http_cookiejar.py.

01651 
01652 def test_main(verbose=None):
01653     test.support.run_unittest(
01654         DateTimeTests,
01655         HeaderTests,
01656         CookieTests,
01657         FileCookieJarTests,
01658         LWPCookieTests,
01659         )

Here is the call graph for this function:

Definition at line 887 of file test_http_cookiejar.py.

00887 
00888     def test_missing_final_slash(self):
00889         # Missing slash from request URL's abs_path should be assumed present.
00890         url = "http://www.acme.com"
00891         c = CookieJar(DefaultCookiePolicy(rfc2965=True))
00892         interact_2965(c, url, "foo=bar; Version=1")
00893         req = urllib.request.Request(url)
00894         self.assertEqual(len(c), 1)
00895         c.add_cookie_header(req)
00896         self.assertTrue(req.has_header("Cookie"))

Here is the call graph for this function:

Definition at line 352 of file test_http_cookiejar.py.

00352 
00353     def test_missing_value(self):
00354         # missing = sign in Cookie: header is regarded by Mozilla as a missing
00355         # name, and by http.cookiejar as a missing value
00356         filename = test.support.TESTFN
00357         c = MozillaCookieJar(filename)
00358         interact_netscape(c, "http://www.acme.com/", 'eggs')
00359         interact_netscape(c, "http://www.acme.com/", '"spam"; path=/foo/')
00360         cookie = c._cookies["www.acme.com"]["/"]["eggs"]
00361         self.assertTrue(cookie.value is None)
00362         self.assertEqual(cookie.name, "eggs")
00363         cookie = c._cookies["www.acme.com"]['/foo/']['"spam"']
00364         self.assertTrue(cookie.value is None)
00365         self.assertEqual(cookie.name, '"spam"')
00366         self.assertEqual(lwp_cookie_str(cookie), (
00367             r'"spam"; path="/foo/"; domain="www.acme.com"; '
00368             'path_spec; discard; version=0'))
00369         old_str = repr(c)
00370         c.save(ignore_expires=True, ignore_discard=True)
00371         try:
00372             c = MozillaCookieJar(filename)
00373             c.revert(ignore_expires=True, ignore_discard=True)
00374         finally:
00375             os.unlink(c.filename)
00376         # cookies unchanged apart from lost info re. whether path was specified
00377         self.assertEqual(
00378             repr(c),
00379             re.sub("path_specified=%s" % True, "path_specified=%s" % False,
00380                    old_str)
00381             )
00382         self.assertEqual(interact_netscape(c, "http://www.acme.com/foo/"),
00383                          '"spam"; eggs')

Here is the call graph for this function:

Definition at line 1486 of file test_http_cookiejar.py.

01486 
01487     def test_mozilla(self):
01488         # Save / load Mozilla/Netscape cookie file format.
01489         year_plus_one = time.localtime()[0] + 1
01490 
01491         filename = test.support.TESTFN
01492 
01493         c = MozillaCookieJar(filename,
01494                              policy=DefaultCookiePolicy(rfc2965=True))
01495         interact_2965(c, "http://www.acme.com/",
01496                       "foo1=bar; max-age=100; Version=1")
01497         interact_2965(c, "http://www.acme.com/",
01498                       'foo2=bar; port="80"; max-age=100; Discard; Version=1')
01499         interact_2965(c, "http://www.acme.com/", "foo3=bar; secure; Version=1")
01500 
01501         expires = "expires=09-Nov-%d 23:12:40 GMT" % (year_plus_one,)
01502         interact_netscape(c, "http://www.foo.com/",
01503                           "fooa=bar; %s" % expires)
01504         interact_netscape(c, "http://www.foo.com/",
01505                           "foob=bar; Domain=.foo.com; %s" % expires)
01506         interact_netscape(c, "http://www.foo.com/",
01507                           "fooc=bar; Domain=www.foo.com; %s" % expires)
01508 
01509         def save_and_restore(cj, ignore_discard):
01510             try:
01511                 cj.save(ignore_discard=ignore_discard)
01512                 new_c = MozillaCookieJar(filename,
01513                                          DefaultCookiePolicy(rfc2965=True))
01514                 new_c.load(ignore_discard=ignore_discard)
01515             finally:
01516                 try: os.unlink(filename)
01517                 except OSError: pass
01518             return new_c
01519 
01520         new_c = save_and_restore(c, True)
01521         self.assertEqual(len(new_c), 6)  # none discarded
01522         self.assertIn("name='foo1', value='bar'", repr(new_c))
01523 
01524         new_c = save_and_restore(c, False)
01525         self.assertEqual(len(new_c), 4)  # 2 of them discarded on save
01526         self.assertIn("name='foo1', value='bar'", repr(new_c))

Here is the call graph for this function:

Definition at line 1527 of file test_http_cookiejar.py.

01527 
01528     def test_netscape_misc(self):
01529         # Some additional Netscape cookies tests.
01530         c = CookieJar()
01531         headers = []
01532         req = urllib.request.Request("http://foo.bar.acme.com/foo")
01533 
01534         # Netscape allows a host part that contains dots
01535         headers.append("Set-Cookie: Customer=WILE_E_COYOTE; domain=.acme.com")
01536         res = FakeResponse(headers, "http://www.acme.com/foo")
01537         c.extract_cookies(res, req)
01538 
01539         # and that the domain is the same as the host without adding a leading
01540         # dot to the domain.  Should not quote even if strange chars are used
01541         # in the cookie value.
01542         headers.append("Set-Cookie: PART_NUMBER=3,4; domain=foo.bar.acme.com")
01543         res = FakeResponse(headers, "http://www.acme.com/foo")
01544         c.extract_cookies(res, req)
01545 
01546         req = urllib.request.Request("http://foo.bar.acme.com/foo")
01547         c.add_cookie_header(req)
01548         self.assertIn("PART_NUMBER=3,4", req.get_header("Cookie"))
01549         self.assertIn("Customer=WILE_E_COYOTE",req.get_header("Cookie"))

Definition at line 966 of file test_http_cookiejar.py.

00966 
00967     def test_no_return_comment(self):
00968         c = CookieJar(DefaultCookiePolicy(rfc2965=True))
00969         url = "http://foo.bar.com/"
00970         interact_2965(c, url, 'spam=eggs; Version=1; '
00971                       'Comment="does anybody read these?"; '
00972                       'CommentURL="http://foo.bar.net/comment.html"')
00973         h = interact_2965(c, url)
00974         self.assertTrue(
00975             "Comment" not in h,
00976             "Comment or CommentURL cookie-attributes returned to server")

Here is the call graph for this function:

Definition at line 415 of file test_http_cookiejar.py.

00415 
00416     def test_ns_parser(self):
00417         c = CookieJar()
00418         interact_netscape(c, "http://www.acme.com/",
00419                           'spam=eggs; DoMain=.acme.com; port; blArgh="feep"')
00420         interact_netscape(c, "http://www.acme.com/", 'ni=ni; port=80,8080')
00421         interact_netscape(c, "http://www.acme.com:80/", 'nini=ni')
00422         interact_netscape(c, "http://www.acme.com:80/", 'foo=bar; expires=')
00423         interact_netscape(c, "http://www.acme.com:80/", 'spam=eggs; '
00424                           'expires="Foo Bar 25 33:22:11 3022"')
00425 
00426         cookie = c._cookies[".acme.com"]["/"]["spam"]
00427         self.assertEqual(cookie.domain, ".acme.com")
00428         self.assertTrue(cookie.domain_specified)
00429         self.assertEqual(cookie.port, DEFAULT_HTTP_PORT)
00430         self.assertTrue(not cookie.port_specified)
00431         # case is preserved
00432         self.assertTrue(cookie.has_nonstandard_attr("blArgh") and
00433                      not cookie.has_nonstandard_attr("blargh"))
00434 
00435         cookie = c._cookies["www.acme.com"]["/"]["ni"]
00436         self.assertEqual(cookie.domain, "www.acme.com")
00437         self.assertTrue(not cookie.domain_specified)
00438         self.assertEqual(cookie.port, "80,8080")
00439         self.assertTrue(cookie.port_specified)
00440 
00441         cookie = c._cookies["www.acme.com"]["/"]["nini"]
00442         self.assertTrue(cookie.port is None)
00443         self.assertTrue(not cookie.port_specified)
00444 
00445         # invalid expires should not cause cookie to be dropped
00446         foo = c._cookies["www.acme.com"]["/"]["foo"]
00447         spam = c._cookies["www.acme.com"]["/"]["foo"]
00448         self.assertTrue(foo.expires is None)
00449         self.assertTrue(spam.expires is None)

Here is the call graph for this function:

Definition at line 450 of file test_http_cookiejar.py.

00450 
00451     def test_ns_parser_special_names(self):
00452         # names such as 'expires' are not special in first name=value pair
00453         # of Set-Cookie: header
00454         c = CookieJar()
00455         interact_netscape(c, "http://www.acme.com/", 'expires=eggs')
00456         interact_netscape(c, "http://www.acme.com/", 'version=eggs; spam=eggs')
00457 
00458         cookies = c._cookies["www.acme.com"]["/"]
00459         self.assertIn('expires', cookies)
00460         self.assertIn('version', cookies)

Here is the call graph for this function:

Definition at line 1009 of file test_http_cookiejar.py.

01009 
01010     def test_parse_ns_headers(self):
01011         # missing domain value (invalid cookie)
01012         self.assertEqual(
01013             parse_ns_headers(["foo=bar; path=/; domain"]),
01014             [[("foo", "bar"),
01015               ("path", "/"), ("domain", None), ("version", "0")]]
01016             )
01017         # invalid expires value
01018         self.assertEqual(
01019             parse_ns_headers(["foo=bar; expires=Foo Bar 12 33:22:11 2000"]),
01020             [[("foo", "bar"), ("expires", None), ("version", "0")]]
01021             )
01022         # missing cookie value (valid cookie)
01023         self.assertEqual(
01024             parse_ns_headers(["foo"]),
01025             [[("foo", None), ("version", "0")]]
01026             )
01027         # shouldn't add version if header is empty
01028         self.assertEqual(parse_ns_headers([""]), [])

Here is the call graph for this function:

Definition at line 920 of file test_http_cookiejar.py.

00920 
00921     def test_path_mirror(self):
00922         pol = DefaultCookiePolicy(rfc2965=True)
00923 
00924         c = CookieJar(pol)
00925         url = "http://foo.bar.com/"
00926         interact_2965(c, url, "spam=eggs; Version=1")
00927         h = interact_2965(c, url)
00928         self.assertNotIn("Path", h, "absent path returned with path present")
00929 
00930         c = CookieJar(pol)
00931         url = "http://foo.bar.com/"
00932         interact_2965(c, url, 'spam=eggs; Version=1; Path=/')
00933         h = interact_2965(c, url)
00934         self.assertIn('$Path="/"', h, "path not returned")

Here is the call graph for this function:

Definition at line 935 of file test_http_cookiejar.py.

00935 
00936     def test_port_mirror(self):
00937         pol = DefaultCookiePolicy(rfc2965=True)
00938 
00939         c = CookieJar(pol)
00940         url = "http://foo.bar.com/"
00941         interact_2965(c, url, "spam=eggs; Version=1")
00942         h = interact_2965(c, url)
00943         self.assertNotIn("Port", h, "absent port returned with port present")
00944 
00945         c = CookieJar(pol)
00946         url = "http://foo.bar.com/"
00947         interact_2965(c, url, "spam=eggs; Version=1; Port")
00948         h = interact_2965(c, url)
00949         self.assertTrue(re.search("\$Port([^=]|$)", h),
00950                      "port with no value not returned with no value")
00951 
00952         c = CookieJar(pol)
00953         url = "http://foo.bar.com/"
00954         interact_2965(c, url, 'spam=eggs; Version=1; Port="80"')
00955         h = interact_2965(c, url)
00956         self.assertIn('$Port="80"', h,
00957                       "port with single value not returned with single value")
00958 
00959         c = CookieJar(pol)
00960         url = "http://foo.bar.com/"
00961         interact_2965(c, url, 'spam=eggs; Version=1; Port="80,8080"')
00962         h = interact_2965(c, url)
00963         self.assertIn('$Port="80,8080"', h,
00964                       "port with multiple values not returned with multiple "
00965                       "values")

Here is the call graph for this function:

Definition at line 881 of file test_http_cookiejar.py.

00881 
00882     def test_quote_cookie_value(self):
00883         c = CookieJar(policy=DefaultCookiePolicy(rfc2965=True))
00884         interact_2965(c, "http://www.acme.com/", r'foo=\b"a"r; Version=1')
00885         h = interact_2965(c, "http://www.acme.com/")
00886         self.assertEqual(h, r'$Version=1; foo=\\b\"a\"r')

Here is the call graph for this function:

Definition at line 625 of file test_http_cookiejar.py.

00625 
00626     def test_reach(self):
00627         self.assertEqual(reach("www.acme.com"), ".acme.com")
00628         self.assertEqual(reach("acme.com"), "acme.com")
00629         self.assertEqual(reach("acme.local"), ".local")
00630         self.assertEqual(reach(".local"), ".local")
00631         self.assertEqual(reach(".com"), ".com")
00632         self.assertEqual(reach("."), ".")
00633         self.assertEqual(reach(""), "")
00634         self.assertEqual(reach("192.168.0.1"), "192.168.0.1")

Here is the call graph for this function:

Definition at line 599 of file test_http_cookiejar.py.

00599 
00600     def test_request_host(self):
00601         # this request is illegal (RFC2616, 14.2.3)
00602         req = urllib.request.Request("http://1.1.1.1/",
00603                                      headers={"Host": "www.acme.com:80"})
00604         # libwww-perl wants this response, but that seems wrong (RFC 2616,
00605         # section 5.2, point 1., and RFC 2965 section 1, paragraph 3)
00606         #self.assertEqual(request_host(req), "www.acme.com")
00607         self.assertEqual(request_host(req), "1.1.1.1")
00608         req = urllib.request.Request("http://www.acme.com/",
00609                                      headers={"Host": "irrelevant.com"})
00610         self.assertEqual(request_host(req), "www.acme.com")
00611         # port shouldn't be in request-host
00612         req = urllib.request.Request("http://www.acme.com:2345/resource.html",
00613                                      headers={"Host": "www.acme.com:5432"})
00614         self.assertEqual(request_host(req), "www.acme.com")

Here is the call graph for this function:

Definition at line 575 of file test_http_cookiejar.py.

00575 
00576     def test_request_path(self):
00577         # with parameters
00578         req = urllib.request.Request(
00579             "http://www.example.com/rheum/rhaponticum;"
00580             "foo=bar;sing=song?apples=pears&spam=eggs#ni")
00581         self.assertEqual(request_path(req),
00582                          "/rheum/rhaponticum;foo=bar;sing=song")
00583         # without parameters
00584         req = urllib.request.Request(
00585             "http://www.example.com/rheum/rhaponticum?"
00586             "apples=pears&spam=eggs#ni")
00587         self.assertEqual(request_path(req), "/rheum/rhaponticum")
00588         # missing final slash
00589         req = urllib.request.Request("http://www.example.com")
00590         self.assertEqual(request_path(req), "/")

Here is the call graph for this function:

Definition at line 591 of file test_http_cookiejar.py.

00591 
00592     def test_request_port(self):
00593         req = urllib.request.Request("http://www.acme.com:1234/",
00594                                      headers={"Host": "www.acme.com:4321"})
00595         self.assertEqual(request_port(req), "1234")
00596         req = urllib.request.Request("http://www.acme.com/",
00597                                      headers={"Host": "www.acme.com:4321"})
00598         self.assertEqual(request_port(req), DEFAULT_HTTP_PORT)

Here is the call graph for this function:

Definition at line 384 of file test_http_cookiejar.py.

00384 
00385     def test_rfc2109_handling(self):
00386         # RFC 2109 cookies are handled as RFC 2965 or Netscape cookies,
00387         # dependent on policy settings
00388         for rfc2109_as_netscape, rfc2965, version in [
00389             # default according to rfc2965 if not explicitly specified
00390             (None, False, 0),
00391             (None, True, 1),
00392             # explicit rfc2109_as_netscape
00393             (False, False, None),  # version None here means no cookie stored
00394             (False, True, 1),
00395             (True, False, 0),
00396             (True, True, 0),
00397             ]:
00398             policy = DefaultCookiePolicy(
00399                 rfc2109_as_netscape=rfc2109_as_netscape,
00400                 rfc2965=rfc2965)
00401             c = CookieJar(policy)
00402             interact_netscape(c, "http://www.example.com/", "ni=ni; Version=1")
00403             try:
00404                 cookie = c._cookies["www.example.com"]["/"]["ni"]
00405             except KeyError:
00406                 self.assertTrue(version is None)  # didn't expect a stored cookie
00407             else:
00408                 self.assertEqual(cookie.version, version)
00409                 # 2965 cookies are unaffected
00410                 interact_2965(c, "http://www.example.com/",
00411                               "foo=bar; Version=1")
00412                 if rfc2965:
00413                     cookie2965 = c._cookies["www.example.com"]["/"]["foo"]
00414                     self.assertEqual(cookie2965.version, 1)

Here is the call graph for this function:

Definition at line 858 of file test_http_cookiejar.py.

00858 
00859     def test_secure(self):
00860         for ns in True, False:
00861             for whitespace in " ", "":
00862                 c = CookieJar()
00863                 if ns:
00864                     pol = DefaultCookiePolicy(rfc2965=False)
00865                     int = interact_netscape
00866                     vs = ""
00867                 else:
00868                     pol = DefaultCookiePolicy(rfc2965=True)
00869                     int = interact_2965
00870                     vs = "; Version=1"
00871                 c.set_policy(pol)
00872                 url = "http://www.acme.com/"
00873                 int(c, url, "foo1=bar%s%s" % (vs, whitespace))
00874                 int(c, url, "foo2=bar%s; secure%s" %  (vs, whitespace))
00875                 self.assertTrue(
00876                     not c._cookies["www.acme.com"]["/"]["foo1"].secure,
00877                     "non-secure cookie registered secure")
00878                 self.assertTrue(
00879                     c._cookies["www.acme.com"]["/"]["foo2"].secure,
00880                     "secure cookie registered non-secure")

Definition at line 1607 of file test_http_cookiejar.py.

01607 
01608     def test_session_cookies(self):
01609         year_plus_one = time.localtime()[0] + 1
01610 
01611         # Check session cookies are deleted properly by
01612         # CookieJar.clear_session_cookies method
01613 
01614         req = urllib.request.Request('http://www.perlmeister.com/scripts')
01615         headers = []
01616         headers.append("Set-Cookie: s1=session;Path=/scripts")
01617         headers.append("Set-Cookie: p1=perm; Domain=.perlmeister.com;"
01618                        "Path=/;expires=Fri, 02-Feb-%d 23:24:20 GMT" %
01619                        year_plus_one)
01620         headers.append("Set-Cookie: p2=perm;Path=/;expires=Fri, "
01621                        "02-Feb-%d 23:24:20 GMT" % year_plus_one)
01622         headers.append("Set-Cookie: s2=session;Path=/scripts;"
01623                        "Domain=.perlmeister.com")
01624         headers.append('Set-Cookie2: s3=session;Version=1;Discard;Path="/"')
01625         res = FakeResponse(headers, 'http://www.perlmeister.com/scripts')
01626 
01627         c = CookieJar()
01628         c.extract_cookies(res, req)
01629         # How many session/permanent cookies do we have?
01630         counter = {"session_after": 0,
01631                    "perm_after": 0,
01632                    "session_before": 0,
01633                    "perm_before": 0}
01634         for cookie in c:
01635             key = "%s_before" % cookie.value
01636             counter[key] = counter[key] + 1
01637         c.clear_session_cookies()
01638         # How many now?
01639         for cookie in c:
01640             key = "%s_after" % cookie.value
01641             counter[key] = counter[key] + 1
01642 
01643         self.assertTrue(not (
01644             # a permanent cookie got lost accidently
01645             counter["perm_after"] != counter["perm_before"] or
01646             # a session cookie hasn't been cleared
01647             counter["session_after"] != 0 or
01648             # we didn't have session cookies in the first place
01649             counter["session_before"] == 0))
01650 

Definition at line 681 of file test_http_cookiejar.py.

00681 
00682     def test_strict_domain(self):
00683         # Cookies whose domain is a country-code tld like .co.uk should
00684         # not be set if CookiePolicy.strict_domain is true.
00685         cp = DefaultCookiePolicy(strict_domain=True)
00686         cj = CookieJar(policy=cp)
00687         interact_netscape(cj, "http://example.co.uk/", 'no=problemo')
00688         interact_netscape(cj, "http://example.co.uk/",
00689                           'okey=dokey; Domain=.example.co.uk')
00690         self.assertEqual(len(cj), 2)
00691         for pseudo_tld in [".co.uk", ".org.za", ".tx.us", ".name.us"]:
00692             interact_netscape(cj, "http://example.%s/" % pseudo_tld,
00693                               'spam=eggs; Domain=.co.uk')
00694             self.assertEqual(len(cj), 2)

Here is the call graph for this function:

Definition at line 695 of file test_http_cookiejar.py.

00695 
00696     def test_two_component_domain_ns(self):
00697         # Netscape: .www.bar.com, www.bar.com, .bar.com, bar.com, no domain
00698         # should all get accepted, as should .acme.com, acme.com and no domain
00699         # for 2-component domains like acme.com.
00700         c = CookieJar()
00701 
00702         # two-component V0 domain is OK
00703         interact_netscape(c, "http://foo.net/", 'ns=bar')
00704         self.assertEqual(len(c), 1)
00705         self.assertEqual(c._cookies["foo.net"]["/"]["ns"].value, "bar")
00706         self.assertEqual(interact_netscape(c, "http://foo.net/"), "ns=bar")
00707         # *will* be returned to any other domain (unlike RFC 2965)...
00708         self.assertEqual(interact_netscape(c, "http://www.foo.net/"),
00709                          "ns=bar")
00710         # ...unless requested otherwise
00711         pol = DefaultCookiePolicy(
00712             strict_ns_domain=DefaultCookiePolicy.DomainStrictNonDomain)
00713         c.set_policy(pol)
00714         self.assertEqual(interact_netscape(c, "http://www.foo.net/"), "")
00715 
00716         # unlike RFC 2965, even explicit two-component domain is OK,
00717         # because .foo.net matches foo.net
00718         interact_netscape(c, "http://foo.net/foo/",
00719                           'spam1=eggs; domain=foo.net')
00720         # even if starts with a dot -- in NS rules, .foo.net matches foo.net!
00721         interact_netscape(c, "http://foo.net/foo/bar/",
00722                           'spam2=eggs; domain=.foo.net')
00723         self.assertEqual(len(c), 3)
00724         self.assertEqual(c._cookies[".foo.net"]["/foo"]["spam1"].value,
00725                          "eggs")
00726         self.assertEqual(c._cookies[".foo.net"]["/foo/bar"]["spam2"].value,
00727                          "eggs")
00728         self.assertEqual(interact_netscape(c, "http://foo.net/foo/bar/"),
00729                          "spam2=eggs; spam1=eggs; ns=bar")
00730 
00731         # top-level domain is too general
00732         interact_netscape(c, "http://foo.net/", 'nini="ni"; domain=.net')
00733         self.assertEqual(len(c), 3)

Here is the call graph for this function:

Netscape protocol doesn't allow non-special top level domains (such

as co.uk) in the domain attribute unless there are at least three

dots in it.

Oh yes it does! Real implementations don't check this, and real cookies (of course) rely on that behaviour. self.assertEqual(len(c), 2)

Definition at line 743 of file test_http_cookiejar.py.

00743 
00744     def test_two_component_domain_rfc2965(self):
00745         pol = DefaultCookiePolicy(rfc2965=True)
00746         c = CookieJar(pol)
00747 
00748         # two-component V1 domain is OK
00749         interact_2965(c, "http://foo.net/", 'foo=bar; Version="1"')
00750         self.assertEqual(len(c), 1)
00751         self.assertEqual(c._cookies["foo.net"]["/"]["foo"].value, "bar")
00752         self.assertEqual(interact_2965(c, "http://foo.net/"),
00753                          "$Version=1; foo=bar")
00754         # won't be returned to any other domain (because domain was implied)
00755         self.assertEqual(interact_2965(c, "http://www.foo.net/"), "")
00756 
00757         # unless domain is given explicitly, because then it must be
00758         # rewritten to start with a dot: foo.net --> .foo.net, which does
00759         # not domain-match foo.net
00760         interact_2965(c, "http://foo.net/foo",
00761                       'spam=eggs; domain=foo.net; path=/foo; Version="1"')
00762         self.assertEqual(len(c), 1)
00763         self.assertEqual(interact_2965(c, "http://foo.net/foo"),
00764                          "$Version=1; foo=bar")
00765 
00766         # explicit foo.net from three-component domain www.foo.net *does* get
00767         # set, because .foo.net domain-matches .foo.net
00768         interact_2965(c, "http://www.foo.net/foo/",
00769                       'spam=eggs; domain=foo.net; Version="1"')
00770         self.assertEqual(c._cookies[".foo.net"]["/foo/"]["spam"].value,
00771                          "eggs")
00772         self.assertEqual(len(c), 2)
00773         self.assertEqual(interact_2965(c, "http://foo.net/foo/"),
00774                          "$Version=1; foo=bar")
00775         self.assertEqual(interact_2965(c, "http://www.foo.net/foo/"),
00776                          '$Version=1; spam=eggs; $Domain="foo.net"')
00777 
00778         # top-level domain is too general
00779         interact_2965(c, "http://foo.net/",
00780                       'ni="ni"; domain=".net"; Version="1"')
00781         self.assertEqual(len(c), 2)
00782 
00783         # RFC 2965 doesn't require blocking this
00784         interact_2965(c, "http://foo.co.uk/",
00785                       'nasty=trick; domain=.co.uk; Version="1"')
00786         self.assertEqual(len(c), 3)

Here is the call graph for this function:

Definition at line 1464 of file test_http_cookiejar.py.

01464 
01465     def test_url_encoding(self):
01466         # Try some URL encodings of the PATHs.
01467         # (the behaviour here has changed from libwww-perl)
01468         c = CookieJar(DefaultCookiePolicy(rfc2965=True))
01469         interact_2965(c, "http://www.acme.com/foo%2f%25/"
01470                          "%3c%3c%0Anew%C3%A5/%C3%A5",
01471                       "foo  =   bar; version    =   1")
01472 
01473         cookie = interact_2965(
01474             c, "http://www.acme.com/foo%2f%25/<<%0anew\345/\346\370\345",
01475             'bar=baz; path="/foo/"; version=1');
01476         version_re = re.compile(r'^\$version=\"?1\"?', re.I)
01477         self.assertIn("foo=bar", cookie)
01478         self.assertTrue(version_re.search(cookie))
01479 
01480         cookie = interact_2965(
01481             c, "http://www.acme.com/foo/%25/<<%0anew\345/\346\370\345")
01482         self.assertTrue(not cookie)
01483 
01484         # unicode URL doesn't raise exception
01485         cookie = interact_2965(c, "http://www.acme.com/\xfc")

Here is the call graph for this function:

Definition at line 671 of file test_http_cookiejar.py.

00671 
00672     def test_wrong_domain(self):
00673         # Cookies whose effective request-host name does not domain-match the
00674         # domain are rejected.
00675 
00676         # XXX far from complete
00677         c = CookieJar()
00678         interact_2965(c, "http://www.nasty.com/",
00679                       'foo=bar; domain=friendly.org; Version="1"')
00680         self.assertEqual(len(c), 0)

Here is the call graph for this function:


Variable Documentation

tuple test.test_http_cookiejar.c = LWPCookieJar(policy=pol)

Definition at line 1456 of file test_http_cookiejar.py.

Initial value:
00001 interact_2965(c, "http://www.sol.no/<oo/",
00002                                r'foo8=bar; version=1; path="/%3coo"')

    cookie = interact_2965(c, "http://www.sol.no/foo/",
                      r'foo8=bar; version=1; path="/%66oo"')

but this is OK, because '<' is not an allowed HTTP URL path character:

Definition at line 1445 of file test_http_cookiejar.py.

Definition at line 1450 of file test_http_cookiejar.py.

Definition at line 1454 of file test_http_cookiejar.py.