|
5 | 5 | import unittest |
6 | 6 | from http import cookies |
7 | 7 | import pickle |
| 8 | +from test import support |
8 | 9 |
|
9 | 10 |
|
10 | 11 | class CookieTests(unittest.TestCase): |
@@ -58,6 +59,43 @@ def test_basic(self): |
58 | 59 | for k, v in sorted(case['dict'].items()): |
59 | 60 | self.assertEqual(C[k].value, v) |
60 | 61 |
|
| 62 | + def test_unquote(self): |
| 63 | + cases = [ |
| 64 | + (r'a="b=\""', 'b="'), |
| 65 | + (r'a="b=\\"', 'b=\\'), |
| 66 | + (r'a="b=\="', 'b=='), |
| 67 | + (r'a="b=\n"', 'b=n'), |
| 68 | + (r'a="b=\042"', 'b="'), |
| 69 | + (r'a="b=\134"', 'b=\\'), |
| 70 | + (r'a="b=\377"', 'b=\xff'), |
| 71 | + (r'a="b=\400"', 'b=400'), |
| 72 | + (r'a="b=\42"', 'b=42'), |
| 73 | + (r'a="b=\\042"', 'b=\\042'), |
| 74 | + (r'a="b=\\134"', 'b=\\134'), |
| 75 | + (r'a="b=\\\""', 'b=\\"'), |
| 76 | + (r'a="b=\\\042"', 'b=\\"'), |
| 77 | + (r'a="b=\134\""', 'b=\\"'), |
| 78 | + (r'a="b=\134\042"', 'b=\\"'), |
| 79 | + ] |
| 80 | + for encoded, decoded in cases: |
| 81 | + with self.subTest(encoded): |
| 82 | + C = cookies.SimpleCookie() |
| 83 | + C.load(encoded) |
| 84 | + self.assertEqual(C['a'].value, decoded) |
| 85 | + |
| 86 | + @support.requires_resource('cpu') |
| 87 | + def test_unquote_large(self): |
| 88 | + n = 10**6 |
| 89 | + for encoded in r'\\', r'\134': |
| 90 | + with self.subTest(encoded): |
| 91 | + data = 'a="b=' + encoded*n + ';"' |
| 92 | + C = cookies.SimpleCookie() |
| 93 | + C.load(data) |
| 94 | + value = C['a'].value |
| 95 | + self.assertEqual(value[:3], 'b=\\') |
| 96 | + self.assertEqual(value[-2:], '\\;') |
| 97 | + self.assertEqual(len(value), n + 3) |
| 98 | + |
61 | 99 | def test_load(self): |
62 | 100 | C = cookies.SimpleCookie() |
63 | 101 | C.load('Customer="WILE_E_COYOTE"; Version=1; Path=/acme') |
|
0 commit comments