Lib/test/test_http_cookiejar.py - Issue 2827: [issue3300] urllib.quote and unquote Code Review (original) (raw)

OLD

NEW

1 """Tests for http/cookiejar.py."""

1 """Tests for http/cookiejar.py."""

2

2

3 import re, os, time, urllib.request

3 import re, os, time, urllib.request

4 from unittest import TestCase

4 from unittest import TestCase

5

5

6 from test import support

6 from test import support

7

7

8 from http.cookiejar import time2isoz, http2time, time2netscape, \

8 from http.cookiejar import time2isoz, http2time, time2netscape, \

9 parse_ns_headers, join_header_words, split_header_words, Cookie, \

9 parse_ns_headers, join_header_words, split_header_words, Cookie, \

10 CookieJar, DefaultCookiePolicy, LWPCookieJar, MozillaCookieJar, \

10 CookieJar, DefaultCookiePolicy, LWPCookieJar, MozillaCookieJar, \

(...skipping 521 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

532 # quoted unsafe

532 # quoted unsafe

533 ("/fo%19o/bar", "/fo%19o/bar"),

533 ("/fo%19o/bar", "/fo%19o/bar"),

534 ("/fo%7do/bar", "/fo%7Do/bar"),

534 ("/fo%7do/bar", "/fo%7Do/bar"),

535 # unquoted safe

535 # unquoted safe

536 ("/foo/bar&", "/foo/bar&"),

536 ("/foo/bar&", "/foo/bar&"),

537 ("/foo//bar", "/foo//bar"),

537 ("/foo//bar", "/foo//bar"),

538 ("\176/foo/bar", "\176/foo/bar"),

538 ("\176/foo/bar", "\176/foo/bar"),

539 # unquoted unsafe

539 # unquoted unsafe

540 ("/foo\031/bar", "/foo%19/bar"),

540 ("/foo\031/bar", "/foo%19/bar"),

541 ("/\175foo/bar", "/%7Dfoo/bar"),

541 ("/\175foo/bar", "/%7Dfoo/bar"),

542 # unicode, latin-1 range

543 ("/foo/bar\u00fc", "/foo/bar%C3%BC"), # UTF-8 encoded

542 # unicode

544 # unicode

543 ("/foo/bar\uabcd", "/foo/bar%EA%AF%8D"), # UTF-8 encoded

545 ("/foo/bar\uabcd", "/foo/bar%EA%AF%8D"), # UTF-8 encoded

544 ]

546 ]

545 for arg, result in cases:

547 for arg, result in cases:

546 self.assertEquals(escape_path(arg), result)

548 self.assertEquals(escape_path(arg), result)

547

549

548 def test_request_path(self):

550 def test_request_path(self):

549 # with parameters

551 # with parameters

550 req = urllib.request.Request(

552 req = urllib.request.Request(

551 "http://www.example.com/rheum/rhaponicum;"

553 "http://www.example.com/rheum/rhaponicum;"

(...skipping 885 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

1437 finally:

1439 finally:

1438 try: os.unlink(filename)

1440 try: os.unlink(filename)

1439 except OSError: pass

1441 except OSError: pass

1440

1442

1441 self.assertEquals(old, repr(c))

1443 self.assertEquals(old, repr(c))

1442

1444

1443 def test_url_encoding(self):

1445 def test_url_encoding(self):

1444 # Try some URL encodings of the PATHs.

1446 # Try some URL encodings of the PATHs.

1445 # (the behaviour here has changed from libwww-perl)

1447 # (the behaviour here has changed from libwww-perl)

1446 c = CookieJar(DefaultCookiePolicy(rfc2965=True))

1448 c = CookieJar(DefaultCookiePolicy(rfc2965=True))

1447 interact_2965(c, "http://www.acme.com/foo%2f%25/%3c%3c%0Anew%E5/%E5",

1449 interact_2965(c, "http://www.acme.com/foo%2f%25/"

1450 "%3c%3c%0Anew%C3%A5/%C3%A5",

1448 "foo = bar; version = 1")

1451 "foo = bar; version = 1")

1449

1452

1450 cookie = interact_2965(

1453 cookie = interact_2965(

1451 c, "http://www.acme.com/foo%2f%25/<<%0anew\345/\346\370\345",

1454 c, "http://www.acme.com/foo%2f%25/<<%0anew\345/\346\370\345",

1452 'bar=baz; path="/foo/"; version=1');

1455 'bar=baz; path="/foo/"; version=1');

1453 version_re = re.compile(r'^\$version=\"?1\"?', re.I)

1456 version_re = re.compile(r'^\$version=\"?1\"?', re.I)

1454 self.assert_("foo=bar" in cookie and version_re.search(cookie))

1457 self.assert_("foo=bar" in cookie and version_re.search(cookie))

1455

1458

1456 cookie = interact_2965(

1459 cookie = interact_2965(

1457 c, "http://www.acme.com/foo/%25/<<%0anew\345/\346\370\345")

1460 c, "http://www.acme.com/foo/%25/<<%0anew\345/\346\370\345")

(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading...

1629 support.run_unittest(

1632 support.run_unittest(

1630 DateTimeTests,

1633 DateTimeTests,

1631 HeaderTests,

1634 HeaderTests,

1632 CookieTests,

1635 CookieTests,

1633 FileCookieJarTests,

1636 FileCookieJarTests,

1634 LWPCookieTests,

1637 LWPCookieTests,

1635 )

1638 )

1636

1639

1637 if __name__ == "__main__":

1640 if __name__ == "__main__":

1638 test_main(verbose=True)

1641 test_main(verbose=True)

OLD

NEW