Skip to content

Commit c8fbccd

Browse files
committed
Stub the browser transport in the solver tests
The solver is only configured when websockets is installed too, so a test that fakes scraper.cdp alone now gets None. Fake the transport with it, and cover the case the check exists for.
1 parent c698e2b commit c8fbccd

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

‎tests/test_http.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ def install(self, monkeypatch, chromium, solver_class):
144144
sys.modules, "scraper.browsers", SimpleNamespace(pick_chromium=lambda: chromium)
145145
)
146146
monkeypatch.setitem(sys.modules, "scraper.cdp", SimpleNamespace(CdpSolver=solver_class))
147+
# The transport too: `scraper.cdp` imports without it and only needs it once it drives a
148+
# browser, so "this machine can run one" is not answered by the module alone.
149+
monkeypatch.setitem(sys.modules, "websockets", SimpleNamespace())
147150

148151
def test_a_machine_with_a_browser_gets_a_solver(self, monkeypatch):
149152
class Solver:
@@ -156,6 +159,31 @@ def __init__(self, **options):
156159
# Hidden first, a window only where one would be seen and only when it is needed.
157160
assert made.options == {"mode": "auto"}
158161

162+
def test_without_the_transport_it_is_absent(self, monkeypatch):
163+
import builtins
164+
import sys
165+
from types import SimpleNamespace
166+
167+
monkeypatch.setitem(
168+
sys.modules,
169+
"scraper.browsers",
170+
SimpleNamespace(pick_chromium=lambda: "/usr/bin/chromium"),
171+
)
172+
monkeypatch.setitem(sys.modules, "scraper.cdp", SimpleNamespace(CdpSolver=object))
173+
monkeypatch.delitem(sys.modules, "websockets", raising=False)
174+
175+
real = builtins.__import__
176+
177+
def refuse(name, *args, **kwargs):
178+
if name == "websockets":
179+
raise ImportError(name)
180+
return real(name, *args, **kwargs)
181+
182+
monkeypatch.setattr(builtins, "__import__", refuse)
183+
# Built anyway, the solver would raise on its first call, and a caller reading that as
184+
# "the host refused us" disables a site the browser never tried.
185+
assert solver() is None
186+
159187
def test_no_browser_installed_is_not_an_error(self, monkeypatch):
160188
self.install(monkeypatch, None, object)
161189
# A browser is heavy and most runs never need one, so absent must not refuse to fetch.

0 commit comments

Comments
 (0)