diff options
author | Senthil Kumaran <senthil@uthcode.com> | 2014-04-20 09:41:29 -0700 |
---|---|---|
committer | Senthil Kumaran <senthil@uthcode.com> | 2014-04-20 09:41:29 -0700 |
commit | 6117e5d8e3f00f3677f07a29b4ec968a5ed8cd76 (patch) | |
tree | 0da26badccffeb64225879a989f56b2ba16b0666 /Lib/test | |
parent | 9077d24d7f85e09e53def11b2beeaf40749e2464 (diff) | |
download | cpython-git-6117e5d8e3f00f3677f07a29b4ec968a5ed8cd76.tar.gz |
urllib.response object to use _TemporaryFileWrapper (and _TemporaryFileCloser)
facility. Provides a better way to handle file descriptor close.
Address issue #15002 . Patch contributed by Christian Theune.
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_urllib_response.py | 65 |
1 files changed, 41 insertions, 24 deletions
diff --git a/Lib/test/test_urllib_response.py b/Lib/test/test_urllib_response.py index fdd33259dd..0eb59426cc 100644 --- a/Lib/test/test_urllib_response.py +++ b/Lib/test/test_urllib_response.py @@ -1,42 +1,59 @@ """Unit tests for code in urllib.response.""" -import test.support +import socket +import tempfile import urllib.response import unittest -class TestFile(object): - - def __init__(self): - self.closed = False - - def read(self, bytes): - pass - - def readline(self): - pass - - def close(self): - self.closed = True - -class Testaddbase(unittest.TestCase): - - # TODO(jhylton): Write tests for other functionality of addbase() +class TestResponse(unittest.TestCase): def setUp(self): - self.fp = TestFile() - self.addbase = urllib.response.addbase(self.fp) + self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.fp = self.sock.makefile('rb') + self.test_headers = {"Host": "www.python.org", + "Connection": "close"} def test_with(self): + addbase = urllib.response.addbase(self.fp) + + self.assertIsInstance(addbase, tempfile._TemporaryFileWrapper) + def f(): - with self.addbase as spam: + with addbase as spam: pass self.assertFalse(self.fp.closed) f() self.assertTrue(self.fp.closed) self.assertRaises(ValueError, f) -def test_main(): - test.support.run_unittest(Testaddbase) + def test_addclosehook(self): + closehook_called = False + + def closehook(): + nonlocal closehook_called + closehook_called = True + + closehook = urllib.response.addclosehook(self.fp, closehook) + closehook.close() + + self.assertTrue(self.fp.closed) + self.assertTrue(closehook_called) + + def test_addinfo(self): + info = urllib.response.addinfo(self.fp, self.test_headers) + self.assertEqual(info.info(), self.test_headers) + + def test_addinfourl(self): + url = "http://www.python.org" + code = 200 + infourl = urllib.response.addinfourl(self.fp, self.test_headers, + url, code) + self.assertEqual(infourl.info(), self.test_headers) + self.assertEqual(infourl.geturl(), url) + self.assertEqual(infourl.getcode(), code) + + def tearDown(self): + self.sock.close() if __name__ == '__main__': - test_main() + unittest.main() |