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

jjlee at codespeak.net jjlee at codespeak.net
Sat Feb 23 20:49:29 CET 2008


Author: jjlee
Date: Sat Feb 23 20:49:27 2008
New Revision: 51828

Modified:
   wwwsearch/mechanize/trunk/mechanize/_mozillacookiejar.py
   wwwsearch/mechanize/trunk/test/test_cookies.py
Log:
 * Handle cookies containing embedded tabs in mozilla format files
 * Remove an assertion about mozilla format cookies file contents  (raise LoadError instead)

Modified: wwwsearch/mechanize/trunk/mechanize/_mozillacookiejar.py
==============================================================================
--- wwwsearch/mechanize/trunk/mechanize/_mozillacookiejar.py	(original)
+++ wwwsearch/mechanize/trunk/mechanize/_mozillacookiejar.py	Sat Feb 23 20:49:27 2008
@@ -80,7 +80,7 @@
                     continue
 
                 domain, domain_specified, path, secure, expires, name, value = \
-                        line.split("\t")
+                    line.split("\t", 6)
                 secure = (secure == "TRUE")
                 domain_specified = (domain_specified == "TRUE")
                 if name == "":
@@ -88,7 +88,9 @@
                     value = None
 
                 initial_dot = domain.startswith(".")
-                assert domain_specified == initial_dot
+                if domain_specified != initial_dot:
+                    raise LoadError("domain and domain specified flag don't "
+                                    "match in %s: %s" % (filename, line))
 
                 discard = False
                 if expires == "":
@@ -113,9 +115,9 @@
                 self.set_cookie(c)
 
         except:
-            reraise_unmasked_exceptions((IOError,))
+            reraise_unmasked_exceptions((IOError, LoadError))
             raise LoadError("invalid Netscape format file %s: %s" %
-                          (filename, line))
+                            (filename, line))
 
     def save(self, filename=None, ignore_discard=False, ignore_expires=False):
         if filename is None:

Modified: wwwsearch/mechanize/trunk/test/test_cookies.py
==============================================================================
--- wwwsearch/mechanize/trunk/test/test_cookies.py	(original)
+++ wwwsearch/mechanize/trunk/test/test_cookies.py	Sat Feb 23 20:49:27 2008
@@ -1,6 +1,6 @@
 """Tests for _ClientCookie."""
 
-import urllib2, re, os, StringIO, mimetools, time
+import urllib2, re, os, StringIO, mimetools, time, tempfile, errno
 from time import localtime
 from unittest import TestCase
 
@@ -1344,6 +1344,46 @@
         assert len(new_c) == 4  # 2 of them discarded on save
         assert repr(new_c).find("name='foo1', value='bar'") != -1
 
+    def test_mozilla_cookiejar_embedded_tab(self):
+        from mechanize import MozillaCookieJar
+        filename = tempfile.mktemp()
+        fh = open(filename, "w")
+        try:
+            fh.write(
+                MozillaCookieJar.header + "\n" +
+                "a.com\tFALSE\t/\tFALSE\t\tname\tval\tstillthevalue\n"
+                "a.com\tFALSE\t/\tFALSE\t\tname2\tvalue\n")
+            fh.close()
+            cj = MozillaCookieJar(filename)
+            cj.revert(ignore_discard=True)
+            cookies = cj._cookies["a.com"]["/"]
+            self.assertEquals(cookies["name"].value, "val\tstillthevalue")
+            self.assertEquals(cookies["name2"].value, "value")
+        finally:
+            try:
+                os.remove(filename)
+            except OSError, exc:
+                if exc.errno != errno.EEXIST:
+                    raise
+
+    def test_mozilla_cookiejar_initial_dot_violation(self):
+        from mechanize import MozillaCookieJar, LoadError
+        filename = tempfile.mktemp()
+        fh = open(filename, "w")
+        try:
+            fh.write(
+                MozillaCookieJar.header + "\n" +
+                ".a.com\tFALSE\t/\tFALSE\t\tname\tvalue\n")
+            fh.close()
+            cj = MozillaCookieJar(filename)
+            self.assertRaises(LoadError, cj.revert, ignore_discard=True)
+        finally:
+            try:
+                os.remove(filename)
+            except OSError, exc:
+                if exc.errno != errno.EEXIST:
+                    raise
+
     def test_netscape_misc(self):
         # Some additional Netscape cookies tests.
         from mechanize import CookieJar, Request


More information about the wwwsearch-commits mailing list