Skip to content

Commit 818a4bc

Browse files
Merge pull request #2167 from VWS-Python/simplify-cid
Simplify tests by not going between CaseInsensitiveDict and dict
2 parents 34fb9ac + 9f9fa0f commit 818a4bc

File tree

6 files changed

+65
-96
lines changed

6 files changed

+65
-96
lines changed

tests/mock_vws/test_authorization_header.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import pytest
1414
import requests
1515
from mock_vws._constants import ResultCodes
16-
from requests.structures import CaseInsensitiveDict
1716
from vws import VWS, CloudRecoService
1817
from vws.exceptions import cloud_reco_exceptions
1918
from vws.exceptions.vws_exceptions import AuthenticationFailure, Fail
@@ -47,13 +46,12 @@ def test_missing(endpoint: Endpoint) -> None:
4746
is given.
4847
"""
4948
date = rfc_1123_date()
50-
headers: dict[str, str] = dict(endpoint.prepared_request.headers) | {
51-
"Date": date,
52-
}
49+
headers = endpoint.prepared_request.headers.copy()
50+
headers.update({"Date": date})
5351

5452
headers.pop("Authorization", None)
5553

56-
endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
54+
endpoint.prepared_request.headers = headers
5755
session = requests.Session()
5856
response = session.send(request=endpoint.prepared_request)
5957
handle_server_errors(response=response)
@@ -98,12 +96,10 @@ def test_one_part_no_space(endpoint: Endpoint) -> None:
9896
# string, but really any string which is not two parts when split on a
9997
# space will do.
10098
authorization_string = "VWS"
101-
headers: dict[str, str] = dict(endpoint.prepared_request.headers) | {
102-
"Authorization": authorization_string,
103-
"Date": date,
104-
}
99+
headers = endpoint.prepared_request.headers.copy()
100+
headers.update({"Authorization": authorization_string, "Date": date})
105101

106-
endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
102+
endpoint.prepared_request.headers = headers
107103
session = requests.Session()
108104
response = session.send(request=endpoint.prepared_request)
109105
handle_server_errors(response=response)
@@ -138,12 +134,10 @@ def test_one_part_with_space(endpoint: Endpoint) -> None:
138134
authorization_string = "VWS "
139135
date = rfc_1123_date()
140136

141-
headers: dict[str, str] = dict(endpoint.prepared_request.headers) | {
142-
"Authorization": authorization_string,
143-
"Date": date,
144-
}
137+
headers = endpoint.prepared_request.headers.copy()
138+
headers.update({"Authorization": authorization_string, "Date": date})
145139

146-
endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
140+
endpoint.prepared_request.headers = headers
147141
session = requests.Session()
148142
response = session.send(request=endpoint.prepared_request)
149143
handle_server_errors(response=response)
@@ -177,12 +171,10 @@ def test_missing_signature(endpoint: Endpoint) -> None:
177171
date = rfc_1123_date()
178172

179173
authorization_string = "VWS foobar:"
180-
headers: dict[str, str] = dict(endpoint.prepared_request.headers) | {
181-
"Authorization": authorization_string,
182-
"Date": date,
183-
}
174+
headers = endpoint.prepared_request.headers.copy()
175+
headers.update({"Authorization": authorization_string, "Date": date})
184176

185-
endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
177+
endpoint.prepared_request.headers = headers
186178
session = requests.Session()
187179
response = session.send(request=endpoint.prepared_request)
188180
handle_server_errors(response=response)

tests/mock_vws/test_content_length.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ def test_not_integer(endpoint: Endpoint) -> None:
4141
A ``BAD_REQUEST`` error is given when the given ``Content-Length`` is
4242
not an integer.
4343
"""
44-
endpoint_headers = dict(endpoint.prepared_request.headers)
45-
if not endpoint_headers.get("Content-Type"):
44+
headers = endpoint.prepared_request.headers.copy()
45+
if not headers.get("Content-Type"):
4646
return
4747

4848
content_length = "0.4"
49-
headers = endpoint_headers | {"Content-Length": content_length}
50-
endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
49+
headers.update({"Content-Length": content_length})
50+
endpoint.prepared_request.headers = headers
5151
session = requests.Session()
5252
response = session.send(request=endpoint.prepared_request)
5353
handle_server_errors(response=response)
@@ -94,16 +94,16 @@ def test_too_large(endpoint: Endpoint) -> None: # pragma: no cover
9494
"""
9595
An error is given if the given content length is too large.
9696
"""
97-
endpoint_headers = dict(endpoint.prepared_request.headers)
98-
if not endpoint_headers.get("Content-Type"):
97+
headers = endpoint.prepared_request.headers.copy()
98+
if not headers.get("Content-Type"):
9999
pytest.skip("No Content-Type header for this request")
100100

