| 1 |
"""Tests for http/cookiejar.py.""" |
| 2 |
|
| 3 |
import os |
| 4 |
import stat |
| 5 |
import sys |
| 6 |
import re |
| 7 |
import test.support |
| 8 |
from test.support import os_helper |
| 9 |
from test.support import warnings_helper |
| 10 |
import time |
| 11 |
import unittest |
| 12 |
import urllib.request |
| 13 |
import pathlib |
| 14 |
|
| 15 |
from http.cookiejar import (time2isoz, http2time, iso2time, time2netscape, |
| 16 |
parse_ns_headers, join_header_words, split_header_words, Cookie, |
| 17 |
CookieJar, DefaultCookiePolicy, LWPCookieJar, MozillaCookieJar, |
| 18 |
LoadError, lwp_cookie_str, DEFAULT_HTTP_PORT, escape_path, |
| 19 |
reach, is_HDN, domain_match, user_domain_match, request_path, |
| 20 |
request_port, request_host) |
| 21 |
|
| 22 |
mswindows = (sys.platform == "win32") |
| 23 |
|
| 24 |
class DateTimeTests(unittest.TestCase): |
| 25 |
|
| 26 |
def test_time2isoz(self): |
| 27 |
base = 1019227000 |
| 28 |
day = 24*3600 |
| 29 |
self.assertEqual(time2isoz(base), "2002-04-19 14:36:40Z") |
| 30 |
self.assertEqual(time2isoz(base+day), "2002-04-20 14:36:40Z") |
| 31 |
self.assertEqual(time2isoz(base+2*day), "2002-04-21 14:36:40Z") |
| 32 |
self.assertEqual(time2isoz(base+3*day), "2002-04-22 14:36:40Z") |
| 33 |
|
| 34 |
az = time2isoz() |
| 35 |
bz = time2isoz(500000) |
| 36 |
for text in (az, bz): |
| 37 |
self.assertRegex(text, r"^\d{4}-\d\d-\d\d \d\d:\d\d:\d\dZ$", |
| 38 |
"bad time2isoz format: %s %s" % (az, bz)) |
| 39 |
|
| 40 |
def test_time2netscape(self): |
| 41 |
base = 1019227000 |
| 42 |
day = 24*3600 |
| 43 |
self.assertEqual(time2netscape(base), "Fri, 19-Apr-2002 14:36:40 GMT") |
| 44 |
self.assertEqual(time2netscape(base+day), |
| 45 |
"Sat, 20-Apr-2002 14:36:40 GMT") |
| 46 |
|
| 47 |
self.assertEqual(time2netscape(base+2*day), |
| 48 |
"Sun, 21-Apr-2002 14:36:40 GMT") |
| 49 |
|
| 50 |
self.assertEqual(time2netscape(base+3*day), |
| 51 |
"Mon, 22-Apr-2002 14:36:40 GMT") |
| 52 |
|
| 53 |
az = time2netscape() |
| 54 |
bz = time2netscape(500000) |
| 55 |
for text in (az, bz): |
| 56 |
|
| 57 |
self.assertRegex( |
| 58 |
text, |
| 59 |
r"[a-zA-Z]{3}, \d{2}-[a-zA-Z]{3}-\d{4} \d{2}:\d{2}:\d{2} GMT$", |
| 60 |
"bad time2netscape format: %s %s" % (az, bz)) |
| 61 |
|
| 62 |
def test_http2time(self): |
| 63 |
def parse_date(text): |
| 64 |
return time.gmtime(http2time(text))[:6] |
| 65 |
|
| 66 |
self.assertEqual(parse_date("01 Jan 2001"), (2001, 1, 1, 0, 0, 0.0)) |
| 67 |
|
| 68 |
|
| 69 |
self.assertEqual(parse_date("03-Feb-20"), (2020, 2, 3, 0, 0, 0.0)) |
| 70 |
|
| 71 |
|
| 72 |
self.assertEqual(parse_date("03-Feb-98"), (1998, 2, 3, 0, 0, 0.0)) |
| 73 |
|
| 74 |
def test_http2time_formats(self): |
| 75 |
|
| 76 |
|
| 77 |
tests = [ |
| 78 |
'Thu, 03 Feb 1994 00:00:00 GMT', |
| 79 |
'Thursday, 03-Feb-94 00:00:00 GMT', |
| 80 |
'Thursday, 03-Feb-1994 00:00:00 GMT', |
| 81 |
|
| 82 |
'03 Feb 1994 00:00:00 GMT', |
| 83 |
'03-Feb-94 00:00:00 GMT', |
| 84 |
'03-Feb-1994 00:00:00 GMT', |
| 85 |
'03-Feb-1994 00:00 GMT', |
| 86 |
'03-Feb-1994 00:00', |
| 87 |
'02-Feb-1994 24:00', |
| 88 |
|
| 89 |
|
| 90 |
'03-Feb-94', |
| 91 |
'03-Feb-1994', |
| 92 |
'03 Feb 1994', |
| 93 |
|
| 94 |
|
| 95 |
' 03 Feb 1994 0:00 ', |
| 96 |
' 03-Feb-1994 ', |
| 97 |
] |
| 98 |
|
| 99 |
test_t = 760233600 |
| 100 |
result = time2isoz(test_t) |
| 101 |
expected = "1994-02-03 00:00:00Z" |
| 102 |
self.assertEqual(result, expected, |
| 103 |
"%s => '%s' (%s)" % (test_t, result, expected)) |
| 104 |
|
| 105 |
for s in tests: |
| 106 |
self.assertEqual(http2time(s), test_t, s) |
| 107 |
self.assertEqual(http2time(s.lower()), test_t, s.lower()) |
| 108 |
self.assertEqual(http2time(s.upper()), test_t, s.upper()) |
| 109 |
|
| 110 |
def test_http2time_garbage(self): |
| 111 |
for test in [ |
| 112 |
'', |
| 113 |
'Garbage', |
| 114 |
'Mandag 16. September 1996', |
| 115 |
'01-00-1980', |
| 116 |
'01-13-1980', |
| 117 |
'00-01-1980', |
| 118 |
'32-01-1980', |
| 119 |
'01-01-1980 25:00:00', |
| 120 |
'01-01-1980 00:61:00', |
| 121 |
'01-01-1980 00:00:62', |
| 122 |
'08-Oct-3697739', |
| 123 |
'08-01-3697739', |
| 124 |
'09 Feb 19942632 22:23:32 GMT', |
| 125 |
'Wed, 09 Feb 1994834 22:23:32 GMT', |
| 126 |
]: |
| 127 |
self.assertIsNone(http2time(test), |
| 128 |
"http2time(%s) is not None\n" |
| 129 |
"http2time(test) %s" % (test, http2time(test))) |
| 130 |
|
| 131 |
def test_http2time_redos_regression_actually_completes(self): |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
http2time("01 Jan 1970{}00:00:00 GMT!".format(" " * 10 ** 5)) |
| 136 |
http2time("01 Jan 1970 00:00:00{}GMT!".format(" " * 10 ** 5)) |
| 137 |
|
| 138 |
def test_iso2time(self): |
| 139 |
def parse_date(text): |
| 140 |
return time.gmtime(iso2time(text))[:6] |
| 141 |
|
| 142 |
|
| 143 |
self.assertEqual(parse_date("19940203T141529Z"), |
| 144 |
(1994, 2, 3, 14, 15, 29)) |
| 145 |
|
| 146 |
|
| 147 |
self.assertEqual(parse_date("1994-02-03 07:15:29 -0700"), |
| 148 |
(1994, 2, 3, 14, 15, 29)) |
| 149 |
|
| 150 |
|
| 151 |
self.assertEqual(parse_date("1994-02-03 19:45:29 +0530"), |
| 152 |
(1994, 2, 3, 14, 15, 29)) |
| 153 |
|
| 154 |
def test_iso2time_formats(self): |
| 155 |
|
| 156 |
tests = [ |
| 157 |
'1994-02-03 00:00:00 -0000', |
| 158 |
'1994-02-03 00:00:00 +0000', |
| 159 |
'1994-02-03 00:00:00', |
| 160 |
'1994-02-03', |
| 161 |
'1994-02-03T00:00:00', |
| 162 |
'19940203', |
| 163 |
'1994-02-02 24:00:00', |
| 164 |
'19940203T000000Z', |
| 165 |
|
| 166 |
|
| 167 |
' 1994-02-03 ', |
| 168 |
' 1994-02-03T00:00:00 ', |
| 169 |
] |
| 170 |
|
| 171 |
test_t = 760233600 |
| 172 |
for s in tests: |
| 173 |
self.assertEqual(iso2time(s), test_t, s) |
| 174 |
self.assertEqual(iso2time(s.lower()), test_t, s.lower()) |
| 175 |
self.assertEqual(iso2time(s.upper()), test_t, s.upper()) |
| 176 |
|
| 177 |
def test_iso2time_garbage(self): |
| 178 |
for test in [ |
| 179 |
'', |
| 180 |
'Garbage', |
| 181 |
'Thursday, 03-Feb-94 00:00:00 GMT', |
| 182 |
'1980-00-01', |
| 183 |
'1980-13-01', |
| 184 |
'1980-01-00', |
| 185 |
'1980-01-32', |
| 186 |
'1980-01-01 25:00:00', |
| 187 |
'1980-01-01 00:61:00', |
| 188 |
'01-01-1980 00:00:62', |
| 189 |
'01-01-1980T00:00:62', |
| 190 |
'19800101T250000Z', |
| 191 |
]: |
| 192 |
self.assertIsNone(iso2time(test), |
| 193 |
"iso2time(%r)" % test) |
| 194 |
|
| 195 |
def test_iso2time_performance_regression(self): |
| 196 |
|
| 197 |
|
| 198 |
iso2time('1994-02-03{}14:15:29 -0100!'.format(' '*10**6)) |
| 199 |
iso2time('1994-02-03 14:15:29{}-0100!'.format(' '*10**6)) |
| 200 |
|
| 201 |
|
| 202 |
class HeaderTests(unittest.TestCase): |
| 203 |
|
| 204 |
def test_parse_ns_headers(self): |
| 205 |
|
| 206 |
expected = [[('foo', 'bar'), ('expires', 2209069412), ('version', '0')]] |
| 207 |
for hdr in [ |
| 208 |
'foo=bar; expires=01 Jan 2040 22:23:32 GMT', |
| 209 |
'foo=bar; expires="01 Jan 2040 22:23:32 GMT"', |
| 210 |
]: |
| 211 |
self.assertEqual(parse_ns_headers([hdr]), expected) |
| 212 |
|
| 213 |
def test_parse_ns_headers_version(self): |
| 214 |
|
| 215 |
|
| 216 |
expected = [[('foo', 'bar'), ('version', '1')]] |
| 217 |
for hdr in [ |
| 218 |
'foo=bar; version="1"', |
| 219 |
'foo=bar; Version="1"', |
| 220 |
]: |
| 221 |
self.assertEqual(parse_ns_headers([hdr]), expected) |
| 222 |
|
| 223 |
def test_parse_ns_headers_special_names(self): |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
hdr = 'expires=01 Jan 2040 22:23:32 GMT' |
| 228 |
expected = [[("expires", "01 Jan 2040 22:23:32 GMT"), ("version", "0")]] |
| 229 |
self.assertEqual(parse_ns_headers([hdr]), expected) |
| 230 |
|
| 231 |
def test_join_header_words(self): |
| 232 |
joined = join_header_words([[("foo", None), ("bar", "baz")]]) |
| 233 |
self.assertEqual(joined, "foo; bar=baz") |
| 234 |
|
| 235 |
self.assertEqual(join_header_words([[]]), "") |
| 236 |
|
| 237 |
def test_split_header_words(self): |
| 238 |
tests = [ |
| 239 |
("foo", [[("foo", None)]]), |
| 240 |
("foo=bar", [[("foo", "bar")]]), |
| 241 |
(" foo ", [[("foo", None)]]), |
| 242 |
(" foo= ", [[("foo", "")]]), |
| 243 |
(" foo=", [[("foo", "")]]), |
| 244 |
(" foo= ; ", [[("foo", "")]]), |
| 245 |
(" foo= ; bar= baz ", [[("foo", ""), ("bar", "baz")]]), |
| 246 |
("foo=bar bar=baz", [[("foo", "bar"), ("bar", "baz")]]), |
| 247 |
|
| 248 |
("foo= bar=baz", [[("foo", "bar=baz")]]), |
| 249 |
("foo=bar;bar=baz", [[("foo", "bar"), ("bar", "baz")]]), |
| 250 |
('foo bar baz', [[("foo", None), ("bar", None), ("baz", None)]]), |
| 251 |
("a, b, c", [[("a", None)], [("b", None)], [("c", None)]]), |
| 252 |
(r'foo; bar=baz, spam=, foo="\,\;\"", bar= ', |
| 253 |
[[("foo", None), ("bar", "baz")], |
| 254 |
[("spam", "")], [("foo", ',;"')], [("bar", "")]]), |
| 255 |
] |
| 256 |
|
| 257 |
for arg, expect in tests: |
| 258 |
try: |
| 259 |
result = split_header_words([arg]) |
| 260 |
except: |
| 261 |
import traceback, io |
| 262 |
f = io.StringIO() |
| 263 |
traceback.print_exc(None, f) |
| 264 |
result = "(error -- traceback follows)\n\n%s" % f.getvalue() |
| 265 |
self.assertEqual(result, expect, """ |
| 266 |
When parsing: '%s' |
| 267 |
Expected: '%s' |
| 268 |
Got: '%s' |
| 269 |
""" % (arg, expect, result)) |
| 270 |
|
| 271 |
def test_roundtrip(self): |
| 272 |
tests = [ |
| 273 |
("foo", "foo"), |
| 274 |
("foo=bar", "foo=bar"), |
| 275 |
(" foo ", "foo"), |
| 276 |
("foo=", 'foo=""'), |
| 277 |
("foo=bar bar=baz", "foo=bar; bar=baz"), |
| 278 |
("foo=bar;bar=baz", "foo=bar; bar=baz"), |
| 279 |
('foo bar baz', "foo; bar; baz"), |
| 280 |
(r'foo="\"" bar="\\"', r'foo="\""; bar="\\"'), |
| 281 |
('foo,,,bar', 'foo, bar'), |
| 282 |
('foo=bar,bar=baz', 'foo=bar, bar=baz'), |
| 283 |
|
| 284 |
('text/html; charset=iso-8859-1', |
| 285 |
'text/html; charset="iso-8859-1"'), |
| 286 |
|
| 287 |
('foo="bar"; port="80,81"; discard, bar=baz', |
| 288 |
'foo=bar; port="80,81"; discard, bar=baz'), |
| 289 |
|
| 290 |
(r'Basic realm="\"foo\\\\bar\""', |
| 291 |
r'Basic; realm="\"foo\\\\bar\""') |
| 292 |
] |
| 293 |
|
| 294 |
for arg, expect in tests: |
| 295 |
input = split_header_words([arg]) |
| 296 |
res = join_header_words(input) |
| 297 |
self.assertEqual(res, expect, """ |
| 298 |
When parsing: '%s' |
| 299 |
Expected: '%s' |
| 300 |
Got: '%s' |
| 301 |
Input was: '%s' |
| 302 |
""" % (arg, expect, res, input)) |
| 303 |
|
| 304 |
|
| 305 |
class FakeResponse: |
| 306 |
def __init__(self, headers=[], url=None): |
| 307 |
""" |
| 308 |
headers: list of RFC822-style 'Key: value' strings |
| 309 |
""" |
| 310 |
import email |
| 311 |
self._headers = email.message_from_string("\n".join(headers)) |
| 312 |
self._url = url |
| 313 |
def info(self): return self._headers |
| 314 |
|
| 315 |
def interact_2965(cookiejar, url, *set_cookie_hdrs): |
| 316 |
return _interact(cookiejar, url, set_cookie_hdrs, "Set-Cookie2") |
| 317 |
|
| 318 |
def interact_netscape(cookiejar, url, *set_cookie_hdrs): |
| 319 |
return _interact(cookiejar, url, set_cookie_hdrs, "Set-Cookie") |
| 320 |
|
| 321 |
def _interact(cookiejar, url, set_cookie_hdrs, hdr_name): |
| 322 |
"""Perform a single request / response cycle, returning Cookie: header.""" |
| 323 |
req = urllib.request.Request(url) |
| 324 |
cookiejar.add_cookie_header(req) |
| 325 |
cookie_hdr = req.get_header("Cookie", "") |
| 326 |
headers = [] |
| 327 |
for hdr in set_cookie_hdrs: |
| 328 |
headers.append("%s: %s" % (hdr_name, hdr)) |
| 329 |
res = FakeResponse(headers, url) |
| 330 |
cookiejar.extract_cookies(res, req) |
| 331 |
return cookie_hdr |
| 332 |
|
| 333 |
|
| 334 |
class FileCookieJarTests(unittest.TestCase): |
| 335 |
def test_constructor_with_str(self): |
| 336 |
filename = os_helper.TESTFN |
| 337 |
c = LWPCookieJar(filename) |
| 338 |
self.assertEqual(c.filename, filename) |
| 339 |
|
| 340 |
def test_constructor_with_path_like(self): |
| 341 |
filename = pathlib.Path(os_helper.TESTFN) |
| 342 |
c = LWPCookieJar(filename) |
| 343 |
self.assertEqual(c.filename, os.fspath(filename)) |
| 344 |
|
| 345 |
def test_constructor_with_none(self): |
| 346 |
c = LWPCookieJar(None) |
| 347 |
self.assertIsNone(c.filename) |
| 348 |
|
| 349 |
def test_constructor_with_other_types(self): |
| 350 |
class A: |
| 351 |
pass |
| 352 |
|
| 353 |
for type_ in (int, float, A): |
| 354 |
with self.subTest(filename=type_): |
| 355 |
with self.assertRaises(TypeError): |
| 356 |
instance = type_() |
| 357 |
c = LWPCookieJar(filename=instance) |
| 358 |
|
| 359 |
def test_lwp_valueless_cookie(self): |
| 360 |
|
| 361 |
filename = os_helper.TESTFN |
| 362 |
c = LWPCookieJar() |
| 363 |
interact_netscape(c, "http://www.acme.com/", 'boo') |
| 364 |
self.assertEqual(c._cookies["www.acme.com"]["/"]["boo"].value, None) |
| 365 |
try: |
| 366 |
c.save(filename, ignore_discard=True) |
| 367 |
c = LWPCookieJar() |
| 368 |
c.load(filename, ignore_discard=True) |
| 369 |
finally: |
| 370 |
os_helper.unlink(filename) |
| 371 |
self.assertEqual(c._cookies["www.acme.com"]["/"]["boo"].value, None) |
| 372 |
|
| 373 |
@unittest.skipIf(mswindows, "windows file permissions are incompatible with file modes") |
| 374 |
@os_helper.skip_unless_working_chmod |
| 375 |
def test_lwp_filepermissions(self): |
| 376 |
|
| 377 |
filename = os_helper.TESTFN |
| 378 |
c = LWPCookieJar() |
| 379 |
interact_netscape(c, "http://www.acme.com/", 'boo') |
| 380 |
try: |
| 381 |
c.save(filename, ignore_discard=True) |
| 382 |
st = os.stat(filename) |
| 383 |
self.assertEqual(stat.S_IMODE(st.st_mode), 0o600) |
| 384 |
finally: |
| 385 |
os_helper.unlink(filename) |
| 386 |
|
| 387 |
@unittest.skipIf(mswindows, "windows file permissions are incompatible with file modes") |
| 388 |
@os_helper.skip_unless_working_chmod |
| 389 |
def test_mozilla_filepermissions(self): |
| 390 |
|
| 391 |
filename = os_helper.TESTFN |
| 392 |
c = MozillaCookieJar() |
| 393 |
interact_netscape(c, "http://www.acme.com/", 'boo') |
| 394 |
try: |
| 395 |
c.save(filename, ignore_discard=True) |
| 396 |
st = os.stat(filename) |
| 397 |
self.assertEqual(stat.S_IMODE(st.st_mode), 0o600) |
| 398 |
finally: |
| 399 |
os_helper.unlink(filename) |
| 400 |
|
| 401 |
@unittest.skipIf(mswindows, "windows file permissions are incompatible with file modes") |
| 402 |
@os_helper.skip_unless_working_chmod |
| 403 |
def test_cookie_files_are_truncated(self): |
| 404 |
filename = os_helper.TESTFN |
| 405 |
for cookiejar_class in (LWPCookieJar, MozillaCookieJar): |
| 406 |
c = cookiejar_class(filename) |
| 407 |
|
| 408 |
req = urllib.request.Request("http://www.acme.com/") |
| 409 |
headers = ["Set-Cookie: pll_lang=en; Max-Age=31536000; path=/"] |
| 410 |
res = FakeResponse(headers, "http://www.acme.com/") |
| 411 |
c.extract_cookies(res, req) |
| 412 |
self.assertEqual(len(c), 1) |
| 413 |
|
| 414 |
try: |
| 415 |
|
| 416 |
c.save() |
| 417 |
|
| 418 |
c.clear() |
| 419 |
c.save() |
| 420 |
|
| 421 |
c.load() |
| 422 |
finally: |
| 423 |
os_helper.unlink(filename) |
| 424 |
|
| 425 |
self.assertEqual(len(c), 0) |
| 426 |
|
| 427 |
def test_bad_magic(self): |
| 428 |
|
| 429 |
filename = os_helper.TESTFN |
| 430 |
for cookiejar_class in LWPCookieJar, MozillaCookieJar: |
| 431 |
c = cookiejar_class() |
| 432 |
try: |
| 433 |
c.load(filename="for this test to work, a file with this " |
| 434 |
"filename should not exist") |
| 435 |
except OSError as exc: |
| 436 |
|
| 437 |
|
| 438 |
self.assertIsNot(exc.__class__, LoadError) |
| 439 |
else: |
| 440 |
self.fail("expected OSError for invalid filename") |
| 441 |
|
| 442 |
|
| 443 |
try: |
| 444 |
with open(filename, "w") as f: |
| 445 |
f.write("oops\n") |
| 446 |
for cookiejar_class in LWPCookieJar, MozillaCookieJar: |
| 447 |
c = cookiejar_class() |
| 448 |
self.assertRaises(LoadError, c.load, filename) |
| 449 |
finally: |
| 450 |
os_helper.unlink(filename) |
| 451 |
|
| 452 |
class CookieTests(unittest.TestCase): |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
def test_domain_return_ok(self): |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
pol = DefaultCookiePolicy() |
| 506 |
for url, domain, ok in [ |
| 507 |
("http://foo.bar.com/", "blah.com", False), |
| 508 |
("http://foo.bar.com/", "rhubarb.blah.com", False), |
| 509 |
("http://foo.bar.com/", "rhubarb.foo.bar.com", False), |
| 510 |
("http://foo.bar.com/", ".foo.bar.com", True), |
| 511 |
("http://foo.bar.com/", "foo.bar.com", True), |
| 512 |
("http://foo.bar.com/", ".bar.com", True), |
| 513 |
("http://foo.bar.com/", "bar.com", True), |
| 514 |
("http://foo.bar.com/", "com", True), |
| 515 |
("http://foo.com/", "rhubarb.foo.com", False), |
| 516 |
("http://foo.com/", ".foo.com", True), |
| 517 |
("http://foo.com/", "foo.com", True), |
| 518 |
("http://foo.com/", "com", True), |
| 519 |
("http://foo/", "rhubarb.foo", False), |
| 520 |
("http://foo/", ".foo", True), |
| 521 |
("http://foo/", "foo", True), |
| 522 |
("http://foo/", "foo.local", True), |
| 523 |
("http://foo/", ".local", True), |
| 524 |
("http://barfoo.com", ".foo.com", False), |
| 525 |
("http://barfoo.com", "foo.com", False), |
| 526 |
]: |
| 527 |
request = urllib.request.Request(url) |
| 528 |
r = pol.domain_return_ok(domain, request) |
| 529 |
if ok: self.assertTrue(r) |
| 530 |
else: self.assertFalse(r) |
| 531 |
|
| 532 |
def test_missing_value(self): |
| 533 |
|
| 534 |
|
| 535 |
filename = os_helper.TESTFN |
| 536 |
c = MozillaCookieJar(filename) |
| 537 |
interact_netscape(c, "http://www.acme.com/", 'eggs') |
| 538 |
interact_netscape(c, "http://www.acme.com/", '"spam"; path=/foo/') |
| 539 |
cookie = c._cookies["www.acme.com"]["/"]["eggs"] |
| 540 |
self.assertIsNone(cookie.value) |
| 541 |
self.assertEqual(cookie.name, "eggs") |
| 542 |
cookie = c._cookies["www.acme.com"]['/foo/']['"spam"'] |
| 543 |
self.assertIsNone(cookie.value) |
| 544 |
self.assertEqual(cookie.name, '"spam"') |
| 545 |
self.assertEqual(lwp_cookie_str(cookie), ( |
| 546 |
r'"spam"; path="/foo/"; domain="www.acme.com"; ' |
| 547 |
'path_spec; discard; version=0')) |
| 548 |
old_str = repr(c) |
| 549 |
c.save(ignore_expires=True, ignore_discard=True) |
| 550 |
try: |
| 551 |
c = MozillaCookieJar(filename) |
| 552 |
c.revert(ignore_expires=True, ignore_discard=True) |
| 553 |
finally: |
| 554 |
os_helper.unlink(c.filename) |
| 555 |
|
| 556 |
self.assertEqual( |
| 557 |
repr(c), |
| 558 |
re.sub("path_specified=%s" % True, "path_specified=%s" % False, |
| 559 |
old_str) |
| 560 |
) |
| 561 |
self.assertEqual(interact_netscape(c, "http://www.acme.com/foo/"), |
| 562 |
'"spam"; eggs') |
| 563 |
|
| 564 |
def test_rfc2109_handling(self): |
| 565 |
|
| 566 |
|
| 567 |
for rfc2109_as_netscape, rfc2965, version in [ |
| 568 |
|
| 569 |
(None, False, 0), |
| 570 |
(None, True, 1), |
| 571 |
|
| 572 |
(False, False, None), |
| 573 |
(False, True, 1), |
| 574 |
(True, False, 0), |
| 575 |
(True, True, 0), |
| 576 |
]: |
| 577 |
policy = DefaultCookiePolicy( |
| 578 |
rfc2109_as_netscape=rfc2109_as_netscape, |
| 579 |
rfc2965=rfc2965) |
| 580 |
c = CookieJar(policy) |
| 581 |
interact_netscape(c, "http://www.example.com/", "ni=ni; Version=1") |
| 582 |
try: |
| 583 |
cookie = c._cookies["www.example.com"]["/"]["ni"] |
| 584 |
except KeyError: |
| 585 |
self.assertIsNone(version) |
| 586 |
else: |
| 587 |
self.assertEqual(cookie.version, version) |
| 588 |
|
| 589 |
interact_2965(c, "http://www.example.com/", |
| 590 |
"foo=bar; Version=1") |
| 591 |
if rfc2965: |
| 592 |
cookie2965 = c._cookies["www.example.com"]["/"]["foo"] |
| 593 |
self.assertEqual(cookie2965.version, 1) |
| 594 |
|
| 595 |
def test_ns_parser(self): |
| 596 |
c = CookieJar() |
| 597 |
interact_netscape(c, "http://www.acme.com/", |
| 598 |
'spam=eggs; DoMain=.acme.com; port; blArgh="feep"') |
| 599 |
interact_netscape(c, "http://www.acme.com/", 'ni=ni; port=80,8080') |
| 600 |
interact_netscape(c, "http://www.acme.com:80/", 'nini=ni') |
| 601 |
interact_netscape(c, "http://www.acme.com:80/", 'foo=bar; expires=') |
| 602 |
interact_netscape(c, "http://www.acme.com:80/", 'spam=eggs; ' |
| 603 |
'expires="Foo Bar 25 33:22:11 3022"') |
| 604 |
interact_netscape(c, 'http://www.acme.com/', 'fortytwo=') |
| 605 |
interact_netscape(c, 'http://www.acme.com/', '=unladenswallow') |
| 606 |
interact_netscape(c, 'http://www.acme.com/', 'holyhandgrenade') |
| 607 |
|
| 608 |
cookie = c._cookies[".acme.com"]["/"]["spam"] |
| 609 |
self.assertEqual(cookie.domain, ".acme.com") |
| 610 |
self.assertTrue(cookie.domain_specified) |
| 611 |
self.assertEqual(cookie.port, DEFAULT_HTTP_PORT) |
| 612 |
self.assertFalse(cookie.port_specified) |
| 613 |
|
| 614 |
self.assertTrue(cookie.has_nonstandard_attr("blArgh")) |
| 615 |
self.assertFalse(cookie.has_nonstandard_attr("blargh")) |
| 616 |
|
| 617 |
cookie = c._cookies["www.acme.com"]["/"]["ni"] |
| 618 |
self.assertEqual(cookie.domain, "www.acme.com") |
| 619 |
self.assertFalse(cookie.domain_specified) |
| 620 |
self.assertEqual(cookie.port, "80,8080") |
| 621 |
self.assertTrue(cookie.port_specified) |
| 622 |
|
| 623 |
cookie = c._cookies["www.acme.com"]["/"]["nini"] |
| 624 |
self.assertIsNone(cookie.port) |
| 625 |
self.assertFalse(cookie.port_specified) |
| 626 |
|
| 627 |
|
| 628 |
foo = c._cookies["www.acme.com"]["/"]["foo"] |
| 629 |
spam = c._cookies["www.acme.com"]["/"]["foo"] |
| 630 |
self.assertIsNone(foo.expires) |
| 631 |
self.assertIsNone(spam.expires) |
| 632 |
|
| 633 |
cookie = c._cookies['www.acme.com']['/']['fortytwo'] |
| 634 |
self.assertIsNotNone(cookie.value) |
| 635 |
self.assertEqual(cookie.value, '') |
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
cookie = c._cookies['www.acme.com']['/']['holyhandgrenade'] |
| 641 |
self.assertIsNone(cookie.value) |
| 642 |
|
| 643 |
def test_ns_parser_special_names(self): |
| 644 |
|
| 645 |
|
| 646 |
c = CookieJar() |
| 647 |
interact_netscape(c, "http://www.acme.com/", 'expires=eggs') |
| 648 |
interact_netscape(c, "http://www.acme.com/", 'version=eggs; spam=eggs') |
| 649 |
|
| 650 |
cookies = c._cookies["www.acme.com"]["/"] |
| 651 |
self.assertIn('expires', cookies) |
| 652 |
self.assertIn('version', cookies) |
| 653 |
|
| 654 |
def test_expires(self): |
| 655 |
|
| 656 |
c = CookieJar() |
| 657 |
future = time2netscape(time.time()+3600) |
| 658 |
|
| 659 |
with warnings_helper.check_no_warnings(self): |
| 660 |
headers = [f"Set-Cookie: FOO=BAR; path=/; expires={future}"] |
| 661 |
req = urllib.request.Request("http://www.coyote.com/") |
| 662 |
res = FakeResponse(headers, "http://www.coyote.com/") |
| 663 |
cookies = c.make_cookies(res, req) |
| 664 |
self.assertEqual(len(cookies), 1) |
| 665 |
self.assertEqual(time2netscape(cookies[0].expires), future) |
| 666 |
|
| 667 |
interact_netscape(c, "http://www.acme.com/", 'spam="bar"; expires=%s' % |
| 668 |
future) |
| 669 |
self.assertEqual(len(c), 1) |
| 670 |
now = time2netscape(time.time()-1) |
| 671 |
|
| 672 |
interact_netscape(c, "http://www.acme.com/", 'foo="eggs"; expires=%s' % |
| 673 |
now) |
| 674 |
h = interact_netscape(c, "http://www.acme.com/") |
| 675 |
self.assertEqual(len(c), 1) |
| 676 |
self.assertIn('spam="bar"', h) |
| 677 |
self.assertNotIn("foo", h) |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
interact_netscape(c, "http://www.acme.com/", 'eggs="bar"; expires=%s' % |
| 682 |
future) |
| 683 |
interact_netscape(c, "http://www.acme.com/", 'bar="bar"; expires=%s' % |
| 684 |
future) |
| 685 |
self.assertEqual(len(c), 3) |
| 686 |
interact_netscape(c, "http://www.acme.com/", 'eggs="bar"; ' |
| 687 |
'expires=%s; max-age=0' % future) |
| 688 |
interact_netscape(c, "http://www.acme.com/", 'bar="bar"; ' |
| 689 |
'max-age=0; expires=%s' % future) |
| 690 |
h = interact_netscape(c, "http://www.acme.com/") |
| 691 |
self.assertEqual(len(c), 1) |
| 692 |
|
| 693 |
|
| 694 |
interact_netscape(c, "http://www.rhubarb.net/", 'whum="fizz"') |
| 695 |
self.assertEqual(len(c), 2) |
| 696 |
c.clear_session_cookies() |
| 697 |
self.assertEqual(len(c), 1) |
| 698 |
self.assertIn('spam="bar"', h) |
| 699 |
|
| 700 |
|
| 701 |
cookie = Cookie(0, "name", "value", |
| 702 |
None, False, "www.python.org", |
| 703 |
True, False, "/", |
| 704 |
False, False, "1444312383.018307", |
| 705 |
False, None, None, |
| 706 |
{}) |
| 707 |
self.assertEqual(cookie.expires, 1444312383) |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
def test_default_path(self): |
| 712 |
|
| 713 |
pol = DefaultCookiePolicy(rfc2965=True) |
| 714 |
|
| 715 |
c = CookieJar(pol) |
| 716 |
interact_2965(c, "http://www.acme.com/", 'spam="bar"; Version="1"') |
| 717 |
self.assertIn("/", c._cookies["www.acme.com"]) |
| 718 |
|
| 719 |
c = CookieJar(pol) |
| 720 |
interact_2965(c, "http://www.acme.com/blah", 'eggs="bar"; Version="1"') |
| 721 |
self.assertIn("/", c._cookies["www.acme.com"]) |
| 722 |
|
| 723 |
c = CookieJar(pol) |
| 724 |
interact_2965(c, "http://www.acme.com/blah/rhubarb", |
| 725 |
'eggs="bar"; Version="1"') |
| 726 |
self.assertIn("/blah/", c._cookies["www.acme.com"]) |
| 727 |
|
| 728 |
c = CookieJar(pol) |
| 729 |
interact_2965(c, "http://www.acme.com/blah/rhubarb/", |
| 730 |
'eggs="bar"; Version="1"') |
| 731 |
self.assertIn("/blah/rhubarb/", c._cookies["www.acme.com"]) |
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
c = CookieJar() |
| 736 |
interact_netscape(c, "http://www.acme.com/", 'spam="bar"') |
| 737 |
self.assertIn("/", c._cookies["www.acme.com"]) |
| 738 |
|
| 739 |
c = CookieJar() |
| 740 |
interact_netscape(c, "http://www.acme.com/blah", 'eggs="bar"') |
| 741 |
self.assertIn("/", c._cookies["www.acme.com"]) |
| 742 |
|
| 743 |
c = CookieJar() |
| 744 |
interact_netscape(c, "http://www.acme.com/blah/rhubarb", 'eggs="bar"') |
| 745 |
self.assertIn("/blah", c._cookies["www.acme.com"]) |
| 746 |
|
| 747 |
c = CookieJar() |
| 748 |
interact_netscape(c, "http://www.acme.com/blah/rhubarb/", 'eggs="bar"') |
| 749 |
self.assertIn("/blah/rhubarb", c._cookies["www.acme.com"]) |
| 750 |
|
| 751 |
def test_default_path_with_query(self): |
| 752 |
cj = CookieJar() |
| 753 |
uri = "http://example.com/?spam/eggs" |
| 754 |
value = 'eggs="bar"' |
| 755 |
interact_netscape(cj, uri, value) |
| 756 |
|
| 757 |
self.assertIn("/", cj._cookies["example.com"]) |
| 758 |
|
| 759 |
self.assertEqual(interact_netscape(cj, uri), value) |
| 760 |
|
| 761 |
def test_escape_path(self): |
| 762 |
cases = [ |
| 763 |
|
| 764 |
("/foo%2f/bar", "/foo%2F/bar"), |
| 765 |
("/foo%2F/bar", "/foo%2F/bar"), |
| 766 |
|
| 767 |
("/foo%%/bar", "/foo%%/bar"), |
| 768 |
|
| 769 |
("/fo%19o/bar", "/fo%19o/bar"), |
| 770 |
("/fo%7do/bar", "/fo%7Do/bar"), |
| 771 |
|
| 772 |
("/foo/bar&", "/foo/bar&"), |
| 773 |
("/foo//bar", "/foo//bar"), |
| 774 |
("\176/foo/bar", "\176/foo/bar"), |
| 775 |
|
| 776 |
("/foo\031/bar", "/foo%19/bar"), |
| 777 |
("/\175foo/bar", "/%7Dfoo/bar"), |
| 778 |
|
| 779 |
("/foo/bar\u00fc", "/foo/bar%C3%BC"), |
| 780 |
|
| 781 |
("/foo/bar\uabcd", "/foo/bar%EA%AF%8D"), |
| 782 |
] |
| 783 |
for arg, result in cases: |
| 784 |
self.assertEqual(escape_path(arg), result) |
| 785 |
|
| 786 |
def test_request_path(self): |
| 787 |
|
| 788 |
req = urllib.request.Request( |
| 789 |
"http://www.example.com/rheum/rhaponticum;" |
| 790 |
"foo=bar;sing=song?apples=pears&spam=eggs#ni") |
| 791 |
self.assertEqual(request_path(req), |
| 792 |
"/rheum/rhaponticum;foo=bar;sing=song") |
| 793 |
|
| 794 |
req = urllib.request.Request( |
| 795 |
"http://www.example.com/rheum/rhaponticum?" |
| 796 |
"apples=pears&spam=eggs#ni") |
| 797 |
self.assertEqual(request_path(req), "/rheum/rhaponticum") |
| 798 |
|
| 799 |
req = urllib.request.Request("http://www.example.com") |
| 800 |
self.assertEqual(request_path(req), "/") |
| 801 |
|
| 802 |
def test_path_prefix_match(self): |
| 803 |
pol = DefaultCookiePolicy() |
| 804 |
strict_ns_path_pol = DefaultCookiePolicy(strict_ns_set_path=True) |
| 805 |
|
| 806 |
c = CookieJar(pol) |
| 807 |
base_url = "http://bar.com" |
| 808 |
interact_netscape(c, base_url, 'spam=eggs; Path=/foo') |
| 809 |
cookie = c._cookies['bar.com']['/foo']['spam'] |
| 810 |
|
| 811 |
for path, ok in [('/foo', True), |
| 812 |
('/foo/', True), |
| 813 |
('/foo/bar', True), |
| 814 |
('/', False), |
| 815 |
('/foobad/foo', False)]: |
| 816 |
url = f'{base_url}{path}' |
| 817 |
req = urllib.request.Request(url) |
| 818 |
h = interact_netscape(c, url) |
| 819 |
if ok: |
| 820 |
self.assertIn('spam=eggs', h, f"cookie not set for {path}") |
| 821 |
self.assertTrue(strict_ns_path_pol.set_ok_path(cookie, req)) |
| 822 |
else: |
| 823 |
self.assertNotIn('spam=eggs', h, f"cookie set for {path}") |
| 824 |
self.assertFalse(strict_ns_path_pol.set_ok_path(cookie, req)) |
| 825 |
|
| 826 |
def test_request_port(self): |
| 827 |
req = urllib.request.Request("http://www.acme.com:1234/", |
| 828 |
headers={"Host": "www.acme.com:4321"}) |
| 829 |
self.assertEqual(request_port(req), "1234") |
| 830 |
req = urllib.request.Request("http://www.acme.com/", |
| 831 |
headers={"Host": "www.acme.com:4321"}) |
| 832 |
self.assertEqual(request_port(req), DEFAULT_HTTP_PORT) |
| 833 |
|
| 834 |
def test_request_host(self): |
| 835 |
|
| 836 |
req = urllib.request.Request("http://1.1.1.1/", |
| 837 |
headers={"Host": "www.acme.com:80"}) |
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
self.assertEqual(request_host(req), "1.1.1.1") |
| 842 |
req = urllib.request.Request("http://www.acme.com/", |
| 843 |
headers={"Host": "irrelevant.com"}) |
| 844 |
self.assertEqual(request_host(req), "www.acme.com") |
| 845 |
|
| 846 |
req = urllib.request.Request("http://www.acme.com:2345/resource.html", |
| 847 |
headers={"Host": "www.acme.com:5432"}) |
| 848 |
self.assertEqual(request_host(req), "www.acme.com") |
| 849 |
|
| 850 |
def test_is_HDN(self): |
| 851 |
self.assertTrue(is_HDN("foo.bar.com")) |
| 852 |
self.assertTrue(is_HDN("1foo2.3bar4.5com")) |
| 853 |
self.assertFalse(is_HDN("192.168.1.1")) |
| 854 |
self.assertFalse(is_HDN("")) |
| 855 |
self.assertFalse(is_HDN(".")) |
| 856 |
self.assertFalse(is_HDN(".foo.bar.com")) |
| 857 |
self.assertFalse(is_HDN("..foo")) |
| 858 |
self.assertFalse(is_HDN("foo.")) |
| 859 |
|
| 860 |
def test_reach(self): |
| 861 |
self.assertEqual(reach("www.acme.com"), ".acme.com") |
| 862 |
self.assertEqual(reach("acme.com"), "acme.com") |
| 863 |
self.assertEqual(reach("acme.local"), ".local") |
| 864 |
self.assertEqual(reach(".local"), ".local") |
| 865 |
self.assertEqual(reach(".com"), ".com") |
| 866 |
self.assertEqual(reach("."), ".") |
| 867 |
self.assertEqual(reach(""), "") |
| 868 |
self.assertEqual(reach("192.168.0.1"), "192.168.0.1") |
| 869 |
|
| 870 |
def test_domain_match(self): |
| 871 |
self.assertTrue(domain_match("192.168.1.1", "192.168.1.1")) |
| 872 |
self.assertFalse(domain_match("192.168.1.1", ".168.1.1")) |
| 873 |
self.assertTrue(domain_match("x.y.com", "x.Y.com")) |
| 874 |
self.assertTrue(domain_match("x.y.com", ".Y.com")) |
| 875 |
self.assertFalse(domain_match("x.y.com", "Y.com")) |
| 876 |
self.assertTrue(domain_match("a.b.c.com", ".c.com")) |
| 877 |
self.assertFalse(domain_match(".c.com", "a.b.c.com")) |
| 878 |
self.assertTrue(domain_match("example.local", ".local")) |
| 879 |
self.assertFalse(domain_match("blah.blah", "")) |
| 880 |
self.assertFalse(domain_match("", ".rhubarb.rhubarb")) |
| 881 |
self.assertTrue(domain_match("", "")) |
| 882 |
|
| 883 |
self.assertTrue(user_domain_match("acme.com", "acme.com")) |
| 884 |
self.assertFalse(user_domain_match("acme.com", ".acme.com")) |
| 885 |
self.assertTrue(user_domain_match("rhubarb.acme.com", ".acme.com")) |
| 886 |
self.assertTrue(user_domain_match("www.rhubarb.acme.com", ".acme.com")) |
| 887 |
self.assertTrue(user_domain_match("x.y.com", "x.Y.com")) |
| 888 |
self.assertTrue(user_domain_match("x.y.com", ".Y.com")) |
| 889 |
self.assertFalse(user_domain_match("x.y.com", "Y.com")) |
| 890 |
self.assertTrue(user_domain_match("y.com", "Y.com")) |
| 891 |
self.assertFalse(user_domain_match(".y.com", "Y.com")) |
| 892 |
self.assertTrue(user_domain_match(".y.com", ".Y.com")) |
| 893 |
self.assertTrue(user_domain_match("x.y.com", ".com")) |
| 894 |
self.assertFalse(user_domain_match("x.y.com", "com")) |
| 895 |
self.assertFalse(user_domain_match("x.y.com", "m")) |
| 896 |
self.assertFalse(user_domain_match("x.y.com", ".m")) |
| 897 |
self.assertFalse(user_domain_match("x.y.com", "")) |
| 898 |
self.assertFalse(user_domain_match("x.y.com", ".")) |
| 899 |
self.assertTrue(user_domain_match("192.168.1.1", "192.168.1.1")) |
| 900 |
|
| 901 |
self.assertFalse(user_domain_match("192.168.1.1", ".168.1.1")) |
| 902 |
self.assertFalse(user_domain_match("192.168.1.1", ".")) |
| 903 |
|
| 904 |
self.assertFalse(user_domain_match("192.168.1.1", "")) |
| 905 |
|
| 906 |
def test_wrong_domain(self): |
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
c = CookieJar() |
| 912 |
interact_2965(c, "http://www.nasty.com/", |
| 913 |
'foo=bar; domain=friendly.org; Version="1"') |
| 914 |
self.assertEqual(len(c), 0) |
| 915 |
|
| 916 |
def test_strict_domain(self): |
| 917 |
|
| 918 |
|
| 919 |
cp = DefaultCookiePolicy(strict_domain=True) |
| 920 |
cj = CookieJar(policy=cp) |
| 921 |
interact_netscape(cj, "http://example.co.uk/", 'no=problemo') |
| 922 |
interact_netscape(cj, "http://example.co.uk/", |
| 923 |
'okey=dokey; Domain=.example.co.uk') |
| 924 |
self.assertEqual(len(cj), 2) |
| 925 |
for pseudo_tld in [".co.uk", ".org.za", ".tx.us", ".name.us"]: |
| 926 |
interact_netscape(cj, "http://example.%s/" % pseudo_tld, |
| 927 |
'spam=eggs; Domain=.co.uk') |
| 928 |
self.assertEqual(len(cj), 2) |
| 929 |
|
| 930 |
def test_two_component_domain_ns(self): |
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
c = CookieJar() |
| 935 |
|
| 936 |
|
| 937 |
interact_netscape(c, "http://foo.net/", 'ns=bar') |
| 938 |
self.assertEqual(len(c), 1) |
| 939 |
self.assertEqual(c._cookies["foo.net"]["/"]["ns"].value, "bar") |
| 940 |
self.assertEqual(interact_netscape(c, "http://foo.net/"), "ns=bar") |
| 941 |
|
| 942 |
self.assertEqual(interact_netscape(c, "http://www.foo.net/"), |
| 943 |
"ns=bar") |
| 944 |
|
| 945 |
pol = DefaultCookiePolicy( |
| 946 |
strict_ns_domain=DefaultCookiePolicy.DomainStrictNonDomain) |
| 947 |
c.set_policy(pol) |
| 948 |
self.assertEqual(interact_netscape(c, "http://www.foo.net/"), "") |
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
interact_netscape(c, "http://foo.net/foo/", |
| 953 |
'spam1=eggs; domain=foo.net') |
| 954 |
|
| 955 |
interact_netscape(c, "http://foo.net/foo/bar/", |
| 956 |
'spam2=eggs; domain=.foo.net') |
| 957 |
self.assertEqual(len(c), 3) |
| 958 |
self.assertEqual(c._cookies[".foo.net"]["/foo"]["spam1"].value, |
| 959 |
"eggs") |
| 960 |
self.assertEqual(c._cookies[".foo.net"]["/foo/bar"]["spam2"].value, |
| 961 |
"eggs") |
| 962 |
self.assertEqual(interact_netscape(c, "http://foo.net/foo/bar/"), |
| 963 |
"spam2=eggs; spam1=eggs; ns=bar") |
| 964 |
|
| 965 |
|
| 966 |
interact_netscape(c, "http://foo.net/", 'nini="ni"; domain=.net') |
| 967 |
self.assertEqual(len(c), 3) |
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
interact_netscape(c, "http://foo.co.uk", 'nasty=trick; domain=.co.uk') |
| 975 |
|
| 976 |
self.assertEqual(len(c), 4) |
| 977 |
|
| 978 |
def test_localhost_domain(self): |
| 979 |
c = CookieJar() |
| 980 |
|
| 981 |
interact_netscape(c, "http://localhost", "foo=bar; domain=localhost;") |
| 982 |
|
| 983 |
self.assertEqual(len(c), 1) |
| 984 |
|
| 985 |
def test_localhost_domain_contents(self): |
| 986 |
c = CookieJar() |
| 987 |
|
| 988 |
interact_netscape(c, "http://localhost", "foo=bar; domain=localhost;") |
| 989 |
|
| 990 |
self.assertEqual(c._cookies[".localhost"]["/"]["foo"].value, "bar") |
| 991 |
|
| 992 |
def test_localhost_domain_contents_2(self): |
| 993 |
c = CookieJar() |
| 994 |
|
| 995 |
interact_netscape(c, "http://localhost", "foo=bar;") |
| 996 |
|
| 997 |
self.assertEqual(c._cookies["localhost.local"]["/"]["foo"].value, "bar") |
| 998 |
|
| 999 |
def test_evil_nonlocal_domain(self): |
| 1000 |
c = CookieJar() |
| 1001 |
|
| 1002 |
interact_netscape(c, "http://evil.com", "foo=bar; domain=.localhost") |
| 1003 |
|
| 1004 |
self.assertEqual(len(c), 0) |
| 1005 |
|
| 1006 |
def test_evil_local_domain(self): |
| 1007 |
c = CookieJar() |
| 1008 |
|
| 1009 |
interact_netscape(c, "http://localhost", "foo=bar; domain=.evil.com") |
| 1010 |
|
| 1011 |
self.assertEqual(len(c), 0) |
| 1012 |
|
| 1013 |
def test_evil_local_domain_2(self): |
| 1014 |
c = CookieJar() |
| 1015 |
|
| 1016 |
interact_netscape(c, "http://localhost", "foo=bar; domain=.someother.local") |
| 1017 |
|
| 1018 |
self.assertEqual(len(c), 0) |
| 1019 |
|
| 1020 |
def test_two_component_domain_rfc2965(self): |
| 1021 |
pol = DefaultCookiePolicy(rfc2965=True) |
| 1022 |
c = CookieJar(pol) |
| 1023 |
|
| 1024 |
|
| 1025 |
interact_2965(c, "http://foo.net/", 'foo=bar; Version="1"') |
| 1026 |
self.assertEqual(len(c), 1) |
| 1027 |
self.assertEqual(c._cookies["foo.net"]["/"]["foo"].value, "bar") |
| 1028 |
self.assertEqual(interact_2965(c, "http://foo.net/"), |
| 1029 |
"$Version=1; foo=bar") |
| 1030 |
|
| 1031 |
self.assertEqual(interact_2965(c, "http://www.foo.net/"), "") |
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
interact_2965(c, "http://foo.net/foo", |
| 1037 |
'spam=eggs; domain=foo.net; path=/foo; Version="1"') |
| 1038 |
self.assertEqual(len(c), 1) |
| 1039 |
self.assertEqual(interact_2965(c, "http://foo.net/foo"), |
| 1040 |
"$Version=1; foo=bar") |
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
interact_2965(c, "http://www.foo.net/foo/", |
| 1045 |
'spam=eggs; domain=foo.net; Version="1"') |
| 1046 |
self.assertEqual(c._cookies[".foo.net"]["/foo/"]["spam"].value, |
| 1047 |
"eggs") |
| 1048 |
self.assertEqual(len(c), 2) |
| 1049 |
self.assertEqual(interact_2965(c, "http://foo.net/foo/"), |
| 1050 |
"$Version=1; foo=bar") |
| 1051 |
self.assertEqual(interact_2965(c, "http://www.foo.net/foo/"), |
| 1052 |
'$Version=1; spam=eggs; $Domain="foo.net"') |
| 1053 |
|
| 1054 |
|
| 1055 |
interact_2965(c, "http://foo.net/", |
| 1056 |
'ni="ni"; domain=".net"; Version="1"') |
| 1057 |
self.assertEqual(len(c), 2) |
| 1058 |
|
| 1059 |
|
| 1060 |
interact_2965(c, "http://foo.co.uk/", |
| 1061 |
'nasty=trick; domain=.co.uk; Version="1"') |
| 1062 |
self.assertEqual(len(c), 3) |
| 1063 |
|
| 1064 |
def test_domain_allow(self): |
| 1065 |
c = CookieJar(policy=DefaultCookiePolicy( |
| 1066 |
blocked_domains=["acme.com"], |
| 1067 |
allowed_domains=["www.acme.com"])) |
| 1068 |
|
| 1069 |
req = urllib.request.Request("http://acme.com/") |
| 1070 |
headers = ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/"] |
| 1071 |
res = FakeResponse(headers, "http://acme.com/") |
| 1072 |
c.extract_cookies(res, req) |
| 1073 |
self.assertEqual(len(c), 0) |
| 1074 |
|
| 1075 |
req = urllib.request.Request("http://www.acme.com/") |
| 1076 |
res = FakeResponse(headers, "http://www.acme.com/") |
| 1077 |
c.extract_cookies(res, req) |
| 1078 |
self.assertEqual(len(c), 1) |
| 1079 |
|
| 1080 |
req = urllib.request.Request("http://www.coyote.com/") |
| 1081 |
res = FakeResponse(headers, "http://www.coyote.com/") |
| 1082 |
c.extract_cookies(res, req) |
| 1083 |
self.assertEqual(len(c), 1) |
| 1084 |
|
| 1085 |
|
| 1086 |
req = urllib.request.Request("http://www.coyote.com/") |
| 1087 |
res = FakeResponse(headers, "http://www.coyote.com/") |
| 1088 |
cookies = c.make_cookies(res, req) |
| 1089 |
c.set_cookie(cookies[0]) |
| 1090 |
self.assertEqual(len(c), 2) |
| 1091 |
|
| 1092 |
c.add_cookie_header(req) |
| 1093 |
self.assertFalse(req.has_header("Cookie")) |
| 1094 |
|
| 1095 |
def test_domain_block(self): |
| 1096 |
pol = DefaultCookiePolicy( |
| 1097 |
rfc2965=True, blocked_domains=[".acme.com"]) |
| 1098 |
c = CookieJar(policy=pol) |
| 1099 |
headers = ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/"] |
| 1100 |
|
| 1101 |
req = urllib.request.Request("http://www.acme.com/") |
| 1102 |
res = FakeResponse(headers, "http://www.acme.com/") |
| 1103 |
c.extract_cookies(res, req) |
| 1104 |
self.assertEqual(len(c), 0) |
| 1105 |
|
| 1106 |
p = pol.set_blocked_domains(["acme.com"]) |
| 1107 |
c.extract_cookies(res, req) |
| 1108 |
self.assertEqual(len(c), 1) |
| 1109 |
|
| 1110 |
c.clear() |
| 1111 |
req = urllib.request.Request("http://www.roadrunner.net/") |
| 1112 |
res = FakeResponse(headers, "http://www.roadrunner.net/") |
| 1113 |
c.extract_cookies(res, req) |
| 1114 |
self.assertEqual(len(c), 1) |
| 1115 |
req = urllib.request.Request("http://www.roadrunner.net/") |
| 1116 |
c.add_cookie_header(req) |
| 1117 |
self.assertTrue(req.has_header("Cookie")) |
| 1118 |
self.assertTrue(req.has_header("Cookie2")) |
| 1119 |
|
| 1120 |
c.clear() |
| 1121 |
pol.set_blocked_domains([".acme.com"]) |
| 1122 |
c.extract_cookies(res, req) |
| 1123 |
self.assertEqual(len(c), 1) |
| 1124 |
|
| 1125 |
|
| 1126 |
req = urllib.request.Request("http://www.acme.com/") |
| 1127 |
res = FakeResponse(headers, "http://www.acme.com/") |
| 1128 |
cookies = c.make_cookies(res, req) |
| 1129 |
c.set_cookie(cookies[0]) |
| 1130 |
self.assertEqual(len(c), 2) |
| 1131 |
|
| 1132 |
c.add_cookie_header(req) |
| 1133 |
self.assertFalse(req.has_header("Cookie")) |
| 1134 |
|
| 1135 |
c.clear() |
| 1136 |
|
| 1137 |
pol.set_blocked_domains([]) |
| 1138 |
req = urllib.request.Request("http://acme.com/") |
| 1139 |
res = FakeResponse(headers, "http://acme.com/") |
| 1140 |
cookies = c.make_cookies(res, req) |
| 1141 |
c.extract_cookies(res, req) |
| 1142 |
self.assertEqual(len(c), 1) |
| 1143 |
|
| 1144 |
req = urllib.request.Request("http://acme.com/") |
| 1145 |
c.add_cookie_header(req) |
| 1146 |
self.assertTrue(req.has_header("Cookie")) |
| 1147 |
|
| 1148 |
req = urllib.request.Request("http://badacme.com/") |
| 1149 |
c.add_cookie_header(req) |
| 1150 |
self.assertFalse(pol.return_ok(cookies[0], req)) |
| 1151 |
self.assertFalse(req.has_header("Cookie")) |
| 1152 |
|
| 1153 |
p = pol.set_blocked_domains(["acme.com"]) |
| 1154 |
req = urllib.request.Request("http://acme.com/") |
| 1155 |
c.add_cookie_header(req) |
| 1156 |
self.assertFalse(req.has_header("Cookie")) |
| 1157 |
|
| 1158 |
req = urllib.request.Request("http://badacme.com/") |
| 1159 |
c.add_cookie_header(req) |
| 1160 |
self.assertFalse(req.has_header("Cookie")) |
| 1161 |
|
| 1162 |
def test_secure(self): |
| 1163 |
for ns in True, False: |
| 1164 |
for whitespace in " ", "": |
| 1165 |
c = CookieJar() |
| 1166 |
if ns: |
| 1167 |
pol = DefaultCookiePolicy(rfc2965=False) |
| 1168 |
int = interact_netscape |
| 1169 |
vs = "" |
| 1170 |
else: |
| 1171 |
pol = DefaultCookiePolicy(rfc2965=True) |
| 1172 |
int = interact_2965 |
| 1173 |
vs = "; Version=1" |
| 1174 |
c.set_policy(pol) |
| 1175 |
url = "http://www.acme.com/" |
| 1176 |
int(c, url, "foo1=bar%s%s" % (vs, whitespace)) |
| 1177 |
int(c, url, "foo2=bar%s; secure%s" % (vs, whitespace)) |
| 1178 |
self.assertFalse( |
| 1179 |
c._cookies["www.acme.com"]["/"]["foo1"].secure, |
| 1180 |
"non-secure cookie registered secure") |
| 1181 |
self.assertTrue( |
| 1182 |
c._cookies["www.acme.com"]["/"]["foo2"].secure, |
| 1183 |
"secure cookie registered non-secure") |
| 1184 |
|
| 1185 |
def test_secure_block(self): |
| 1186 |
pol = DefaultCookiePolicy() |
| 1187 |
c = CookieJar(policy=pol) |
| 1188 |
|
| 1189 |
headers = ["Set-Cookie: session=narf; secure; path=/"] |
| 1190 |
req = urllib.request.Request("https://www.acme.com/") |
| 1191 |
res = FakeResponse(headers, "https://www.acme.com/") |
| 1192 |
c.extract_cookies(res, req) |
| 1193 |
self.assertEqual(len(c), 1) |
| 1194 |
|
| 1195 |
req = urllib.request.Request("https://www.acme.com/") |
| 1196 |
c.add_cookie_header(req) |
| 1197 |
self.assertTrue(req.has_header("Cookie")) |
| 1198 |
|
| 1199 |
req = urllib.request.Request("http://www.acme.com/") |
| 1200 |
c.add_cookie_header(req) |
| 1201 |
self.assertFalse(req.has_header("Cookie")) |
| 1202 |
|
| 1203 |
|
| 1204 |
req = urllib.request.Request("wss://www.acme.com/") |
| 1205 |
c.add_cookie_header(req) |
| 1206 |
self.assertTrue(req.has_header("Cookie")) |
| 1207 |
|
| 1208 |
|
| 1209 |
req = urllib.request.Request("ws://www.acme.com/") |
| 1210 |
c.add_cookie_header(req) |
| 1211 |
self.assertFalse(req.has_header("Cookie")) |
| 1212 |
|
| 1213 |
def test_custom_secure_protocols(self): |
| 1214 |
pol = DefaultCookiePolicy(secure_protocols=["foos"]) |
| 1215 |
c = CookieJar(policy=pol) |
| 1216 |
|
| 1217 |
headers = ["Set-Cookie: session=narf; secure; path=/"] |
| 1218 |
req = urllib.request.Request("https://www.acme.com/") |
| 1219 |
res = FakeResponse(headers, "https://www.acme.com/") |
| 1220 |
c.extract_cookies(res, req) |
| 1221 |
self.assertEqual(len(c), 1) |
| 1222 |
|
| 1223 |
|
| 1224 |
req = urllib.request.Request("https://www.acme.com/") |
| 1225 |
c.add_cookie_header(req) |
| 1226 |
self.assertFalse(req.has_header("Cookie")) |
| 1227 |
|
| 1228 |
req = urllib.request.Request("http://www.acme.com/") |
| 1229 |
c.add_cookie_header(req) |
| 1230 |
self.assertFalse(req.has_header("Cookie")) |
| 1231 |
|
| 1232 |
req = urllib.request.Request("foos://www.acme.com/") |
| 1233 |
c.add_cookie_header(req) |
| 1234 |
self.assertTrue(req.has_header("Cookie")) |
| 1235 |
|
| 1236 |
req = urllib.request.Request("foo://www.acme.com/") |
| 1237 |
c.add_cookie_header(req) |
| 1238 |
self.assertFalse(req.has_header("Cookie")) |
| 1239 |
|
| 1240 |
def test_quote_cookie_value(self): |
| 1241 |
c = CookieJar(policy=DefaultCookiePolicy(rfc2965=True)) |
| 1242 |
interact_2965(c, "http://www.acme.com/", r'foo=\b"a"r; Version=1') |
| 1243 |
h = interact_2965(c, "http://www.acme.com/") |
| 1244 |
self.assertEqual(h, r'$Version=1; foo=\\b\"a\"r') |
| 1245 |
|
| 1246 |
def test_missing_final_slash(self): |
| 1247 |
|
| 1248 |
url = "http://www.acme.com" |
| 1249 |
c = CookieJar(DefaultCookiePolicy(rfc2965=True)) |
| 1250 |
interact_2965(c, url, "foo=bar; Version=1") |
| 1251 |
req = urllib.request.Request(url) |
| 1252 |
self.assertEqual(len(c), 1) |
| 1253 |
c.add_cookie_header(req) |
| 1254 |
self.assertTrue(req.has_header("Cookie")) |
| 1255 |
|
| 1256 |
def test_domain_mirror(self): |
| 1257 |
pol = DefaultCookiePolicy(rfc2965=True) |
| 1258 |
|
| 1259 |
c = CookieJar(pol) |
| 1260 |
url = "http://foo.bar.com/" |
| 1261 |
interact_2965(c, url, "spam=eggs; Version=1") |
| 1262 |
h = interact_2965(c, url) |
| 1263 |
self.assertNotIn("Domain", h, |
| 1264 |
"absent domain returned with domain present") |
| 1265 |
|
| 1266 |
c = CookieJar(pol) |
| 1267 |
url = "http://foo.bar.com/" |
| 1268 |
interact_2965(c, url, 'spam=eggs; Version=1; Domain=.bar.com') |
| 1269 |
h = interact_2965(c, url) |
| 1270 |
self.assertIn('$Domain=".bar.com"', h, "domain not returned") |
| 1271 |
|
| 1272 |
c = CookieJar(pol) |
| 1273 |
url = "http://foo.bar.com/" |
| 1274 |
|
| 1275 |
interact_2965(c, url, 'spam=eggs; Version=1; Domain=bar.com') |
| 1276 |
h = interact_2965(c, url) |
| 1277 |
self.assertIn('$Domain="bar.com"', h, "domain not returned") |
| 1278 |
|
| 1279 |
def test_path_mirror(self): |
| 1280 |
pol = DefaultCookiePolicy(rfc2965=True) |
| 1281 |
|
| 1282 |
c = CookieJar(pol) |
| 1283 |
url = "http://foo.bar.com/" |
| 1284 |
interact_2965(c, url, "spam=eggs; Version=1") |
| 1285 |
h = interact_2965(c, url) |
| 1286 |
self.assertNotIn("Path", h, "absent path returned with path present") |
| 1287 |
|
| 1288 |
c = CookieJar(pol) |
| 1289 |
url = "http://foo.bar.com/" |
| 1290 |
interact_2965(c, url, 'spam=eggs; Version=1; Path=/') |
| 1291 |
h = interact_2965(c, url) |
| 1292 |
self.assertIn('$Path="/"', h, "path not returned") |
| 1293 |
|
| 1294 |
def test_port_mirror(self): |
| 1295 |
pol = DefaultCookiePolicy(rfc2965=True) |
| 1296 |
|
| 1297 |
c = CookieJar(pol) |
| 1298 |
url = "http://foo.bar.com/" |
| 1299 |
interact_2965(c, url, "spam=eggs; Version=1") |
| 1300 |
h = interact_2965(c, url) |
| 1301 |
self.assertNotIn("Port", h, "absent port returned with port present") |
| 1302 |
|
| 1303 |
c = CookieJar(pol) |
| 1304 |
url = "http://foo.bar.com/" |
| 1305 |
interact_2965(c, url, "spam=eggs; Version=1; Port") |
| 1306 |
h = interact_2965(c, url) |
| 1307 |
self.assertRegex(h, r"\$Port([^=]|$)", |
| 1308 |
"port with no value not returned with no value") |
| 1309 |
|
| 1310 |
c = CookieJar(pol) |
| 1311 |
url = "http://foo.bar.com/" |
| 1312 |
interact_2965(c, url, 'spam=eggs; Version=1; Port="80"') |
| 1313 |
h = interact_2965(c, url) |
| 1314 |
self.assertIn('$Port="80"', h, |
| 1315 |
"port with single value not returned with single value") |
| 1316 |
|
| 1317 |
c = CookieJar(pol) |
| 1318 |
url = "http://foo.bar.com/" |
| 1319 |
interact_2965(c, url, 'spam=eggs; Version=1; Port="80,8080"') |
| 1320 |
h = interact_2965(c, url) |
| 1321 |
self.assertIn('$Port="80,8080"', h, |
| 1322 |
"port with multiple values not returned with multiple " |
| 1323 |
"values") |
| 1324 |
|
| 1325 |
def test_no_return_comment(self): |
| 1326 |
c = CookieJar(DefaultCookiePolicy(rfc2965=True)) |
| 1327 |
url = "http://foo.bar.com/" |
| 1328 |
interact_2965(c, url, 'spam=eggs; Version=1; ' |
| 1329 |
'Comment="does anybody read these?"; ' |
| 1330 |
'CommentURL="http://foo.bar.net/comment.html"') |
| 1331 |
h = interact_2965(c, url) |
| 1332 |
self.assertNotIn("Comment", h, |
| 1333 |
"Comment or CommentURL cookie-attributes returned to server") |
| 1334 |
|
| 1335 |
def test_Cookie_iterator(self): |
| 1336 |
cs = CookieJar(DefaultCookiePolicy(rfc2965=True)) |
| 1337 |
|
| 1338 |
interact_2965(cs, "http://blah.spam.org/", 'foo=eggs; Version=1; ' |
| 1339 |
'Comment="does anybody read these?"; ' |
| 1340 |
'CommentURL="http://foo.bar.net/comment.html"') |
| 1341 |
interact_netscape(cs, "http://www.acme.com/blah/", "spam=bar; secure") |
| 1342 |
interact_2965(cs, "http://www.acme.com/blah/", |
| 1343 |
"foo=bar; secure; Version=1") |
| 1344 |
interact_2965(cs, "http://www.acme.com/blah/", |
| 1345 |
"foo=bar; path=/; Version=1") |
| 1346 |
interact_2965(cs, "http://www.sol.no", |
| 1347 |
r'bang=wallop; version=1; domain=".sol.no"; ' |
| 1348 |
r'port="90,100, 80,8080"; ' |
| 1349 |
r'max-age=100; Comment = "Just kidding! (\"|\\\\) "') |
| 1350 |
|
| 1351 |
versions = [1, 0, 1, 1, 1] |
| 1352 |
names = ["foo", "spam", "foo", "foo", "bang"] |
| 1353 |
domains = ["blah.spam.org", "www.acme.com", "www.acme.com", |
| 1354 |
"www.acme.com", ".sol.no"] |
| 1355 |
paths = ["/", "/blah", "/blah/", "/", "/"] |
| 1356 |
|
| 1357 |
for i in range(4): |
| 1358 |
i = 0 |
| 1359 |
for c in cs: |
| 1360 |
self.assertIsInstance(c, Cookie) |
| 1361 |
self.assertEqual(c.version, versions[i]) |
| 1362 |
self.assertEqual(c.name, names[i]) |
| 1363 |
self.assertEqual(c.domain, domains[i]) |
| 1364 |
self.assertEqual(c.path, paths[i]) |
| 1365 |
i = i + 1 |
| 1366 |
|
| 1367 |
def test_parse_ns_headers(self): |
| 1368 |
|
| 1369 |
self.assertEqual( |
| 1370 |
parse_ns_headers(["foo=bar; path=/; domain"]), |
| 1371 |
[[("foo", "bar"), |
| 1372 |
("path", "/"), ("domain", None), ("version", "0")]] |
| 1373 |
) |
| 1374 |
|
| 1375 |
self.assertEqual( |
| 1376 |
parse_ns_headers(["foo=bar; expires=Foo Bar 12 33:22:11 2000"]), |
| 1377 |
[[("foo", "bar"), ("expires", None), ("version", "0")]] |
| 1378 |
) |
| 1379 |
|
| 1380 |
self.assertEqual( |
| 1381 |
parse_ns_headers(["foo"]), |
| 1382 |
[[("foo", None), ("version", "0")]] |
| 1383 |
) |
| 1384 |
|
| 1385 |
self.assertEqual( |
| 1386 |
parse_ns_headers(['foo=bar; expires']), |
| 1387 |
[[('foo', 'bar'), ('expires', None), ('version', '0')]]) |
| 1388 |
self.assertEqual( |
| 1389 |
parse_ns_headers(['foo=bar; version']), |
| 1390 |
[[('foo', 'bar'), ('version', None)]]) |
| 1391 |
|
| 1392 |
self.assertEqual(parse_ns_headers([""]), []) |
| 1393 |
|
| 1394 |
def test_bad_cookie_header(self): |
| 1395 |
|
| 1396 |
def cookiejar_from_cookie_headers(headers): |
| 1397 |
c = CookieJar() |
| 1398 |
req = urllib.request.Request("http://www.example.com/") |
| 1399 |
r = FakeResponse(headers, "http://www.example.com/") |
| 1400 |
c.extract_cookies(r, req) |
| 1401 |
return c |
| 1402 |
|
| 1403 |
future = time2netscape(time.time()+3600) |
| 1404 |
|
| 1405 |
|
| 1406 |
for headers in [ |
| 1407 |
["Set-Cookie: "], |
| 1408 |
["Set-Cookie2: "], |
| 1409 |
|
| 1410 |
["Set-Cookie2: a=foo; path=/; Version=1; domain"], |
| 1411 |
|
| 1412 |
["Set-Cookie: b=foo; max-age=oops"], |
| 1413 |
|
| 1414 |
["Set-Cookie: b=foo; version=spam"], |
| 1415 |
["Set-Cookie:; Expires=%s" % future], |
| 1416 |
]: |
| 1417 |
c = cookiejar_from_cookie_headers(headers) |
| 1418 |
|
| 1419 |
self.assertEqual(len(c), 0) |
| 1420 |
|
| 1421 |
|
| 1422 |
headers = ["Set-Cookie: c=foo; expires=Foo Bar 12 33:22:11 2000"] |
| 1423 |
c = cookiejar_from_cookie_headers(headers) |
| 1424 |
cookie = c._cookies["www.example.com"]["/"]["c"] |
| 1425 |
self.assertIsNone(cookie.expires) |
| 1426 |
|
| 1427 |
|
| 1428 |
class LWPCookieTests(unittest.TestCase): |
| 1429 |
|
| 1430 |
|
| 1431 |
def test_netscape_example_1(self): |
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
year_plus_one = time.localtime()[0] + 1 |
| 1469 |
|
| 1470 |
headers = [] |
| 1471 |
|
| 1472 |
c = CookieJar(DefaultCookiePolicy(rfc2965 = True)) |
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
req = urllib.request.Request("http://www.acme.com:80/", |
| 1477 |
headers={"Host": "www.acme.com:80"}) |
| 1478 |
|
| 1479 |
headers.append( |
| 1480 |
"Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/ ; " |
| 1481 |
"expires=Wednesday, 09-Nov-%d 23:12:40 GMT" % year_plus_one) |
| 1482 |
res = FakeResponse(headers, "http://www.acme.com/") |
| 1483 |
c.extract_cookies(res, req) |
| 1484 |
|
| 1485 |
req = urllib.request.Request("http://www.acme.com/") |
| 1486 |
c.add_cookie_header(req) |
| 1487 |
|
| 1488 |
self.assertEqual(req.get_header("Cookie"), "CUSTOMER=WILE_E_COYOTE") |
| 1489 |
self.assertEqual(req.get_header("Cookie2"), '$Version="1"') |
| 1490 |
|
| 1491 |
headers.append("Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/") |
| 1492 |
res = FakeResponse(headers, "http://www.acme.com/") |
| 1493 |
c.extract_cookies(res, req) |
| 1494 |
|
| 1495 |
req = urllib.request.Request("http://www.acme.com/foo/bar") |
| 1496 |
c.add_cookie_header(req) |
| 1497 |
|
| 1498 |
h = req.get_header("Cookie") |
| 1499 |
self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h) |
| 1500 |
self.assertIn("CUSTOMER=WILE_E_COYOTE", h) |
| 1501 |
|
| 1502 |
headers.append('Set-Cookie: SHIPPING=FEDEX; path=/foo') |
| 1503 |
res = FakeResponse(headers, "http://www.acme.com") |
| 1504 |
c.extract_cookies(res, req) |
| 1505 |
|
| 1506 |
req = urllib.request.Request("http://www.acme.com/") |
| 1507 |
c.add_cookie_header(req) |
| 1508 |
|
| 1509 |
h = req.get_header("Cookie") |
| 1510 |
self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h) |
| 1511 |
self.assertIn("CUSTOMER=WILE_E_COYOTE", h) |
| 1512 |
self.assertNotIn("SHIPPING=FEDEX", h) |
| 1513 |
|
| 1514 |
req = urllib.request.Request("http://www.acme.com/foo/") |
| 1515 |
c.add_cookie_header(req) |
| 1516 |
|
| 1517 |
h = req.get_header("Cookie") |
| 1518 |
self.assertIn("PART_NUMBER=ROCKET_LAUNCHER_0001", h) |
| 1519 |
self.assertIn("CUSTOMER=WILE_E_COYOTE", h) |
| 1520 |
self.assertTrue(h.startswith("SHIPPING=FEDEX;")) |
| 1521 |
|
| 1522 |
def test_netscape_example_2(self): |
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
|
| 1540 |
|
| 1541 |
|
| 1542 |
|
| 1543 |
|
| 1544 |
|
| 1545 |
|
| 1546 |
c = CookieJar() |
| 1547 |
headers = [] |
| 1548 |
|
| 1549 |
req = urllib.request.Request("http://www.acme.com/") |
| 1550 |
headers.append("Set-Cookie: PART_NUMBER=ROCKET_LAUNCHER_0001; path=/") |
| 1551 |
res = FakeResponse(headers, "http://www.acme.com/") |
| 1552 |
|
| 1553 |
c.extract_cookies(res, req) |
| 1554 |
|
| 1555 |
req = urllib.request.Request("http://www.acme.com/") |
| 1556 |
c.add_cookie_header(req) |
| 1557 |
|
| 1558 |
self.assertEqual(req.get_header("Cookie"), |
| 1559 |
"PART_NUMBER=ROCKET_LAUNCHER_0001") |
| 1560 |
|
| 1561 |
headers.append( |
| 1562 |
"Set-Cookie: PART_NUMBER=RIDING_ROCKET_0023; path=/ammo") |
| 1563 |
res = FakeResponse(headers, "http://www.acme.com/") |
| 1564 |
c.extract_cookies(res, req) |
| 1565 |
|
| 1566 |
req = urllib.request.Request("http://www.acme.com/ammo") |
| 1567 |
c.add_cookie_header(req) |
| 1568 |
|
| 1569 |
self.assertRegex(req.get_header("Cookie"), |
| 1570 |
r"PART_NUMBER=RIDING_ROCKET_0023;\s*" |
| 1571 |
"PART_NUMBER=ROCKET_LAUNCHER_0001") |
| 1572 |
|
| 1573 |
def test_ietf_example_1(self): |
| 1574 |
|
| 1575 |
|
| 1576 |
|
| 1577 |
|
| 1578 |
|
| 1579 |
c = CookieJar(DefaultCookiePolicy(rfc2965=True)) |
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
|
| 1585 |
|
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
|
| 1590 |
|
| 1591 |
|
| 1592 |
|
| 1593 |
|
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
cookie = interact_2965( |
| 1602 |
c, 'http://www.acme.com/acme/login', |
| 1603 |
'Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"') |
| 1604 |
self.assertFalse(cookie) |
| 1605 |
|
| 1606 |
|
| 1607 |
|
| 1608 |
|
| 1609 |
|
| 1610 |
|
| 1611 |
|
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
cookie = interact_2965(c, 'http://www.acme.com/acme/pickitem', |
| 1624 |
'Part_Number="Rocket_Launcher_0001"; ' |
| 1625 |
'Version="1"; Path="/acme"'); |
| 1626 |
self.assertRegex(cookie, |
| 1627 |
r'^\$Version="?1"?; Customer="?WILE_E_COYOTE"?; \$Path="/acme"$') |
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
|
| 1638 |
|
| 1639 |
|
| 1640 |
|
| 1641 |
|
| 1642 |
|
| 1643 |
|
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
cookie = interact_2965(c, "http://www.acme.com/acme/shipping", |
| 1648 |
'Shipping="FedEx"; Version="1"; Path="/acme"') |
| 1649 |
|
| 1650 |
self.assertRegex(cookie, r'^\$Version="?1"?;') |
| 1651 |
self.assertRegex(cookie, r'Part_Number="?Rocket_Launcher_0001"?;' |
| 1652 |
r'\s*\$Path="\/acme"') |
| 1653 |
self.assertRegex(cookie, r'Customer="?WILE_E_COYOTE"?;' |
| 1654 |
r'\s*\$Path="\/acme"') |
| 1655 |
|
| 1656 |
|
| 1657 |
|
| 1658 |
|
| 1659 |
|
| 1660 |
|
| 1661 |
|
| 1662 |
|
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
|
| 1672 |
|
| 1673 |
|
| 1674 |
cookie = interact_2965(c, "http://www.acme.com/acme/process") |
| 1675 |
self.assertRegex(cookie, r'Shipping="?FedEx"?;\s*\$Path="\/acme"') |
| 1676 |
self.assertIn("WILE_E_COYOTE", cookie) |
| 1677 |
|
| 1678 |
|
| 1679 |
|
| 1680 |
|
| 1681 |
|
| 1682 |
|
| 1683 |
|
| 1684 |
|
| 1685 |
def test_ietf_example_2(self): |
| 1686 |
|
| 1687 |
|
| 1688 |
|
| 1689 |
|
| 1690 |
|
| 1691 |
|
| 1692 |
c = CookieJar(DefaultCookiePolicy(rfc2965=True)) |
| 1693 |
|
| 1694 |
|
| 1695 |
|
| 1696 |
|
| 1697 |
|
| 1698 |
|
| 1699 |
|
| 1700 |
|
| 1701 |
|
| 1702 |
|
| 1703 |
|
| 1704 |
|
| 1705 |
interact_2965( |
| 1706 |
c, "http://www.acme.com/acme/ammo/specific", |
| 1707 |
'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"', |
| 1708 |
'Part_Number="Riding_Rocket_0023"; Version="1"; Path="/acme/ammo"') |
| 1709 |
|
| 1710 |
|
| 1711 |
|
| 1712 |
|
| 1713 |
|
| 1714 |
|
| 1715 |
|
| 1716 |
|
| 1717 |
|
| 1718 |
|
| 1719 |
|
| 1720 |
|
| 1721 |
|
| 1722 |
cookie = interact_2965(c, "http://www.acme.com/acme/ammo/...") |
| 1723 |
self.assertRegex(cookie, r"Riding_Rocket_0023.*Rocket_Launcher_0001") |
| 1724 |
|
| 1725 |
|
| 1726 |
|
| 1727 |
|
| 1728 |
|
| 1729 |
|
| 1730 |
|
| 1731 |
|
| 1732 |
|
| 1733 |
|
| 1734 |
cookie = interact_2965(c, "http://www.acme.com/acme/parts/") |
| 1735 |
self.assertIn("Rocket_Launcher_0001", cookie) |
| 1736 |
self.assertNotIn("Riding_Rocket_0023", cookie) |
| 1737 |
|
| 1738 |
def test_rejection(self): |
| 1739 |
|
| 1740 |
pol = DefaultCookiePolicy(rfc2965=True) |
| 1741 |
|
| 1742 |
c = LWPCookieJar(policy=pol) |
| 1743 |
|
| 1744 |
max_age = "max-age=3600" |
| 1745 |
|
| 1746 |
|
| 1747 |
cookie = interact_2965(c, "http://www.acme.com", |
| 1748 |
'foo=bar; domain=".com"; version=1') |
| 1749 |
self.assertFalse(c) |
| 1750 |
|
| 1751 |
|
| 1752 |
cookie = interact_2965(c, "http://www.acme.com", |
| 1753 |
'ping=pong; domain="acme.com"; version=1') |
| 1754 |
self.assertEqual(len(c), 1) |
| 1755 |
|
| 1756 |
|
| 1757 |
cookie = interact_2965(c, "http://www.a.acme.com", |
| 1758 |
'whiz=bang; domain="acme.com"; version=1') |
| 1759 |
self.assertEqual(len(c), 1) |
| 1760 |
|
| 1761 |
|
| 1762 |
cookie = interact_2965(c, "http://www.a.acme.com", |
| 1763 |
'wow=flutter; domain=".a.acme.com"; version=1') |
| 1764 |
self.assertEqual(len(c), 2) |
| 1765 |
|
| 1766 |
|
| 1767 |
cookie = interact_2965(c, "http://125.125.125.125", |
| 1768 |
'zzzz=ping; domain="125.125.125"; version=1') |
| 1769 |
self.assertEqual(len(c), 2) |
| 1770 |
|
| 1771 |
|
| 1772 |
cookie = interact_2965(c, "http://www.sol.no", |
| 1773 |
'blah=rhubarb; domain=".sol.no"; path="/foo"; ' |
| 1774 |
'version=1') |
| 1775 |
self.assertEqual(len(c), 2) |
| 1776 |
|
| 1777 |
|
| 1778 |
cookie = interact_2965(c, "http://www.sol.no/foo/bar", |
| 1779 |
'bing=bong; domain=".sol.no"; path="/foo"; ' |
| 1780 |
'version=1') |
| 1781 |
self.assertEqual(len(c), 3) |
| 1782 |
|
| 1783 |
|
| 1784 |
cookie = interact_2965(c, "http://www.sol.no", |
| 1785 |
'whiz=ffft; domain=".sol.no"; port="90,100"; ' |
| 1786 |
'version=1') |
| 1787 |
self.assertEqual(len(c), 3) |
| 1788 |
|
| 1789 |
|
| 1790 |
cookie = interact_2965( |
| 1791 |
c, "http://www.sol.no", |
| 1792 |
r'bang=wallop; version=1; domain=".sol.no"; ' |
| 1793 |
r'port="90,100, 80,8080"; ' |
| 1794 |
r'max-age=100; Comment = "Just kidding! (\"|\\\\) "') |
| 1795 |
self.assertEqual(len(c), 4) |
| 1796 |
|
| 1797 |
|
| 1798 |
cookie = interact_2965(c, "http://www.sol.no", |
| 1799 |
'foo9=bar; version=1; domain=".sol.no"; port; ' |
| 1800 |
'max-age=100;') |
| 1801 |
self.assertEqual(len(c), 5) |
| 1802 |
|
| 1803 |
|
| 1804 |
|
| 1805 |
|
| 1806 |
|
| 1807 |
|
| 1808 |
|
| 1809 |
|
| 1810 |
cookie = interact_2965(c, "http://www.sol.no/<oo/", |
| 1811 |
r'foo8=bar; version=1; path="/%3coo"') |
| 1812 |
self.assertEqual(len(c), 6) |
| 1813 |
|
| 1814 |
|
| 1815 |
filename = os_helper.TESTFN |
| 1816 |
|
| 1817 |
try: |
| 1818 |
c.save(filename, ignore_discard=True) |
| 1819 |
old = repr(c) |
| 1820 |
|
| 1821 |
c = LWPCookieJar(policy=pol) |
| 1822 |
c.load(filename, ignore_discard=True) |
| 1823 |
finally: |
| 1824 |
os_helper.unlink(filename) |
| 1825 |
|
| 1826 |
self.assertEqual(old, repr(c)) |
| 1827 |
|
| 1828 |
def test_url_encoding(self): |
| 1829 |
|
| 1830 |
|
| 1831 |
c = CookieJar(DefaultCookiePolicy(rfc2965=True)) |
| 1832 |
interact_2965(c, "http://www.acme.com/foo%2f%25/" |
| 1833 |
"%3c%3c%0Anew%C3%A5/%C3%A5", |
| 1834 |
"foo = bar; version = 1") |
| 1835 |
|
| 1836 |
cookie = interact_2965( |
| 1837 |
c, "http://www.acme.com/foo%2f%25/<<%0anew\345/\346\370\345", |
| 1838 |
'bar=baz; path="/foo/"; version=1'); |
| 1839 |
version_re = re.compile(r'^\$version=\"?1\"?', re.I) |
| 1840 |
self.assertIn("foo=bar", cookie) |
| 1841 |
self.assertRegex(cookie, version_re) |
| 1842 |
|
| 1843 |
cookie = interact_2965( |
| 1844 |
c, "http://www.acme.com/foo/%25/<<%0anew\345/\346\370\345") |
| 1845 |
self.assertFalse(cookie) |
| 1846 |
|
| 1847 |
|
| 1848 |
cookie = interact_2965(c, "http://www.acme.com/\xfc") |
| 1849 |
|
| 1850 |
def test_mozilla(self): |
| 1851 |
|
| 1852 |
year_plus_one = time.localtime()[0] + 1 |
| 1853 |
|
| 1854 |
filename = os_helper.TESTFN |
| 1855 |
|
| 1856 |
c = MozillaCookieJar(filename, |
| 1857 |
policy=DefaultCookiePolicy(rfc2965=True)) |
| 1858 |
interact_2965(c, "http://www.acme.com/", |
| 1859 |
"foo1=bar; max-age=100; Version=1") |
| 1860 |
interact_2965(c, "http://www.acme.com/", |
| 1861 |
'foo2=bar; port="80"; max-age=100; Discard; Version=1') |
| 1862 |
interact_2965(c, "http://www.acme.com/", "foo3=bar; secure; Version=1") |
| 1863 |
|
| 1864 |
expires = "expires=09-Nov-%d 23:12:40 GMT" % (year_plus_one,) |
| 1865 |
interact_netscape(c, "http://www.foo.com/", |
| 1866 |
"fooa=bar; %s" % expires) |
| 1867 |
interact_netscape(c, "http://www.foo.com/", |
| 1868 |
"foob=bar; Domain=.foo.com; %s" % expires) |
| 1869 |
interact_netscape(c, "http://www.foo.com/", |
| 1870 |
"fooc=bar; Domain=www.foo.com; %s" % expires) |
| 1871 |
|
| 1872 |
for cookie in c: |
| 1873 |
if cookie.name == "foo1": |
| 1874 |
cookie.set_nonstandard_attr("HTTPOnly", "") |
| 1875 |
|
| 1876 |
def save_and_restore(cj, ignore_discard): |
| 1877 |
try: |
| 1878 |
cj.save(ignore_discard=ignore_discard) |
| 1879 |
new_c = MozillaCookieJar(filename, |
| 1880 |
DefaultCookiePolicy(rfc2965=True)) |
| 1881 |
new_c.load(ignore_discard=ignore_discard) |
| 1882 |
finally: |
| 1883 |
os_helper.unlink(filename) |
| 1884 |
return new_c |
| 1885 |
|
| 1886 |
new_c = save_and_restore(c, True) |
| 1887 |
self.assertEqual(len(new_c), 6) |
| 1888 |
self.assertIn("name='foo1', value='bar'", repr(new_c)) |
| 1889 |
self.assertIn("rest={'HTTPOnly': ''}", repr(new_c)) |
| 1890 |
|
| 1891 |
new_c = save_and_restore(c, False) |
| 1892 |
self.assertEqual(len(new_c), 4) |
| 1893 |
self.assertIn("name='foo1', value='bar'", repr(new_c)) |
| 1894 |
|
| 1895 |
def test_netscape_misc(self): |
| 1896 |
|
| 1897 |
c = CookieJar() |
| 1898 |
headers = [] |
| 1899 |
req = urllib.request.Request("http://foo.bar.acme.com/foo") |
| 1900 |
|
| 1901 |
|
| 1902 |
headers.append("Set-Cookie: Customer=WILE_E_COYOTE; domain=.acme.com") |
| 1903 |
res = FakeResponse(headers, "http://www.acme.com/foo") |
| 1904 |
c.extract_cookies(res, req) |
| 1905 |
|
| 1906 |
|
| 1907 |
|
| 1908 |
|
| 1909 |
headers.append("Set-Cookie: PART_NUMBER=3,4; domain=foo.bar.acme.com") |
| 1910 |
res = FakeResponse(headers, "http://www.acme.com/foo") |
| 1911 |
c.extract_cookies(res, req) |
| 1912 |
|
| 1913 |
req = urllib.request.Request("http://foo.bar.acme.com/foo") |
| 1914 |
c.add_cookie_header(req) |
| 1915 |
self.assertIn("PART_NUMBER=3,4", req.get_header("Cookie")) |
| 1916 |
self.assertIn("Customer=WILE_E_COYOTE",req.get_header("Cookie")) |
| 1917 |
|
| 1918 |
def test_intranet_domains_2965(self): |
| 1919 |
|
| 1920 |
c = CookieJar(DefaultCookiePolicy(rfc2965=True)) |
| 1921 |
interact_2965(c, "http://example/", |
| 1922 |
"foo1=bar; PORT; Discard; Version=1;") |
| 1923 |
cookie = interact_2965(c, "http://example/", |
| 1924 |
'foo2=bar; domain=".local"; Version=1') |
| 1925 |
self.assertIn("foo1=bar", cookie) |
| 1926 |
|
| 1927 |
interact_2965(c, "http://example/", 'foo3=bar; Version=1') |
| 1928 |
cookie = interact_2965(c, "http://example/") |
| 1929 |
self.assertIn("foo2=bar", cookie) |
| 1930 |
self.assertEqual(len(c), 3) |
| 1931 |
|
| 1932 |
def test_intranet_domains_ns(self): |
| 1933 |
c = CookieJar(DefaultCookiePolicy(rfc2965 = False)) |
| 1934 |
interact_netscape(c, "http://example/", "foo1=bar") |
| 1935 |
cookie = interact_netscape(c, "http://example/", |
| 1936 |
'foo2=bar; domain=.local') |
| 1937 |
self.assertEqual(len(c), 2) |
| 1938 |
self.assertIn("foo1=bar", cookie) |
| 1939 |
|
| 1940 |
cookie = interact_netscape(c, "http://example/") |
| 1941 |
self.assertIn("foo2=bar", cookie) |
| 1942 |
self.assertEqual(len(c), 2) |
| 1943 |
|
| 1944 |
def test_empty_path(self): |
| 1945 |
|
| 1946 |
|
| 1947 |
|
| 1948 |
|
| 1949 |
|
| 1950 |
|
| 1951 |
|
| 1952 |
c = CookieJar(DefaultCookiePolicy(rfc2965 = True)) |
| 1953 |
headers = [] |
| 1954 |
|
| 1955 |
req = urllib.request.Request("http://www.ants.com/") |
| 1956 |
headers.append("Set-Cookie: JSESSIONID=ABCDERANDOM123; Path=") |
| 1957 |
res = FakeResponse(headers, "http://www.ants.com/") |
| 1958 |
c.extract_cookies(res, req) |
| 1959 |
|
| 1960 |
req = urllib.request.Request("http://www.ants.com/") |
| 1961 |
c.add_cookie_header(req) |
| 1962 |
|
| 1963 |
self.assertEqual(req.get_header("Cookie"), |
| 1964 |
"JSESSIONID=ABCDERANDOM123") |
| 1965 |
self.assertEqual(req.get_header("Cookie2"), '$Version="1"') |
| 1966 |
|
| 1967 |
|
| 1968 |
req = urllib.request.Request("http://www.ants.com:8080") |
| 1969 |
c.add_cookie_header(req) |
| 1970 |
|
| 1971 |
self.assertEqual(req.get_header("Cookie"), |
| 1972 |
"JSESSIONID=ABCDERANDOM123") |
| 1973 |
self.assertEqual(req.get_header("Cookie2"), '$Version="1"') |
| 1974 |
|
| 1975 |
def test_session_cookies(self): |
| 1976 |
year_plus_one = time.localtime()[0] + 1 |
| 1977 |
|
| 1978 |
|
| 1979 |
|
| 1980 |
|
| 1981 |
req = urllib.request.Request('http://www.perlmeister.com/scripts') |
| 1982 |
headers = [] |
| 1983 |
headers.append("Set-Cookie: s1=session;Path=/scripts") |
| 1984 |
headers.append("Set-Cookie: p1=perm; Domain=.perlmeister.com;" |
| 1985 |
"Path=/;expires=Fri, 02-Feb-%d 23:24:20 GMT" % |
| 1986 |
year_plus_one) |
| 1987 |
headers.append("Set-Cookie: p2=perm;Path=/;expires=Fri, " |
| 1988 |
"02-Feb-%d 23:24:20 GMT" % year_plus_one) |
| 1989 |
headers.append("Set-Cookie: s2=session;Path=/scripts;" |
| 1990 |
"Domain=.perlmeister.com") |
| 1991 |
headers.append('Set-Cookie2: s3=session;Version=1;Discard;Path="/"') |
| 1992 |
res = FakeResponse(headers, 'http://www.perlmeister.com/scripts') |
| 1993 |
|
| 1994 |
c = CookieJar() |
| 1995 |
c.extract_cookies(res, req) |
| 1996 |
|
| 1997 |
counter = {"session_after": 0, |
| 1998 |
"perm_after": 0, |
| 1999 |
"session_before": 0, |
| 2000 |
"perm_before": 0} |
| 2001 |
for cookie in c: |
| 2002 |
key = "%s_before" % cookie.value |
| 2003 |
counter[key] = counter[key] + 1 |
| 2004 |
c.clear_session_cookies() |
| 2005 |
|
| 2006 |
for cookie in c: |
| 2007 |
key = "%s_after" % cookie.value |
| 2008 |
counter[key] = counter[key] + 1 |
| 2009 |
|
| 2010 |
|
| 2011 |
self.assertEqual(counter["perm_after"], counter["perm_before"]) |
| 2012 |
|
| 2013 |
self.assertEqual(counter["session_after"], 0) |
| 2014 |
|
| 2015 |
self.assertNotEqual(counter["session_before"], 0) |
| 2016 |
|
| 2017 |
|
| 2018 |
if __name__ == "__main__": |
| 2019 |
unittest.main() |