- user_agent в запросе → 422 с подсказкой брать UA из ответа: UA, отличный от реального Chrome, расходится с userAgentData и движком, Cloudflare такой браузер не пропускает (замер: родной 3/3, Chrome 147 и Windows — 0) (#87) - профиль хранит только отпечаток (экран, DPR), UA не подменяется (#89) - docs/Usage.md: практики для клиентов API — тот же UA и прокси, что у браузера, сессии, cookies, ошибки и повторы, таймауты, пример на httpx - README, docs/API.md, docs/Introduction.md; тесты под новый контракт Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
47 lines
1.9 KiB
Python
47 lines
1.9 KiB
Python
import json
|
|
|
|
from api import profile
|
|
|
|
|
|
def test_fingerprint_generated_once(tmp_path):
|
|
first = profile.load_or_create(tmp_path)
|
|
assert profile.load_or_create(tmp_path) == first
|
|
assert json.loads((tmp_path / profile.SETTINGS_FILE).read_text()) == first
|
|
|
|
screen = first["fingerprint"]["screen"]
|
|
assert screen["width"] >= profile.MIN_SCREEN[0] and screen["height"] >= profile.MIN_SCREEN[1]
|
|
assert first["fingerprint"]["device_scale_factor"] >= 1
|
|
assert "user_agent" not in first
|
|
|
|
|
|
def test_old_profile_settings_get_fingerprint(tmp_path):
|
|
"""Профиль из версии, где сохранялся user_agent клиента: UA отбрасывается, отпечаток создаётся."""
|
|
(tmp_path / profile.SETTINGS_FILE).write_text(json.dumps({"user_agent": "First/1"}))
|
|
settings = profile.load_or_create(tmp_path)
|
|
assert set(settings) == {"fingerprint"}
|
|
|
|
|
|
def test_profiles_get_independent_fingerprints(tmp_path):
|
|
"""Отпечатки генерируются независимо: из выборки профилей получается больше одного варианта."""
|
|
variants = set()
|
|
for i in range(30):
|
|
d = tmp_path / str(i)
|
|
d.mkdir()
|
|
fp = profile.load_or_create(d)["fingerprint"]
|
|
variants.add((fp["screen"]["width"], fp["screen"]["height"], fp["device_scale_factor"]))
|
|
assert len(variants) > 1
|
|
|
|
|
|
def test_generate_falls_back_when_browserforge_fails(monkeypatch):
|
|
class Failing:
|
|
def generate(self, **kwargs):
|
|
raise ValueError("Cannot generate headers")
|
|
|
|
monkeypatch.setattr(profile, "FingerprintGenerator", Failing)
|
|
assert profile.generate_fingerprint(attempts=2) == {"screen": {"width": 1920, "height": 1080}, "device_scale_factor": 1}
|
|
|
|
|
|
def test_browser_kwargs_do_not_touch_user_agent():
|
|
kwargs = profile.browser_kwargs({"fingerprint": {"screen": {"width": 2560, "height": 1440}, "device_scale_factor": 1.25}})
|
|
assert kwargs == {"additional_args": {"screen": {"width": 2560, "height": 1440}, "device_scale_factor": 1.25}}
|