1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
import json
import time
from http import cookies
from typing import Any, Optional
from aiohttp import web
from aiohttp.test_utils import TestClient
from aiohttp.typedefs import Handler
from aiohttp_session import SimpleCookieStorage, get_session, session_middleware
from .typedefs import AiohttpClient
def make_cookie(
client: TestClient,
data: Any,
path: Optional[str] = None,
domain: Optional[str] = None,
) -> None:
session_data = {"session": data, "created": int(time.time())}
C = cookies.SimpleCookie()
value = json.dumps(session_data)
C["AIOHTTP_SESSION"] = value
C["AIOHTTP_SESSION"]["path"] = path
C["AIOHTTP_SESSION"]["domain"] = domain
client.session.cookie_jar.update_cookies(C)
def create_app(
handler: Handler, path: Optional[str] = None, domain: Optional[str] = None
) -> web.Application:
storage = SimpleCookieStorage(max_age=10, path="/anotherpath", domain="127.0.0.1")
middleware = session_middleware(storage)
app = web.Application(middlewares=[middleware])
app.router.add_route("GET", "/", handler)
app.router.add_route("GET", "/anotherpath", handler)
return app
async def test_with_same_path_domain(aiohttp_client: AiohttpClient) -> None:
async def handler(request: web.Request) -> web.StreamResponse:
session = await get_session(request)
session["c"] = 3
return web.Response(body=b"OK")
client = await aiohttp_client(create_app(handler))
make_cookie(client, {"a": 1, "b": 2}, path="/anotherpath", domain="127.0.0.1")
resp = await client.get("/anotherpath")
assert resp.status == 200
morsel = resp.cookies["AIOHTTP_SESSION"]
cookie_data = json.loads(morsel.value)
assert "session" in cookie_data
assert "a" in cookie_data["session"]
assert "b" in cookie_data["session"]
assert "c" in cookie_data["session"]
assert "created" in cookie_data
assert cookie_data["session"]["a"] == 1
assert cookie_data["session"]["b"] == 2
assert cookie_data["session"]["c"] == 3
assert morsel["httponly"]
assert "/anotherpath" == morsel["path"]
assert "127.0.0.1" == morsel["domain"]
async def test_with_different_path(aiohttp_client: AiohttpClient) -> None:
async def handler(request: web.Request) -> web.StreamResponse:
session = await get_session(request)
session["c"] = 3
return web.Response(body=b"OK")
client = await aiohttp_client(create_app(handler))
make_cookie(client, {"a": 1, "b": 2}, path="/NotTheSame", domain="127.0.0.1")
resp = await client.get("/anotherpath")
assert resp.status == 200
morsel = resp.cookies["AIOHTTP_SESSION"]
cookie_data = json.loads(morsel.value)
assert "session" in cookie_data
assert "a" not in cookie_data["session"]
assert "b" not in cookie_data["session"]
assert "c" in cookie_data["session"]
assert "created" in cookie_data
assert cookie_data["session"]["c"] == 3
assert morsel["httponly"]
assert "/anotherpath" == morsel["path"]
assert "127.0.0.1" == morsel["domain"]
async def test_with_different_domain(aiohttp_client: AiohttpClient) -> None:
async def handler(request: web.Request) -> web.StreamResponse:
session = await get_session(request)
session["c"] = 3
return web.Response(body=b"OK")
client = await aiohttp_client(create_app(handler))
make_cookie(client, {"a": 1, "b": 2}, path="/anotherpath", domain="localhost")
resp = await client.get("/anotherpath")
assert resp.status == 200
morsel = resp.cookies["AIOHTTP_SESSION"]
cookie_data = json.loads(morsel.value)
assert "session" in cookie_data
assert "a" not in cookie_data["session"]
assert "b" not in cookie_data["session"]
assert "c" in cookie_data["session"]
assert "created" in cookie_data
assert cookie_data["session"]["c"] == 3
assert morsel["httponly"]
assert "/anotherpath" == morsel["path"]
assert "127.0.0.1" == morsel["domain"]
async def test_invalidate_with_same_path_domain(aiohttp_client: AiohttpClient) -> None:
async def handler(request: web.Request) -> web.StreamResponse:
session = await get_session(request)
session.invalidate()
return web.Response(body=b"OK")
client = await aiohttp_client(create_app(handler))
make_cookie(client, {"a": 1, "b": 2}, path="/anotherpath", domain="127.0.0.1")
resp = await client.get("/anotherpath")
assert resp.status == 200
morsel = resp.cookies["AIOHTTP_SESSION"]
cookie_data = json.loads(morsel.value)
assert {} == cookie_data
assert morsel["httponly"]
assert "/anotherpath" == morsel["path"]
assert "127.0.0.1" == morsel["domain"]
|