[Python-checkins] cpython (2.7): Issue #19524: Port fakehttp() from Py3 c1fb19907cc4 for use in test_urllib2

martin.panter python-checkins at python.org
Mon May 16 04:14:55 EDT 2016


https://hg.python.org/cpython/rev/19e4e0b7f1bd
changeset:   101369:19e4e0b7f1bd
branch:      2.7
parent:      101366:c3346c89f6d4
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sat Sep 06 21:41:39 2014 +0300
summary:
  Issue #19524: Port fakehttp() from Py3 c1fb19907cc4 for use in test_urllib2

files:
  Lib/test/test_urllib.py  |  61 +++++++++++++++------------
  Lib/test/test_urllib2.py |   1 +
  2 files changed, 34 insertions(+), 28 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
@@ -3,12 +3,12 @@
 import collections
 import urllib
 import httplib
+import io
 import unittest
 import os
 import sys
 import mimetools
 import tempfile
-import StringIO
 
 from test import test_support
 from base64 import b64encode
@@ -22,37 +22,42 @@
     return "%" + hex_repr
 
 
+def fakehttp(fakedata):
+    class FakeSocket(io.BytesIO):
+
+        def sendall(self, data):
+            FakeHTTPConnection.buf = data
+
+        def makefile(self, *args, **kwds):
+            return self
+
+        def read(self, amt=None):
+            if self.closed:
+                return b""
+            return io.BytesIO.read(self, amt)
+
+        def readline(self, length=None):
+            if self.closed:
+                return b""
+            return io.BytesIO.readline(self, length)
+
+    class FakeHTTPConnection(httplib.HTTPConnection):
+
+        # buffer to store data for verification in urlopen tests.
+        buf = ""
+        fakesock = FakeSocket(fakedata)
+
+        def connect(self):
+            self.sock = self.fakesock
+
+    return FakeHTTPConnection
+
+
 class FakeHTTPMixin(object):
     def fakehttp(self, fakedata):
-        class FakeSocket(StringIO.StringIO):
-
-            def sendall(self, data):
-                FakeHTTPConnection.buf = data
-
-            def makefile(self, *args, **kwds):
-                return self
-
-            def read(self, amt=None):
-                if self.closed:
-                    return ""
-                return StringIO.StringIO.read(self, amt)
-
-            def readline(self, length=None):
-                if self.closed:
-                    return ""
-                return StringIO.StringIO.readline(self, length)
-
-        class FakeHTTPConnection(httplib.HTTPConnection):
-
-            # buffer to store data for verification in urlopen tests.
-            buf = ""
-
-            def connect(self):
-                self.sock = FakeSocket(fakedata)
-
         assert httplib.HTTP._connection_class == httplib.HTTPConnection
 
-        httplib.HTTP._connection_class = FakeHTTPConnection
+        httplib.HTTP._connection_class = fakehttp(fakedata)
 
     def unfakehttp(self):
         httplib.HTTP._connection_class = httplib.HTTPConnection
diff --git a/Lib/test/test_urllib2.py b/Lib/test/test_urllib2.py
--- a/Lib/test/test_urllib2.py
+++ b/Lib/test/test_urllib2.py
@@ -1,5 +1,6 @@
 import unittest
 from test import test_support
+from test import test_urllib
 
 import os
 import socket

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


More information about the Python-checkins mailing list