Skip to content

Commit

Permalink
Added type annotations to test_session.py (encode#2466)
Browse files Browse the repository at this point in the history
Co-authored-by: Scirlat Danut <scirlatdanut@scirlats-mini.lan>
  • Loading branch information
2 people authored and Rocky Allen committed Mar 18, 2024
1 parent 29a6af0 commit 40bccc0
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions tests/middleware/test_session.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
import re
from typing import Callable

from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.middleware.sessions import SessionMiddleware
from starlette.requests import Request
from starlette.responses import JSONResponse
from starlette.routing import Mount, Route
from starlette.testclient import TestClient

TestClientFactory = Callable[..., TestClient]

def view_session(request):

def view_session(request: Request) -> JSONResponse:
return JSONResponse({"session": request.session})


async def update_session(request):
async def update_session(request: Request) -> JSONResponse:
data = await request.json()
request.session.update(data)
return JSONResponse({"session": request.session})


async def clear_session(request):
async def clear_session(request: Request) -> JSONResponse:
request.session.clear()
return JSONResponse({"session": request.session})


def test_session(test_client_factory):
def test_session(test_client_factory: TestClientFactory) -> None:
app = Starlette(
routes=[
Route("/view_session", endpoint=view_session),
Expand Down Expand Up @@ -56,7 +60,7 @@ def test_session(test_client_factory):
assert response.json() == {"session": {}}


def test_session_expires(test_client_factory):
def test_session_expires(test_client_factory: TestClientFactory) -> None:
app = Starlette(
routes=[
Route("/view_session", endpoint=view_session),
Expand All @@ -80,7 +84,7 @@ def test_session_expires(test_client_factory):
assert response.json() == {"session": {}}


def test_secure_session(test_client_factory):
def test_secure_session(test_client_factory: TestClientFactory) -> None:
app = Starlette(
routes=[
Route("/view_session", endpoint=view_session),
Expand Down Expand Up @@ -119,7 +123,7 @@ def test_secure_session(test_client_factory):
assert response.json() == {"session": {}}


def test_session_cookie_subpath(test_client_factory):
def test_session_cookie_subpath(test_client_factory: TestClientFactory) -> None:
second_app = Starlette(
routes=[
Route("/update_session", endpoint=update_session, methods=["POST"]),
Expand All @@ -139,7 +143,7 @@ def test_session_cookie_subpath(test_client_factory):
assert cookie_path == "/second_app"


def test_invalid_session_cookie(test_client_factory):
def test_invalid_session_cookie(test_client_factory: TestClientFactory) -> None:
app = Starlette(
routes=[
Route("/view_session", endpoint=view_session),
Expand All @@ -158,7 +162,7 @@ def test_invalid_session_cookie(test_client_factory):
assert response.json() == {"session": {}}


def test_session_cookie(test_client_factory):
def test_session_cookie(test_client_factory: TestClientFactory) -> None:
app = Starlette(
routes=[
Route("/view_session", endpoint=view_session),
Expand All @@ -180,7 +184,7 @@ def test_session_cookie(test_client_factory):
assert response.json() == {"session": {}}


def test_domain_cookie(test_client_factory):
def test_domain_cookie(test_client_factory: TestClientFactory) -> None:
app = Starlette(
routes=[
Route("/view_session", endpoint=view_session),
Expand Down

0 comments on commit 40bccc0

Please sign in to comment.