Skip to content

Commit 2453344

Browse files
authoredApr 21, 2023
Merge pull request #3254 from bdarnell/fix-set-cookie-case
web: Restore case-insensitivity of set_cookie args
2 parents 298dc39 + d3e8181 commit 2453344

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed
 

‎docs/releases.rst

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Release notes
44
.. toctree::
55
:maxdepth: 2
66

7+
releases/v6.3.1
78
releases/v6.3.0
89
releases/v6.2.0
910
releases/v6.1.0

‎docs/releases/v6.3.1.rst

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
What's new in Tornado 6.3.1
2+
===========================
3+
4+
Apr 21, 2023
5+
------------
6+
7+
``tornado.web``
8+
~~~~~~~~~~~~~~~
9+
10+
- `.RequestHandler.set_cookie` once again accepts capitalized keyword arguments
11+
for backwards compatibility. This is deprecated and in Tornado 7.0 only lowercase
12+
arguments will be accepted.

‎tornado/test/web_test.py

+13
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from tornado.simple_httpclient import SimpleAsyncHTTPClient
1818
from tornado.template import DictLoader
1919
from tornado.testing import AsyncHTTPTestCase, AsyncTestCase, ExpectLog, gen_test
20+
from tornado.test.util import ignore_deprecation
2021
from tornado.util import ObjectDict, unicode_type
2122
from tornado.web import (
2223
Application,
@@ -318,6 +319,11 @@ def get(self):
318319
self.set_cookie("c", "1", httponly=True)
319320
self.set_cookie("d", "1", httponly=False)
320321

322+
class SetCookieDeprecatedArgs(RequestHandler):
323+
def get(self):
324+
# Mixed case is supported, but deprecated
325+
self.set_cookie("a", "b", HttpOnly=True, pATH="/foo")
326+
321327
return [
322328
("/set", SetCookieHandler),
323329
("/get", GetCookieHandler),
@@ -327,6 +333,7 @@ def get(self):
327333
("/set_max_age", SetCookieMaxAgeHandler),
328334
("/set_expires_days", SetCookieExpiresDaysHandler),
329335
("/set_falsy_flags", SetCookieFalsyFlags),
336+
("/set_deprecated", SetCookieDeprecatedArgs),
330337
]
331338

332339
def test_set_cookie(self):
@@ -413,6 +420,12 @@ def test_set_cookie_false_flags(self):
413420
self.assertEqual(headers[2].lower(), "c=1; httponly; path=/")
414421
self.assertEqual(headers[3].lower(), "d=1; path=/")
415422

423+
def test_set_cookie_deprecated(self):
424+
with ignore_deprecation():
425+
response = self.fetch("/set_deprecated")
426+
header = response.headers.get("Set-Cookie")
427+
self.assertEqual(header, "a=b; HttpOnly; Path=/foo")
428+
416429

417430
class AuthRedirectRequestHandler(RequestHandler):
418431
def initialize(self, login_url):

‎tornado/web.py

+17
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ async def main():
7979
import sys
8080
import threading
8181
import time
82+
import warnings
8283
import tornado
8384
import traceback
8485
import types
@@ -607,6 +608,7 @@ def set_cookie(
607608
httponly: bool = False,
608609
secure: bool = False,
609610
samesite: Optional[str] = None,
611+
**kwargs: Any,
610612
) -> None:
611613
"""Sets an outgoing cookie name/value with the given options.
612614
@@ -623,6 +625,10 @@ def set_cookie(
623625
to set an expiration time in days from today (if both are set, ``expires``
624626
is used).
625627
628+
.. deprecated:: 6.3
629+
Keyword arguments are currently accepted case-insensitively.
630+
In Tornado 7.0 this will be changed to only accept lowercase
631+
arguments.
626632
"""
627633
# The cookie library only accepts type str, in both python 2 and 3
628634
name = escape.native_str(name)
@@ -657,6 +663,17 @@ def set_cookie(
657663
morsel["secure"] = True
658664
if samesite:
659665
morsel["samesite"] = samesite
666+
if kwargs:
667+
# The setitem interface is case-insensitive, so continue to support
668+
# kwargs for backwards compatibility until we can remove deprecated
669+
# features.
670+
for k, v in kwargs.items():
671+
morsel[k] = v
672+
warnings.warn(
673+
f"Deprecated arguments to set_cookie: {set(kwargs.keys())} "
674+
"(should be lowercase)",
675+
DeprecationWarning,
676+
)
660677

661678
def clear_cookie(self, name: str, **kwargs: Any) -> None:
662679
"""Deletes the cookie with the given name.

0 commit comments

Comments
 (0)