Skip to content

Simplify tests by not going between CaseInsensitiveDict and dict #2167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 12 additions & 20 deletions tests/mock_vws/test_authorization_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import pytest
import requests
from mock_vws._constants import ResultCodes
from requests.structures import CaseInsensitiveDict
from vws import VWS, CloudRecoService
from vws.exceptions import cloud_reco_exceptions
from vws.exceptions.vws_exceptions import AuthenticationFailure, Fail
Expand Down Expand Up @@ -47,13 +46,12 @@
is given.
"""
date = rfc_1123_date()
headers: dict[str, str] = dict(endpoint.prepared_request.headers) | {
"Date": date,
}
headers = endpoint.prepared_request.headers.copy()
headers.update({"Date": date})

Check warning on line 50 in tests/mock_vws/test_authorization_header.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_authorization_header.py#L49-L50

Added lines #L49 - L50 were not covered by tests

headers.pop("Authorization", None)

endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
endpoint.prepared_request.headers = headers

Check warning on line 54 in tests/mock_vws/test_authorization_header.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_authorization_header.py#L54

Added line #L54 was not covered by tests
session = requests.Session()
response = session.send(request=endpoint.prepared_request)
handle_server_errors(response=response)
Expand Down Expand Up @@ -98,12 +96,10 @@
# string, but really any string which is not two parts when split on a
# space will do.
authorization_string = "VWS"
headers: dict[str, str] = dict(endpoint.prepared_request.headers) | {
"Authorization": authorization_string,
"Date": date,
}
headers = endpoint.prepared_request.headers.copy()
headers.update({"Authorization": authorization_string, "Date": date})

endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
endpoint.prepared_request.headers = headers
session = requests.Session()
response = session.send(request=endpoint.prepared_request)
handle_server_errors(response=response)
Expand Down Expand Up @@ -138,12 +134,10 @@
authorization_string = "VWS "
date = rfc_1123_date()

headers: dict[str, str] = dict(endpoint.prepared_request.headers) | {
"Authorization": authorization_string,
"Date": date,
}
headers = endpoint.prepared_request.headers.copy()
headers.update({"Authorization": authorization_string, "Date": date})

Check warning on line 138 in tests/mock_vws/test_authorization_header.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_authorization_header.py#L137-L138

Added lines #L137 - L138 were not covered by tests

endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
endpoint.prepared_request.headers = headers

Check warning on line 140 in tests/mock_vws/test_authorization_header.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_authorization_header.py#L140

Added line #L140 was not covered by tests
session = requests.Session()
response = session.send(request=endpoint.prepared_request)
handle_server_errors(response=response)
Expand Down Expand Up @@ -177,12 +171,10 @@
date = rfc_1123_date()

authorization_string = "VWS foobar:"
headers: dict[str, str] = dict(endpoint.prepared_request.headers) | {
"Authorization": authorization_string,
"Date": date,
}
headers = endpoint.prepared_request.headers.copy()
headers.update({"Authorization": authorization_string, "Date": date})

Check warning on line 175 in tests/mock_vws/test_authorization_header.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_authorization_header.py#L174-L175

Added lines #L174 - L175 were not covered by tests

endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
endpoint.prepared_request.headers = headers

Check warning on line 177 in tests/mock_vws/test_authorization_header.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_authorization_header.py#L177

Added line #L177 was not covered by tests
session = requests.Session()
response = session.send(request=endpoint.prepared_request)
handle_server_errors(response=response)
Expand Down
28 changes: 14 additions & 14 deletions tests/mock_vws/test_content_length.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@
A ``BAD_REQUEST`` error is given when the given ``Content-Length`` is
not an integer.
"""
endpoint_headers = dict(endpoint.prepared_request.headers)
if not endpoint_headers.get("Content-Type"):
headers = endpoint.prepared_request.headers.copy()

Check warning on line 44 in tests/mock_vws/test_content_length.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_content_length.py#L44

Added line #L44 was not covered by tests
if not headers.get("Content-Type"):
return

content_length = "0.4"
headers = endpoint_headers | {"Content-Length": content_length}
endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
headers.update({"Content-Length": content_length})
endpoint.prepared_request.headers = headers

Check warning on line 50 in tests/mock_vws/test_content_length.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_content_length.py#L49-L50