101101
url = str(endpoint.prepared_request.url)
102102
netloc = urlparse(url).netloc
103-
content_length = str(int(endpoint_headers["Content-Length"]) + 1)
104-
headers = endpoint_headers | {"Content-Length": content_length}
103+
content_length = str(int(headers["Content-Length"]) + 1)
104+
headers.update({"Content-Length": content_length})
105105

106-
endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
106+
endpoint.prepared_request.headers = headers
107107
session = requests.Session()
108108
response = session.send(request=endpoint.prepared_request)
109109
# We do not use ``handle_server_errors`` here because we do not want to
@@ -141,14 +141,14 @@ def test_too_small(endpoint: Endpoint) -> None:
141141
An ``UNAUTHORIZED`` response is given if the given content length is
142142
too small.
143143
"""
144-
endpoint_headers = dict(endpoint.prepared_request.headers)
145-
if not endpoint_headers.get("Content-Type"):
144+
headers = endpoint.prepared_request.headers.copy()
145+
if not headers.get("Content-Type"):
146146
return
147147

148-
content_length = str(int(endpoint_headers["Content-Length"]) - 1)
149-
headers = endpoint_headers | {"Content-Length": content_length}
148+
content_length = str(int(headers["Content-Length"]) - 1)
149+
headers.update({"Content-Length": content_length})
150150

151-
endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
151+
endpoint.prepared_request.headers = headers
152152
session = requests.Session()
153153
response = session.send(request=endpoint.prepared_request)
154154
handle_server_errors(response=response)

tests/mock_vws/test_date_header.py

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import requests
1515
from freezegun import freeze_time
1616
from mock_vws._constants import ResultCodes
17-
from requests.structures import CaseInsensitiveDict
1817
from vws_auth_tools import authorization_header, rfc_1123_date
1918

2019
from tests.mock_vws.utils.assertions import (
@@ -45,8 +44,6 @@ def test_no_date_header(endpoint: Endpoint) -> None:
4544
"""
4645
A `BAD_REQUEST` response is returned when no `Date` header is given.
4746
"""
48-
endpoint_headers = dict(endpoint.prepared_request.headers)
49-
5047
authorization_string = authorization_header(
5148
access_key=endpoint.access_key,
5249
secret_key=endpoint.secret_key,
@@ -57,11 +54,10 @@ def test_no_date_header(endpoint: Endpoint) -> None:
5754
request_path=endpoint.prepared_request.path_url,
5855
)
5956

60-
headers: dict[str, str] = endpoint_headers | {
61-
"Authorization": authorization_string,
62-
}
57+
headers = endpoint.prepared_request.headers.copy()
58+
headers.update({"Authorization": authorization_string})
6359
headers.pop("Date", None)
64-
endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
60+
endpoint.prepared_request.headers = headers
6561
session = requests.Session()
6662
response = session.send(request=endpoint.prepared_request)
6763
handle_server_errors(response=response)
@@ -109,7 +105,7 @@ def test_incorrect_date_format(endpoint: Endpoint) -> None:
109105
now = datetime.now(tz=gmt)
110106
date_incorrect_format = now.strftime("%a %b %d %H:%M:%S")
111107

112-
endpoint_headers = dict(endpoint.prepared_request.headers)
108+
headers = endpoint.prepared_request.headers.copy()
113109

114110
authorization_string = authorization_header(
115111
access_key=endpoint.access_key,
@@ -121,12 +117,14 @@ def test_incorrect_date_format(endpoint: Endpoint) -> None:
121117
request_path=endpoint.prepared_request.path_url,
122118
)
123119

124-
headers: dict[str, str] = endpoint_headers | {
125-
"Authorization": authorization_string,
126-
"Date": date_incorrect_format,
127-
}
120+
headers.update(
121+
{
122+
"Authorization": authorization_string,
123+
"Date": date_incorrect_format,
124+
},
125+
)
128126

129-
endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
127+
endpoint.prepared_request.headers = headers
130128
session = requests.Session()
131129
response = session.send(request=endpoint.prepared_request)
132130
handle_server_errors(response=response)
@@ -180,8 +178,6 @@ def test_date_out_of_range_after(endpoint: Endpoint) -> None:
180178
with freeze_time(datetime.now(tz=gmt) + time_difference_from_now):
181179
date = rfc_1123_date()
182180

183-
endpoint_headers = dict(endpoint.prepared_request.headers)
184-
185181
authorization_string = authorization_header(
186182
access_key=endpoint.access_key,
187183
secret_key=endpoint.secret_key,
@@ -192,12 +188,10 @@ def test_date_out_of_range_after(endpoint: Endpoint) -> None:
192188
request_path=endpoint.prepared_request.path_url,
193189
)
194190

195-
headers = endpoint_headers | {
196-
"Authorization": authorization_string,
197-
"Date": date,
198-
}
191+
headers = endpoint.prepared_request.headers.copy()
192+
headers.update({"Authorization": authorization_string, "Date": date})
199193

200-
endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
194+
endpoint.prepared_request.headers = headers
201195
session = requests.Session()
202196
response = session.send(request=endpoint.prepared_request)
203197
handle_server_errors(response=response)
@@ -244,8 +238,6 @@ def test_date_out_of_range_before(endpoint: Endpoint) -> None:
244238
with freeze_time(datetime.now(tz=gmt) - time_difference_from_now):
245239
date = rfc_1123_date()
246240

247-
endpoint_headers = dict(endpoint.prepared_request.headers)
248-
249241
authorization_string = authorization_header(
250242
access_key=endpoint.access_key,
251243
secret_key=endpoint.secret_key,
@@ -256,12 +248,10 @@ def test_date_out_of_range_before(endpoint: Endpoint) -> None:
256248
request_path=endpoint.prepared_request.path_url,
257249
)
258250

259-
headers = endpoint_headers | {
260-
"Authorization": authorization_string,
261-
"Date": date,
262-
}
251+
headers = endpoint.prepared_request.headers.copy()
252+
headers.update({"Authorization": authorization_string, "Date": date})
263253

264-
endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
254+
endpoint.prepared_request.headers = headers
265255
session = requests.Session()
266256
response = session.send(request=endpoint.prepared_request)
267257
handle_server_errors(response=response)
@@ -307,7 +297,7 @@ def test_date_in_range_after(endpoint: Endpoint) -> None:
307297
with freeze_time(datetime.now(tz=gmt) + time_difference_from_now):
308298
date = rfc_1123_date()
309299

310-
endpoint_headers = dict(endpoint.prepared_request.headers)
300+
headers = endpoint.prepared_request.headers.copy()
311301

312302
authorization_string = authorization_header(
313303
access_key=endpoint.access_key,
@@ -319,12 +309,9 @@ def test_date_in_range_after(endpoint: Endpoint) -> None:
319309
request_path=endpoint.prepared_request.path_url,
320310
)
321311

322-
headers: dict[str, str] = endpoint_headers | {
323-
"Authorization": authorization_string,
324-
"Date": date,
325-
}
312+
headers.update({"Authorization": authorization_string, "Date": date})
326313

327-
endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
314+
endpoint.prepared_request.headers = headers
328315
session = requests.Session()
329316
response = session.send(request=endpoint.prepared_request)
330317
handle_server_errors(response=response)
@@ -361,8 +348,6 @@ def test_date_in_range_before(endpoint: Endpoint) -> None:
361348
with freeze_time(datetime.now(tz=gmt) - time_difference_from_now):
362349
date = rfc_1123_date()
363350

364-
endpoint_headers = dict(endpoint.prepared_request.headers)
365-
366351
authorization_string = authorization_header(
367352
access_key=endpoint.access_key,
368353
secret_key=endpoint.secret_key,
@@ -373,12 +358,10 @@ def test_date_in_range_before(endpoint: Endpoint) -> None:
373358
request_path=endpoint.prepared_request.path_url,
374359
)
375360

376-
headers: dict[str, str] = endpoint_headers | {
377-
"Authorization": authorization_string,
378-
"Date": date,
379-
}
361+
headers = endpoint.prepared_request.headers.copy()
362+
headers.update({"Authorization": authorization_string, "Date": date})
380363

381-
endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
364+
endpoint.prepared_request.headers = headers
382365
session = requests.Session()
383366
response = session.send(request=endpoint.prepared_request)
384367
handle_server_errors(response=response)

tests/mock_vws/test_invalid_json.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import requests
1515
from freezegun import freeze_time
1616
from mock_vws._constants import ResultCodes
17-
from requests.structures import CaseInsensitiveDict
1817
from vws_auth_tools import authorization_header, rfc_1123_date
1918

2019
from tests.mock_vws.utils.assertions import (
@@ -47,7 +46,6 @@ def test_invalid_json(endpoint: Endpoint) -> None:
4746
with freeze_time(time_to_freeze):
4847
date = rfc_1123_date()
4948

50-
endpoint_headers = dict(endpoint.prepared_request.headers)
5149
authorization_string = authorization_header(
5250
access_key=endpoint.access_key,
5351
secret_key=endpoint.secret_key,
@@ -58,13 +56,11 @@ def test_invalid_json(endpoint: Endpoint) -> None:
5856
request_path=endpoint.prepared_request.path_url,
5957
)
6058

61-
headers = endpoint_headers | {
62-
"Authorization": authorization_string,
63-
"Date": date,
64-
}
59+
headers = endpoint.prepared_request.headers.copy()
60+
headers.update({"Authorization": authorization_string, "Date": date})
6561

6662
endpoint.prepared_request.body = content
67-
endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
63+
endpoint.prepared_request.headers = headers
6864
endpoint.prepared_request.prepare_content_length(body=content)
6965
session = requests.Session()
7066
response = session.send(request=endpoint.prepared_request)
@@ -119,7 +115,6 @@ def test_invalid_json_with_skewed_time(endpoint: Endpoint) -> None:
119115
with freeze_time(time_to_freeze):
120116
date = rfc_1123_date()
121117

122-
endpoint_headers = dict(endpoint.prepared_request.headers)
123118
authorization_string = authorization_header(
124119
access_key=endpoint.access_key,
125120
secret_key=endpoint.secret_key,
@@ -130,13 +125,11 @@ def test_invalid_json_with_skewed_time(endpoint: Endpoint) -> None:
130125
request_path=endpoint.prepared_request.path_url,
131126
)
132127

133-
headers = endpoint_headers | {
134-
"Authorization": authorization_string,
135-
"Date": date,
136-
}
128+
headers = endpoint.prepared_request.headers.copy()
129+
headers.update({"Authorization": authorization_string, "Date": date})
137130

138131
endpoint.prepared_request.body = content
139-
endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
132+
endpoint.prepared_request.headers = headers
140133
endpoint.prepared_request.prepare_content_length(body=content)
141134
session = requests.Session()
142135
response = session.send(request=endpoint.prepared_request)

tests/mock_vws/test_unexpected_json.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
import pytest
1313
import requests
14-
from requests.structures import CaseInsensitiveDict
1514
from vws_auth_tools import authorization_header, rfc_1123_date
1615

1716
from tests.mock_vws.utils.assertions import assert_vwq_failure
@@ -44,7 +43,7 @@ def test_does_not_take_data(endpoint: Endpoint) -> None:
4443
content_type = "application/json"
4544
date = rfc_1123_date()
4645

47-
endpoint_headers = dict(endpoint.prepared_request.headers)
46+
headers = endpoint.prepared_request.headers.copy()
4847

4948
authorization_string = authorization_header(
5049
access_key=endpoint.access_key,
@@ -56,14 +55,16 @@ def test_does_not_take_data(endpoint: Endpoint) -> None:
5655
request_path=endpoint.prepared_request.path_url,
5756
)
5857

59-
headers = endpoint_headers | {
60-
"Authorization": authorization_string,
61-
"Date": date,
62-
"Content-Type": content_type,
63-
}
58+
headers.update(
59+
{
60+
"Authorization": authorization_string,
61+
"Date": date,
62+
"Content-Type": content_type,
63+
},
64+
)
6465

6566
endpoint.prepared_request.body = content
66-
endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
67+
endpoint.prepared_request.headers = headers
6768
endpoint.prepared_request.prepare_content_length(body=content)
6869
session = requests.Session()
6970
response = session.send(request=endpoint.prepared_request)

tests/mock_vws/utils/assertions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def assert_query_success(response: requests.Response) -> None:
187187

188188
assert json.loads(response.text)["result_code"] == "Success"
189189
assert_valid_date_header(response=response)
190-
copied_response_headers = dict(copy.deepcopy(response.headers))
190+
copied_response_headers = response.headers.copy()
191191
copied_response_headers.pop("Date")
192192

193193
# In the mock, all responses have the ``Content-Encoding`` ``gzip``.

0 commit comments

Comments
 (0)