mirror of
https://github.com/python/cpython.git
synced 2025-07-29 14:15:07 +00:00
Added a 'create_connect()' function to socket.py, which creates a
connection with an optional timeout, and modified httplib.py to use this function in HTTPConnection. Applies patch 1676823.
This commit is contained in:
parent
f102e24bd3
commit
07c78be0b4
7 changed files with 181 additions and 25 deletions
|
@ -1,6 +1,7 @@
|
|||
import httplib
|
||||
import StringIO
|
||||
import sys
|
||||
import socket
|
||||
|
||||
from unittest import TestCase
|
||||
|
||||
|
@ -149,8 +150,47 @@ class OfflineTest(TestCase):
|
|||
def test_responses(self):
|
||||
self.assertEquals(httplib.responses[httplib.NOT_FOUND], "Not Found")
|
||||
|
||||
PORT = 50003
|
||||
HOST = "localhost"
|
||||
|
||||
class TimeoutTest(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
self.serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
global PORT
|
||||
PORT = test_support.bind_port(self.serv, HOST, PORT)
|
||||
self.serv.listen(1)
|
||||
|
||||
def tearDown(self):
|
||||
self.serv.close()
|
||||
self.serv = None
|
||||
|
||||
def testTimeoutAttribute(self):
|
||||
'''This will prove that the timeout gets through
|
||||
HTTPConnection and into the socket.
|
||||
'''
|
||||
# default
|
||||
httpConn = httplib.HTTPConnection(HOST, PORT)
|
||||
httpConn.connect()
|
||||
self.assertTrue(httpConn.sock.gettimeout() is None)
|
||||
|
||||
# a value
|
||||
httpConn = httplib.HTTPConnection(HOST, PORT, timeout=10)
|
||||
httpConn.connect()
|
||||
self.assertEqual(httpConn.sock.gettimeout(), 10)
|
||||
|
||||
# None, having other default
|
||||
previous = socket.getdefaulttimeout()
|
||||
socket.setdefaulttimeout(10)
|
||||
httpConn = httplib.HTTPConnection(HOST, PORT, timeout=None)
|
||||
httpConn.connect()
|
||||
socket.setdefaulttimeout(previous)
|
||||
self.assertEqual(httpConn.sock.gettimeout(), 10)
|
||||
|
||||
|
||||
def test_main(verbose=None):
|
||||
test_support.run_unittest(HeaderTests, OfflineTest, BasicTest)
|
||||
test_support.run_unittest(HeaderTests, OfflineTest, BasicTest, TimeoutTest)
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_main()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue