File: test_cookie.py

package info (click to toggle)
litestar 2.21.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 12,568 kB
  • sloc: python: 70,588; makefile: 254; javascript: 104; sh: 60
file content (49 lines) | stat: -rw-r--r-- 1,630 bytes parent folder | download
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
from datetime import datetime, timedelta

from time_machine import travel

from litestar.datastructures import Cookie


def test_basic_cookie_as_header() -> None:
    cookie = Cookie(key="key")
    assert cookie.to_header() == 'Set-Cookie: key=""; Path=/; SameSite=lax'


@travel(datetime.utcnow, tick=False)
def test_cookie_as_header() -> None:
    expires_sec = 123
    cookie = Cookie(
        key="key",
        value="value",
        path="/path",
        expires=expires_sec,
        domain="domain.com",
        secure=True,
        httponly=True,
        samesite="strict",
    )

    now = datetime.utcnow()
    expected_expired = (now + timedelta(seconds=expires_sec)).strftime("%a, %d %b %Y %H:%M:%S GMT")
    assert cookie.to_header() == (
        f"Set-Cookie: key=value; Domain=domain.com; expires={expected_expired}; HttpOnly; Path=/path; SameSite=strict; Secure"
    )


def test_cookie_with_max_age_as_header() -> None:
    cookie = Cookie(key="key", max_age=10)
    assert cookie.to_header() == 'Set-Cookie: key=""; Max-Age=10; Path=/; SameSite=lax'


def test_cookie_as_header_without_header_name() -> None:
    cookie = Cookie(key="key")
    assert cookie.to_header(header="") == 'key=""; Path=/; SameSite=lax'


def test_equality() -> None:
    assert Cookie(key="key") == Cookie(key="key")
    assert Cookie(key="key") != Cookie(key="key", path="/test")
    assert Cookie(key="key", path="/test") != Cookie(key="key", path="/test", domain="localhost")
    assert Cookie(key="key", path="/test", domain="localhost") == Cookie(key="key", path="/test", domain="localhost")
    assert Cookie(key="key") != "key"