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

jjlee at codespeak.net jjlee at codespeak.net
Wed Dec 20 02:25:53 CET 2006


Author: jjlee
Date: Wed Dec 20 02:25:50 2006
New Revision: 35909

Modified:
   wwwsearch/mechanize/trunk/mechanize/_html.py
   wwwsearch/mechanize/trunk/mechanize/_response.py
   wwwsearch/mechanize/trunk/test/test_html.py
Log:
Base tag without href attribute would override document URL with a None value, causing a crash (bignate at gmail.com)

Modified: wwwsearch/mechanize/trunk/mechanize/_html.py
==============================================================================
--- wwwsearch/mechanize/trunk/mechanize/_html.py	(original)
+++ wwwsearch/mechanize/trunk/mechanize/_html.py	Wed Dec 20 02:25:50 2006
@@ -135,7 +135,9 @@
             if token.type == "endtag":
                 continue
             if token.data == "base":
-                base_url = dict(token.attrs).get("href")
+                base_href = dict(token.attrs).get("href")
+                if base_href is not None:
+                    base_url = base_href
                 continue
             attrs = dict(token.attrs)
             tag = token.data
@@ -353,7 +355,9 @@
                 attrs = bs.unescape_attrs(link.attrs)
                 attrs_dict = dict(attrs)
                 if link.name == "base":
-                    base_url = attrs_dict.get("href")
+                    base_href = attrs_dict.get("href")
+                    if base_href is not None:
+                        base_url = base_href
                     continue
                 url_attr = self.urltags[link.name]
                 url = attrs_dict.get(url_attr)

Modified: wwwsearch/mechanize/trunk/mechanize/_response.py
==============================================================================
--- wwwsearch/mechanize/trunk/mechanize/_response.py	(original)
+++ wwwsearch/mechanize/trunk/mechanize/_response.py	Wed Dec 20 02:25:50 2006
@@ -362,6 +362,11 @@
                   url="http://example.com/", code=200, msg="OK"):
     return make_response(data, headers, url, code, msg)
 
+def test_html_response(data='test data', headers=[],
+                       url="http://example.com/", code=200, msg="OK"):
+    headers += [("Content-type", "text/html")]
+    return make_response(data, headers, url, code, msg)
+
 def make_response(data, headers, url, code, msg):
     """Convenient factory for objects implementing response interface.
 

Modified: wwwsearch/mechanize/trunk/test/test_html.py
==============================================================================
--- wwwsearch/mechanize/trunk/test/test_html.py	(original)
+++ wwwsearch/mechanize/trunk/test/test_html.py	Wed Dec 20 02:25:50 2006
@@ -3,6 +3,7 @@
 from unittest import TestCase
 
 import mechanize
+from mechanize._response import test_html_response
 
 
 class RegressionTests(TestCase):
@@ -10,11 +11,19 @@
     def test_close_base_tag(self):
         # any document containing a </base> tag used to cause an exception
         br = mechanize.Browser()
-        response = mechanize.make_response(
-            "</base>", [("Content-type", "text/html")], "", 200, "OK")
+        response = test_html_response("</base>")
         br.set_response(response)
         list(br.links())
 
+    def test_bad_base_tag(self):
+        # a document with a base tag with no href used to cause an exception
+        for factory in [mechanize.DefaultFactory(), mechanize.RobustFactory()]:
+            br = mechanize.Browser(factory=factory)
+            response = test_html_response(
+                "<BASE TARGET='_main'><a href='http://example.com/'>eg</a>")
+            br.set_response(response)
+            list(br.links())
+
 
 class CachingGeneratorFunctionTests(TestCase):
 


More information about the wwwsearch-commits mailing list