Skip to content

Commit eee3546

Browse files
committed
Allow utf-8 encoded strings as content
1 parent 197a054 commit eee3546

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/vws_auth_tools/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def authorization_header(
2626
access_key: str,
2727
secret_key: str,
2828
method: str,
29-
content: bytes,
29+
content: str | bytes | None,
3030
content_type: str,
3131
date: str,
3232
request_path: str,
@@ -59,6 +59,13 @@ def authorization_header(
5959
"""
6060
# Ignore a warning that MD5 is insecure - VWS requires it.
6161
hashed = hashlib.md5() # noqa: S324
62+
63+
if content is None:
64+
content = b""
65+
66+
if isinstance(content, str):
67+
content = content.encode(encoding="utf-8")
68+
6269
hashed.update(content)
6370
content_md5_hex = hashed.hexdigest()
6471

tests/test_vws_auth_tools.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import datetime
44
from zoneinfo import ZoneInfo
55

6+
import pytest
67
import vws_auth_tools
78
from freezegun import freeze_time
89

@@ -36,7 +37,8 @@ def test_rfc_1123_date() -> None:
3637
assert result == "Thu, 05 Feb 2015 14:55:12 GMT"
3738

3839

39-
def test_authorization_header() -> None:
40+
@pytest.mark.parametrize("content", [b"some_bytes", "some_bytes"])
41+
def test_authorization_header(content: bytes | str) -> None:
4042
"""The Authorization header is constructed as documented.
4143
4244
This example has been run on known-working code and so any refactor should
@@ -46,7 +48,6 @@ def test_authorization_header() -> None:
4648
# Ignore "Possible hardcoded password" as it is appropriate here.
4749
secret_key = "my_secret_key" # noqa: S105
4850
method = "HTTPMETHOD"
49-
content = b"some_bytes"
5051
content_type = "some/content/type"
5152
date = "some_date_string"
5253
request_path = "/foo"

0 commit comments

Comments
 (0)