Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(612)

Side by Side Diff: python2/httplib2test.py

Issue 6506074: Making httplib2.Http instances pickleable. (Closed)
Patch Set: Adding unit tests for pickled Http objects. Created 1 year, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « python2/httplib2/__init__.py ('k') | python3/httplib2/__init__.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python2.4 1 #!/usr/bin/env python2.4
2 """ 2 """
3 httplib2test 3 httplib2test
4 4
5 A set of unit tests for httplib2.py. 5 A set of unit tests for httplib2.py.
6 6
7 Requires Python 2.4 or later 7 Requires Python 2.4 or later
8 """ 8 """
9 9
10 __author__ = "Joe Gregorio (joe@bitworking.org)" 10 __author__ = "Joe Gregorio (joe@bitworking.org)"
11 __copyright__ = "Copyright 2006, Joe Gregorio" 11 __copyright__ = "Copyright 2006, Joe Gregorio"
12 __contributors__ = [] 12 __contributors__ = []
13 __license__ = "MIT" 13 __license__ = "MIT"
14 __history__ = """ """ 14 __history__ = """ """
15 __version__ = "0.1 ($Rev: 118 $)" 15 __version__ = "0.1 ($Rev: 118 $)"
16 16
17 17
18 import StringIO 18 import StringIO
19 import base64 19 import base64
20 import httplib 20 import httplib
21 import httplib2 21 import httplib2
22 import os 22 import os
23 import pickle
23 import socket 24 import socket
24 import sys 25 import sys
25 import time 26 import time
26 import unittest 27 import unittest
27 import urlparse 28 import urlparse
28 29
29 try: 30 try:
30 import ssl 31 import ssl
31 except ImportError: 32 except ImportError:
32 pass 33 pass
33 34
34 # Python 2.3 support 35 # Python 2.3 support
35 if not hasattr(unittest.TestCase, 'assertTrue'): 36 if not hasattr(unittest.TestCase, 'assertTrue'):
36 unittest.TestCase.assertTrue = unittest.TestCase.failUnless 37 unittest.TestCase.assertTrue = unittest.TestCase.failUnless
37 unittest.TestCase.assertFalse = unittest.TestCase.failIf 38 unittest.TestCase.assertFalse = unittest.TestCase.failIf
38 39
39 # The test resources base uri 40 # The test resources base uri
40 base = 'http://bitworking.org/projects/httplib2/test/' 41 base = 'http://bitworking.org/projects/httplib2/test/'
41 #base = 'http://localhost/projects/httplib2/test/' 42 #base = 'http://localhost/projects/httplib2/test/'
42 cacheDirName = ".cache" 43 cacheDirName = ".cache"
44 pickleTemplate = (
jcgregorio_google 2012/09/13 11:30:33 This is going to be fragile. For the test just pic
dhermes 2012/09/13 15:40:52 I got rid of the fragility and used (object).__dic
45 "ccopy_reg\n_reconstructor\np0\n(chttplib2\nHttp\np1\nc__builtin__"
46 "\nobject\np2\nNtp3\nRp4\n(dp5\nS'connections'\np6\n(dp7\nsS'forwa"
47 "rd_authorization_headers'\np8\nI00\nsS'authorizations'\np9\n(lp10"
48 "\nsS'proxy_info'\np11\nchttplib2\nproxy_info_from_environment\np1"
49 "2\nsS'follow_redirects'\np13\nI01\nsS'cache'\np14\ng0\n(chttplib2"
50 "\nFileCache\np15\ng2\nNtp16\nRp17\n(dp18\nS'safe'\np19\nchttplib2"
51 "\nsafename\np20\nsg14\nS'.cache'\np21\nsbsS'force_exception_to_st"
52 "atus_code'\np22\nI00\nsS'ca_certs'\np23\nNsS'optimistic_concurren"
53 "cy_methods'\np24\n(lp25\nS'PUT'\np26\naS'PATCH'\np27\nasS'follow_"
54 "all_redirects'\np28\nI00\nsS'timeout'\np29\nNsS'credentials'\np30"
55 "\ng0\n(chttplib2\nCredentials\np31\ng2\nNtp32\nRp33\n(dp34\ng30\n"
56 "(lp35\nsbsS'certificates'\np36\ng0\n(chttplib2\nKeyCerts\np37\ng2"
57 "\nNtp38\nRp39\n(dp40\ng30\n(lp41\nsbsS'ignore_etag'\np42\nI00\nsS"
58 "'disable_ssl_certificate_validation'\np43\nI0%s\nsb.")
43 59
44 60
45 class CredentialsTest(unittest.TestCase): 61 class CredentialsTest(unittest.TestCase):
46 def test(self): 62 def test(self):
47 c = httplib2.Credentials() 63 c = httplib2.Credentials()
48 c.add("joe", "password") 64 c.add("joe", "password")
49 self.assertEqual(("joe", "password"), list(c.iter("bitworking.org"))[0]) 65 self.assertEqual(("joe", "password"), list(c.iter("bitworking.org"))[0])
50 self.assertEqual(("joe", "password"), list(c.iter(""))[0]) 66 self.assertEqual(("joe", "password"), list(c.iter(""))[0])
51 c.add("fred", "password2", "wellformedweb.org") 67 c.add("fred", "password2", "wellformedweb.org")
52 self.assertEqual(("joe", "password"), list(c.iter("bitworking.org"))[0]) 68 self.assertEqual(("joe", "password"), list(c.iter("bitworking.org"))[0])
(...skipping 1122 matching lines...) Expand 10 before | Expand all | Expand 10 after
1175 1191
1176 def testConnectionClose(self): 1192 def testConnectionClose(self):
1177 uri = "http://www.google.com/" 1193 uri = "http://www.google.com/"
1178 (response, content) = self.http.request(uri, "GET") 1194 (response, content) = self.http.request(uri, "GET")
1179 for c in self.http.connections.values(): 1195 for c in self.http.connections.values():
1180 self.assertNotEqual(None, c.sock) 1196 self.assertNotEqual(None, c.sock)
1181 (response, content) = self.http.request(uri, "GET", headers={"connection ": "close"}) 1197 (response, content) = self.http.request(uri, "GET", headers={"connection ": "close"})
1182 for c in self.http.connections.values(): 1198 for c in self.http.connections.values():
1183 self.assertEqual(None, c.sock) 1199 self.assertEqual(None, c.sock)
1184 1200
1201 def testPickleHttp(self):
1202 if sys.version_info < (2, 6):
1203 pickle_value = pickleTemplate % 1
1204 else:
1205 pickle_value = pickleTemplate % 0
1206
1207 pickled_http = pickle.dumps(self.http)
1208 self.assertEqual(pickled_http, pickle_value)
1209
1210 def testPickleCustomRequestHttp(self):
1211 def dummy_request(*args, **kwargs):
1212 return new_request(*args, **kwargs)
1213 dummy_request.dummy_attr = 'dummy_value'
1214
1215 self.http.request = dummy_request
1216 pickled_http = pickle.dumps(self.http)
1217 self.assertFalse("S'request'" in pickled_http)
1185 1218
1186 try: 1219 try:
1187 import memcache 1220 import memcache
1188 class HttpTestMemCached(HttpTest): 1221 class HttpTestMemCached(HttpTest):
1189 def setUp(self): 1222 def setUp(self):
1190 self.cache = memcache.Client(['127.0.0.1:11211'], debug=0) 1223 self.cache = memcache.Client(['127.0.0.1:11211'], debug=0)
1191 #self.cache = memcache.Client(['10.0.0.4:11211'], debug=1) 1224 #self.cache = memcache.Client(['10.0.0.4:11211'], debug=1)
1192 self.http = httplib2.Http(self.cache) 1225 self.http = httplib2.Http(self.cache)
1193 self.cache.flush_all() 1226 self.cache.flush_all()
1194 # Not exactly sure why the sleep is needed here, but 1227 # Not exactly sure why the sleep is needed here, but
(...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after
1577 1610
1578 class TestProxyInfo(unittest.TestCase): 1611 class TestProxyInfo(unittest.TestCase):
1579 def setUp(self): 1612 def setUp(self):
1580 self.orig_env = dict(os.environ) 1613 self.orig_env = dict(os.environ)
1581 1614
1582 def tearDown(self): 1615 def tearDown(self):
1583 os.environ.clear() 1616 os.environ.clear()
1584 os.environ.update(self.orig_env) 1617 os.environ.update(self.orig_env)
1585 1618
1586 def test_from_url(self): 1619 def test_from_url(self):
1587 pi = httplib2.ProxyInfo.from_url('http://myproxy.example.com') 1620 pi = httplib2.proxy_info_from_url('http://myproxy.example.com')
1588 self.assertEquals(pi.proxy_host, 'myproxy.example.com') 1621 self.assertEquals(pi.proxy_host, 'myproxy.example.com')
1589 self.assertEquals(pi.proxy_port, 80) 1622 self.assertEquals(pi.proxy_port, 80)
1590 self.assertEquals(pi.proxy_user, None) 1623 self.assertEquals(pi.proxy_user, None)
1591 1624
1592 def test_from_url_ident(self): 1625 def test_from_url_ident(self):
1593 pi = httplib2.ProxyInfo.from_url('http://zoidberg:fish@someproxy:99') 1626 pi = httplib2.proxy_info_from_url('http://zoidberg:fish@someproxy:99')
1594 self.assertEquals(pi.proxy_host, 'someproxy') 1627 self.assertEquals(pi.proxy_host, 'someproxy')
1595 self.assertEquals(pi.proxy_port, 99) 1628 self.assertEquals(pi.proxy_port, 99)
1596 self.assertEquals(pi.proxy_user, 'zoidberg') 1629 self.assertEquals(pi.proxy_user, 'zoidberg')
1597 self.assertEquals(pi.proxy_pass, 'fish') 1630 self.assertEquals(pi.proxy_pass, 'fish')
1598 1631
1599 def test_from_env(self): 1632 def test_from_env(self):
1600 os.environ['http_proxy'] = 'http://myproxy.example.com:8080' 1633 os.environ['http_proxy'] = 'http://myproxy.example.com:8080'
1601 pi = httplib2.ProxyInfo.from_environment() 1634 pi = httplib2.proxy_info_from_environment()
1602 self.assertEquals(pi.proxy_host, 'myproxy.example.com') 1635 self.assertEquals(pi.proxy_host, 'myproxy.example.com')
1603 self.assertEquals(pi.proxy_port, 8080) 1636 self.assertEquals(pi.proxy_port, 8080)
1604 self.assertEquals(pi.bypass_hosts, []) 1637 self.assertEquals(pi.bypass_hosts, [])
1605 1638
1606 def test_from_env_no_proxy(self): 1639 def test_from_env_no_proxy(self):
1607 os.environ['http_proxy'] = 'http://myproxy.example.com:80' 1640 os.environ['http_proxy'] = 'http://myproxy.example.com:80'
1608 os.environ['https_proxy'] = 'http://myproxy.example.com:81' 1641 os.environ['https_proxy'] = 'http://myproxy.example.com:81'
1609 os.environ['no_proxy'] = 'localhost,otherhost.domain.local' 1642 os.environ['no_proxy'] = 'localhost,otherhost.domain.local'
1610 pi = httplib2.ProxyInfo.from_environment('https') 1643 pi = httplib2.proxy_info_from_environment('https')
1611 self.assertEquals(pi.proxy_host, 'myproxy.example.com') 1644 self.assertEquals(pi.proxy_host, 'myproxy.example.com')
1612 self.assertEquals(pi.proxy_port, 81) 1645 self.assertEquals(pi.proxy_port, 81)
1613 self.assertEquals(pi.bypass_hosts, ['localhost', 1646 self.assertEquals(pi.bypass_hosts, ['localhost',
1614 'otherhost.domain.local']) 1647 'otherhost.domain.local'])
1615 1648
1616 def test_from_env_none(self): 1649 def test_from_env_none(self):
1617 os.environ.clear() 1650 os.environ.clear()
1618 pi = httplib2.ProxyInfo.from_environment() 1651 pi = httplib2.proxy_info_from_environment()
1619 self.assertEquals(pi, None) 1652 self.assertEquals(pi, None)
1620 1653
1621 def test_applies_to(self): 1654 def test_applies_to(self):
1622 os.environ['http_proxy'] = 'http://myproxy.example.com:80' 1655 os.environ['http_proxy'] = 'http://myproxy.example.com:80'
1623 os.environ['https_proxy'] = 'http://myproxy.example.com:81' 1656 os.environ['https_proxy'] = 'http://myproxy.example.com:81'
1624 os.environ['no_proxy'] = 'localhost,otherhost.domain.local,example.com' 1657 os.environ['no_proxy'] = 'localhost,otherhost.domain.local,example.com'
1625 pi = httplib2.ProxyInfo.from_environment() 1658 pi = httplib2.proxy_info_from_environment()
1626 self.assertFalse(pi.applies_to('localhost')) 1659 self.assertFalse(pi.applies_to('localhost'))
1627 self.assertTrue(pi.applies_to('www.google.com')) 1660 self.assertTrue(pi.applies_to('www.google.com'))
1628 self.assertFalse(pi.applies_to('www.example.com')) 1661 self.assertFalse(pi.applies_to('www.example.com'))
1629 1662
1630 def test_no_proxy_star(self): 1663 def test_no_proxy_star(self):
1631 os.environ['http_proxy'] = 'http://myproxy.example.com:80' 1664 os.environ['http_proxy'] = 'http://myproxy.example.com:80'
1632 os.environ['NO_PROXY'] = '*' 1665 os.environ['NO_PROXY'] = '*'
1633 pi = httplib2.ProxyInfo.from_environment() 1666 pi = httplib2.proxy_info_from_environment()
1634 for host in ('localhost', '169.254.38.192', 'www.google.com'): 1667 for host in ('localhost', '169.254.38.192', 'www.google.com'):
1635 self.assertFalse(pi.applies_to(host)) 1668 self.assertFalse(pi.applies_to(host))
1636 1669
1637 1670
1638 if __name__ == '__main__': 1671 if __name__ == '__main__':
1639 unittest.main() 1672 unittest.main()
OLDNEW
« no previous file with comments | « python2/httplib2/__init__.py ('k') | python3/httplib2/__init__.py » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld 1278:e6ce13d99bf5