diff --git a/scrapy/tests/test_utils_response.py b/scrapy/tests/test_utils_response.py
index 9281f4f40..97d443ef3 100644
--- a/scrapy/tests/test_utils_response.py
+++ b/scrapy/tests/test_utils_response.py
@@ -1,9 +1,9 @@
import unittest
from scrapy.xlib.BeautifulSoup import BeautifulSoup
-from scrapy.http import Response, TextResponse
+from scrapy.http import Response, TextResponse, HtmlResponse
from 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_browser
class ResponseUtilsTest(unittest.TestCase):
dummy_response = TextResponse(url='http://example.org/', body='dummy_response')
@@ -131,5 +131,15 @@ class ResponseUtilsTest(unittest.TestCase):
assert soup1 is soup2
assert soup1 is not soup3
+ def test_open_in_browser(self):
+ url = "http:///www.example.com/some/page.html"
+ body = "
test page test body "
+ response = HtmlResponse(url, body=body)
+ newbody = open_in_browser(response, debug=True)
+ assert '' % 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.py
index 6ed6f43a3..66aacacc5 100644
--- a/scrapy/utils/response.py
+++ b/scrapy/utils/response.py
@@ -92,7 +92,7 @@ def response_httprepr(response):
s += response.body
return s
-def open_in_browser(response):
+def open_in_browser(response, debug=False):
"""Open the given response in a local web browser, populating the
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 body
webbrowser.open("file://%s" % fname)