[Python-checkins] cpython (3.5): Issue #21069: Move test_fileno() from test_urllibnet and rewrite it

martin.panter python-checkins at python.org
Sat Apr 9 10:12:50 EDT 2016


https://hg.python.org/cpython/rev/00240ddce1d0
changeset:   100894:00240ddce1d0
branch:      3.5
parent:      100890:0e19f421dc9e
user:        Martin Panter <vadmium+py at gmail.com>
date:        Sat Apr 09 14:03:17 2016 +0000
summary:
  Issue #21069: Move test_fileno() from test_urllibnet and rewrite it

* No longer attempts to close already freed socket file descriptor
* Use socket object to be compatible with Windows
* Do not use a timeout to avoid complication with non-blocking mode
* Use internal localhost server rather than depending on a third party
* Avoid trouble with buffered HTTP data by testing tunnelled CONNECT data

files:
  Lib/test/test_httplib.py   |  41 ++++++++++++++++++++++++++
  Lib/test/test_urllibnet.py |  11 ------
  2 files changed, 41 insertions(+), 11 deletions(-)


diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
--- a/Lib/test/test_httplib.py
+++ b/Lib/test/test_httplib.py
@@ -915,6 +915,47 @@
         self.assertEqual(sock.file.read(), extradata) #we read to the end
         resp.close()
 
+    def test_response_fileno(self):
+        # Make sure fd returned by fileno is valid.
+        threading = support.import_module("threading")
+
+        serv = socket.socket(
+            socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
+        self.addCleanup(serv.close)
+        serv.bind((HOST, 0))
+        serv.listen()
+
+        result = None
+        def run_server():
+            [conn, address] = serv.accept()
+            with conn, conn.makefile("rb") as reader:
+                # Read the request header until a blank line
+                while True:
+                    line = reader.readline()
+                    if not line.rstrip(b"\r\n"):
+                        break
+                conn.sendall(b"HTTP/1.1 200 Connection established\r\n\r\n")
+                nonlocal result
+                result = reader.read()
+
+        thread = threading.Thread(target=run_server)
+        thread.start()
+        conn = client.HTTPConnection(*serv.getsockname())
+        conn.request("CONNECT", "dummy:1234")
+        response = conn.getresponse()
+        try:
+            self.assertEqual(response.status, client.OK)
+            s = socket.socket(fileno=response.fileno())
+            try:
+                s.sendall(b"proxied data\n")
+            finally:
+                s.detach()
+        finally:
+            response.close()
+            conn.close()
+            thread.join()
+        self.assertEqual(result, b"proxied data\n")
+
 class ExtendedReadTest(TestCase):
     """
     Test peek(), read1(), readline()
diff --git a/Lib/test/test_urllibnet.py b/Lib/test/test_urllibnet.py
--- a/Lib/test/test_urllibnet.py
+++ b/Lib/test/test_urllibnet.py
@@ -99,17 +99,6 @@
                 open_url.close()
             self.assertEqual(code, 404)
 
-    # On Windows, socket handles are not file descriptors; this
-    # test can't pass on Windows.
-    @unittest.skipIf(sys.platform in ('win32',), 'not appropriate for Windows')
-    def test_fileno(self):
-        # Make sure fd returned by fileno is valid.
-        with self.urlopen("http://www.google.com/") as open_url:
-            fd = open_url.fileno()
-            with os.fdopen(fd, 'rb') as f:
-                self.assertTrue(f.read(), "reading from file created using fd "
-                                          "returned by fileno failed")
-
     def test_bad_address(self):
         # Make sure proper exception is raised when connecting to a bogus
         # address.

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list