mirror of https://github.com/scrapy/scrapy.git
Added tests to open_in_browser() function
This commit is contained in:
parent
2ab94d75e2
commit
38a296aa2c
|
|
@ -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 = "<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()
|
||||
|
|
|
|||
|
|
@ -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 <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 body
|
||||
webbrowser.open("file://%s" % fname)
|
||||
|
|
|
|||
Loading…
Reference in New Issue