Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Test coverage for open_in_browser base tag injection (#5319)

Samuel Marchal75ed765476a2
diff --git a/tests/test_utils_response.py b/tests/test_utils_response.py
index d6f4c0bb5..0a09f6109 100644
--- a/tests/test_utils_response.py
+++ b/tests/test_utils_response.py
@@ -83,3 +83,56 @@ class ResponseUtilsTest(unittest.TestCase):
self.assertEqual(response_status_message(200), '200 OK')
self.assertEqual(response_status_message(404), '404 Not Found')
self.assertEqual(response_status_message(573), "573 Unknown Status")
+
+ def test_inject_base_url(self):
+ url = "http://www.example.com"
+
+ def check_base_url(burl):
+ path = urlparse(burl).path
+ if not os.path.exists(path):
+ path = burl.replace('file://', '')
+ with open(path, "rb") as f:
+ bbody = f.read()
+ self.assertEqual(bbody.count(b'<base href="' + to_bytes(url) + b'">'), 1)
+ return True
+
+ r1 = HtmlResponse(url, body=b"""
+ <html>
+ <head><title>Dummy</title></head>
+ <body><p>Hello world.</p></body>
+ </html>""")
+ r2 = HtmlResponse(url, body=b"""
+ <html>
+ <head id="foo"><title>Dummy</title></head>
+ <body>Hello world.</body>
+ </html>""")
+ r3 = HtmlResponse(url, body=b"""
+ <html>
+ <head><title>Dummy</title></head>
+ <body>
+ <header>Hello header</header>
+ <p>Hello world.</p>
+ </body>
+ </html>""")
+ r4 = HtmlResponse(url, body=b"""
+ <html>
+ <!-- <head>Dummy comment</head> -->
+ <head><title>Dummy</title></head>
+ <body><p>Hello world.</p></body>
+ </html>""")
+ r5 = HtmlResponse(url, body=b"""
+ <html>
+ <!--[if IE]>
+ <head><title>IE head</title></head>
+ <![endif]-->
+ <!--[if !IE]>-->
+ <head><title>Standard head</title></head>
+ <!--<![endif]-->
+ <body><p>Hello world.</p></body>
+ </html>""")
+
+ 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"