Back to index

python3.2  3.2.2
Public Member Functions
test.test_http_cookiejar.LWPCookieTests Class Reference

List of all members.

Public Member Functions

def test_netscape_example_1
def test_netscape_example_2
def test_ietf_example_1
def test_ietf_example_2
def test_rejection

Detailed Description

Definition at line 1060 of file test_http_cookiejar.py.


Member Function Documentation

Definition at line 1205 of file test_http_cookiejar.py.

01205 
01206     def test_ietf_example_1(self):
01207         #-------------------------------------------------------------------
01208         # Then we test with the examples from draft-ietf-http-state-man-mec-03.txt
01209         #
01210         # 5.  EXAMPLES
01211 
01212         c = CookieJar(DefaultCookiePolicy(rfc2965=True))
01213 
01214         #
01215         # 5.1  Example 1
01216         #
01217         # Most detail of request and response headers has been omitted.  Assume
01218         # the user agent has no stored cookies.
01219         #
01220         #   1.  User Agent -> Server
01221         #
01222         #       POST /acme/login HTTP/1.1
01223         #       [form data]
01224         #
01225         #       User identifies self via a form.
01226         #
01227         #   2.  Server -> User Agent
01228         #
01229         #       HTTP/1.1 200 OK
01230         #       Set-Cookie2: Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"
01231         #
01232         #       Cookie reflects user's identity.
01233 
01234         cookie = interact_2965(
01235             c, 'http://www.acme.com/acme/login',
01236             'Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"')
01237         self.assertTrue(not cookie)
01238 
01239         #
01240         #   3.  User Agent -> Server
01241         #
01242         #       POST /acme/pickitem HTTP/1.1
01243         #       Cookie: $Version="1"; Customer="WILE_E_COYOTE"; $Path="/acme"
01244         #       [form data]
01245         #
01246         #       User selects an item for ``shopping basket.''
01247         #
01248         #   4.  Server -> User Agent
01249         #
01250         #       HTTP/1.1 200 OK
01251         #       Set-Cookie2: Part_Number="Rocket_Launcher_0001"; Version="1";
01252         #               Path="/acme"
01253         #
01254         #       Shopping basket contains an item.
01255 
01256         cookie = interact_2965(c, 'http://www.acme.com/acme/pickitem',
01257                                'Part_Number="Rocket_Launcher_0001"; '
01258                                'Version="1"; Path="/acme"');
01259         self.assertTrue(re.search(
01260             r'^\$Version="?1"?; Customer="?WILE_E_COYOTE"?; \$Path="/acme"$',
01261             cookie))
01262 
01263         #
01264         #   5.  User Agent -> Server
01265         #
01266         #       POST /acme/shipping HTTP/1.1
01267         #       Cookie: $Version="1";
01268         #               Customer="WILE_E_COYOTE"; $Path="/acme";
01269         #               Part_Number="Rocket_Launcher_0001"; $Path="/acme"
01270         #       [form data]
01271         #
01272         #       User selects shipping method from form.
01273         #
01274         #   6.  Server -> User Agent
01275         #
01276         #       HTTP/1.1 200 OK
01277         #       Set-Cookie2: Shipping="FedEx"; Version="1"; Path="/acme"
01278         #
01279         #       New cookie reflects shipping method.
01280 
01281         cookie = interact_2965(c, "http://www.acme.com/acme/shipping",
01282                                'Shipping="FedEx"; Version="1"; Path="/acme"')
01283 
01284         self.assertTrue(re.search(r'^\$Version="?1"?;', cookie))
01285         self.assertTrue(re.search(r'Part_Number="?Rocket_Launcher_0001"?;'
01286                                '\s*\$Path="\/acme"', cookie))
01287         self.assertTrue(re.search(r'Customer="?WILE_E_COYOTE"?;\s*\$Path="\/acme"',
01288                                cookie))
01289 
01290         #
01291         #   7.  User Agent -> Server
01292         #
01293         #       POST /acme/process HTTP/1.1
01294         #       Cookie: $Version="1";
01295         #               Customer="WILE_E_COYOTE"; $Path="/acme";
01296         #               Part_Number="Rocket_Launcher_0001"; $Path="/acme";
01297         #               Shipping="FedEx"; $Path="/acme"
01298         #       [form data]
01299         #
01300         #       User chooses to process order.
01301         #
01302         #   8.  Server -> User Agent
01303         #
01304         #       HTTP/1.1 200 OK
01305         #
01306         #       Transaction is complete.
01307 
01308         cookie = interact_2965(c, "http://www.acme.com/acme/process")
01309         self.assertTrue(
01310             re.search(r'Shipping="?FedEx"?;\s*\$Path="\/acme"', cookie) and
01311             "WILE_E_COYOTE" in cookie)
01312 
01313         #
01314         # The user agent makes a series of requests on the origin server, after
01315         # each of which it receives a new cookie.  All the cookies have the same
01316         # Path attribute and (default) domain.  Because the request URLs all have
01317         # /acme as a prefix, and that matches the Path attribute, each request
01318         # contains all the cookies received so far.

