from tornado.simple_httpclient import SimpleAsyncHTTPClient
from tornado.template import DictLoader
from tornado.testing import AsyncHTTPTestCase, AsyncTestCase, ExpectLog, gen_test
+from tornado.test.util import ignore_deprecation
from tornado.util import ObjectDict, unicode_type
from tornado.web import (
Application,
self.set_cookie("c", "1", httponly=True)
self.set_cookie("d", "1", httponly=False)
+ class SetCookieDeprecatedArgs(RequestHandler):
+ def get(self):
+ # Mixed case is supported, but deprecated
+ self.set_cookie("a", "b", HttpOnly=True, pATH="/foo")
+
return [
("/set", SetCookieHandler),
("/get", GetCookieHandler),
("/set_max_age", SetCookieMaxAgeHandler),
("/set_expires_days", SetCookieExpiresDaysHandler),
("/set_falsy_flags", SetCookieFalsyFlags),
+ ("/set_deprecated", SetCookieDeprecatedArgs),
]
def test_set_cookie(self):
self.assertEqual(headers[2].lower(), "c=1; httponly; path=/")
self.assertEqual(headers[3].lower(), "d=1; path=/")
+ def test_set_cookie_deprecated(self):
+ with ignore_deprecation():
+ response = self.fetch("/set_deprecated")
+ header = response.headers.get("Set-Cookie")
+ self.assertEqual(header, "a=b; HttpOnly; Path=/foo")
+
class AuthRedirectRequestHandler(RequestHandler):
def initialize(self, login_url):
import sys
import threading
import time
+import warnings
import tornado
import traceback
import types
httponly: bool = False,
secure: bool = False,
samesite: Optional[str] = None,
+ **kwargs: Any,
) -> None:
"""Sets an outgoing cookie name/value with the given options.
to set an expiration time in days from today (if both are set, ``expires``
is used).
+ .. deprecated:: 6.3
+ Keyword arguments are currently accepted case-insensitively.
+ In Tornado 7.0 this will be changed to only accept lowercase
+ arguments.
"""
# The cookie library only accepts type str, in both python 2 and 3
name = escape.native_str(name)
morsel["secure"] = True
if samesite:
morsel["samesite"] = samesite
+ if kwargs:
+ # The setitem interface is case-insensitive, so continue to support
+ # kwargs for backwards compatibility until we can remove deprecated
+ # features.
+ for k, v in kwargs.items():
+ morsel[k] = v
+ warnings.warn(
+ f"Deprecated arguments to set_cookie: {set(kwargs.keys())} "
+ "(should be lowercase)",
+ DeprecationWarning,
+ )
def clear_cookie(self, name: str, **kwargs: Any) -> None:
"""Deletes the cookie with the given name.