diff --git a/scrapy/utils/response.py b/scrapy/utils/response.py
index 8b2417bfb..abb5f6a70 100644
--- a/scrapy/utils/response.py
+++ b/scrapy/utils/response.py
@@ -99,7 +99,7 @@ def open_in_browser(
if isinstance(response, HtmlResponse):
if b"
Hello world.
""", - ) - r2 = HtmlResponse( - url, - body=b""" + id="Simple", + ), + pytest.param( + b"""Hello world.
""", - ) - r4 = HtmlResponse( - url, - body=b""" + id="Misleading tag", + ), + pytest.param( + b"""Hello world.
""", - ) - r5 = HtmlResponse( - url, - body=b""" + id="Misleading comment", + ), + pytest.param( + b"""Hello world.
""", - ) + id="Conditional comment", + ), + ], +) +def test_inject_base_url(body: bytes) -> None: + url = "http://www.example.com" - assert open_in_browser(r1, _openfunc=check_base_url), "Inject base url" - assert open_in_browser(r2, _openfunc=check_base_url), ( - "Inject base url with argumented head" - ) - assert open_in_browser(r3, _openfunc=check_base_url), ( - "Inject unique base url with misleading tag" - ) - assert open_in_browser(r4, _openfunc=check_base_url), ( - "Inject unique base url with misleading comment" - ) - assert open_in_browser(r5, _openfunc=check_base_url), ( - "Inject unique base url with conditional comment" - ) + def check_base_url(burl): + path = urlparse(burl).path + if not path or not Path(path).exists(): + path = burl.replace("file://", "") + bbody = Path(path).read_bytes() + assert bbody.count(b'>