mirror of
https://github.com/scrapy/scrapy.git
synced 2025-02-24 17:04:00 +00:00
better implementation of open_in_browser() tests
This commit is contained in:
parent
38a296aa2c
commit
54ae2c36d0
@ -1,4 +1,5 @@
|
||||
import unittest
|
||||
import urlparse
|
||||
|
||||
from scrapy.xlib.BeautifulSoup import BeautifulSoup
|
||||
from scrapy.http import Response, TextResponse, HtmlResponse
|
||||
@ -134,10 +135,13 @@ class ResponseUtilsTest(unittest.TestCase):
|
||||
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>"
|
||||
def browser_open(burl):
|
||||
bbody = open(urlparse.urlparse(burl).path).read()
|
||||
assert '<base href="%s">' % url in bbody, "<base> tag not added"
|
||||
return True
|
||||
response = HtmlResponse(url, body=body)
|
||||
newbody = open_in_browser(response, debug=True)
|
||||
assert '<base href="%s">' % url in newbody
|
||||
|
||||
assert open_in_browser(response, _openfunc=browser_open), \
|
||||
"Browser not called"
|
||||
self.assertRaises(TypeError, open_in_browser, Response(url, body=body), \
|
||||
debug=True)
|
||||
|
||||
|
@ -92,7 +92,7 @@ def response_httprepr(response):
|
||||
s += response.body
|
||||
return s
|
||||
|
||||
def open_in_browser(response, debug=False):
|
||||
def open_in_browser(response, _openfunc=webbrowser.open):
|
||||
"""Open the given response in a local web browser, populating the <base>
|
||||
tag for external links to work
|
||||
"""
|
||||
@ -106,8 +106,4 @@ def open_in_browser(response, debug=False):
|
||||
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)
|
||||
return _openfunc("file://%s" % fname)
|
||||
|
Loading…
x
Reference in New Issue
Block a user