|
1 | 1 | # Simple test suite for http/cookies.py |
2 | 2 |
|
3 | 3 | import copy |
4 | | -from test.support import run_unittest, run_doctest |
5 | 4 | import unittest |
| 5 | +import doctest |
6 | 6 | from http import cookies |
7 | 7 | import pickle |
| 8 | +from test import support |
| 9 | +from test.support.testcase import ExtraAssertions |
8 | 10 |
|
9 | 11 |
|
10 | | -class CookieTests(unittest.TestCase): |
| 12 | +class CookieTests(unittest.TestCase, ExtraAssertions): |
11 | 13 |
|
12 | 14 | def test_basic(self): |
13 | 15 | cases = [ |
@@ -58,6 +60,90 @@ def test_basic(self): |
58 | 60 | for k, v in sorted(case['dict'].items()): |
59 | 61 | self.assertEqual(C[k].value, v) |
60 | 62 |
|
| 63 | + def test_obsolete_rfc850_date_format(self): |
| 64 | + # Test cases with different days and dates in obsolete RFC 850 format |
| 65 | + test_cases = [ |
| 66 | + # from RFC 850, change EST to GMT |
| 67 | + # https://datatracker.ietf.org/doc/html/rfc850#section-2 |
| 68 | + { |
| 69 | + 'data': 'key=value; expires=Saturday, 01-Jan-83 00:00:00 GMT', |
| 70 | + 'output': 'Saturday, 01-Jan-83 00:00:00 GMT' |
| 71 | + }, |
| 72 | + { |
| 73 | + 'data': 'key=value; expires=Friday, 19-Nov-82 16:59:30 GMT', |
| 74 | + 'output': 'Friday, 19-Nov-82 16:59:30 GMT' |
| 75 | + }, |
| 76 | + # from RFC 9110 |
| 77 | + # https://www.rfc-editor.org/rfc/rfc9110.html#section-5.6.7-6 |
| 78 | + { |
| 79 | + 'data': 'key=value; expires=Sunday, 06-Nov-94 08:49:37 GMT', |
| 80 | + 'output': 'Sunday, 06-Nov-94 08:49:37 GMT' |
| 81 | + }, |
| 82 | + # other test cases |
| 83 | + { |
| 84 | + 'data': 'key=value; expires=Wednesday, 09-Nov-94 08:49:37 GMT', |
| 85 | + 'output': 'Wednesday, 09-Nov-94 08:49:37 GMT' |
| 86 | + }, |
| 87 | + { |
| 88 | + 'data': 'key=value; expires=Friday, 11-Nov-94 08:49:37 GMT', |
| 89 | + 'output': 'Friday, 11-Nov-94 08:49:37 GMT' |
| 90 | + }, |
| 91 | + { |
| 92 | + 'data': 'key=value; expires=Monday, 14-Nov-94 08:49:37 GMT', |
| 93 | + 'output': 'Monday, 14-Nov-94 08:49:37 GMT' |
| 94 | + }, |
| 95 | + ] |
| 96 | + |
| 97 | + for case in test_cases: |
| 98 | + with self.subTest(data=case['data']): |
| 99 | + C = cookies.SimpleCookie() |
| 100 | + C.load(case['data']) |
| 101 | + |
| 102 | + # Extract the cookie name from the data string |
| 103 | + cookie_name = case['data'].split('=')[0] |
| 104 | + |
| 105 | + # Check if the cookie is loaded correctly |
| 106 | + self.assertIn(cookie_name, C) |
| 107 | + self.assertEqual(C[cookie_name].get('expires'), case['output']) |
| 108 | + |
| 109 | + def test_unquote(self): |
| 110 | + cases = [ |
| 111 | + (r'a="b=\""', 'b="'), |
| 112 | + (r'a="b=\\"', 'b=\\'), |
| 113 | + (r'a="b=\="', 'b=='), |
| 114 | + (r'a="b=\n"', 'b=n'), |
| 115 | + (r'a="b=\042"', 'b="'), |
| 116 | + (r'a="b=\134"', 'b=\\'), |
| 117 | + (r'a="b=\377"', 'b=\xff'), |
| 118 | + (r'a="b=\400"', 'b=400'), |
| 119 | + (r'a="b=\42"', 'b=42'), |
| 120 | + (r'a="b=\\042"', 'b=\\042'), |
| 121 | + (r'a="b=\\134"', 'b=\\134'), |
| 122 | + (r'a="b=\\\""', 'b=\\"'), |
| 123 | + (r'a="b=\\\042"', 'b=\\"'), |
| 124 | + (r'a="b=\134\""', 'b=\\"'), |
| 125 | + (r'a="b=\134\042"', 'b=\\"'), |
| 126 | + ] |
| 127 | + for encoded, decoded in cases: |
| 128 | + with self.subTest(encoded): |
| 129 | + C = cookies.SimpleCookie() |
| 130 | + C.load(encoded) |
| 131 | + self.assertEqual(C['a'].value, decoded) |
| 132 | + |
| 133 | + @support.requires_resource('cpu') |
| 134 | + def test_unquote_large(self): |
| 135 | + #n = 10**6 |
| 136 | + n = 10**4 # XXX: RUSTPYTHON; This takes more than 10 minutes to run. lower to 4 |
| 137 | + for encoded in r'\\', r'\134': |
| 138 | + with self.subTest(encoded): |
| 139 | + data = 'a="b=' + encoded*n + ';"' |
| 140 | + C = cookies.SimpleCookie() |
| 141 | + C.load(data) |
| 142 | + value = C['a'].value |
| 143 | + self.assertEqual(value[:3], 'b=\\') |
| 144 | + self.assertEqual(value[-2:], '\\;') |
| 145 | + self.assertEqual(len(value), n + 3) |
| 146 | + |
61 | 147 | def test_load(self): |
62 | 148 | C = cookies.SimpleCookie() |
63 | 149 | C.load('Customer="WILE_E_COYOTE"; Version=1; Path=/acme') |
@@ -96,7 +182,7 @@ def test_special_attrs(self): |
96 | 182 | C = cookies.SimpleCookie('Customer="WILE_E_COYOTE"') |
97 | 183 | C['Customer']['expires'] = 0 |
98 | 184 | # can't test exact output, it always depends on current date/time |
99 | | - self.assertTrue(C.output().endswith('GMT')) |
| 185 | + self.assertEndsWith(C.output(), 'GMT') |
100 | 186 |
|
101 | 187 | # loading 'expires' |
102 | 188 | C = cookies.SimpleCookie() |
@@ -479,9 +565,11 @@ def test_repr(self): |
479 | 565 | r'Set-Cookie: key=coded_val; ' |
480 | 566 | r'expires=\w+, \d+ \w+ \d+ \d+:\d+:\d+ \w+') |
481 | 567 |
|
482 | | -def test_main(): |
483 | | - run_unittest(CookieTests, MorselTests) |
484 | | - run_doctest(cookies) |
| 568 | + |
| 569 | +def load_tests(loader, tests, pattern): |
| 570 | + tests.addTest(doctest.DocTestSuite(cookies)) |
| 571 | + return tests |
| 572 | + |
485 | 573 |
|
486 | 574 | if __name__ == '__main__': |
487 | | - test_main() |
| 575 | + unittest.main() |
0 commit comments