Fix request_to_curl() corrupting dict cookies with bytes keys/values

PR #7603 made the list-cookie branch of request_to_curl() bytes-safe via
_cookie_value_to_unicode(), but left the sibling dict-cookie branch using
raw f-string interpolation. A dict cookie with bytes keys/values (a
supported and common form, e.g. Request(url, cookies={b"k": b"v"})) was
rendered as --cookie 'b'k'=b'v'' instead of --cookie 'k=v', producing a
broken curl command.

Route the dict branch through the same _cookie_value_to_unicode() helper,
mirroring the list branch. Add a regression test.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
greymoth-jp 2026-06-26 04:19:53 +09:00
parent 74e6b61071
commit 1d965dbc71
2 changed files with 17 additions and 1 deletions

View File

@ -204,7 +204,10 @@ def request_to_curl(request: Request) -> str:
cookies = ""
if request.cookies:
if isinstance(request.cookies, dict):
cookie = "; ".join(f"{k}={v}" for k, v in request.cookies.items())
cookie = "; ".join(
f"{_cookie_value_to_unicode(k)}={_cookie_value_to_unicode(v)}"
for k, v in request.cookies.items()
)
cookies = f"--cookie '{cookie}'"
elif isinstance(request.cookies, list):
cookie = "; ".join(

View File

@ -401,6 +401,19 @@ class TestRequestToCurl:
)
self._test_request(request_object, expected_curl_command)
def test_cookies_dict_bytes(self):
request_object = Request(
"https://www.httpbin.org/post",
method="POST",
cookies={b"foo": b"bar"},
body=json.dumps({"foo": "bar"}),
)
expected_curl_command = (
"curl -X POST https://www.httpbin.org/post"
" --data-raw '{\"foo\": \"bar\"}' --cookie 'foo=bar'"
)
self._test_request(request_object, expected_curl_command)
def test_cookies_list(self):
request_object = Request(
"https://www.httpbin.org/post",