changeset: 101369:19e4e0b7f1bd branch: 2.7 parent: 101366:c3346c89f6d4 user: Serhiy Storchaka date: Sat Sep 06 21:41:39 2014 +0300 files: Lib/test/test_urllib.py Lib/test/test_urllib2.py description: Issue #19524: Port fakehttp() from Py3 c1fb19907cc4 for use in test_urllib2 diff -r c3346c89f6d4 -r 19e4e0b7f1bd Lib/test/test_urllib.py --- a/Lib/test/test_urllib.py Mon May 16 10:12:02 2016 +0300 +++ b/Lib/test/test_urllib.py Sat Sep 06 21:41:39 2014 +0300 @@ -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 -r c3346c89f6d4 -r 19e4e0b7f1bd Lib/test/test_urllib2.py --- a/Lib/test/test_urllib2.py Mon May 16 10:12:02 2016 +0300 +++ b/Lib/test/test_urllib2.py Sat Sep 06 21:41:39 2014 +0300 @@ -1,5 +1,6 @@ import unittest from test import test_support +from test import test_urllib import os import socket