1 | n/a | """Unit tests for code in urllib.response.""" |
---|
2 | n/a | |
---|
3 | n/a | import socket |
---|
4 | n/a | import tempfile |
---|
5 | n/a | import urllib.response |
---|
6 | n/a | import unittest |
---|
7 | n/a | |
---|
8 | n/a | class TestResponse(unittest.TestCase): |
---|
9 | n/a | |
---|
10 | n/a | def setUp(self): |
---|
11 | n/a | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
---|
12 | n/a | self.fp = self.sock.makefile('rb') |
---|
13 | n/a | self.test_headers = {"Host": "www.python.org", |
---|
14 | n/a | "Connection": "close"} |
---|
15 | n/a | |
---|
16 | n/a | def test_with(self): |
---|
17 | n/a | addbase = urllib.response.addbase(self.fp) |
---|
18 | n/a | |
---|
19 | n/a | self.assertIsInstance(addbase, tempfile._TemporaryFileWrapper) |
---|
20 | n/a | |
---|
21 | n/a | def f(): |
---|
22 | n/a | with addbase as spam: |
---|
23 | n/a | pass |
---|
24 | n/a | self.assertFalse(self.fp.closed) |
---|
25 | n/a | f() |
---|
26 | n/a | self.assertTrue(self.fp.closed) |
---|
27 | n/a | self.assertRaises(ValueError, f) |
---|
28 | n/a | |
---|
29 | n/a | def test_addclosehook(self): |
---|
30 | n/a | closehook_called = False |
---|
31 | n/a | |
---|
32 | n/a | def closehook(): |
---|
33 | n/a | nonlocal closehook_called |
---|
34 | n/a | closehook_called = True |
---|
35 | n/a | |
---|
36 | n/a | closehook = urllib.response.addclosehook(self.fp, closehook) |
---|
37 | n/a | closehook.close() |
---|
38 | n/a | |
---|
39 | n/a | self.assertTrue(self.fp.closed) |
---|
40 | n/a | self.assertTrue(closehook_called) |
---|
41 | n/a | |
---|
42 | n/a | def test_addinfo(self): |
---|
43 | n/a | info = urllib.response.addinfo(self.fp, self.test_headers) |
---|
44 | n/a | self.assertEqual(info.info(), self.test_headers) |
---|
45 | n/a | |
---|
46 | n/a | def test_addinfourl(self): |
---|
47 | n/a | url = "http://www.python.org" |
---|
48 | n/a | code = 200 |
---|
49 | n/a | infourl = urllib.response.addinfourl(self.fp, self.test_headers, |
---|
50 | n/a | url, code) |
---|
51 | n/a | self.assertEqual(infourl.info(), self.test_headers) |
---|
52 | n/a | self.assertEqual(infourl.geturl(), url) |
---|
53 | n/a | self.assertEqual(infourl.getcode(), code) |
---|
54 | n/a | |
---|
55 | n/a | def tearDown(self): |
---|
56 | n/a | self.sock.close() |
---|
57 | n/a | |
---|
58 | n/a | if __name__ == '__main__': |
---|
59 | n/a | unittest.main() |
---|