[wwwsearch-commits] r44827 - in wwwsearch/mechanize/trunk: mechanize test

jjlee at codespeak.net jjlee at codespeak.net
Sat Jul 7 18:39:31 CEST 2007


Author: jjlee
Date: Sat Jul  7 18:39:31 2007
New Revision: 44827

Modified:
   wwwsearch/mechanize/trunk/mechanize/_http.py
   wwwsearch/mechanize/trunk/test/test_urllib2.py
Log:
* Log skipped Refreshes
* Add some more Refresh tests


Modified: wwwsearch/mechanize/trunk/mechanize/_http.py
==============================================================================
--- wwwsearch/mechanize/trunk/mechanize/_http.py	(original)
+++ wwwsearch/mechanize/trunk/mechanize/_http.py	Sat Jul  7 18:39:31 2007
@@ -540,6 +540,7 @@
     def __init__(self, max_time=0, honor_time=True):
         self.max_time = max_time
         self.honor_time = honor_time
+        self._sleep = time.sleep
 
     def http_response(self, request, response):
         code, msg, hdrs = response.code, response.msg, response.info()
@@ -551,16 +552,19 @@
             except ValueError:
                 debug("bad Refresh header: %r" % refresh)
                 return response
+
             if newurl is None:
                 newurl = response.geturl()
             if (self.max_time is None) or (pause <= self.max_time):
                 if pause > 1E-3 and self.honor_time:
-                    time.sleep(pause)
+                    self._sleep(pause)
                 hdrs["location"] = newurl
                 # hardcoded http is NOT a bug
                 response = self.parent.error(
                     "http", request, response,
                     "refresh", msg, hdrs)
+            else:
+                debug("Refresh header ignored: %r" % refresh)
 
         return response
 

Modified: wwwsearch/mechanize/trunk/test/test_urllib2.py
==============================================================================
--- wwwsearch/mechanize/trunk/test/test_urllib2.py	(original)
+++ wwwsearch/mechanize/trunk/test/test_urllib2.py	Sat Jul  7 18:39:31 2007
@@ -919,6 +919,48 @@
                 self.assertEqual(o.proto, "http")
                 self.assertEqual(o.args, (req, r, "refresh", "OK", headers))
 
+    def test_refresh_honor_time(self):
+        class SleepTester:
+            def __init__(self, test, seconds):
+                self._test = test
+                if seconds is 0:
+                    seconds = None  # don't expect a sleep for 0 seconds
+                self._expected = seconds
+                self._got = None
+            def sleep(self, seconds):
+                self._got = seconds
+            def verify(self):
+                self._test.assertEqual(self._expected, self._got)
+        class Opener:
+            called = False
+            def error(self, *args, **kwds):
+                self.called = True
+        def test(rp, header, refresh_after):
+            expect_refresh = refresh_after is not None
+            opener = Opener()
+            rp.parent = opener
+            st = SleepTester(self, refresh_after)
+            rp._sleep = st.sleep
+            rp.http_response(Request("http://example.com"),
+                             test_response(headers=[("Refresh", header)]),
+                             )
+            self.assertEqual(expect_refresh, opener.called)
+            st.verify()
+
+        # by default, only zero-time refreshes are honoured
+        test(HTTPRefreshProcessor(), "0", 0)
+        test(HTTPRefreshProcessor(), "2", None)
+
+        # if requested, more than zero seconds are allowed
+        test(HTTPRefreshProcessor(max_time=None), "2", 2)
+        test(HTTPRefreshProcessor(max_time=30), "2", 2)
+
+        # no sleep if we don't "honor_time"
+        test(HTTPRefreshProcessor(max_time=30, honor_time=False), "2", 0)
+
+        # request for too-long wait before refreshing --> no refresh occurs
+        test(HTTPRefreshProcessor(max_time=30), "60", None)
+
     def test_redirect(self):
         from_url = "http://example.com/a.html"
         to_url = "http://example.com/b.html"


More information about the wwwsearch-commits mailing list