Skip to content

Commit

Permalink
Merge pull request #61 from alex-oleshkevich/timedelta-support
Browse files Browse the repository at this point in the history
allow datetime.timedelta as lifetime value
  • Loading branch information
alex-oleshkevich authored Apr 27, 2023
2 parents 564314c + 8534b6a commit aa8d432
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
12 changes: 6 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ default_stages: [ commit ]

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
rev: v4.4.0
hooks:
- id: check-yaml
- id: check-case-conflict
Expand All @@ -19,23 +19,23 @@ repos:
- id: check-yaml

- repo: https://github.com/pycqa/isort
rev: 5.10.1
rev: 5.12.0
hooks:
- id: isort

- repo: https://github.com/psf/black.git
rev: 22.10.0
rev: 23.3.0
hooks:
- id: black
args: [ --config=pyproject.toml ]

- repo: https://github.com/pycqa/flake8
rev: 5.0.4
rev: 6.0.0
hooks:
- id: flake8

- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v0.990'
rev: 'v1.2.0'
hooks:
- id: mypy
additional_dependencies:
Expand All @@ -46,7 +46,7 @@ repos:
- fastapi

- repo: https://github.com/myint/docformatter.git
rev: v1.5.0
rev: v1.6.4
hooks:
- id: docformatter
args: [ '--in-place', '--wrap-summaries=120', '--wrap-descriptions=120', '--pre-summary-newline' ]
4 changes: 3 additions & 1 deletion starsessions/middleware.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import re
import typing
from starlette.datastructures import MutableHeaders
Expand Down Expand Up @@ -34,7 +35,7 @@ def __init__(
self,
app: ASGIApp,
store: SessionStore,
lifetime: int = 0, # session-only
lifetime: typing.Union[int, datetime.timedelta] = 0, # session-only
rolling: bool = False,
cookie_name: str = "session",
cookie_same_site: str = "lax",
Expand All @@ -43,6 +44,7 @@ def __init__(
cookie_path: typing.Optional[str] = None,
serializer: typing.Optional[Serializer] = None,
) -> None:
lifetime = int(lifetime.total_seconds() if isinstance(lifetime, datetime.timedelta) else lifetime)
assert lifetime >= 0, "Session lifetime cannot be less than zero seconds."

self.app = app
Expand Down
11 changes: 11 additions & 0 deletions tests/test_middleware.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import pytest
from starlette.requests import HTTPConnection
from starlette.responses import JSONResponse, Response
Expand Down Expand Up @@ -234,3 +235,13 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None:
client = TestClient(app)
response = client.get("/")
assert "max-age" not in response.headers["set-cookie"].lower()


def test_session_timedelta_lifetime(store: SessionStore) -> None:
"""It should accept datetime.timedelta as lifetime value."""

async def app(scope: Scope, receive: Receive, send: Send) -> None:
pass

app = SessionMiddleware(app, store=store, lifetime=datetime.timedelta(seconds=60))
assert app.lifetime == 60

0 comments on commit aa8d432

Please sign in to comment.