Here is the call graph for this function:

Definition at line 1319 of file test_http_cookiejar.py.

01319 
01320     def test_ietf_example_2(self):
01321         # 5.2  Example 2
01322         #
01323         # This example illustrates the effect of the Path attribute.  All detail
01324         # of request and response headers has been omitted.  Assume the user agent
01325         # has no stored cookies.
01326 
01327         c = CookieJar(DefaultCookiePolicy(rfc2965=True))
01328 
01329         # Imagine the user agent has received, in response to earlier requests,
01330         # the response headers
01331         #
01332         # Set-Cookie2: Part_Number="Rocket_Launcher_0001"; Version="1";
01333         #         Path="/acme"
01334         #
01335         # and
01336         #
01337         # Set-Cookie2: Part_Number="Riding_Rocket_0023"; Version="1";
01338         #         Path="/acme/ammo"
01339 
01340         interact_2965(
01341             c, "http://www.acme.com/acme/ammo/specific",
01342             'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"',
01343             'Part_Number="Riding_Rocket_0023"; Version="1"; Path="/acme/ammo"')
01344 
01345         # A subsequent request by the user agent to the (same) server for URLs of
01346         # the form /acme/ammo/...  would include the following request header:
01347         #
01348         # Cookie: $Version="1";
01349         #         Part_Number="Riding_Rocket_0023"; $Path="/acme/ammo";
01350         #         Part_Number="Rocket_Launcher_0001"; $Path="/acme"
01351         #
01352         # Note that the NAME=VALUE pair for the cookie with the more specific Path
01353         # attribute, /acme/ammo, comes before the one with the less specific Path
01354         # attribute, /acme.  Further note that the same cookie name appears more
01355         # than once.
01356 
01357         cookie = interact_2965(c, "http://www.acme.com/acme/ammo/...")
01358         self.assertTrue(
01359             re.search(r"Riding_Rocket_0023.*Rocket_Launcher_0001", cookie))
01360 
01361         # A subsequent request by the user agent to the (same) server for a URL of
01362         # the form /acme/parts/ would include the following request header:
01363         #
01364         # Cookie: $Version="1"; Part_Number="Rocket_Launcher_0001"; $Path="/acme"
01365         #
01366         # Here, the second cookie's Path attribute /acme/ammo is not a prefix of
01367         # the request URL, /acme/parts/, so the cookie does not get forwarded to
01368         # the server.
01369 
01370         cookie = interact_2965(c, "http://www.acme.com/acme/parts/")
01371         self.assertIn("Rocket_Launcher_0001", cookie)
01372         self.assertNotIn("Riding_Rocket_0023", cookie)

Here is the call graph for this function:

Definition at line 1063 of file test_http_cookiejar.py.

