[wwwsearch-commits] r23245 - in wwwsearch/mechanize/trunk: . mechanize

jjlee at codespeak.net jjlee at codespeak.net
Sun Feb 12 03:27:29 CET 2006


Author: jjlee
Date: Sun Feb 12 03:27:27 2006
New Revision: 23245

Modified:
   wwwsearch/mechanize/trunk/functional_tests.py
   wwwsearch/mechanize/trunk/mechanize/_mechanize.py
   wwwsearch/mechanize/trunk/setup.py
   wwwsearch/mechanize/trunk/test.py
Log:
Remove temporary hack that worked around ClientCookie response bug by not .copy()ing responses; Add Browser.set_response(); Add some more functional tests; Depend on latest ClientCookie (fixed major response issue); Always refer to ._response as 'current' not 'last' in docstrings

Modified: wwwsearch/mechanize/trunk/functional_tests.py
==============================================================================
--- wwwsearch/mechanize/trunk/functional_tests.py	(original)
+++ wwwsearch/mechanize/trunk/functional_tests.py	Sun Feb 12 03:27:27 2006
@@ -26,6 +26,14 @@
         self.browser.open('/mechanize/')
         self.assertEqual(self.browser.title(), 'mechanize')
 
+    def test_reread(self):
+        r = self.browser.open('http://wwwsearch.sourceforge.net/')
+        data = r.read()
+        r.close()
+        r.seek(0)
+        self.assertEqual(r.read(), data)
+        self.assertEqual(self.browser.response().read(), data)
+
     def test_error_recovery(self):
         self.assertRaises(OSError, self.browser.open,
                           'file:///c|thisnoexistyiufheiurgbueirgbue')
@@ -44,6 +52,34 @@
 
 class ResponseTests(TestCase):
 
+    def test_seek(self):
+        from mechanize import Browser
+        br = Browser()
+        r = br.open("http://wwwsearch.sourceforge.net/")
+        html = r.read()
+        r.seek(0)
+        self.assertEqual(r.read(), html)
+
+    def test_set_response(self):
+        from mechanize import Browser
+        br = Browser()
+        r = br.open("http://wwwsearch.sourceforge.net/")
+        html = r.read()
+        self.assertEqual(br.title(), "Python bits")
+
+        newhtml = """<html><body><a href="spam">click me</a></body></html>"""
+
+        r.set_data(newhtml)
+        self.assertEqual(r.read(), newhtml)
+        self.assertEqual(br.response().read(), html)
+        br.response().set_data(newhtml)
+        self.assertEqual(br.response().read(), html)
+        self.assertEqual(br.links()[0].url, 'http://sourceforge.net')
+
+        br.set_response(r)
+        self.assertEqual(br.response().read(), newhtml)
+        self.assertEqual(br.links()[0].url, "spam")
+
     def test_close_pickle_load(self):
         print ("Test test_close_pickle_load is expected to fail unless Python "
                "standard library patch http://python.org/sf/1144636 has been "

Modified: wwwsearch/mechanize/trunk/mechanize/_mechanize.py
==============================================================================
--- wwwsearch/mechanize/trunk/mechanize/_mechanize.py	(original)
+++ wwwsearch/mechanize/trunk/mechanize/_mechanize.py	Sun Feb 12 03:27:27 2006
@@ -470,7 +470,7 @@
 
     Public attributes:
 
-    request: last request (ClientCookie.Request or urllib2.Request)
+    request: current request (ClientCookie.Request or urllib2.Request)
     form: currently selected form (see .select_form())
     default_encoding: character encoding used if no encoding is found in the
      response (you should turn on HTTP-EQUIV handling if you want the best
@@ -595,13 +595,23 @@
         self._parse_html(self._response)
         if not success:
             raise error
-        #return copy.copy(self._response)
-        return self._response
+        return copy.copy(self._response)
 
     def response(self):
-        """Return last response (as return value of urllib2.urlopen())."""
-        #return copy.copy(self._response)
-        return self._response
+        """Return a copy of the current response.
+
+        The returned object has the same interface as the object returned by
+        .open() (or urllib2.urlopen()).
+
+        """
+        return copy.copy(self._response)
+
+    def set_response(self, response):
+        """Replace current response with response."""
+        if not hasattr(response, "seek"):
+            response = response_seek_wrapper(self._response)
+        self._response = response
+        self._parse_html(self._response)
 
     def geturl(self):
         """Get URL of current document."""

Modified: wwwsearch/mechanize/trunk/setup.py
==============================================================================
--- wwwsearch/mechanize/trunk/setup.py	(original)
+++ wwwsearch/mechanize/trunk/setup.py	Sun Feb 12 03:27:27 2006
@@ -41,7 +41,7 @@
 VERSION = "0.0.12a"
 INSTALL_REQUIRES = [
     "ClientForm>=0.2.2.dev_r22910, ==dev",
-    "ClientCookie>=1.2.0.dev_r22696, ==dev",
+    "ClientCookie>=1.2.0.dev_r23242, ==dev",
     "pullparser>=0.0.8.dev_r21645, ==dev"]
 NAME = "mechanize"
 PACKAGE = True

Modified: wwwsearch/mechanize/trunk/test.py
==============================================================================
--- wwwsearch/mechanize/trunk/test.py	(original)
+++ wwwsearch/mechanize/trunk/test.py	Sun Feb 12 03:27:27 2006
@@ -554,6 +554,36 @@
             self.assertEqual([link.absolute_url for link in b.links()], urls)
 
 
+class ResponseTests(TestCase):
+    def test_set_response(self):
+        from ClientCookie import response_seek_wrapper
+
+        br = TestBrowser()
+        url = "http://example.com/"
+        html = """<html><body><a href="spam">click me</a></body></html>"""
+        headers = {"content-type": "text/html"}
+        r = response_seek_wrapper(MockResponse(url, html, headers))
+        br.add_handler(MockHandler([("http_open", r)]))
+
+        r = br.open(url)
+        self.assertEqual(r.read(), html)
+        r.seek(0)
+        self.assertEqual(br.links()[0].url, "spam")
+
+        newhtml = """<html><body><a href="eggs">click me</a></body></html>"""
+
+        r.set_data(newhtml)
+        self.assertEqual(r.read(), newhtml)
+        self.assertEqual(br.response().read(), html)
+        br.response().set_data(newhtml)
+        self.assertEqual(br.response().read(), html)
+        self.assertEqual(br.links()[0].url, "spam")
+
+        br.set_response(r)
+        self.assertEqual(br.response().read(), newhtml)
+        self.assertEqual(br.links()[0].url, "eggs")
+
+
 class UserAgentTests(TestCase):
     def test_set_handled_schemes(self):
         import mechanize


More information about the wwwsearch-commits mailing list