-
-
Notifications
You must be signed in to change notification settings - Fork 933
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support str
and datetime
on expires
parameter on the set_cookie
method
#1908
Conversation
… expires was string or returns datetime in cookie format
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One test is enough. Make sure to pass a datetime
, and assert the cookie has the format specified.
…om delete cookie. Removes a cookie test
The build is failing on some database test. The tests pass locally. I don't think it is related to the changes I made, but if it is, please let me know, and I can try to figure out what is going on to fix it. I saw another PR that has the same database error. |
The tests are passing now :) |
Cool. Thank you. I just have one doubt here... Why does being an |
Let me investigate that this morning |
Yes, My guess is that at some point browser programmers thought that |
Is there anything else to do? |
Actually, I've found out why we accept
Just some references. |
f816a0a
to
0433643
Compare
datetime
on expires
parameter on the set_cookie
method
a72d8ba
to
d5199ac
Compare
If no one has anything against, I'm going to merge this when the release is ready. |
Looks fine to me. |
datetime
on expires
parameter on the set_cookie
methodstr
and datetime
on expires
parameter on the set_cookie
method
@florimondmanca I tried on this commit 6db2870 But it was flaky... Do you mean something different? |
@Kludex I played with it locally. I feel like pytest's Diff: diff --git a/requirements.txt b/requirements.txt
index 500a9b5..15aa541 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -9,7 +9,6 @@ flake8==3.9.2
importlib-metadata==4.13.0
isort==5.10.1
mypy==0.991
-time-machine==2.9.0
typing_extensions==4.4.0
types-contextvars==2.4.7
types-PyYAML==6.0.12.3
diff --git a/tests/test_responses.py b/tests/test_responses.py
index bf54772..aa2f5c4 100644
--- a/tests/test_responses.py
+++ b/tests/test_responses.py
@@ -1,6 +1,6 @@
import os
-from datetime import datetime, timedelta, timezone
-from email.utils import format_datetime
+import datetime as dt
+import time
from http.cookies import SimpleCookie
import anyio
@@ -291,8 +291,10 @@ def test_file_response_with_inline_disposition(tmpdir, test_client_factory):
assert response.headers["content-disposition"] == expected_disposition
-def test_set_cookie(test_client_factory, time_machine):
- time_machine.move_to(datetime(2100, 1, 22, 12, 0, 0))
+def test_set_cookie(test_client_factory, monkeypatch):
+ # Mock time used as a reference for `Expires` by stdlib `SimpleCookie`.
+ mocked_now = dt.datetime(2100, 1, 22, 12, 0, 0, tzinfo=dt.timezone.utc)
+ monkeypatch.setattr(time, "time", lambda: mocked_now.timestamp())
async def app(scope, receive, send):
response = Response("Hello, world!", media_type="text/plain")
@@ -320,21 +322,23 @@ def test_set_cookie(test_client_factory, time_machine):
@pytest.mark.parametrize(
- "expires_factory",
+ "expires",
[
- lambda: datetime.now(timezone.utc) + timedelta(seconds=10),
- lambda: format_datetime(
- datetime.now(timezone.utc) + timedelta(seconds=10), usegmt=True
+ pytest.param(
+ dt.datetime(2100, 1, 22, 12, 0, 10, tzinfo=dt.timezone.utc), id="datetime"
),
- lambda: 10,
+ pytest.param("Fri, 22 Jan 2100 12:00:10 GMT", id="str"),
+ pytest.param(10, id="int"),
],
)
-def test_expires_on_set_cookie(test_client_factory, expires_factory, time_machine):
- time_machine.move_to(datetime(2100, 1, 22, 12, 0, 0))
+def test_expires_on_set_cookie(test_client_factory, monkeypatch, expires):
+ # Mock time used as a reference for `Expires` by stdlib `SimpleCookie`.
+ mocked_now = dt.datetime(2100, 1, 22, 12, 0, 0, tzinfo=dt.timezone.utc)
+ monkeypatch.setattr(time, "time", lambda: mocked_now.timestamp())
async def app(scope, receive, send):
response = Response("Hello, world!", media_type="text/plain")
- response.set_cookie("mycookie", "myvalue", expires=expires_factory())
+ response.set_cookie("mycookie", "myvalue", expires=expires)
await response(scope, receive, send)
client = test_client_factory(app) |
I've applied this. Thanks! |
…e` method (#1908) Co-authored-by: Hugo Estrada <hugoestrada@cal.berkeley.edu> Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com> Co-authored-by: Florimond Manca <florimond.manca@protonmail.com>
What?
The PR implements the type changes suggested in the discussion. We are adding
typing.Optional[typing.Union[datetime, str]
to set cookie.Why?
Expiration was previously set as an integer. Reviewing https://www.rfc-editor.org/rfc/rfc6265 and https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie doesn't show that an integer is the right type for a cookie date.
How?
Following the instructions given in the discussion.
Notes
This is my first PR to this project, as part of Hacktober fest :)
Edit by @Kludex :
Missing