01063 
01064     def test_netscape_example_1(self):
01065         #-------------------------------------------------------------------
01066         # First we check that it works for the original example at
01067         # http://www.netscape.com/newsref/std/cookie_spec.html
01068 
01069         # Client requests a document, and receives in the response:
01070         #
01071         #       Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/; expires=Wednesday, 09-Nov-99 23:12:40 GMT
01072         #
01073         # When client requests a URL in path "/" on this server, it sends:
01074         #
01075         #       Cookie: CUSTOMER=WILE_E_COYOTE
01076         #
01077         # Client requests a document, and receives in the response:
01078         #
01079         #       Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/
01080         #
01081         # When client requests a URL in path "/" on this server, it sends:
01082         #
01083         #       Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001
01084         #
01085         # Client receives:
01086         #
01087         #       Set-Cookie: SHIPPING=FEDEX; path=/fo
01088         #
01089         # When client requests a URL in path "/" on this server, it sends:
01090         #
01091         #       Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001
01092         #
01093         # When client requests a URL in path "/foo" on this server, it sends:
01094         #
01095         #       Cookie: CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001; SHIPPING=FEDEX
01096         #
01097         # The last Cookie is buggy, because both specifications say that the
01098         # most specific cookie must be sent first.  SHIPPING=FEDEX is the
01099         # most specific and should thus be first.
01100 
01101         year_plus_one = time.localtime()[0] + 1
01102 
01103         headers = []
01104 
01105         c = CookieJar(DefaultCookiePolicy(rfc2965 = True))
01106 
01107         #req = urllib.request.Request("http://1.1.1.1/",
01108         #              headers={"Host": "www.acme.com:80"})
01109         req = urllib.request.Request("http://www.acme.com:80/",
01110                       headers={"Host": "www.acme.com:80"})
01111 
01112         headers.append(
01113             "Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/ ; "
01114             "expires=Wednesday, 09-Nov-%d 23:12:40 GMT" % year_plus_one)
01115         res = FakeResponse(headers, "http://www.acme.com/")
01116         c.extract_cookies(res, req)
01117 
01118         req = urllib.request.Request("http://www.acme.com/")
01119         c.add_cookie_header(req)
01120 
01121         self.assertEqual(req.get_header("Cookie"), "CUSTOMER=WILE_E_COYOTE")
01122         self.assertEqual(req.get_header("Cookie2"), '$Version="1"')
01123 
01124         headers.append("Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/")
01125         res = FakeResponse(headers, "http://www.acme.com/")
01126         c.extract_cookies(res, req)
01127 
01128         req = urllib.request.Request("http://www.acme.com/foo/bar")
01129         c.add_cookie_header(req)
01130 
01131         h = req.get_header("Cookie")
01132         self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h)
01133         self.assertIn("CUSTOMER=WILE_E_COYOTE", h)
01134 
01135         headers.append('Set-Cookie: SHIPPING=FEDEX; path=/foo')
01136         res = FakeResponse(headers, "http://www.acme.com")
01137         c.extract_cookies(res, req)
01138 
01139         req = urllib.request.Request("http://www.acme.com/")
01140         c.add_cookie_header(req)
01141 
01142         h = req.get_header("Cookie")
01143         self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h)
01144         self.assertIn("CUSTOMER=WILE_E_COYOTE", h)
01145         self.assertNotIn("SHIPPING=FEDEX", h)
01146 
01147         req = urllib.request.Request("http://www.acme.com/foo/")
01148         c.add_cookie_header(req)
01149 
01150         h = req.get_header("Cookie")
01151         self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h)
01152         self.assertIn("CUSTOMER=WILE_E_COYOTE", h)
01153         self.assertTrue(h.startswith("SHIPPING=FEDEX;"))

Here is the call graph for this function:

Definition at line 1154 of file test_http_cookiejar.py.

01154 
01155     def test_netscape_example_2(self):
01156         # Second Example transaction sequence:
01157         #
01158         # Assume all mappings from above have been cleared.
01159         #
01160         # Client receives:
01161         #
01162         #       Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/
01163         #
01164         # When client requests a URL in path "/" on this server, it sends:
01165         #
01166         #       Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001
01167         #
01168         # Client receives:
01169         #
01170         #       Set-Cookie: PART_NUMBER=RIDING_ROCKET_0023; path=/ammo
01171         #
01172         # When client requests a URL in path "/ammo" on this server, it sends:
01173         #
01174         #       Cookie: PART_NUMBER=RIDING_ROCKET_0023; PART_NUMBER=ROCKET_LAUNCHER_0001
01175         #
01176         #       NOTE: There are two name/value pairs named "PART_NUMBER" due to
01177         #       the inheritance of the "/" mapping in addition to the "/ammo" mapping.
01178 
01179         c = CookieJar()
01180         headers = []
01181 
01182         req = urllib.request.Request("http://www.acme.com/")
01183         headers.append("Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/")
01184         res = FakeResponse(headers, "http://www.acme.com/")
01185 
01186         c.extract_cookies(res, req)
01187 
01188         req = urllib.request.Request("http://www.acme.com/")
01189         c.add_cookie_header(req)
01190 
01191         self.assertEqual(req.get_header("Cookie"),
01192                          "PART_NUMBER=ROCKET_LAUNCHER_0001")
01193 
01194         headers.append(
01195             "Set-Cookie: PART_NUMBER=RIDING_ROCKET_0023; path=/ammo")
01196         res = FakeResponse(headers, "http://www.acme.com/")
01197         c.extract_cookies(res, req)
01198 
01199         req = urllib.request.Request("http://www.acme.com/ammo")
01200         c.add_cookie_header(req)
01201 
01202         self.assertTrue(re.search(r"PART_NUMBER=RIDING_ROCKET_0023;\s*"
01203                                "PART_NUMBER=ROCKET_LAUNCHER_0001",
01204                                req.get_header("Cookie")))

