[wwwsearch-commits] r44829 - in wwwsearch/mechanize/trunk: . test-tools
jjlee at codespeak.net
jjlee at codespeak.net
Sat Jul 7 19:09:27 CEST 2007
Author: jjlee
Date: Sat Jul 7 19:09:27 2007
New Revision: 44829
Modified:
wwwsearch/mechanize/trunk/functional_tests.py
wwwsearch/mechanize/trunk/test-tools/cookietest.cgi
wwwsearch/mechanize/trunk/test-tools/testprogram.py
wwwsearch/mechanize/trunk/test-tools/twisted-localserver.py
Log:
* Add a functional test for Refresh
* Update docstrings / comments re local functional testing server
Modified: wwwsearch/mechanize/trunk/functional_tests.py
==============================================================================
--- wwwsearch/mechanize/trunk/functional_tests.py (original)
+++ wwwsearch/mechanize/trunk/functional_tests.py Sat Jul 7 19:09:27 2007
@@ -4,7 +4,7 @@
# thanks Moof (aka Giles Antonio Radford) for some of these
-import os, sys
+import os, sys, urllib
from unittest import TestCase
import mechanize
@@ -15,13 +15,6 @@
HTTPRedirectDebugProcessor, HTTPResponseDebugProcessor
from mechanize._rfc3986 import urljoin
-# XXX
-# document twisted.web2 install (I forgot how I did it -- reinstall!)
-# implement remaining stuff used by functional_tests.py
-# in twisted-localserver.py:
-# - 302 followed by 404 response
-# - helper cgi script for cookies &c.
-
#from cookielib import CookieJar
#from urllib2 import build_opener, install_opener, urlopen
#from urllib2 import HTTPCookieProcessor, HTTPHandler
@@ -90,6 +83,26 @@
self.assertEqual(r.code, 200)
self.assert_("GeneralFAQ.html" in r.read(2048))
+ def test_refresh(self):
+ def refresh_request(seconds):
+ uri = urljoin(self.uri, "/cgi-bin/cookietest.cgi")
+ val = urllib.quote_plus('%d; url="%s"' % (seconds, self.uri))
+ return uri + ("?refresh=%s" % val)
+ r = self.browser.open(refresh_request(5))
+ self.assertEqual(r.geturl(), self.uri)
+ # Refresh with pause > 30 seconds is ignored by default (these long
+ # refreshes tend to be there only because the website owner wants you
+ # to see the latest news, or whatever -- they're not essential to the
+ # operation of the site, and not really useful or appropriate when
+ # scraping).
+ refresh_uri = refresh_request(60)
+ r = self.browser.open(refresh_uri)
+ self.assertEqual(r.geturl(), refresh_uri)
+ # allow long refreshes (note we don't actually wait 60 seconds by default)
+ self.browser.set_handle_refresh(True, max_time=None)
+ r = self.browser.open(refresh_request(60))
+ self.assertEqual(r.geturl(), self.uri)
+
def test_file_url(self):
url = "file://%s" % sanepathname2url(
os.path.abspath('functional_tests.py'))
@@ -409,9 +422,8 @@
- start a local Twisted HTTP server and run the functional
tests against that, rather than against SourceForge
(quicker!)
- Note not all the functional tests use the local server yet
- -- some currently always access the internet regardless of
- this option and the --uri option.
+ If this option doesn't work on Windows/Mac, somebody please
+ tell me about it, or I'll never find out...
"""
prog = testprogram.TestProgram(
["functional_tests"],
Modified: wwwsearch/mechanize/trunk/test-tools/cookietest.cgi
==============================================================================
--- wwwsearch/mechanize/trunk/test-tools/cookietest.cgi (original)
+++ wwwsearch/mechanize/trunk/test-tools/cookietest.cgi Sat Jul 7 19:09:27 2007
@@ -3,9 +3,12 @@
# This is used by functional_tests.py
+#import cgitb; cgitb.enable()
+
print "Content-Type: text/html"
print "Set-Cookie: foo=bar\n"
-import sys, os, string, cgi, Cookie
+import sys, os, string, cgi, Cookie, urllib
+from xml.sax import saxutils
from types import ListType
@@ -13,8 +16,18 @@
cookie = Cookie.SimpleCookie()
cookieHdr = os.environ.get("HTTP_COOKIE", "")
cookie.load(cookieHdr)
-if not cookie.has_key("foo"):
+form = cgi.FieldStorage()
+refresh_value = None
+if form.has_key("refresh"):
+ refresh = form["refresh"]
+ if not isinstance(refresh, ListType):
+ refresh_value = refresh.value
+if refresh_value is not None:
+ print '<meta http-equiv="refresh" content=%s>' % (
+ saxutils.quoteattr(urllib.unquote_plus(refresh_value)))
+elif not cookie.has_key("foo"):
print '<meta http-equiv="refresh" content="5">'
+
print "</head>"
print "<p>Received cookies:</p>"
print "<pre>"
@@ -26,7 +39,6 @@
print "<pre>"
print cgi.escape(os.environ.get("HTTP_REFERER", ""))
print "</pre>"
-form = cgi.FieldStorage()
print "<p>Received parameters:</p>"
print "<pre>"
for k in form.keys():
Modified: wwwsearch/mechanize/trunk/test-tools/testprogram.py
==============================================================================
--- wwwsearch/mechanize/trunk/test-tools/testprogram.py (original)
+++ wwwsearch/mechanize/trunk/test-tools/testprogram.py Sat Jul 7 19:09:27 2007
@@ -210,7 +210,7 @@
"""
def __init__(self, moduleNames, localServerProcess, defaultTest=None,
argv=None, testRunner=None, testLoader=defaultTestLoader,
- defaultUri="http://wwwsearch.sf.net/",
+ defaultUri="http://wwwsearch.sourceforge.net/",
usageExamples=USAGE_EXAMPLES,
):
self.modules = []
Modified: wwwsearch/mechanize/trunk/test-tools/twisted-localserver.py
==============================================================================
--- wwwsearch/mechanize/trunk/test-tools/twisted-localserver.py (original)
+++ wwwsearch/mechanize/trunk/test-tools/twisted-localserver.py Sat Jul 7 19:09:27 2007
@@ -9,9 +9,9 @@
python test-tools/twisted-localserver.py 8042
python functional_tests.py --uri=http://localhost:8042/
-You need Twisted XXX version to run it:
+You need twisted.web2 to run it. On ubuntu feisty, you can install it like so:
-XXX installation instructions
+sudo apt-get install python-twisted-web2
"""
import sys, re
More information about the wwwsearch-commits
mailing list