-
Notifications
You must be signed in to change notification settings - Fork 421
/
Copy pathtest_header_serializer.py
222 lines (179 loc) · 7.59 KB
/
test_header_serializer.py
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
from uuid import uuid4
import pytest
from requests import Request
from aws_lambda_powertools.shared.cookies import Cookie
from tests.e2e.utils import data_fetcher
@pytest.fixture
def alb_basic_listener_endpoint(infrastructure: dict) -> str:
dns_name = infrastructure.get("ALBDnsName")
port = infrastructure.get("ALBBasicListenerPort", "")
return f"http://{dns_name}:{port}"
@pytest.fixture
def alb_multi_value_header_listener_endpoint(infrastructure: dict) -> str:
dns_name = infrastructure.get("ALBDnsName")
port = infrastructure.get("ALBMultiValueHeaderListenerPort", "")
return f"http://{dns_name}:{port}"
@pytest.fixture
def apigw_rest_endpoint(infrastructure: dict) -> str:
return infrastructure.get("APIGatewayRestUrl", "")
@pytest.fixture
def apigw_http_endpoint(infrastructure: dict) -> str:
return infrastructure.get("APIGatewayHTTPUrl", "")
@pytest.fixture
def lambda_function_url_endpoint(infrastructure: dict) -> str:
return infrastructure.get("LambdaFunctionUrl", "")
@pytest.mark.xdist_group(name="event_handler")
def test_alb_headers_serializer(alb_basic_listener_endpoint):
# GIVEN
url = f"{alb_basic_listener_endpoint}/todos"
body = "Hello World"
status_code = 200
headers = {"Content-Type": "text/plain", "Vary": ["Accept-Encoding", "User-Agent"]}
cookies = [
Cookie(name="session_id", value=str(uuid4()), secure=True, http_only=True),
Cookie(name="ab_experiment", value="3"),
]
last_cookie = cookies[-1]
# WHEN
response = data_fetcher.get_http_response(
Request(
method="POST",
url=url,
json={"body": body, "status_code": status_code, "headers": headers, "cookies": list(map(str, cookies))},
)
)
# THEN
assert response.status_code == status_code
# response.content is a binary string, needs to be decoded to compare with the real string
assert response.content.decode("ascii") == body
# Only the last header should be set
for key, value in headers.items():
assert key in response.headers
value = value if isinstance(value, str) else sorted(value)[-1]
assert response.headers[key] == value
# Only the last cookie should be set
assert len(response.cookies.items()) == 1
assert last_cookie.name in response.cookies
assert response.cookies.get(last_cookie.name) == last_cookie.value
@pytest.mark.xdist_group(name="event_handler")
def test_alb_multi_value_headers_serializer(alb_multi_value_header_listener_endpoint):
# GIVEN
url = f"{alb_multi_value_header_listener_endpoint}/todos"
body = "Hello World"
status_code = 200
headers = {"Content-Type": "text/plain", "Vary": ["Accept-Encoding", "User-Agent"]}
cookies = [
Cookie(name="session_id", value=str(uuid4()), secure=True, http_only=True),
Cookie(name="ab_experiment", value="3"),
]
# WHEN
response = data_fetcher.get_http_response(
Request(
method="POST",
url=url,
json={"body": body, "status_code": status_code, "headers": headers, "cookies": list(map(str, cookies))},
)
)
# THEN
assert response.status_code == status_code
# response.content is a binary string, needs to be decoded to compare with the real string
assert response.content.decode("ascii") == body
for key, value in headers.items():
assert key in response.headers
value = value if isinstance(value, str) else ", ".join(sorted(value))
# ALB sorts the header values randomly, so we have to re-order them for comparison here
returned_value = ", ".join(sorted(response.headers[key].split(", ")))
assert returned_value == value
for cookie in cookies:
assert cookie.name in response.cookies
assert response.cookies.get(cookie.name) == cookie.value
@pytest.mark.xdist_group(name="event_handler")
def test_api_gateway_rest_headers_serializer(apigw_rest_endpoint):
# GIVEN
url = f"{apigw_rest_endpoint}todos"
body = "Hello World"
status_code = 200
headers = {"Content-Type": "text/plain", "Vary": ["Accept-Encoding", "User-Agent"]}
cookies = [
Cookie(name="session_id", value=str(uuid4()), secure=True, http_only=True),
Cookie(name="ab_experiment", value="3"),
]
# WHEN
response = data_fetcher.get_http_response(
Request(
method="POST",
url=url,
json={"body": body, "status_code": status_code, "headers": headers, "cookies": list(map(str, cookies))},
)
)
# THEN
assert response.status_code == status_code
# response.content is a binary string, needs to be decoded to compare with the real string
assert response.content.decode("ascii") == body
for key, value in headers.items():
assert key in response.headers
value = value if isinstance(value, str) else ", ".join(sorted(value))
assert response.headers[key] == value
for cookie in cookies:
assert cookie.name in response.cookies
assert response.cookies.get(cookie.name) == cookie.value
@pytest.mark.xdist_group(name="event_handler")
def test_api_gateway_http_headers_serializer(apigw_http_endpoint):
# GIVEN
url = f"{apigw_http_endpoint}todos"
body = "Hello World"
status_code = 200
headers = {"Content-Type": "text/plain", "Vary": ["Accept-Encoding", "User-Agent"]}
cookies = [
Cookie(name="session_id", value=str(uuid4()), secure=True, http_only=True),
Cookie(name="ab_experiment", value="3"),
]
# WHEN
response = data_fetcher.get_http_response(
Request(
method="POST",
url=url,
json={"body": body, "status_code": status_code, "headers": headers, "cookies": list(map(str, cookies))},
)
)
# THEN
assert response.status_code == status_code
# response.content is a binary string, needs to be decoded to compare with the real string
assert response.content.decode("ascii") == body
for key, value in headers.items():
assert key in response.headers
value = value if isinstance(value, str) else ", ".join(sorted(value))
assert response.headers[key] == value
for cookie in cookies:
assert cookie.name in response.cookies
assert response.cookies.get(cookie.name) == cookie.value
@pytest.mark.xdist_group(name="event_handler")
def test_lambda_function_url_headers_serializer(lambda_function_url_endpoint):
# GIVEN
url = f"{lambda_function_url_endpoint}todos" # the function url endpoint already has the trailing /
body = "Hello World"
status_code = 200
headers = {"Content-Type": "text/plain", "Vary": ["Accept-Encoding", "User-Agent"]}
cookies = [
Cookie(name="session_id", value=str(uuid4()), secure=True, http_only=True),
Cookie(name="ab_experiment", value="3"),
]
# WHEN
response = data_fetcher.get_http_response(
Request(
method="POST",
url=url,
json={"body": body, "status_code": status_code, "headers": headers, "cookies": list(map(str, cookies))},
)
)
# THEN
assert response.status_code == status_code
# response.content is a binary string, needs to be decoded to compare with the real string
assert response.content.decode("ascii") == body
for key, value in headers.items():
assert key in response.headers
value = value if isinstance(value, str) else ", ".join(sorted(value))
assert response.headers[key] == value
for cookie in cookies:
assert cookie.name in response.cookies
assert response.cookies.get(cookie.name) == cookie.value