File: test_response_to_fastapi.py

package info (click to toggle)
python-cross-web 0.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 744 kB
  • sloc: python: 2,262; sh: 23; makefile: 10
file content (124 lines) | stat: -rw-r--r-- 3,680 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
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
import pytest
from unittest.mock import Mock, patch
from cross_web.response import Cookie, Response


@pytest.mark.asyncio
async def test_to_fastapi_with_all_cookie_params() -> None:
    """Test to_fastapi with cookies having all parameters set"""
    cookies = [
        Cookie(
            name="session",
            value="abc123",
            secure=True,
            path="/api",
            domain="example.com",
            max_age=3600,
            httponly=True,
            samesite="strict",
        ),
        Cookie(
            name="prefs",
            value="dark_mode",
            secure=False,
            path=None,
            domain=None,
            max_age=None,
            httponly=False,
            samesite="lax",
        ),
    ]

    response = Response(
        status_code=200, body="Success", headers={"X-Custom": "Header"}, cookies=cookies
    )

    # Mock FastAPI Response
    with patch("fastapi.Response") as MockFastAPIResponse:
        mock_fastapi_response = Mock()
        MockFastAPIResponse.return_value = mock_fastapi_response

        result = response.to_fastapi()

        # Verify FastAPI Response was created with correct params
        MockFastAPIResponse.assert_called_once_with(
            status_code=200, headers={"X-Custom": "Header"}, content="Success"
        )

        # Verify set_cookie was called for each cookie with all params
        assert mock_fastapi_response.set_cookie.call_count == 2

        # First cookie call
        mock_fastapi_response.set_cookie.assert_any_call(
            "session",
            "abc123",
            secure=True,
            path="/api",
            domain="example.com",
            max_age=3600,
            httponly=True,
            samesite="strict",
        )

        # Second cookie call
        mock_fastapi_response.set_cookie.assert_any_call(
            "prefs",
            "dark_mode",
            secure=False,
            path=None,
            domain=None,
            max_age=None,
            httponly=False,
            samesite="lax",
        )

        assert result is mock_fastapi_response


@pytest.mark.asyncio
async def test_to_fastapi_no_cookies() -> None:
    """Test to_fastapi when cookies is None"""
    response = Response(
        status_code=404,
        body="Not Found",
        headers={"Content-Type": "text/plain"},
        cookies=None,
    )

    with patch("fastapi.Response") as MockFastAPIResponse:
        mock_fastapi_response = Mock()
        MockFastAPIResponse.return_value = mock_fastapi_response

        result = response.to_fastapi()

        # Verify FastAPI Response was created
        MockFastAPIResponse.assert_called_once_with(
            status_code=404, headers={"Content-Type": "text/plain"}, content="Not Found"
        )

        # Verify set_cookie was never called
        mock_fastapi_response.set_cookie.assert_not_called()

        assert result is mock_fastapi_response


@pytest.mark.asyncio
async def test_to_fastapi_empty_cookies_list() -> None:
    """Test to_fastapi when cookies is empty list"""
    response = Response(status_code=204, body=None, headers=None, cookies=[])

    with patch("fastapi.Response") as MockFastAPIResponse:
        mock_fastapi_response = Mock()
        MockFastAPIResponse.return_value = mock_fastapi_response

        result = response.to_fastapi()

        # Verify FastAPI Response was created
        MockFastAPIResponse.assert_called_once_with(
            status_code=204, headers=None, content=None
        )

        # Verify set_cookie was never called
        mock_fastapi_response.set_cookie.assert_not_called()

        assert result is mock_fastapi_response