mirror of https://github.com/scrapy/scrapy.git
Fix request_to_curl handling of verbose cookies dictionaries (#7603)
This commit is contained in:
parent
4f241b73be
commit
cfef12392a
|
|
@ -179,6 +179,12 @@ def _get_method(obj: Any, name: Any) -> Any:
|
||||||
raise ValueError(f"Method {name!r} not found in: {obj}") from None
|
raise ValueError(f"Method {name!r} not found in: {obj}") from None
|
||||||
|
|
||||||
|
|
||||||
|
def _cookie_value_to_unicode(value: str | bytes | float) -> str:
|
||||||
|
if isinstance(value, bytes):
|
||||||
|
return value.decode()
|
||||||
|
return str(value)
|
||||||
|
|
||||||
|
|
||||||
def request_to_curl(request: Request) -> str:
|
def request_to_curl(request: Request) -> str:
|
||||||
"""
|
"""
|
||||||
Converts a :class:`~scrapy.Request` object to a curl command.
|
Converts a :class:`~scrapy.Request` object to a curl command.
|
||||||
|
|
@ -202,7 +208,9 @@ def request_to_curl(request: Request) -> str:
|
||||||
cookies = f"--cookie '{cookie}'"
|
cookies = f"--cookie '{cookie}'"
|
||||||
elif isinstance(request.cookies, list):
|
elif isinstance(request.cookies, list):
|
||||||
cookie = "; ".join(
|
cookie = "; ".join(
|
||||||
f"{next(iter(c.keys()))}={next(iter(c.values()))}"
|
f"{_cookie_value_to_unicode(c['name'])}={_cookie_value_to_unicode(c['value'])}"
|
||||||
|
if "name" in c and "value" in c
|
||||||
|
else f"{next(iter(c.keys()))}={next(iter(c.values()))}"
|
||||||
for c in request.cookies
|
for c in request.cookies
|
||||||
)
|
)
|
||||||
cookies = f"--cookie '{cookie}'"
|
cookies = f"--cookie '{cookie}'"
|
||||||
|
|
|
||||||
|
|
@ -413,3 +413,45 @@ class TestRequestToCurl:
|
||||||
" --data-raw '{\"foo\": \"bar\"}' --cookie 'foo=bar'"
|
" --data-raw '{\"foo\": \"bar\"}' --cookie 'foo=bar'"
|
||||||
)
|
)
|
||||||
self._test_request(request_object, expected_curl_command)
|
self._test_request(request_object, expected_curl_command)
|
||||||
|
|
||||||
|
def test_cookies_list_verbose(self):
|
||||||
|
request_object = Request(
|
||||||
|
"https://www.httpbin.org/post",
|
||||||
|
method="POST",
|
||||||
|
cookies=[
|
||||||
|
{
|
||||||
|
"name": b"foo",
|
||||||
|
"value": b"bar",
|
||||||
|
"domain": "example.com",
|
||||||
|
"path": "/",
|
||||||
|
"secure": True,
|
||||||
|
}
|
||||||
|
],
|
||||||
|
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_verbose_non_string_value(self):
|
||||||
|
request_object = Request(
|
||||||
|
"https://www.httpbin.org/post",
|
||||||
|
method="POST",
|
||||||
|
cookies=[
|
||||||
|
{
|
||||||
|
"name": "foo",
|
||||||
|
"value": 1,
|
||||||
|
"domain": "example.com",
|
||||||
|
"path": "/",
|
||||||
|
"secure": True,
|
||||||
|
}
|
||||||
|
],
|
||||||
|
body=json.dumps({"foo": "bar"}),
|
||||||
|
)
|
||||||
|
expected_curl_command = (
|
||||||
|
"curl -X POST https://www.httpbin.org/post"
|
||||||
|
" --data-raw '{\"foo\": \"bar\"}' --cookie 'foo=1'"
|
||||||
|
)
|
||||||
|
self._test_request(request_object, expected_curl_command)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue