Skip to content

Commit

Permalink
bugfix: fix impersonate=chrome not working, check the return value of…
Browse files Browse the repository at this point in the history
… impersonate
  • Loading branch information
perklet committed Mar 1, 2024
1 parent 0ebcb7e commit 9f6ff3c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
18 changes: 15 additions & 3 deletions curl_cffi/requests/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ class BrowserType(str, Enum):
def has(cls, item):
return item in cls.__members__

@classmethod
def normalize(cls, item):
if item == "chrome":
return cls.chrome
elif item == "safari":
return cls.safari
elif item == "safari_ios":
return cls.safari_ios
else:
return item


class BrowserSpec:
"""A more structured way of selecting browsers"""
Expand Down Expand Up @@ -455,9 +466,10 @@ def _set_curl_options(
self.default_headers if default_headers is None else default_headers
)
if impersonate:
if not BrowserType.has(impersonate):
raise RequestsError(f"impersonate {impersonate} is not supported")
c.impersonate(impersonate, default_headers=default_headers)
impersonate = BrowserType.normalize(impersonate)
ret = c.impersonate(impersonate, default_headers=default_headers)
if ret != 0:
raise RequestsError(f"Impersonating {impersonate} is not supported")

# http_version, after impersonate, which will change this to http2
http_version = http_version or self.http_version
Expand Down
6 changes: 3 additions & 3 deletions tests/unittest/test_curl.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def test_timeout(server):
url = str(server.url.copy_with(path="/slow_response"))
c.setopt(CurlOpt.URL, url.encode())
c.setopt(CurlOpt.TIMEOUT_MS, 100)
with pytest.raises(CurlError, match=r"ErrCode: 28"):
with pytest.raises(CurlError, match=r"curl: \(28\)"):
c.perform()


Expand All @@ -149,7 +149,7 @@ def test_repeated_headers_after_error(server):
c.setopt(CurlOpt.URL, url.encode())
c.setopt(CurlOpt.TIMEOUT_MS, 100)
c.setopt(CurlOpt.HTTPHEADER, [b"Foo: bar"])
with pytest.raises(CurlError, match=r"ErrCode: 28"):
with pytest.raises(CurlError, match=r"curl: \(28\)"):
c.perform()

# another request
Expand Down Expand Up @@ -202,7 +202,7 @@ def test_https_proxy_using_connect(server):
c.setopt(CurlOpt.HTTPPROXYTUNNEL, 1)
buffer = BytesIO()
c.setopt(CurlOpt.WRITEDATA, buffer)
with pytest.raises(CurlError, match=r"ErrCode: 35"):
with pytest.raises(CurlError, match=r"curl: \(35\)"):
c.perform()


Expand Down
26 changes: 26 additions & 0 deletions tests/unittest/test_impersonate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pytest

from curl_cffi import requests
from curl_cffi.const import CurlHttpVersion


def test_impersonate_with_version(server):
# the test server does not understand http/2
r = requests.get(str(server.url), impersonate="chrome120", http_version=CurlHttpVersion.V1_1)
assert r.status_code == 200
r = requests.get(str(server.url), impersonate="safari17_0", http_version=CurlHttpVersion.V1_1)
assert r.status_code == 200


def test_impersonate_without_version(server):
r = requests.get(str(server.url), impersonate="chrome", http_version=CurlHttpVersion.V1_1)
assert r.status_code == 200
r = requests.get(str(server.url), impersonate="safari_ios", http_version=CurlHttpVersion.V1_1)
assert r.status_code == 200


def test_impersonate_non_exist(server):
with pytest.raises(requests.RequestsError, match="Impersonating"):
requests.get(str(server.url), impersonate="edge")
with pytest.raises(requests.RequestsError, match="Impersonating"):
requests.get(str(server.url), impersonate="chrome2952")

0 comments on commit 9f6ff3c

Please sign in to comment.