Skip to content

Do not unnecessarily copy request headers in tests #2168

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 1 commit 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
25 changes: 11 additions & 14 deletions tests/mock_vws/test_authorization_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,9 @@
is given.
"""
date = rfc_1123_date()
headers = endpoint.prepared_request.headers.copy()
headers.update({"Date": date})
endpoint.prepared_request.headers.update({"Date": date})
endpoint.prepared_request.headers.pop("Authorization", None)

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 = headers
session = requests.Session()
response = session.send(request=endpoint.prepared_request)
handle_server_errors(response=response)
Expand Down Expand Up @@ -96,10 +93,10 @@
# string, but really any string which is not two parts when split on a
# space will do.
authorization_string = "VWS"
headers = endpoint.prepared_request.headers.copy()
headers.update({"Authorization": authorization_string, "Date": date})
endpoint.prepared_request.headers.update(

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

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_authorization_header.py#L96

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

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

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

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

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_authorization_header.py#L134

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

endpoint.prepared_request.headers = headers
session = requests.Session()
response = session.send(request=endpoint.prepared_request)
handle_server_errors(response=response)
Expand Down Expand Up @@ -171,10 +168,10 @@
date = rfc_1123_date()

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

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

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_authorization_header.py#L171

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

endpoint.prepared_request.headers = headers
session = requests.Session()
response = session.send(request=endpoint.prepared_request)
handle_server_errors(response=response)
Expand Down
32 changes: 18 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.
"""
headers = endpoint.prepared_request.headers.copy()
if not headers.get("Content-Type"):
if not endpoint.prepared_request.headers.get("Content-Type"):
return

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

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

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_content_length.py#L48

Added line #L48 was not covered by tests
{"Content-Length": content_length},
)
session = requests.Session()
response = session.send(request=endpoint.prepared_request)
handle_server_errors(response=response)
Expand Down Expand Up @@ -94,16 +94,18 @@
"""
An error is given if the given content length is too large.
"""
headers = endpoint.prepared_request.headers.copy()
if not headers.get("Content-Type"):
if not endpoint.prepared_request.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(headers["Content-Length"]) + 1)
headers.update({"Content-Length": content_length})
content_length = str(
int(endpoint.prepared_request.headers["Content-Length"]) + 1
)
endpoint.prepared_request.headers.update(
{"Content-Length": content_length}
)

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 +143,16 @@
An ``UNAUTHORIZED`` response is given if the given content length is
too small.
"""
headers = endpoint.prepared_request.headers.copy()
if not headers.get("Content-Type"):
if not endpoint.prepared_request.headers.get("Content-Type"):
return

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

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#L149

Added line #L149 was not covered by tests
int(endpoint.prepared_request.headers["Content-Length"]) - 1
)
endpoint.prepared_request.headers.update(

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

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_content_length.py#L152

Added line #L152 was not covered by tests
{"Content-Length": content_length}
)

endpoint.prepared_request.headers = headers
session = requests.Session()
response = session.send(request=endpoint.prepared_request)
handle_server_errors(response=response)
Expand Down
38 changes: 17 additions & 21 deletions tests/mock_vws/test_date_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@
request_path=endpoint.prepared_request.path_url,
)

headers = endpoint.prepared_request.headers.copy()
headers.update({"Authorization": authorization_string})
headers.pop("Date", None)
endpoint.prepared_request.headers = headers
endpoint.prepared_request.headers.update(

Check warning on line 57 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

Added line #L57 was not covered by tests
{"Authorization": authorization_string}
)
endpoint.prepared_request.headers.pop("Date", None)

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 @@ -105,8 +105,6 @@
now = datetime.now(tz=gmt)
date_incorrect_format = now.strftime("%a %b %d %H:%M:%S")

headers = endpoint.prepared_request.headers.copy()

authorization_string = authorization_header(
access_key=endpoint.access_key,
secret_key=endpoint.secret_key,
Expand All @@ -117,14 +115,13 @@
request_path=endpoint.prepared_request.path_url,
)

headers.update(
endpoint.prepared_request.headers.update(

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

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_date_header.py#L118

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

endpoint.prepared_request.headers = headers
session = requests.Session()
response = session.send(request=endpoint.prepared_request)
handle_server_errors(response=response)
Expand Down Expand Up @@ -188,10 +185,10 @@
request_path=endpoint.prepared_request.path_url,
)

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

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

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_date_header.py#L188

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

endpoint.prepared_request.headers = headers
session = requests.Session()
response = session.send(request=endpoint.prepared_request)
handle_server_errors(response=response)
Expand Down Expand Up @@ -248,10 +245,10 @@
request_path=endpoint.prepared_request.path_url,
)

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

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

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_date_header.py#L248

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

endpoint.prepared_request.headers = headers
session = requests.Session()
response = session.send(request=endpoint.prepared_request)
handle_server_errors(response=response)
Expand Down Expand Up @@ -297,8 +294,6 @@
with freeze_time(datetime.now(tz=gmt) + time_difference_from_now):
date = rfc_1123_date()

headers = endpoint.prepared_request.headers.copy()

authorization_string = authorization_header(
access_key=endpoint.access_key,
secret_key=endpoint.secret_key,
Expand All @@ -309,9 +304,10 @@
request_path=endpoint.prepared_request.path_url,
)

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

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

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_date_header.py#L307

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

endpoint.prepared_request.headers = headers
session = requests.Session()
response = session.send(request=endpoint.prepared_request)
handle_server_errors(response=response)
Expand Down Expand Up @@ -358,10 +354,10 @@
request_path=endpoint.prepared_request.path_url,
)

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

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

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_date_header.py#L357

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

endpoint.prepared_request.headers = headers
session = requests.Session()
response = session.send(request=endpoint.prepared_request)
handle_server_errors(response=response)
Expand Down
12 changes: 6 additions & 6 deletions tests/mock_vws/test_invalid_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@
request_path=endpoint.prepared_request.path_url,
)

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

Check warning on line 59 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

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

endpoint.prepared_request.body = content
endpoint.prepared_request.headers = headers
endpoint.prepared_request.prepare_content_length(body=content)
session = requests.Session()
response = session.send(request=endpoint.prepared_request)
Expand Down Expand Up @@ -125,11 +125,11 @@
request_path=endpoint.prepared_request.path_url,
)

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

Check warning on line 128 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

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

endpoint.prepared_request.body = content
endpoint.prepared_request.headers = headers
endpoint.prepared_request.prepare_content_length(body=content)
session = requests.Session()
response = session.send(request=endpoint.prepared_request)
Expand Down
5 changes: 1 addition & 4 deletions tests/mock_vws/test_unexpected_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
content_type = "application/json"
date = rfc_1123_date()

headers = endpoint.prepared_request.headers.copy()

authorization_string = authorization_header(
access_key=endpoint.access_key,
secret_key=endpoint.secret_key,
Expand All @@ -55,7 +53,7 @@
request_path=endpoint.prepared_request.path_url,
)

headers.update(
endpoint.prepared_request.headers.update(

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

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_unexpected_json.py#L56

Added line #L56 was not covered by tests
{
"Authorization": authorization_string,
"Date": date,
Expand All @@ -64,7 +62,6 @@
)

endpoint.prepared_request.body = content
endpoint.prepared_request.headers = headers
endpoint.prepared_request.prepare_content_length(body=content)
session = requests.Session()
response = session.send(request=endpoint.prepared_request)
Expand Down
Loading