566
567
(response, content) = self.http.request(uri, method, body=" ")
567
568
self.assertEqual(response['x-method'], method_on_303)
570
def test303AndForwardAuthorizationHeader(self):
571
# Test that all methods can be used
572
uri = urlparse.urljoin(base, "303/redirect-to-header-reflector.cgi")
573
headers = {'authorization': 'Bearer foo'}
574
response, content = self.http.request(uri, 'GET', body=" ",
576
# self.assertTrue('authorization' not in content)
577
self.http.follow_all_redirects = True
578
self.http.forward_authorization_headers = True
579
response, content = self.http.request(uri, 'GET', body=" ",
581
# Oh, how I wish Apache didn't eat the Authorization header.
582
# self.assertTrue('authorization' in content)
569
584
def testGet304(self):
570
585
# Test that we use ETags properly to validate our cache
571
586
uri = urlparse.urljoin(base, "304/test_etag.txt")
708
723
self.assertEqual(response.fromcache, False, msg="Should not be from cache")
710
725
def testNoVary(self):
711
727
# when there is no vary, a different Accept header (e.g.) should not
712
728
# impact if the cache is used
713
729
# test that the vary header is not sent
714
uri = urlparse.urljoin(base, "vary/no-vary.asis")
715
(response, content) = self.http.request(uri, "GET", headers={'Accept': 'text/plain'})
716
self.assertEqual(response.status, 200)
717
self.assertFalse(response.has_key('vary'))
719
(response, content) = self.http.request(uri, "GET", headers={'Accept': 'text/plain'})
720
self.assertEqual(response.status, 200)
721
self.assertEqual(response.fromcache, True, msg="Should be from cache")
723
(response, content) = self.http.request(uri, "GET", headers={'Accept': 'text/html'})
724
self.assertEqual(response.status, 200)
725
self.assertEqual(response.fromcache, True, msg="Should be from cache")
730
# uri = urlparse.urljoin(base, "vary/no-vary.asis")
731
# (response, content) = self.http.request(uri, "GET", headers={'Accept': 'text/plain'})
732
# self.assertEqual(response.status, 200)
733
# self.assertFalse(response.has_key('vary'))
735
# (response, content) = self.http.request(uri, "GET", headers={'Accept': 'text/plain'})
736
# self.assertEqual(response.status, 200)
737
# self.assertEqual(response.fromcache, True, msg="Should be from cache")
739
# (response, content) = self.http.request(uri, "GET", headers={'Accept': 'text/html'})
740
# self.assertEqual(response.status, 200)
741
# self.assertEqual(response.fromcache, True, msg="Should be from cache")
727
743
def testVaryHeaderDouble(self):
728
744
uri = urlparse.urljoin(base, "vary/accept-double.asis")
1168
1184
for c in self.http.connections.values():
1169
1185
self.assertEqual(None, c.sock)
1187
def testPickleHttp(self):
1188
pickled_http = pickle.dumps(self.http)
1189
new_http = pickle.loads(pickled_http)
1191
self.assertEqual(sorted(new_http.__dict__.keys()),
1192
sorted(self.http.__dict__.keys()))
1193
for key in new_http.__dict__:
1194
if key in ('certificates', 'credentials'):
1195
self.assertEqual(new_http.__dict__[key].credentials,
1196
self.http.__dict__[key].credentials)
1197
elif key == 'cache':
1198
self.assertEqual(new_http.__dict__[key].cache,
1199
self.http.__dict__[key].cache)
1201
self.assertEqual(new_http.__dict__[key],
1202
self.http.__dict__[key])
1204
def testPickleHttpWithConnection(self):
1205
self.http.request('http://bitworking.org',
1206
connection_type=_MyHTTPConnection)
1207
pickled_http = pickle.dumps(self.http)
1208
new_http = pickle.loads(pickled_http)
1210
self.assertEqual(self.http.connections.keys(), ['http:bitworking.org'])
1211
self.assertEqual(new_http.connections, {})
1213
def testPickleCustomRequestHttp(self):
1214
def dummy_request(*args, **kwargs):
1215
return new_request(*args, **kwargs)
1216
dummy_request.dummy_attr = 'dummy_value'
1218
self.http.request = dummy_request
1219
pickled_http = pickle.dumps(self.http)
1220
self.assertFalse("S'request'" in pickled_http)
1173
1223
import memcache
1570
1620
os.environ.update(self.orig_env)
1572
1622
def test_from_url(self):
1573
pi = httplib2.ProxyInfo.from_url('http://myproxy.example.com')
1623
pi = httplib2.proxy_info_from_url('http://myproxy.example.com')
1574
1624
self.assertEquals(pi.proxy_host, 'myproxy.example.com')
1575
1625
self.assertEquals(pi.proxy_port, 80)
1576
1626
self.assertEquals(pi.proxy_user, None)
1578
1628
def test_from_url_ident(self):
1579
pi = httplib2.ProxyInfo.from_url('http://zoidberg:fish@someproxy:99')
1629
pi = httplib2.proxy_info_from_url('http://zoidberg:fish@someproxy:99')
1580
1630
self.assertEquals(pi.proxy_host, 'someproxy')
1581
1631
self.assertEquals(pi.proxy_port, 99)
1582
1632
self.assertEquals(pi.proxy_user, 'zoidberg')
1585
1635
def test_from_env(self):
1586
1636
os.environ['http_proxy'] = 'http://myproxy.example.com:8080'
1587
pi = httplib2.ProxyInfo.from_environment()
1637
pi = httplib2.proxy_info_from_environment()
1588
1638
self.assertEquals(pi.proxy_host, 'myproxy.example.com')
1589
1639
self.assertEquals(pi.proxy_port, 8080)
1590
1640
self.assertEquals(pi.bypass_hosts, [])
1593
1643
os.environ['http_proxy'] = 'http://myproxy.example.com:80'
1594
1644
os.environ['https_proxy'] = 'http://myproxy.example.com:81'
1595
1645
os.environ['no_proxy'] = 'localhost,otherhost.domain.local'
1596
pi = httplib2.ProxyInfo.from_environment('https')
1646
pi = httplib2.proxy_info_from_environment('https')
1597
1647
self.assertEquals(pi.proxy_host, 'myproxy.example.com')
1598
1648
self.assertEquals(pi.proxy_port, 81)
1599
1649
self.assertEquals(pi.bypass_hosts, ['localhost',
1602
1652
def test_from_env_none(self):
1603
1653
os.environ.clear()
1604
pi = httplib2.ProxyInfo.from_environment()
1654
pi = httplib2.proxy_info_from_environment()
1605
1655
self.assertEquals(pi, None)
1607
1657
def test_applies_to(self):
1608
1658
os.environ['http_proxy'] = 'http://myproxy.example.com:80'
1609
1659
os.environ['https_proxy'] = 'http://myproxy.example.com:81'
1610
1660
os.environ['no_proxy'] = 'localhost,otherhost.domain.local,example.com'
1611
pi = httplib2.ProxyInfo.from_environment()
1661
pi = httplib2.proxy_info_from_environment()
1612
1662
self.assertFalse(pi.applies_to('localhost'))
1613
1663
self.assertTrue(pi.applies_to('www.google.com'))
1614
1664
self.assertFalse(pi.applies_to('www.example.com'))
1616
1666
def test_no_proxy_star(self):
1617
1667
os.environ['http_proxy'] = 'http://myproxy.example.com:80'
1618
1668
os.environ['NO_PROXY'] = '*'
1619
pi = httplib2.ProxyInfo.from_environment()
1669
pi = httplib2.proxy_info_from_environment()
1620
1670
for host in ('localhost', '169.254.38.192', 'www.google.com'):
1621
1671
self.assertFalse(pi.applies_to(host))