mirror of https://github.com/scrapy/scrapy.git
Fix the substitution pattern in open_in_browser(). (#7459)
* Fix the substitution pattern in open_in_browser(). * Formatting.
This commit is contained in:
parent
4e25686b20
commit
fc4c57e795
|
|
@ -99,7 +99,7 @@ def open_in_browser(
|
||||||
if isinstance(response, HtmlResponse):
|
if isinstance(response, HtmlResponse):
|
||||||
if b"<base" not in body:
|
if b"<base" not in body:
|
||||||
_remove_html_comments(body)
|
_remove_html_comments(body)
|
||||||
repl = rf'\0<base href="{response.url}">'
|
repl = rf'\g<0><base href="{response.url}">'
|
||||||
body = re.sub(rb"<head(?:[^<>]*?>)", to_bytes(repl), body, count=1)
|
body = re.sub(rb"<head(?:[^<>]*?>)", to_bytes(repl), body, count=1)
|
||||||
ext = ".html"
|
ext = ".html"
|
||||||
elif isinstance(response, TextResponse):
|
elif isinstance(response, TextResponse):
|
||||||
|
|
|
||||||
|
|
@ -111,36 +111,27 @@ def test_response_status_message():
|
||||||
assert response_status_message(573) == "573 Unknown Status"
|
assert response_status_message(573) == "573 Unknown Status"
|
||||||
|
|
||||||
|
|
||||||
def test_inject_base_url():
|
@pytest.mark.parametrize(
|
||||||
url = "http://www.example.com"
|
"body",
|
||||||
|
[
|
||||||
def check_base_url(burl):
|
pytest.param(
|
||||||
path = urlparse(burl).path
|
b"""
|
||||||
if not path or not Path(path).exists():
|
|
||||||
path = burl.replace("file://", "")
|
|
||||||
bbody = Path(path).read_bytes()
|
|
||||||
assert bbody.count(b'<base href="' + to_bytes(url) + b'">') == 1
|
|
||||||
return True
|
|
||||||
|
|
||||||
r1 = HtmlResponse(
|
|
||||||
url,
|
|
||||||
body=b"""
|
|
||||||
<html>
|
<html>
|
||||||
<head><title>Dummy</title></head>
|
<head><title>Dummy</title></head>
|
||||||
<body><p>Hello world.</p></body>
|
<body><p>Hello world.</p></body>
|
||||||
</html>""",
|
</html>""",
|
||||||
)
|
id="Simple",
|
||||||
r2 = HtmlResponse(
|
),
|
||||||
url,
|
pytest.param(
|
||||||
body=b"""
|
b"""
|
||||||
<html>
|
<html>
|
||||||
<head id="foo"><title>Dummy</title></head>
|
<head id="foo"><title>Dummy</title></head>
|
||||||
<body>Hello world.</body>
|
<body>Hello world.</body>
|
||||||
</html>""",
|
</html>""",
|
||||||
)
|
id="<head> with attrs",
|
||||||
r3 = HtmlResponse(
|
),
|
||||||
url,
|
pytest.param(
|
||||||
body=b"""
|
b"""
|
||||||
<html>
|
<html>
|
||||||
<head><title>Dummy</title></head>
|
<head><title>Dummy</title></head>
|
||||||
<body>
|
<body>
|
||||||
|
|
@ -148,19 +139,19 @@ def test_inject_base_url():
|
||||||
<p>Hello world.</p>
|
<p>Hello world.</p>
|
||||||
</body>
|
</body>
|
||||||
</html>""",
|
</html>""",
|
||||||
)
|
id="Misleading tag",
|
||||||
r4 = HtmlResponse(
|
),
|
||||||
url,
|
pytest.param(
|
||||||
body=b"""
|
b"""
|
||||||
<html>
|
<html>
|
||||||
<!-- <head>Dummy comment</head> -->
|
<!-- <head>Dummy comment</head> -->
|
||||||
<head><title>Dummy</title></head>
|
<head><title>Dummy</title></head>
|
||||||
<body><p>Hello world.</p></body>
|
<body><p>Hello world.</p></body>
|
||||||
</html>""",
|
</html>""",
|
||||||
)
|
id="Misleading comment",
|
||||||
r5 = HtmlResponse(
|
),
|
||||||
url,
|
pytest.param(
|
||||||
body=b"""
|
b"""
|
||||||
<html>
|
<html>
|
||||||
<!--[if IE]>
|
<!--[if IE]>
|
||||||
<head><title>IE head</title></head>
|
<head><title>IE head</title></head>
|
||||||
|
|
@ -170,21 +161,24 @@ def test_inject_base_url():
|
||||||
<!--<![endif]-->
|
<!--<![endif]-->
|
||||||
<body><p>Hello world.</p></body>
|
<body><p>Hello world.</p></body>
|
||||||
</html>""",
|
</html>""",
|
||||||
)
|
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"
|
def check_base_url(burl):
|
||||||
assert open_in_browser(r2, _openfunc=check_base_url), (
|
path = urlparse(burl).path
|
||||||
"Inject base url with argumented head"
|
if not path or not Path(path).exists():
|
||||||
)
|
path = burl.replace("file://", "")
|
||||||
assert open_in_browser(r3, _openfunc=check_base_url), (
|
bbody = Path(path).read_bytes()
|
||||||
"Inject unique base url with misleading tag"
|
assert bbody.count(b'><base href="' + to_bytes(url) + b'">') == 1
|
||||||
)
|
assert b"<head" in bbody
|
||||||
assert open_in_browser(r4, _openfunc=check_base_url), (
|
return True
|
||||||
"Inject unique base url with misleading comment"
|
|
||||||
)
|
resp = HtmlResponse(url, body=body)
|
||||||
assert open_in_browser(r5, _openfunc=check_base_url), (
|
assert open_in_browser(resp, _openfunc=check_base_url)
|
||||||
"Inject unique base url with conditional comment"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_open_in_browser_redos_comment():
|
def test_open_in_browser_redos_comment():
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue