[Jython-checkins] jython: Import from cpython 2.6:

frank.wierzbicki jython-checkins at python.org
Mon Oct 10 03:36:29 CEST 2011


http://hg.python.org/jython/rev/f37130c7dfbd
changeset:   6251:f37130c7dfbd
user:        Frank Wierzbicki <fwierzbicki at gmail.com>
date:        Sun Oct 09 18:34:45 2011 -0700
summary:
  Import from cpython 2.6:
Lib/test/test_urllib.py at 71579:b9a95ce2692c

files:
  Lib/test/test_urllib.py |  211 +++++++++++++++++++++++++--
  1 files changed, 195 insertions(+), 16 deletions(-)


diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
--- a/Lib/test/test_urllib.py
+++ b/Lib/test/test_urllib.py
@@ -43,7 +43,7 @@
     def test_interface(self):
         # Make sure object returned by urlopen() has the specified methods
         for attr in ("read", "readline", "readlines", "fileno",
-                     "close", "info", "geturl", "__iter__"):
+                     "close", "info", "geturl", "getcode", "__iter__"):
             self.assert_(hasattr(self.returned_obj, attr),
                          "object returned by urlopen() lacks %s attribute" %
                          attr)
@@ -66,10 +66,8 @@
 
     def test_fileno(self):
         file_num = self.returned_obj.fileno()
-
-        # JYTHON - does not apply, since fileno's are not int's
-        #self.assert_(isinstance(file_num, int),
-        #             "fileno() did not return an int")
+        self.assert_(isinstance(file_num, int),
+                     "fileno() did not return an int")
         self.assertEqual(os.read(file_num, len(self.text)), self.text,
                          "Reading on the file descriptor returned by fileno() "
                          "did not return the expected text")
@@ -85,6 +83,9 @@
     def test_geturl(self):
         self.assertEqual(self.returned_obj.geturl(), self.pathname)
 
+    def test_getcode(self):
+        self.assertEqual(self.returned_obj.getcode(), None)
+
     def test_iter(self):
         # Test iterator
         # Don't need to count number of iterations since test would fail the
@@ -93,6 +94,28 @@
         for line in self.returned_obj.__iter__():
             self.assertEqual(line, self.text)
 
+class ProxyTests(unittest.TestCase):
+
+    def setUp(self):
+        # Records changes to env vars
+        self.env = test_support.EnvironmentVarGuard()
+        # Delete all proxy related env vars
+        for k in os.environ.keys():
+            if 'proxy' in k.lower():
+                self.env.unset(k)
+
+    def tearDown(self):
+        # Restore all proxy related env vars
+        self.env.__exit__()
+        del self.env
+
+    def test_getproxies_environment_keep_no_proxies(self):
+        self.env.set('NO_PROXY', 'localhost')
+        proxies = urllib.getproxies_environment()
+        # getproxies_environment use lowered case truncated (no '_proxy') keys
+        self.assertEquals('localhost', proxies['no'])
+
+
 class urlopen_HttpTests(unittest.TestCase):
     """Test urlopen() opening a fake http connection."""
 
@@ -121,12 +144,41 @@
             fp = urllib.urlopen("http://python.org/")
             self.assertEqual(fp.readline(), 'Hello!')
             self.assertEqual(fp.readline(), '')
+            self.assertEqual(fp.geturl(), 'http://python.org/')
+            self.assertEqual(fp.getcode(), 200)
+        finally:
+            self.unfakehttp()
+
+    def test_read_bogus(self):
+        # urlopen() should raise IOError for many error codes.
+        self.fakehttp('''HTTP/1.1 401 Authentication Required
+Date: Wed, 02 Jan 2008 03:03:54 GMT
+Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
+Connection: close
+Content-Type: text/html; charset=iso-8859-1
+''')
+        try:
+            self.assertRaises(IOError, urllib.urlopen, "http://python.org/")
+        finally:
+            self.unfakehttp()
+
+    def test_invalid_redirect(self):
+        # urlopen() should raise IOError for many error codes.
+        self.fakehttp("""HTTP/1.1 302 Found
+Date: Wed, 02 Jan 2008 03:03:54 GMT
+Server: Apache/1.3.33 (Debian GNU/Linux) mod_ssl/2.8.22 OpenSSL/0.9.7e
+Location: file:README
+Connection: close
+Content-Type: text/html; charset=iso-8859-1
+""")
+        try:
+            self.assertRaises(IOError, urllib.urlopen, "http://python.org/")
         finally:
             self.unfakehttp()
 
     def test_empty_socket(self):
-        """urlopen() raises IOError if the underlying socket does not send any
-        data. (#1680230) """
+        # urlopen() raises IOError if the underlying socket does not send any
+        # data. (#1680230)
         self.fakehttp('')
         try:
             self.assertRaises(IOError, urllib.urlopen, 'http://something')
@@ -402,6 +454,32 @@
                          "using unquote(): not all characters escaped: "
                          "%s" % result)
 
+    def test_unquoting_badpercent(self):
+        # Test unquoting on bad percent-escapes
+        given = '%xab'
+        expect = given
+        result = urllib.unquote(given)
+        self.assertEqual(expect, result, "using unquote(): %r != %r"
+                         % (expect, result))
+        given = '%x'
+        expect = given
+        result = urllib.unquote(given)
+        self.assertEqual(expect, result, "using unquote(): %r != %r"
+                         % (expect, result))
+        given = '%'
+        expect = given
+        result = urllib.unquote(given)
+        self.assertEqual(expect, result, "using unquote(): %r != %r"
+                         % (expect, result))
+
+    def test_unquoting_mixed_case(self):
+        # Test unquoting on mixed-case hex digits in the percent-escapes
+        given = '%Ab%eA'
+        expect = '\xab\xea'
+        result = urllib.unquote(given)
+        self.assertEqual(expect, result, "using unquote(): %r != %r"
+                         % (expect, result))
+
     def test_unquoting_parts(self):
         # Make sure unquoting works when have non-quoted characters
         # interspersed
@@ -543,18 +621,119 @@
                          "url2pathname() failed; %s != %s" %
                          (expect, result))
 
+class URLopener_Tests(unittest.TestCase):
+    """Testcase to test the open method of URLopener class."""
+    def test_quoted_open(self):
+        class DummyURLopener(urllib.URLopener):
+            def open_spam(self, url):
+                return url
+
+        self.assertEqual(DummyURLopener().open(
+            'spam://example/ /'),'//example/%20/')
+
+        # test the safe characters are not quoted by urlopen
+        self.assertEqual(DummyURLopener().open(
+            "spam://c:|windows%/:=&?~#+!$,;'@()*[]|/path/"),
+            "//c:|windows%/:=&?~#+!$,;'@()*[]|/path/")
+
+
+# Just commented them out.
+# Can't really tell why keep failing in windows and sparc.
+# Everywhere else they work ok, but on those machines, someteimes
+# fail in one of the tests, sometimes in other. I have a linux, and
+# the tests go ok.
+# If anybody has one of the problematic enviroments, please help!
+# .   Facundo
+#
+# def server(evt):
+#     import socket, time
+#     serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+#     serv.settimeout(3)
+#     serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+#     serv.bind(("", 9093))
+#     serv.listen(5)
+#     try:
+#         conn, addr = serv.accept()
+#         conn.send("1 Hola mundo\n")
+#         cantdata = 0
+#         while cantdata < 13:
+#             data = conn.recv(13-cantdata)
+#             cantdata += len(data)
+#             time.sleep(.3)
+#         conn.send("2 No more lines\n")
+#         conn.close()
+#     except socket.timeout:
+#         pass
+#     finally:
+#         serv.close()
+#         evt.set()
+#
+# class FTPWrapperTests(unittest.TestCase):
+#
+#     def setUp(self):
+#         import ftplib, time, threading
+#         ftplib.FTP.port = 9093
+#         self.evt = threading.Event()
+#         threading.Thread(target=server, args=(self.evt,)).start()
+#         time.sleep(.1)
+#
+#     def tearDown(self):
+#         self.evt.wait()
+#
+#     def testBasic(self):
+#         # connects
+#         ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
+#         ftp.close()
+#
+#     def testTimeoutNone(self):
+#         # global default timeout is ignored
+#         import socket
+#         self.assert_(socket.getdefaulttimeout() is None)
+#         socket.setdefaulttimeout(30)
+#         try:
+#             ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
+#         finally:
+#             socket.setdefaulttimeout(None)
+#         self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
+#         ftp.close()
+#
+#     def testTimeoutDefault(self):
+#         # global default timeout is used
+#         import socket
+#         self.assert_(socket.getdefaulttimeout() is None)
+#         socket.setdefaulttimeout(30)
+#         try:
+#             ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [])
+#         finally:
+#             socket.setdefaulttimeout(None)
+#         self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
+#         ftp.close()
+#
+#     def testTimeoutValue(self):
+#         ftp = urllib.ftpwrapper("myuser", "mypass", "localhost", 9093, [],
+#                                 timeout=30)
+#         self.assertEqual(ftp.ftp.sock.gettimeout(), 30)
+#         ftp.close()
+
 
 
 def test_main():
-    test_support.run_unittest(
-        urlopen_FileTests,
-        urlopen_HttpTests,
-        urlretrieve_FileTests,
-        QuotingTests,
-        UnquotingTests,
-        urlencode_Tests,
-        Pathname_Tests
-    )
+    import warnings
+    with warnings.catch_warnings():
+        warnings.filterwarnings('ignore', ".*urllib\.urlopen.*Python 3.0",
+                                DeprecationWarning)
+        test_support.run_unittest(
+            urlopen_FileTests,
+            urlopen_HttpTests,
+            urlretrieve_FileTests,
+            ProxyTests,
+            QuotingTests,
+            UnquotingTests,
+            urlencode_Tests,
+            Pathname_Tests,
+            URLopener_Tests,
+            #FTPWrapperTests,
+        )
 
 
 

-- 
Repository URL: http://hg.python.org/jython


More information about the Jython-checkins mailing list