Here is the call graph for this function:

Definition at line 1373 of file test_http_cookiejar.py.

01373 
01374     def test_rejection(self):
01375         # Test rejection of Set-Cookie2 responses based on domain, path, port.
01376         pol = DefaultCookiePolicy(rfc2965=True)
01377 
01378         c = LWPCookieJar(policy=pol)
01379 
01380         max_age = "max-age=3600"
01381 
01382         # illegal domain (no embedded dots)
01383         cookie = interact_2965(c, "http://www.acme.com",
01384                                'foo=bar; domain=".com"; version=1')
01385         self.assertTrue(not c)
01386 
01387         # legal domain
01388         cookie = interact_2965(c, "http://www.acme.com",
01389                                'ping=pong; domain="acme.com"; version=1')
01390         self.assertEqual(len(c), 1)
01391 
01392         # illegal domain (host prefix "www.a" contains a dot)
01393         cookie = interact_2965(c, "http://www.a.acme.com",
01394                                'whiz=bang; domain="acme.com"; version=1')
01395         self.assertEqual(len(c), 1)
01396 
01397         # legal domain
01398         cookie = interact_2965(c, "http://www.a.acme.com",
01399                                'wow=flutter; domain=".a.acme.com"; version=1')
01400         self.assertEqual(len(c), 2)
01401 
01402         # can't partially match an IP-address
01403         cookie = interact_2965(c, "http://125.125.125.125",
01404                                'zzzz=ping; domain="125.125.125"; version=1')
01405         self.assertEqual(len(c), 2)
01406 
01407         # illegal path (must be prefix of request path)
01408         cookie = interact_2965(c, "http://www.sol.no",
01409                                'blah=rhubarb; domain=".sol.no"; path="/foo"; '
01410                                'version=1')
01411         self.assertEqual(len(c), 2)
01412 
01413         # legal path
01414         cookie = interact_2965(c, "http://www.sol.no/foo/bar",
01415                                'bing=bong; domain=".sol.no"; path="/foo"; '
01416                                'version=1')
01417         self.assertEqual(len(c), 3)
01418 
01419         # illegal port (request-port not in list)
01420         cookie = interact_2965(c, "http://www.sol.no",
01421                                'whiz=ffft; domain=".sol.no"; port="90,100"; '
01422                                'version=1')
01423         self.assertEqual(len(c), 3)
01424 
01425         # legal port
01426         cookie = interact_2965(
01427             c, "http://www.sol.no",
01428             r'bang=wallop; version=1; domain=".sol.no"; '
01429             r'port="90,100, 80,8080"; '
01430             r'max-age=100; Comment = "Just kidding! (\"|\\\\) "')
01431         self.assertEqual(len(c), 4)
01432 
01433         # port attribute without any value (current port)
01434         cookie = interact_2965(c, "http://www.sol.no",
01435                                'foo9=bar; version=1; domain=".sol.no"; port; '
01436                                'max-age=100;')
01437         self.assertEqual(len(c), 5)
01438 
01439         # encoded path
01440         # LWP has this test, but unescaping allowed path characters seems
        # like a bad idea, so I think this should fail:

Here is the call graph for this function:


The documentation for this class was generated from the following file: