summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_httplib.py37
-rw-r--r--Lib/test/test_socket.py15
2 files changed, 49 insertions, 3 deletions
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
index 37cda5d..705ceec 100644
--- a/Lib/test/test_httplib.py
+++ b/Lib/test/test_httplib.py
@@ -4,7 +4,8 @@ import io
import array
import socket
-from unittest import TestCase
+import unittest
+TestCase = unittest.TestCase
from test import support
@@ -263,6 +264,38 @@ class OfflineTest(TestCase):
def test_responses(self):
self.assertEquals(client.responses[client.NOT_FOUND], "Not Found")
+
+class SourceAddressTest(TestCase):
+ def setUp(self):
+ self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ self.port = support.bind_port(self.serv)
+ self.source_port = support.find_unused_port()
+ self.serv.listen(5)
+ self.conn = None
+
+ def tearDown(self):
+ if self.conn:
+ self.conn.close()
+ self.conn = None
+ self.serv.close()
+ self.serv = None
+
+ def testHTTPConnectionSourceAddress(self):
+ self.conn = client.HTTPConnection(HOST, self.port,
+ source_address=('', self.source_port))
+ self.conn.connect()
+ self.assertEqual(self.conn.sock.getsockname()[1], self.source_port)
+
+ @unittest.skipIf(not hasattr(client, 'HTTPSConnection'),
+ 'http.client.HTTPSConnection not defined')
+ def testHTTPSConnectionSourceAddress(self):
+ self.conn = client.HTTPSConnection(HOST, self.port,
+ source_address=('', self.source_port))
+ # We don't test anything here other the constructor not barfing as
+ # this code doesn't deal with setting up an active running SSL server
+ # for an ssl_wrapped connect() to actually return from.
+
+
class TimeoutTest(TestCase):
PORT = None
@@ -390,7 +423,7 @@ class RequestBodyTest(TestCase):
def test_main(verbose=None):
support.run_unittest(HeaderTests, OfflineTest, BasicTest, TimeoutTest,
- HTTPSTimeoutTest, RequestBodyTest)
+ HTTPSTimeoutTest, RequestBodyTest, SourceAddressTest)
if __name__ == '__main__':
test_main()
diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py
index db32335..62c2e25 100644
--- a/Lib/test/test_socket.py
+++ b/Lib/test/test_socket.py
@@ -993,7 +993,7 @@ class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest):
ThreadableTest.__init__(self)
def clientSetUp(self):
- pass
+ self.source_port = support.find_unused_port()
def clientTearDown(self):
self.cli.close()
@@ -1008,6 +1008,19 @@ class NetworkConnectionAttributesTest(SocketTCPTest, ThreadableTest):
self.cli = socket.create_connection((HOST, self.port), timeout=30)
self.assertEqual(self.cli.family, 2)
+ testSourcePort = _justAccept
+ def _testSourcePort(self):
+ self.cli = socket.create_connection((HOST, self.port), timeout=30,
+ source_address=('', self.source_port))
+ self.assertEqual(self.cli.getsockname()[1], self.source_port)
+
+ testSourceAddress = _justAccept
+ def _testSourceAddress(self):
+ self.cli = socket.create_connection(
+ (HOST, self.port), 30, ('127.0.0.1', self.source_port))
+ self.assertEqual(self.cli.getsockname(),
+ ('127.0.0.1', self.source_port))
+
testTimeoutDefault = _justAccept
def _testTimeoutDefault(self):
# passing no explicit timeout uses socket's global default