MCPcopy Index your code
hub / github.com/python/cpython / fakehttp

Function fakehttp

Lib/test/test_urllib.py:38–82  ·  view source on GitHub ↗
(fakedata, mock_close=False)

Source from the content-addressed store, hash-verified

36
37
38def fakehttp(fakedata, mock_close=False):
39 class FakeSocket(io.BytesIO):
40 io_refs = 1
41
42 def sendall(self, data):
43 FakeHTTPConnection.buf = data
44
45 def makefile(self, *args, **kwds):
46 self.io_refs += 1
47 return self
48
49 def read(self, amt=None):
50 if self.closed:
51 return b""
52 return io.BytesIO.read(self, amt)
53
54 def readline(self, length=None):
55 if self.closed:
56 return b""
57 return io.BytesIO.readline(self, length)
58
59 def close(self):
60 self.io_refs -= 1
61 if self.io_refs == 0:
62 io.BytesIO.close(self)
63
64 class FakeHTTPConnection(http.client.HTTPConnection):
65
66 # buffer to store data for verification in urlopen tests.
67 buf = None
68
69 def connect(self):
70 self.sock = FakeSocket(self.fakedata)
71 type(self).fakesock = self.sock
72
73 if mock_close:
74 # bpo-36918: HTTPConnection destructor calls close() which calls
75 # flush(). Problem: flush() calls self.fp.flush() which raises
76 # "ValueError: I/O operation on closed file" which is logged as an
77 # "Exception ignored in". Override close() to silence this error.
78 def close(self):
79 pass
80 FakeHTTPConnection.fakedata = fakedata
81
82 return FakeHTTPConnection
83
84
85class FakeHTTPMixin(object):

Callers 1

fakehttpMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…