cpython: 00240ddce1d0 (original) (raw)
Mercurial > cpython
changeset 100894:00240ddce1d0 3.5
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 [#21069]
Martin Panter vadmium+py@gmail.com | |
---|---|
date | Sat, 09 Apr 2016 14:03:17 +0000 |
parents | 0e19f421dc9e |
children | 4c19396bd4a0 15cbeb389f17 |
files | Lib/test/test_httplib.py Lib/test/test_urllibnet.py |
diffstat | 2 files changed, 41 insertions(+), 11 deletions(-)[+] [-] Lib/test/test_httplib.py 41 Lib/test/test_urllibnet.py 11 |
line wrap: on
line diff
--- a/Lib/test/test_httplib.py +++ b/Lib/test/test_httplib.py @@ -915,6 +915,47 @@ class BasicTest(TestCase): 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.[](#l1.8)
threading = support.import_module("threading")[](#l1.9)
serv = socket.socket([](#l1.11)
socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)[](#l1.12)
self.addCleanup(serv.close)[](#l1.13)
serv.bind((HOST, 0))[](#l1.14)
serv.listen()[](#l1.15)
result = None[](#l1.17)
def run_server():[](#l1.18)
[conn, address] = serv.accept()[](#l1.19)
with conn, conn.makefile("rb") as reader:[](#l1.20)
# Read the request header until a blank line[](#l1.21)
while True:[](#l1.22)
line = reader.readline()[](#l1.23)
if not line.rstrip(b"\r\n"):[](#l1.24)
break[](#l1.25)
conn.sendall(b"HTTP/1.1 200 Connection established\r\n\r\n")[](#l1.26)
nonlocal result[](#l1.27)
result = reader.read()[](#l1.28)
thread = threading.Thread(target=run_server)[](#l1.30)
thread.start()[](#l1.31)
conn = client.HTTPConnection(*serv.getsockname())[](#l1.32)
conn.request("CONNECT", "dummy:1234")[](#l1.33)
response = conn.getresponse()[](#l1.34)
try:[](#l1.35)
self.assertEqual(response.status, client.OK)[](#l1.36)
s = socket.socket(fileno=response.fileno())[](#l1.37)
try:[](#l1.38)
s.sendall(b"proxied data\n")[](#l1.39)
finally:[](#l1.40)
s.detach()[](#l1.41)
finally:[](#l1.42)
response.close()[](#l1.43)
conn.close()[](#l1.44)
thread.join()[](#l1.45)
self.assertEqual(result, b"proxied data\n")[](#l1.46)
+ class ExtendedReadTest(TestCase): """ Test peek(), read1(), readline()
--- a/Lib/test/test_urllibnet.py +++ b/Lib/test/test_urllibnet.py @@ -99,17 +99,6 @@ class urlopenNetworkTests(unittest.TestC 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.[](#l2.11)
with self.urlopen("http://www.google.com/") as open_url:[](#l2.12)
fd = open_url.fileno()[](#l2.13)
with os.fdopen(fd, 'rb') as f:[](#l2.14)
self.assertTrue(f.read(), "reading from file created using fd "[](#l2.15)
"returned by fileno failed")[](#l2.16)
- def test_bad_address(self): # Make sure proper exception is raised when connecting to a bogus # address.