diff options
| author | Georg Brandl <georg@python.org> | 2007-01-21 10:35:10 (GMT) |
|---|---|---|
| committer | Georg Brandl <georg@python.org> | 2007-01-21 10:35:10 (GMT) |
| commit | dd7b0525e902ee12a612611ea082e12f5999c823 (patch) | |
| tree | ea6fda1769b0311d8f166d05d32d112afaa82c25 /Lib/test/test_urllib2net.py | |
| parent | b84c13792db49abdfac97663badfeda0bba11279 (diff) | |
| download | cpython-dd7b0525e902ee12a612611ea082e12f5999c823.zip cpython-dd7b0525e902ee12a612611ea082e12f5999c823.tar.gz cpython-dd7b0525e902ee12a612611ea082e12f5999c823.tar.bz2 | |
Patch #1627441: close sockets properly in urllib2.
Diffstat (limited to 'Lib/test/test_urllib2net.py')
| -rw-r--r-- | Lib/test/test_urllib2net.py | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/Lib/test/test_urllib2net.py b/Lib/test/test_urllib2net.py index 00cf202..b271626 100644 --- a/Lib/test/test_urllib2net.py +++ b/Lib/test/test_urllib2net.py @@ -64,6 +64,27 @@ class AuthTests(unittest.TestCase): # urllib2.urlopen, "http://evil:thing@example.com") +class CloseSocketTest(unittest.TestCase): + + def test_close(self): + import socket, httplib, gc + + # calling .close() on urllib2's response objects should close the + # underlying socket + + # delve deep into response to fetch socket._socketobject + response = urllib2.urlopen("http://www.python.org/") + abused_fileobject = response.fp + self.assert_(abused_fileobject.__class__ is socket._fileobject) + httpresponse = abused_fileobject._sock + self.assert_(httpresponse.__class__ is httplib.HTTPResponse) + fileobject = httpresponse.fp + self.assert_(fileobject.__class__ is socket._fileobject) + + self.assert_(not fileobject.closed) + response.close() + self.assert_(fileobject.closed) + class urlopenNetworkTests(unittest.TestCase): """Tests urllib2.urlopen using the network. @@ -263,8 +284,12 @@ class OtherNetworkTests(unittest.TestCase): def test_main(): test_support.requires("network") - test_support.run_unittest(URLTimeoutTest, urlopenNetworkTests, - AuthTests, OtherNetworkTests) + test_support.run_unittest(URLTimeoutTest, + urlopenNetworkTests, + AuthTests, + OtherNetworkTests, + CloseSocketTest, + ) if __name__ == "__main__": test_main() |
