from pathlib import Path
from time import process_time
from urllib.parse import urlparse
import pytest
from scrapy.http import HtmlResponse, Response, TextResponse
from scrapy.utils.python import to_bytes
from scrapy.utils.response import (
_remove_html_comments,
get_base_url,
get_meta_refresh,
open_in_browser,
response_status_message,
)
def _read_browser_output(burl: str):
path = urlparse(burl).path
if not path or not Path(path).exists():
path = burl.replace("file://", "")
return Path(path).read_bytes()
def test_open_in_browser():
url = "http://www.example.com/some/page.html"
body = (
b"
test page test body "
)
def browser_open(burl: str) -> bool:
bbody = _read_browser_output(burl)
assert b'' in bbody
return True
response = HtmlResponse(url, body=body)
assert open_in_browser(response, _openfunc=browser_open), "Browser not called"
resp = Response(url, body=body)
with pytest.raises(TypeError):
open_in_browser(resp, _openfunc=browser_open) # type: ignore[arg-type]
def test_get_meta_refresh():
r1 = HtmlResponse(
"http://www.example.com",
body=b"""
Dummy
blahablsdfsal&
""",
)
r2 = HtmlResponse(
"http://www.example.com",
body=b"""
Dummy
blahablsdfsal&
""",
)
r3 = HtmlResponse(
"http://www.example.com",
body=b"""