File: test_chalice.py

package info (click to toggle)
python-lia 0.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 744 kB
  • sloc: python: 2,262; sh: 23; makefile: 10
file content (166 lines) | stat: -rw-r--r-- 4,959 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import pytest

from lia import ChaliceHTTPRequestAdapter

pytestmark = [pytest.mark.chalice]


def test_chalice_adapter() -> None:
    from chalice.app import Request

    # Create a Chalice request
    event = {
        "headers": {"Content-Type": "application/json"},
        "multiValueQueryStringParameters": {"query": ["test"]},
        "pathParameters": None,
        "stageVariables": None,
        "body": "dGVzdCBib2R5",  # base64 encoded "test body"
        "isBase64Encoded": True,
        "requestContext": {
            "httpMethod": "POST",
            "stage": "dev",
            "domainName": "api.example.com",
            "path": "/graphql",
            "resourcePath": "/graphql",
        },
    }

    request = Request(event)

    adapter = ChaliceHTTPRequestAdapter(request)

    assert adapter.query_params == {"query": "test"}
    assert adapter.body == b"test body"
    assert adapter.method == "POST"
    assert adapter.headers["Content-Type"] == "application/json"
    assert adapter.content_type == "application/json"
    assert adapter.url == "https://api.example.com/dev/graphql?query=test"

    # Test cookie parsing
    event["headers"]["Cookie"] = "session=123; user=john"  # type: ignore[index]
    request = Request(event)
    adapter = ChaliceHTTPRequestAdapter(request)
    assert adapter.cookies == {"session": "123", "user": "john"}

    # Test NotImplementedError for form data
    with pytest.raises(NotImplementedError):
        adapter.post_data

    with pytest.raises(NotImplementedError):
        adapter.files

    with pytest.raises(NotImplementedError):
        adapter.get_form_data()


def test_chalice_adapter_non_base64() -> None:
    from chalice.app import Request

    # Create a Chalice request with non-base64 body
    event = {
        "headers": {"Content-Type": "application/json"},
        "multiValueQueryStringParameters": {"query": ["test"]},
        "pathParameters": None,
        "stageVariables": None,
        "body": '{"key": "value"}',
        "isBase64Encoded": False,
        "requestContext": {
            "httpMethod": "POST",
            "stage": "dev",
            "domainName": "api.example.com",
            "path": "/graphql",
            "resourcePath": "/graphql",
        },
    }

    request = Request(event)

    adapter = ChaliceHTTPRequestAdapter(request)

    assert adapter.query_params == {"query": "test"}
    assert adapter.body == b'{"key": "value"}'
    assert adapter.method == "POST"
    assert adapter.headers["Content-Type"] == "application/json"
    assert adapter.content_type == "application/json"
    assert adapter.url == "https://api.example.com/dev/graphql?query=test"


def test_chalice_adapter_prod_stage() -> None:
    """Test Chalice adapter with prod stage - line 62"""
    from chalice.app import Request

    event = {
        "headers": {},
        "multiValueQueryStringParameters": {},
        "pathParameters": None,
        "stageVariables": None,
        "body": "",
        "isBase64Encoded": False,
        "requestContext": {
            "httpMethod": "GET",
            "stage": "prod",  # This triggers line 62
            "domainName": "api.example.com",
            "path": "/test",
            "resourcePath": "/test",
        },
    }

    request = Request(event)
    adapter = ChaliceHTTPRequestAdapter(request)

    # URL should not include /prod
    assert adapter.url == "https://api.example.com/test"


def test_chalice_adapter_no_stage() -> None:
    """Test Chalice adapter with no stage - line 62"""
    from chalice.app import Request

    event = {
        "headers": {},
        "multiValueQueryStringParameters": {},
        "pathParameters": None,
        "stageVariables": None,
        "body": "",
        "isBase64Encoded": False,
        "requestContext": {
            "httpMethod": "GET",
            "stage": "",  # Empty stage also triggers line 62
            "domainName": "api.example.com",
            "path": "/test",
            "resourcePath": "/test",
        },
    }

    request = Request(event)
    adapter = ChaliceHTTPRequestAdapter(request)

    # URL should not include stage
    assert adapter.url == "https://api.example.com/test"


def test_chalice_adapter_no_cookies() -> None:
    """Test Chalice adapter with no cookies - line 79"""
    from chalice.app import Request

    event = {
        "headers": {},  # No Cookie header
        "multiValueQueryStringParameters": {},
        "pathParameters": None,
        "stageVariables": None,
        "body": "",
        "isBase64Encoded": False,
        "requestContext": {
            "httpMethod": "GET",
            "stage": "dev",
            "domainName": "api.example.com",
            "path": "/test",
            "resourcePath": "/test",
        },
    }

    request = Request(event)
    adapter = ChaliceHTTPRequestAdapter(request)

    # Should return empty dict when no Cookie header - line 79
    assert adapter.cookies == {}