65
64
from bzrlib.transport.http._pycurl import PyCurlTransport
68
load_tests = load_tests_apply_scenarios
71
def vary_by_http_client_implementation():
72
"""Test the two libraries we can use, pycurl and urllib."""
67
def load_tests(standard_tests, module, loader):
68
"""Multiply tests for http clients and protocol versions."""
69
result = loader.suiteClass()
71
# one for each transport implementation
72
t_tests, remaining_tests = tests.split_suite_by_condition(
73
standard_tests, tests.condition_isinstance((
74
TestHttpTransportRegistration,
75
TestHttpTransportUrls,
73
78
transport_scenarios = [
74
79
('urllib', dict(_transport=_urllib.HttpTransport_urllib,
75
80
_server=http_server.HttpServer_urllib,
80
85
('pycurl', dict(_transport=PyCurlTransport,
81
86
_server=http_server.HttpServer_PyCurl,
82
87
_url_protocol='http+pycurl',)))
83
return transport_scenarios
86
def vary_by_http_protocol_version():
87
"""Test on http/1.0 and 1.1"""
89
('HTTP/1.0', dict(_protocol_version='HTTP/1.0')),
90
('HTTP/1.1', dict(_protocol_version='HTTP/1.1')),
94
def vary_by_http_proxy_auth_scheme():
88
tests.multiply_tests(t_tests, transport_scenarios, result)
90
protocol_scenarios = [
91
('HTTP/1.0', dict(_protocol_version='HTTP/1.0')),
92
('HTTP/1.1', dict(_protocol_version='HTTP/1.1')),
95
# some tests are parametrized by the protocol version only
96
p_tests, remaining_tests = tests.split_suite_by_condition(
97
remaining_tests, tests.condition_isinstance((
100
tests.multiply_tests(p_tests, protocol_scenarios, result)
102
# each implementation tested with each HTTP version
103
tp_tests, remaining_tests = tests.split_suite_by_condition(
104
remaining_tests, tests.condition_isinstance((
105
SmartHTTPTunnellingTest,
106
TestDoCatchRedirections,
108
TestHTTPRedirections,
109
TestHTTPSilentRedirections,
110
TestLimitedRangeRequestServer,
114
TestSpecificRequestHandler,
116
tp_scenarios = tests.multiply_scenarios(transport_scenarios,
118
tests.multiply_tests(tp_tests, tp_scenarios, result)
120
# proxy auth: each auth scheme on all http versions on all implementations.
121
tppa_tests, remaining_tests = tests.split_suite_by_condition(
122
remaining_tests, tests.condition_isinstance((
125
proxy_auth_scheme_scenarios = [
96
126
('basic', dict(_auth_server=http_utils.ProxyBasicAuthServer)),
97
127
('digest', dict(_auth_server=http_utils.ProxyDigestAuthServer)),
99
dict(_auth_server=http_utils.ProxyBasicAndDigestAuthServer)),
129
dict(_auth_server=http_utils.ProxyBasicAndDigestAuthServer)),
103
def vary_by_http_auth_scheme():
131
tppa_scenarios = tests.multiply_scenarios(tp_scenarios,
132
proxy_auth_scheme_scenarios)
133
tests.multiply_tests(tppa_tests, tppa_scenarios, result)
135
# auth: each auth scheme on all http versions on all implementations.
136
tpa_tests, remaining_tests = tests.split_suite_by_condition(
137
remaining_tests, tests.condition_isinstance((
140
auth_scheme_scenarios = [
105
141
('basic', dict(_auth_server=http_utils.HTTPBasicAuthServer)),
106
142
('digest', dict(_auth_server=http_utils.HTTPDigestAuthServer)),
108
dict(_auth_server=http_utils.HTTPBasicAndDigestAuthServer)),
144
dict(_auth_server=http_utils.HTTPBasicAndDigestAuthServer)),
112
def vary_by_http_activity():
146
tpa_scenarios = tests.multiply_scenarios(tp_scenarios,
147
auth_scheme_scenarios)
148
tests.multiply_tests(tpa_tests, tpa_scenarios, result)
150
# activity: on all http[s] versions on all implementations
151
tpact_tests, remaining_tests = tests.split_suite_by_condition(
152
remaining_tests, tests.condition_isinstance((
113
155
activity_scenarios = [
114
156
('urllib,http', dict(_activity_server=ActivityHTTPServer,
115
_transport=_urllib.HttpTransport_urllib,)),
157
_transport=_urllib.HttpTransport_urllib,)),
117
159
if tests.HTTPSServerFeature.available():
118
160
activity_scenarios.append(
119
161
('urllib,https', dict(_activity_server=ActivityHTTPSServer,
120
_transport=_urllib.HttpTransport_urllib,)),)
162
_transport=_urllib.HttpTransport_urllib,)),)
121
163
if features.pycurl.available():
122
164
activity_scenarios.append(
123
165
('pycurl,http', dict(_activity_server=ActivityHTTPServer,
124
_transport=PyCurlTransport,)),)
166
_transport=PyCurlTransport,)),)
125
167
if tests.HTTPSServerFeature.available():
126
168
from bzrlib.tests import (
351
401
class TestHttpTransportUrls(tests.TestCase):
352
402
"""Test the http urls."""
354
scenarios = vary_by_http_client_implementation()
356
404
def test_abs_url(self):
357
405
"""Construction of absolute http URLs"""
358
t = self._transport('http://example.com/bzr/bzr.dev/')
406
t = self._transport('http://bazaar-vcs.org/bzr/bzr.dev/')
359
407
eq = self.assertEqualDiff
360
eq(t.abspath('.'), 'http://example.com/bzr/bzr.dev')
361
eq(t.abspath('foo/bar'), 'http://example.com/bzr/bzr.dev/foo/bar')
362
eq(t.abspath('.bzr'), 'http://example.com/bzr/bzr.dev/.bzr')
408
eq(t.abspath('.'), 'http://bazaar-vcs.org/bzr/bzr.dev')
409
eq(t.abspath('foo/bar'), 'http://bazaar-vcs.org/bzr/bzr.dev/foo/bar')
410
eq(t.abspath('.bzr'), 'http://bazaar-vcs.org/bzr/bzr.dev/.bzr')
363
411
eq(t.abspath('.bzr/1//2/./3'),
364
'http://example.com/bzr/bzr.dev/.bzr/1/2/3')
412
'http://bazaar-vcs.org/bzr/bzr.dev/.bzr/1/2/3')
366
414
def test_invalid_http_urls(self):
367
415
"""Trap invalid construction of urls"""
368
self._transport('http://example.com/bzr/bzr.dev/')
416
t = self._transport('http://bazaar-vcs.org/bzr/bzr.dev/')
369
417
self.assertRaises(errors.InvalidURL,
371
'http://http://example.com/bzr/bzr.dev/')
419
'http://http://bazaar-vcs.org/bzr/bzr.dev/')
373
421
def test_http_root_urls(self):
374
422
"""Construction of URLs from server root"""
375
t = self._transport('http://example.com/')
423
t = self._transport('http://bzr.ozlabs.org/')
376
424
eq = self.assertEqualDiff
377
425
eq(t.abspath('.bzr/tree-version'),
378
'http://example.com/.bzr/tree-version')
426
'http://bzr.ozlabs.org/.bzr/tree-version')
380
428
def test_http_impl_urls(self):
381
429
"""There are servers which ask for particular clients to connect"""
1074
1095
Only the urllib implementation is tested here.
1099
tests.TestCase.setUp(self)
1101
self.addCleanup(self._restore_env)
1103
def _install_env(self, env):
1104
for name, value in env.iteritems():
1105
self._old_env[name] = osutils.set_or_unset_env(name, value)
1107
def _restore_env(self):
1108
for name, value in self._old_env.iteritems():
1109
osutils.set_or_unset_env(name, value)
1077
1111
def _proxied_request(self):
1078
1112
handler = _urllib2_wrappers.ProxyHandler()
1079
1113
request = _urllib2_wrappers.Request('GET','http://baz/buzzle')
1080
1114
handler.set_proxy(request, 'http')
1083
def assertEvaluateProxyBypass(self, expected, host, no_proxy):
1084
handler = _urllib2_wrappers.ProxyHandler()
1085
self.assertEquals(expected,
1086
handler.evaluate_proxy_bypass(host, no_proxy))
1088
1117
def test_empty_user(self):
1089
self.overrideEnv('http_proxy', 'http://bar.com')
1118
self._install_env({'http_proxy': 'http://bar.com'})
1090
1119
request = self._proxied_request()
1091
1120
self.assertFalse(request.headers.has_key('Proxy-authorization'))
1093
1122
def test_invalid_proxy(self):
1094
1123
"""A proxy env variable without scheme"""
1095
self.overrideEnv('http_proxy', 'host:1234')
1124
self._install_env({'http_proxy': 'host:1234'})
1096
1125
self.assertRaises(errors.InvalidURL, self._proxied_request)
1098
def test_evaluate_proxy_bypass_true(self):
1099
"""The host is not proxied"""
1100
self.assertEvaluateProxyBypass(True, 'example.com', 'example.com')
1101
self.assertEvaluateProxyBypass(True, 'bzr.example.com', '*example.com')
1103
def test_evaluate_proxy_bypass_false(self):
1104
"""The host is proxied"""
1105
self.assertEvaluateProxyBypass(False, 'bzr.example.com', None)
1107
def test_evaluate_proxy_bypass_unknown(self):
1108
"""The host is not explicitly proxied"""
1109
self.assertEvaluateProxyBypass(None, 'example.com', 'not.example.com')
1110
self.assertEvaluateProxyBypass(None, 'bzr.example.com', 'example.com')
1112
def test_evaluate_proxy_bypass_empty_entries(self):
1113
"""Ignore empty entries"""
1114
self.assertEvaluateProxyBypass(None, 'example.com', '')
1115
self.assertEvaluateProxyBypass(None, 'example.com', ',')
1116
self.assertEvaluateProxyBypass(None, 'example.com', 'foo,,bar')
1119
1128
class TestProxyHttpServer(http_utils.TestCaseWithTwoWebservers):
1120
1129
"""Tests proxy server.
1149
1153
self.no_proxy_host = self.server_host_port
1150
1154
# The secondary server is the proxy
1151
1155
self.proxy_url = self.get_secondary_url()
1153
1158
def _testing_pycurl(self):
1154
1159
# TODO: This is duplicated for lots of the classes in this file
1155
1160
return (features.pycurl.available()
1156
1161
and self._transport == PyCurlTransport)
1158
def assertProxied(self):
1159
t = self.get_readonly_transport()
1160
self.assertEqual('proxied contents of foo\n', t.get('foo').read())
1162
def assertNotProxied(self):
1163
t = self.get_readonly_transport()
1164
self.assertEqual('contents of foo\n', t.get('foo').read())
1163
def _install_env(self, env):
1164
for name, value in env.iteritems():
1165
self._old_env[name] = osutils.set_or_unset_env(name, value)
1167
def _restore_env(self):
1168
for name, value in self._old_env.iteritems():
1169
osutils.set_or_unset_env(name, value)
1171
def proxied_in_env(self, env):
1172
self._install_env(env)
1173
t = self.get_readonly_transport()
1175
self.assertEqual('proxied contents of foo\n', t.get('foo').read())
1179
def not_proxied_in_env(self, env):
1180
self._install_env(env)
1181
t = self.get_readonly_transport()
1183
self.assertEqual('contents of foo\n', t.get('foo').read())
1166
1187
def test_http_proxy(self):
1167
self.overrideEnv('http_proxy', self.proxy_url)
1168
self.assertProxied()
1188
self.proxied_in_env({'http_proxy': self.proxy_url})
1170
1190
def test_HTTP_PROXY(self):
1171
1191
if self._testing_pycurl():
1174
1194
# about. Should we ?)
1175
1195
raise tests.TestNotApplicable(
1176
1196
'pycurl does not check HTTP_PROXY for security reasons')
1177
self.overrideEnv('HTTP_PROXY', self.proxy_url)
1178
self.assertProxied()
1197
self.proxied_in_env({'HTTP_PROXY': self.proxy_url})
1180
1199
def test_all_proxy(self):
1181
self.overrideEnv('all_proxy', self.proxy_url)
1182
self.assertProxied()
1200
self.proxied_in_env({'all_proxy': self.proxy_url})
1184
1202
def test_ALL_PROXY(self):
1185
self.overrideEnv('ALL_PROXY', self.proxy_url)
1186
self.assertProxied()
1203
self.proxied_in_env({'ALL_PROXY': self.proxy_url})
1188
1205
def test_http_proxy_with_no_proxy(self):
1189
self.overrideEnv('no_proxy', self.no_proxy_host)
1190
self.overrideEnv('http_proxy', self.proxy_url)
1191
self.assertNotProxied()
1206
self.not_proxied_in_env({'http_proxy': self.proxy_url,
1207
'no_proxy': self.no_proxy_host})
1193
1209
def test_HTTP_PROXY_with_NO_PROXY(self):
1194
1210
if self._testing_pycurl():
1195
1211
raise tests.TestNotApplicable(
1196
1212
'pycurl does not check HTTP_PROXY for security reasons')
1197
self.overrideEnv('NO_PROXY', self.no_proxy_host)
1198
self.overrideEnv('HTTP_PROXY', self.proxy_url)
1199
self.assertNotProxied()
1213
self.not_proxied_in_env({'HTTP_PROXY': self.proxy_url,
1214
'NO_PROXY': self.no_proxy_host})
1201
1216
def test_all_proxy_with_no_proxy(self):
1202
self.overrideEnv('no_proxy', self.no_proxy_host)
1203
self.overrideEnv('all_proxy', self.proxy_url)
1204
self.assertNotProxied()
1217
self.not_proxied_in_env({'all_proxy': self.proxy_url,
1218
'no_proxy': self.no_proxy_host})
1206
1220
def test_ALL_PROXY_with_NO_PROXY(self):
1207
self.overrideEnv('NO_PROXY', self.no_proxy_host)
1208
self.overrideEnv('ALL_PROXY', self.proxy_url)
1209
self.assertNotProxied()
1221
self.not_proxied_in_env({'ALL_PROXY': self.proxy_url,
1222
'NO_PROXY': self.no_proxy_host})
1211
1224
def test_http_proxy_without_scheme(self):
1212
self.overrideEnv('http_proxy', self.server_host_port)
1213
1225
if self._testing_pycurl():
1214
1226
# pycurl *ignores* invalid proxy env variables. If that ever change
1215
1227
# in the future, this test will fail indicating that pycurl do not
1216
1228
# ignore anymore such variables.
1217
self.assertNotProxied()
1229
self.not_proxied_in_env({'http_proxy': self.server_host_port})
1219
self.assertRaises(errors.InvalidURL, self.assertProxied)
1231
self.assertRaises(errors.InvalidURL,
1232
self.proxied_in_env,
1233
{'http_proxy': self.server_host_port})
1222
1236
class TestRanges(http_utils.TestCaseWithWebserver):
1223
1237
"""Test the Range header in GET methods."""
1225
scenarios = multiply_scenarios(
1226
vary_by_http_client_implementation(),
1227
vary_by_http_protocol_version(),
1230
1239
def setUp(self):
1231
1240
http_utils.TestCaseWithWebserver.setUp(self)
1232
1241
self.build_tree_contents([('a', '0123456789')],)
1622
1610
# Only one 'Authentication Required' error should occur
1623
1611
self.assertEqual(1, self.server.auth_required_errors)
1613
def test_user_from_auth_conf(self):
1614
if self._testing_pycurl():
1615
raise tests.TestNotApplicable(
1616
'pycurl does not support authentication.conf')
1619
self.server.add_user(user, password)
1620
# Create a minimal config file with the right password
1621
conf = config.AuthenticationConfig()
1622
conf._get_config().update(
1623
{'httptest': {'scheme': 'http', 'port': self.server.port,
1624
'user': user, 'password': password}})
1626
t = self.get_user_transport(None, None)
1627
# Issue a request to the server to connect
1628
self.assertEqual('contents of a\n', t.get('a').read())
1629
# Only one 'Authentication Required' error should occur
1630
self.assertEqual(1, self.server.auth_required_errors)
1625
1632
def test_changing_nonce(self):
1626
1633
if self._auth_server not in (http_utils.HTTPDigestAuthServer,
1627
1634
http_utils.ProxyDigestAuthServer):
1643
1650
# initial 'who are you' and a second 'who are you' with the new nonce)
1644
1651
self.assertEqual(2, self.server.auth_required_errors)
1646
def test_user_from_auth_conf(self):
1647
if self._testing_pycurl():
1648
raise tests.TestNotApplicable(
1649
'pycurl does not support authentication.conf')
1652
self.server.add_user(user, password)
1653
_setup_authentication_config(
1655
port=self.server.port,
1658
t = self.get_user_transport(None, None)
1659
# Issue a request to the server to connect
1660
self.assertEqual('contents of a\n', t.get('a').read())
1661
# Only one 'Authentication Required' error should occur
1662
self.assertEqual(1, self.server.auth_required_errors)
1665
def _setup_authentication_config(**kwargs):
1666
conf = config.AuthenticationConfig()
1667
conf._get_config().update({'httptest': kwargs})
1672
class TestUrllib2AuthHandler(tests.TestCaseWithTransport):
1673
"""Unit tests for glue by which urllib2 asks us for authentication"""
1675
def test_get_user_password_without_port(self):
1676
"""We cope if urllib2 doesn't tell us the port.
1678
See https://bugs.launchpad.net/bzr/+bug/654684
1682
_setup_authentication_config(
1687
handler = _urllib2_wrappers.HTTPAuthHandler()
1688
got_pass = handler.get_user_password(dict(
1695
self.assertEquals((user, password), got_pass)
1698
1655
class TestProxyAuth(TestAuth):
1699
1656
"""Test proxy authentication schemes."""
1701
scenarios = multiply_scenarios(
1702
vary_by_http_client_implementation(),
1703
vary_by_http_protocol_version(),
1704
vary_by_http_proxy_auth_scheme(),
1707
1658
_auth_header = 'Proxy-authorization'
1708
1659
_password_prompt_prefix = 'Proxy '
1709
1660
_username_prompt_prefix = 'Proxy '
1711
1662
def setUp(self):
1712
1663
super(TestProxyAuth, self).setUp()
1665
self.addCleanup(self._restore_env)
1713
1666
# Override the contents to avoid false positives
1714
1667
self.build_tree_contents([('a', 'not proxied contents of a\n'),
1715
1668
('b', 'not proxied contents of b\n'),
1720
1673
def get_user_transport(self, user, password):
1721
self.overrideEnv('all_proxy', self.get_user_url(user, password))
1674
self._install_env({'all_proxy': self.get_user_url(user, password)})
1722
1675
return TestAuth.get_user_transport(self, user, password)
1677
def _install_env(self, env):
1678
for name, value in env.iteritems():
1679
self._old_env[name] = osutils.set_or_unset_env(name, value)
1681
def _restore_env(self):
1682
for name, value in self._old_env.iteritems():
1683
osutils.set_or_unset_env(name, value)
1724
1685
def test_empty_pass(self):
1725
1686
if self._testing_pycurl():