Added lines #L49 - L50 were not covered by tests
session = requests.Session()
response = session.send(request=endpoint.prepared_request)
handle_server_errors(response=response)
Expand Down Expand Up @@ -94,16 +94,16 @@
"""
An error is given if the given content length is too large.
"""
endpoint_headers = dict(endpoint.prepared_request.headers)
if not endpoint_headers.get("Content-Type"):
headers = endpoint.prepared_request.headers.copy()
if not headers.get("Content-Type"):
pytest.skip("No Content-Type header for this request")

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

endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
endpoint.prepared_request.headers = headers
session = requests.Session()
response = session.send(request=endpoint.prepared_request)
# We do not use ``handle_server_errors`` here because we do not want to
Expand Down Expand Up @@ -141,14 +141,14 @@
An ``UNAUTHORIZED`` response is given if the given content length is
too small.
"""
endpoint_headers = dict(endpoint.prepared_request.headers)
if not endpoint_headers.get("Content-Type"):
headers = endpoint.prepared_request.headers.copy()

Check warning on line 144 in tests/mock_vws/test_content_length.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_content_length.py#L144

Added line #L144 was not covered by tests
if not headers.get("Content-Type"):
return

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

Check warning on line 149 in tests/mock_vws/test_content_length.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_content_length.py#L148-L149

Added lines #L148 - L149 were not covered by tests

endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
endpoint.prepared_request.headers = headers

Check warning on line 151 in tests/mock_vws/test_content_length.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_content_length.py#L151

Added line #L151 was not covered by tests
session = requests.Session()
response = session.send(request=endpoint.prepared_request)
handle_server_errors(response=response)
Expand Down
63 changes: 23 additions & 40 deletions tests/mock_vws/test_date_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import requests
from freezegun import freeze_time
from mock_vws._constants import ResultCodes
from requests.structures import CaseInsensitiveDict
from vws_auth_tools import authorization_header, rfc_1123_date

from tests.mock_vws.utils.assertions import (
Expand Down Expand Up @@ -45,8 +44,6 @@
"""
A `BAD_REQUEST` response is returned when no `Date` header is given.
"""
endpoint_headers = dict(endpoint.prepared_request.headers)

authorization_string = authorization_header(
access_key=endpoint.access_key,
secret_key=endpoint.secret_key,
Expand All @@ -57,11 +54,10 @@
request_path=endpoint.prepared_request.path_url,
)

headers: dict[str, str] = endpoint_headers | {
"Authorization": authorization_string,
}
headers = endpoint.prepared_request.headers.copy()
headers.update({"Authorization": authorization_string})

Check warning on line 58 in tests/mock_vws/test_date_header.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_date_header.py#L57-L58

Added lines #L57 - L58 were not covered by tests
headers.pop("Date", None)
endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
endpoint.prepared_request.headers = headers

Check warning on line 60 in tests/mock_vws/test_date_header.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_date_header.py#L60

Added line #L60 was not covered by tests
session = requests.Session()
response = session.send(request=endpoint.prepared_request)
handle_server_errors(response=response)
Expand Down Expand Up @@ -109,7 +105,7 @@
now = datetime.now(tz=gmt)
date_incorrect_format = now.strftime("%a %b %d %H:%M:%S")

endpoint_headers = dict(endpoint.prepared_request.headers)
headers = endpoint.prepared_request.headers.copy()

Check warning on line 108 in tests/mock_vws/test_date_header.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_date_header.py#L108

Added line #L108 was not covered by tests

authorization_string = authorization_header(
access_key=endpoint.access_key,
Expand All @@ -121,12 +117,14 @@
request_path=endpoint.prepared_request.path_url,
)

headers: dict[str, str] = endpoint_headers | {
"Authorization": authorization_string,
"Date": date_incorrect_format,
}
headers.update(

Check warning on line 120 in tests/mock_vws/test_date_header.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_date_header.py#L120

Added line #L120 was not covered by tests
{
"Authorization": authorization_string,
"Date": date_incorrect_format,
},
)

endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
endpoint.prepared_request.headers = headers

Check warning on line 127 in tests/mock_vws/test_date_header.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_date_header.py#L127

Added line #L127 was not covered by tests
session = requests.Session()
response = session.send(request=endpoint.prepared_request)
handle_server_errors(response=response)
Expand Down Expand Up @@ -180,8 +178,6 @@
with freeze_time(datetime.now(tz=gmt) + time_difference_from_now):
date = rfc_1123_date()

endpoint_headers = dict(endpoint.prepared_request.headers)

authorization_string = authorization_header(
access_key=endpoint.access_key,
secret_key=endpoint.secret_key,
Expand All @@ -192,12 +188,10 @@
request_path=endpoint.prepared_request.path_url,
)

headers = endpoint_headers | {
"Authorization": authorization_string,
"Date": date,
}
headers = endpoint.prepared_request.headers.copy()
headers.update({"Authorization": authorization_string, "Date": date})

Check warning on line 192 in tests/mock_vws/test_date_header.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_date_header.py#L191-L192

Added lines #L191 - L192 were not covered by tests

endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
endpoint.prepared_request.headers = headers

Check warning on line 194 in tests/mock_vws/test_date_header.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_date_header.py#L194

Added line #L194 was not covered by tests
session = requests.Session()
response = session.send(request=endpoint.prepared_request)
handle_server_errors(response=response)
Expand Down Expand Up @@ -244,8 +238,6 @@
with freeze_time(datetime.now(tz=gmt) - time_difference_from_now):
date = rfc_1123_date()

endpoint_headers = dict(endpoint.prepared_request.headers)

authorization_string = authorization_header(
access_key=endpoint.access_key,
secret_key=endpoint.secret_key,
Expand All @@ -256,12 +248,10 @@
request_path=endpoint.prepared_request.path_url,
)

headers = endpoint_headers | {
"Authorization": authorization_string,
"Date": date,
}
headers = endpoint.prepared_request.headers.copy()
headers.update({"Authorization": authorization_string, "Date": date})

Check warning on line 252 in tests/mock_vws/test_date_header.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_date_header.py#L251-L252

Added lines #L251 - L252 were not covered by tests

endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
endpoint.prepared_request.headers = headers

Check warning on line 254 in tests/mock_vws/test_date_header.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_date_header.py#L254

Added line #L254 was not covered by tests
session = requests.Session()
response = session.send(request=endpoint.prepared_request)
handle_server_errors(response=response)
Expand Down Expand Up @@ -307,7 +297,7 @@
with freeze_time(datetime.now(tz=gmt) + time_difference_from_now):
date = rfc_1123_date()

endpoint_headers = dict(endpoint.prepared_request.headers)
headers = endpoint.prepared_request.headers.copy()

Check warning on line 300 in tests/mock_vws/test_date_header.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_date_header.py#L300

Added line #L300 was not covered by tests

authorization_string = authorization_header(
access_key=endpoint.access_key,
Expand All @@ -319,12 +309,9 @@
request_path=endpoint.prepared_request.path_url,
)

headers: dict[str, str] = endpoint_headers | {
"Authorization": authorization_string,
"Date": date,
}
headers.update({"Authorization": authorization_string, "Date": date})

Check warning on line 312 in tests/mock_vws/test_date_header.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_date_header.py#L312

Added line #L312 was not covered by tests

endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
endpoint.prepared_request.headers = headers

Check warning on line 314 in tests/mock_vws/test_date_header.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_date_header.py#L314

Added line #L314 was not covered by tests
session = requests.Session()
response = session.send(request=endpoint.prepared_request)
handle_server_errors(response=response)
Expand Down Expand Up @@ -361,8 +348,6 @@
with freeze_time(datetime.now(tz=gmt) - time_difference_from_now):
date = rfc_1123_date()

endpoint_headers = dict(endpoint.prepared_request.headers)

authorization_string = authorization_header(
access_key=endpoint.access_key,
secret_key=endpoint.secret_key,
Expand All @@ -373,12 +358,10 @@
request_path=endpoint.prepared_request.path_url,
)

headers: dict[str, str] = endpoint_headers | {
"Authorization": authorization_string,
"Date": date,
}
headers = endpoint.prepared_request.headers.copy()
headers.update({"Authorization": authorization_string, "Date": date})

Check warning on line 362 in tests/mock_vws/test_date_header.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_date_header.py#L361-L362

Added lines #L361 - L362 were not covered by tests

endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
endpoint.prepared_request.headers = headers

Check warning on line 364 in tests/mock_vws/test_date_header.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_date_header.py#L364

Added line #L364 was not covered by tests
session = requests.Session()
response = session.send(request=endpoint.prepared_request)
handle_server_errors(response=response)
Expand Down
19 changes: 6 additions & 13 deletions tests/mock_vws/test_invalid_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import requests
from freezegun import freeze_time
from mock_vws._constants import ResultCodes
from requests.structures import CaseInsensitiveDict
from vws_auth_tools import authorization_header, rfc_1123_date

from tests.mock_vws.utils.assertions import (
Expand Down Expand Up @@ -47,7 +46,6 @@
with freeze_time(time_to_freeze):
date = rfc_1123_date()

endpoint_headers = dict(endpoint.prepared_request.headers)
authorization_string = authorization_header(
access_key=endpoint.access_key,
secret_key=endpoint.secret_key,
Expand All @@ -58,13 +56,11 @@
request_path=endpoint.prepared_request.path_url,
)

headers = endpoint_headers | {
"Authorization": authorization_string,
"Date": date,
}
headers = endpoint.prepared_request.headers.copy()
headers.update({"Authorization": authorization_string, "Date": date})

Check warning on line 60 in tests/mock_vws/test_invalid_json.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_invalid_json.py#L59-L60

Added lines #L59 - L60 were not covered by tests

endpoint.prepared_request.body = content
endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
endpoint.prepared_request.headers = headers

Check warning on line 63 in tests/mock_vws/test_invalid_json.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_invalid_json.py#L63

Added line #L63 was not covered by tests
endpoint.prepared_request.prepare_content_length(body=content)
session = requests.Session()
response = session.send(request=endpoint.prepared_request)
Expand Down Expand Up @@ -119,7 +115,6 @@
with freeze_time(time_to_freeze):
date = rfc_1123_date()

endpoint_headers = dict(endpoint.prepared_request.headers)
authorization_string = authorization_header(
access_key=endpoint.access_key,
secret_key=endpoint.secret_key,
Expand All @@ -130,13 +125,11 @@
request_path=endpoint.prepared_request.path_url,
)

headers = endpoint_headers | {
"Authorization": authorization_string,
"Date": date,
}
headers = endpoint.prepared_request.headers.copy()
headers.update({"Authorization": authorization_string, "Date": date})

Check warning on line 129 in tests/mock_vws/test_invalid_json.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_invalid_json.py#L128-L129

Added lines #L128 - L129 were not covered by tests

endpoint.prepared_request.body = content
endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
endpoint.prepared_request.headers = headers

Check warning on line 132 in tests/mock_vws/test_invalid_json.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_invalid_json.py#L132

Added line #L132 was not covered by tests
endpoint.prepared_request.prepare_content_length(body=content)
session = requests.Session()
response = session.send(request=endpoint.prepared_request)
Expand Down
17 changes: 9 additions & 8 deletions tests/mock_vws/test_unexpected_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import pytest
import requests
from requests.structures import CaseInsensitiveDict
from vws_auth_tools import authorization_header, rfc_1123_date

from tests.mock_vws.utils.assertions import assert_vwq_failure
Expand Down Expand Up @@ -44,7 +43,7 @@
content_type = "application/json"
date = rfc_1123_date()

endpoint_headers = dict(endpoint.prepared_request.headers)
headers = endpoint.prepared_request.headers.copy()

Check warning on line 46 in tests/mock_vws/test_unexpected_json.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_unexpected_json.py#L46

Added line #L46 was not covered by tests

authorization_string = authorization_header(
access_key=endpoint.access_key,
Expand All @@ -56,14 +55,16 @@
request_path=endpoint.prepared_request.path_url,
)

headers = endpoint_headers | {
"Authorization": authorization_string,
"Date": date,
"Content-Type": content_type,
}
headers.update(

Check warning on line 58 in tests/mock_vws/test_unexpected_json.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_unexpected_json.py#L58

Added line #L58 was not covered by tests
{
"Authorization": authorization_string,
"Date": date,
"Content-Type": content_type,
},
)

endpoint.prepared_request.body = content
endpoint.prepared_request.headers = CaseInsensitiveDict(data=headers)
endpoint.prepared_request.headers = headers

Check warning on line 67 in tests/mock_vws/test_unexpected_json.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_unexpected_json.py#L67

Added line #L67 was not covered by tests
endpoint.prepared_request.prepare_content_length(body=content)
session = requests.Session()
response = session.send(request=endpoint.prepared_request)
Expand Down
2 changes: 1 addition & 1 deletion tests/mock_vws/utils/assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def assert_query_success(response: requests.Response) -> None:

assert json.loads(response.text)["result_code"] == "Success"
assert_valid_date_header(response=response)
copied_response_headers = dict(copy.deepcopy(response.headers))
copied_response_headers = response.headers.copy()
copied_response_headers.pop("Date")

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