import re
import string
import types
+lazy import warnings
__all__ = ["CookieError", "BaseCookie", "SimpleCookie"]
def __repr__(self):
return '<%s: %s>' % (self.__class__.__name__, self.OutputString())
- def js_output(self, attrs=None):
+
+ def _js_output(self, attrs=None):
+ """Internal implementation without deprecation warning."""
import base64
# Print javascript
output_string = self.OutputString(attrs)
</script>
""" % (output_encoded,)
+ def js_output(self, attrs=None):
+ warnings._deprecated(
+ "http.cookies.Morsel.js_output",
+ message=warnings._DEPRECATED_MSG + "; use output() instead",
+ remove=(3, 19),
+ )
+ return self._js_output(attrs)
+
def OutputString(self, attrs=None):
# Build up our result
#
def js_output(self, attrs=None):
"""Return a string suitable for JavaScript."""
+ warnings._deprecated(
+ "http.cookies.BaseCookie.js_output",
+ message=warnings._DEPRECATED_MSG + "; use output() instead",
+ remove=(3, 19),
+ )
result = []
items = sorted(self.items())
for key, value in items:
- result.append(value.js_output(attrs))
+ result.append(value._js_output(attrs))
return _nulljoin(result)
def load(self, rawdata):
self.assertEqual(C.output(['path']),
'Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme')
cookie_encoded = base64.b64encode(b'Customer="WILE_E_COYOTE"; Path=/acme; Version=1').decode('ascii')
- self.assertEqual(C.js_output(), fr"""
+ with self.assertWarnsRegex(DeprecationWarning, r"BaseCookie\.js_output"):
+ self.assertEqual(C.js_output(), fr"""
<script type="text/javascript">
<!-- begin hiding
document.cookie = atob("{cookie_encoded}");
</script>
""")
cookie_encoded = base64.b64encode(b'Customer="WILE_E_COYOTE"; Path=/acme').decode('ascii')
- self.assertEqual(C.js_output(['path']), fr"""
+ with self.assertWarnsRegex(DeprecationWarning, r"BaseCookie\.js_output"):
+ self.assertEqual(C.js_output(['path']), fr"""
<script type="text/javascript">
<!-- begin hiding
document.cookie = atob("{cookie_encoded}");
self.assertEqual(C.output(['path']),
'Set-Cookie: Customer="WILE_E_COYOTE"; Path=/acme')
expected_encoded_cookie = base64.b64encode(b'Customer=\"WILE_E_COYOTE\"; Path=/acme; Version=1').decode('ascii')
- self.assertEqual(C.js_output(), fr"""
+ with self.assertWarnsRegex(DeprecationWarning, r"BaseCookie\.js_output"):
+ self.assertEqual(C.js_output(), fr"""
<script type="text/javascript">
<!-- begin hiding
document.cookie = atob("{expected_encoded_cookie}");
</script>
""")
expected_encoded_cookie = base64.b64encode(b'Customer=\"WILE_E_COYOTE\"; Path=/acme').decode('ascii')
- self.assertEqual(C.js_output(['path']), fr"""
+ with self.assertWarnsRegex(DeprecationWarning, r"BaseCookie\.js_output"):
+ self.assertEqual(C.js_output(['path']), fr"""
<script type="text/javascript">
<!-- begin hiding
document.cookie = atob("{expected_encoded_cookie}");
// end hiding -->
</script>
""" % (expected_encoded_cookie,)
- self.assertEqual(M.js_output(), expected_js_output)
+ with self.assertWarnsRegex(DeprecationWarning, r"Morsel\.js_output"):
+ self.assertEqual(M.js_output(), expected_js_output)
for i in ["foo bar", "foo@bar"]:
# Try some illegal characters
self.assertRaises(cookies.CookieError,
cookie = cookies.SimpleCookie()
cookie["cookie"] = morsel
with self.assertRaises(cookies.CookieError):
- cookie.js_output()
+ with self.assertWarnsRegex(DeprecationWarning, r"Morsel\.js_output"):
+ cookie.js_output()
morsel = cookies.Morsel()
morsel.set("key", "value", "coded-value")
cookie = cookies.SimpleCookie()
cookie["cookie"] = morsel
with self.assertRaises(cookies.CookieError):
- cookie.js_output()
+ with self.assertWarnsRegex(DeprecationWarning, r"Morsel\.js_output"):
+ cookie.js_output()
+ def test_morsel_js_output_deprecated(self):
+ morsel = cookies.Morsel()
+ morsel.set("key", "value", "value")
+ with self.assertWarnsRegex(DeprecationWarning, r"Morsel\.js_output") as cm:
+ result = morsel.js_output()
+ self.assertEqual(cm.filename, __file__)
+ self.assertIn("document.cookie", result)
+
+
+ def test_basecookie_js_output_warns_once(self):
+ C = cookies.SimpleCookie()
+ C["key"] = "value"
+ with self.assertWarns(DeprecationWarning) as cm:
+ C.js_output()
+ deprecation_warnings = [
+ w for w in cm.warnings if issubclass(w.category, DeprecationWarning)
+ ]
+ self.assertEqual(len(deprecation_warnings), 1)
+ self.assertRegex(str(deprecation_warnings[0].message), r"BaseCookie\.js_output")
+ self.assertEqual(cm.filename, __file__)
def load_tests(loader, tests, pattern):
tests.addTest(doctest.DocTestSuite(cookies))