Skip to content

Commit

Permalink
tests: use time-machine on test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
Kludex committed Feb 4, 2023
1 parent d6b28a7 commit d5199ac
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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
Expand Down
2 changes: 1 addition & 1 deletion starlette/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def set_cookie(
key: str,
value: str = "",
max_age: typing.Optional[int] = None,
expires: typing.Optional[typing.Union[datetime, int]] = None,
expires: typing.Optional[typing.Union[datetime, str, int]] = None,
path: str = "/",
domain: typing.Optional[str] = None,
secure: bool = False,
Expand Down
26 changes: 16 additions & 10 deletions tests/test_responses.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import time
from datetime import datetime, timedelta, timezone
from email.utils import formatdate
from email.utils import format_datetime
from http.cookies import SimpleCookie

import anyio
Expand Down Expand Up @@ -292,8 +291,8 @@ 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):
set_cookie_expires = formatdate(time.time() + 10, usegmt=True)
def test_set_cookie(test_client_factory, time_machine):
time_machine.move_to(datetime(2100, 1, 22, 12, 0, 0))

async def app(scope, receive, send):
response = Response("Hello, world!", media_type="text/plain")
Expand All @@ -315,26 +314,33 @@ async def app(scope, receive, send):
assert response.text == "Hello, world!"
assert (
response.headers["set-cookie"]
== f"mycookie=myvalue; Domain=localhost; expires={set_cookie_expires}; "
== "mycookie=myvalue; Domain=localhost; expires=Fri, 22 Jan 2100 12:00:10 GMT; "
"HttpOnly; Max-Age=10; Path=/; SameSite=none; Secure"
)


@pytest.mark.parametrize(
"expires", [datetime.now(timezone.utc) + timedelta(seconds=10), 10]
"expires_factory",
[
lambda: datetime.now(timezone.utc) + timedelta(seconds=10),
lambda: format_datetime(
datetime.now(timezone.utc) + timedelta(seconds=10), usegmt=True
),
lambda: 10,
],
)
def test_expires_on_set_cookie(test_client_factory, expires):
set_cookie_expires = formatdate(time.time() + 10, usegmt=True)
def test_expires_on_set_cookie(test_client_factory, expires_factory, time_machine):
time_machine.move_to(datetime(2100, 1, 22, 12, 0, 0))

async def app(scope, receive, send):
response = Response("Hello, world!", media_type="text/plain")
response.set_cookie("mycookie", "myvalue", expires=expires)
response.set_cookie("mycookie", "myvalue", expires=expires_factory())
await response(scope, receive, send)

client = test_client_factory(app)
response = client.get("/")
cookie: SimpleCookie = SimpleCookie(response.headers.get("set-cookie"))
assert cookie["mycookie"]["expires"] == set_cookie_expires
assert cookie["mycookie"]["expires"] == "Fri, 22 Jan 2100 12:00:10 GMT"


def test_delete_cookie(test_client_factory):
Expand Down

0 comments on commit d5199ac

Please sign in to comment.