mirror of https://github.com/scrapy/scrapy.git
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:
parent
74e6b61071
commit
1d965dbc71
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
Loading…
Reference in New Issue