]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #19524: Port fakehttp() from Py3 c1fb19907cc4 for use in test_urllib2
authorSerhiy Storchaka <storchaka@gmail.com>
Sat, 6 Sep 2014 18:41:39 +0000 (21:41 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Sat, 6 Sep 2014 18:41:39 +0000 (21:41 +0300)
Lib/test/test_urllib.py
Lib/test/test_urllib2.py

index 0a82cb7e35a476a80351b2e1ce2a10606b91f54b..c18e738f8987371e88baf72b7c01825a64fc4926 100644 (file)
@@ -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 @@ def hexescape(char):
     return "%" + hex_repr
 
 
-class FakeHTTPMixin(object):
-    def fakehttp(self, fakedata):
-        class FakeSocket(StringIO.StringIO):
+def fakehttp(fakedata):
+    class FakeSocket(io.BytesIO):
+
+        def sendall(self, data):
+            FakeHTTPConnection.buf = data
 
-            def sendall(self, data):
-                FakeHTTPConnection.buf = data
+        def makefile(self, *args, **kwds):
+            return self
 
-            def makefile(self, *args, **kwds):
-                return self
+        def read(self, amt=None):
+            if self.closed:
+                return b""
+            return io.BytesIO.read(self, amt)
 
-            def read(self, amt=None):
-                if self.closed:
-                    return ""
-                return StringIO.StringIO.read(self, amt)
+        def readline(self, length=None):
+            if self.closed:
+                return b""
+            return io.BytesIO.readline(self, length)
 
-            def readline(self, length=None):
-                if self.closed:
-                    return ""
-                return StringIO.StringIO.readline(self, length)
+    class FakeHTTPConnection(httplib.HTTPConnection):
 
-        class FakeHTTPConnection(httplib.HTTPConnection):
+        # buffer to store data for verification in urlopen tests.
+        buf = ""
+        fakesock = FakeSocket(fakedata)
 
-            # buffer to store data for verification in urlopen tests.
-            buf = ""
+        def connect(self):
+            self.sock = self.fakesock
 
-            def connect(self):
-                self.sock = FakeSocket(fakedata)
+    return FakeHTTPConnection
 
+
+class FakeHTTPMixin(object):
+    def fakehttp(self, 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
index a6889cc619c947713a987b0d2f1440e64ffdc4d6..30c3d2b46422d0771232ce6da51e12c8746a2c21 100644 (file)
@@ -1,5 +1,6 @@
 import unittest
 from test import test_support
+from test import test_urllib
 
 import os
 import socket