Scrapy, a fast high-level web crawling & scraping framework for Python.
Added tests to open_in_browser() function
Pablo Hoffman38a296aa2c04
diff --git a/scrapy/tests/test_utils_response.py b/scrapy/tests/test_utils_response.pyindex 9281f4f40..97d443ef3 100644--- a/scrapy/tests/test_utils_response.py+++ b/scrapy/tests/test_utils_response.py@@ -1,9 +1,9 @@import unittestfrom scrapy.xlib.BeautifulSoup import BeautifulSoup-from scrapy.http import Response, TextResponse+from scrapy.http import Response, TextResponse, HtmlResponsefrom scrapy.utils.response import body_or_str, get_base_url, get_meta_refresh, \- response_httprepr, get_cached_beautifulsoup+ response_httprepr, get_cached_beautifulsoup, open_in_browserclass ResponseUtilsTest(unittest.TestCase):dummy_response = TextResponse(url='http://example.org/', body='dummy_response')@@ -131,5 +131,15 @@ class ResponseUtilsTest(unittest.TestCase):assert soup1 is soup2assert soup1 is not soup3+ def test_open_in_browser(self):+ url = "http:///www.example.com/some/page.html"+ body = "<html> <head> <title>test page</title> </head> <body>test body</body> </html>"+ response = HtmlResponse(url, body=body)+ newbody = open_in_browser(response, debug=True)+ assert '<base href="%s">' % url in newbody++ self.assertRaises(TypeError, open_in_browser, Response(url, body=body), \+ debug=True)+if __name__ == "__main__":unittest.main()diff --git a/scrapy/utils/response.py b/scrapy/utils/response.pyindex 6ed6f43a3..66aacacc5 100644--- a/scrapy/utils/response.py+++ b/scrapy/utils/response.py@@ -92,7 +92,7 @@ def response_httprepr(response):s += response.bodyreturn s-def open_in_browser(response):+def open_in_browser(response, debug=False):"""Open the given response in a local web browser, populating the <base>tag for external links to work"""@@ -106,4 +106,8 @@ def open_in_browser(response):fd, fname = tempfile.mkstemp('.html')os.write(fd, body)os.close(fd)+ if debug: # for testing purposes only+ body = open(fname).read()+ os.remove(fname)+ return bodywebbrowser.open("file://%s" % fname)