64
65
from bzrlib.transport.http._pycurl import PyCurlTransport
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,
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."""
78
73
transport_scenarios = [
79
74
('urllib', dict(_transport=_urllib.HttpTransport_urllib,
80
75
_server=http_server.HttpServer_urllib,
85
80
('pycurl', dict(_transport=PyCurlTransport,
86
81
_server=http_server.HttpServer_PyCurl,
87
82
_url_protocol='http+pycurl',)))
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 = [
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():
126
96
('basic', dict(_auth_server=http_utils.ProxyBasicAuthServer)),
127
97
('digest', dict(_auth_server=http_utils.ProxyDigestAuthServer)),
129
dict(_auth_server=http_utils.ProxyBasicAndDigestAuthServer)),
99
dict(_auth_server=http_utils.ProxyBasicAndDigestAuthServer)),
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 = [
103
def vary_by_http_auth_scheme():
141
105
('basic', dict(_auth_server=http_utils.HTTPBasicAuthServer)),
142
106
('digest', dict(_auth_server=http_utils.HTTPDigestAuthServer)),
144
dict(_auth_server=http_utils.HTTPBasicAndDigestAuthServer)),
108
dict(_auth_server=http_utils.HTTPBasicAndDigestAuthServer)),
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((
112
def vary_by_http_activity():
155
113
activity_scenarios = [
156
114
('urllib,http', dict(_activity_server=ActivityHTTPServer,
157
_transport=_urllib.HttpTransport_urllib,)),
115
_transport=_urllib.HttpTransport_urllib,)),
159
117
if tests.HTTPSServerFeature.available():
160
118
activity_scenarios.append(
161
119
('urllib,https', dict(_activity_server=ActivityHTTPSServer,
162
_transport=_urllib.HttpTransport_urllib,)),)
120
_transport=_urllib.HttpTransport_urllib,)),)
163
121
if features.pycurl.available():
164
122
activity_scenarios.append(
165
123
('pycurl,http', dict(_activity_server=ActivityHTTPServer,
166
_transport=PyCurlTransport,)),)
124
_transport=PyCurlTransport,)),)
167
125
if tests.HTTPSServerFeature.available():
168
126
from bzrlib.tests import (
305
255
self.assertEqual('realm="Thou should not pass"', remainder)
258
class TestHTTPRangeParsing(tests.TestCase):
261
super(TestHTTPRangeParsing, self).setUp()
262
# We focus on range parsing here and ignore everything else
263
class RequestHandler(http_server.TestingHTTPRequestHandler):
264
def setup(self): pass
265
def handle(self): pass
266
def finish(self): pass
268
self.req_handler = RequestHandler(None, None, None)
270
def assertRanges(self, ranges, header, file_size):
271
self.assertEquals(ranges,
272
self.req_handler._parse_ranges(header, file_size))
274
def test_simple_range(self):
275
self.assertRanges([(0,2)], 'bytes=0-2', 12)
278
self.assertRanges([(8, 11)], 'bytes=-4', 12)
280
def test_tail_bigger_than_file(self):
281
self.assertRanges([(0, 11)], 'bytes=-99', 12)
283
def test_range_without_end(self):
284
self.assertRanges([(4, 11)], 'bytes=4-', 12)
286
def test_invalid_ranges(self):
287
self.assertRanges(None, 'bytes=12-22', 12)
288
self.assertRanges(None, 'bytes=1-3,12-22', 12)
289
self.assertRanges(None, 'bytes=-', 12)
308
292
class TestHTTPServer(tests.TestCase):
309
293
"""Test the HTTP servers implementations."""
401
385
class TestHttpTransportUrls(tests.TestCase):
402
386
"""Test the http urls."""
388
scenarios = vary_by_http_client_implementation()
404
390
def test_abs_url(self):
405
391
"""Construction of absolute http URLs"""
406
t = self._transport('http://bazaar-vcs.org/bzr/bzr.dev/')
392
t = self._transport('http://example.com/bzr/bzr.dev/')
407
393
eq = self.assertEqualDiff
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')
394
eq(t.abspath('.'), 'http://example.com/bzr/bzr.dev')
395
eq(t.abspath('foo/bar'), 'http://example.com/bzr/bzr.dev/foo/bar')
396
eq(t.abspath('.bzr'), 'http://example.com/bzr/bzr.dev/.bzr')
411
397
eq(t.abspath('.bzr/1//2/./3'),
412
'http://bazaar-vcs.org/bzr/bzr.dev/.bzr/1/2/3')
398
'http://example.com/bzr/bzr.dev/.bzr/1/2/3')
414
400
def test_invalid_http_urls(self):
415
401
"""Trap invalid construction of urls"""
416
t = self._transport('http://bazaar-vcs.org/bzr/bzr.dev/')
402
self._transport('http://example.com/bzr/bzr.dev/')
417
403
self.assertRaises(errors.InvalidURL,
419
'http://http://bazaar-vcs.org/bzr/bzr.dev/')
405
'http://http://example.com/bzr/bzr.dev/')
421
407
def test_http_root_urls(self):
422
408
"""Construction of URLs from server root"""
423
t = self._transport('http://bzr.ozlabs.org/')
409
t = self._transport('http://example.com/')
424
410
eq = self.assertEqualDiff
425
411
eq(t.abspath('.bzr/tree-version'),
426
'http://bzr.ozlabs.org/.bzr/tree-version')
412
'http://example.com/.bzr/tree-version')
428
414
def test_http_impl_urls(self):
429
415
"""There are servers which ask for particular clients to connect"""
1095
1108
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)
1111
1111
def _proxied_request(self):
1112
1112
handler = _urllib2_wrappers.ProxyHandler()
1113
request = _urllib2_wrappers.Request('GET','http://baz/buzzle')
1113
request = _urllib2_wrappers.Request('GET', 'http://baz/buzzle')
1114
1114
handler.set_proxy(request, 'http')
1117
def assertEvaluateProxyBypass(self, expected, host, no_proxy):
1118
handler = _urllib2_wrappers.ProxyHandler()
1119
self.assertEquals(expected,
1120
handler.evaluate_proxy_bypass(host, no_proxy))
1117
1122
def test_empty_user(self):
1118
self._install_env({'http_proxy': 'http://bar.com'})
1123
self.overrideEnv('http_proxy', 'http://bar.com')
1124
request = self._proxied_request()
1125
self.assertFalse(request.headers.has_key('Proxy-authorization'))
1127
def test_user_with_at(self):
1128
self.overrideEnv('http_proxy',
1129
'http://username@domain:password@proxy_host:1234')
1119
1130
request = self._proxied_request()
1120
1131
self.assertFalse(request.headers.has_key('Proxy-authorization'))
1122
1133
def test_invalid_proxy(self):
1123
1134
"""A proxy env variable without scheme"""
1124
self._install_env({'http_proxy': 'host:1234'})
1135
self.overrideEnv('http_proxy', 'host:1234')
1125
1136
self.assertRaises(errors.InvalidURL, self._proxied_request)
1138
def test_evaluate_proxy_bypass_true(self):
1139
"""The host is not proxied"""
1140
self.assertEvaluateProxyBypass(True, 'example.com', 'example.com')
1141
self.assertEvaluateProxyBypass(True, 'bzr.example.com', '*example.com')
1143
def test_evaluate_proxy_bypass_false(self):
1144
"""The host is proxied"""
1145
self.assertEvaluateProxyBypass(False, 'bzr.example.com', None)
1147
def test_evaluate_proxy_bypass_unknown(self):
1148
"""The host is not explicitly proxied"""
1149
self.assertEvaluateProxyBypass(None, 'example.com', 'not.example.com')
1150
self.assertEvaluateProxyBypass(None, 'bzr.example.com', 'example.com')
1152
def test_evaluate_proxy_bypass_empty_entries(self):
1153
"""Ignore empty entries"""
1154
self.assertEvaluateProxyBypass(None, 'example.com', '')
1155
self.assertEvaluateProxyBypass(None, 'example.com', ',')
1156
self.assertEvaluateProxyBypass(None, 'example.com', 'foo,,bar')
1128
1159
class TestProxyHttpServer(http_utils.TestCaseWithTwoWebservers):
1129
1160
"""Tests proxy server.
1153
1189
self.no_proxy_host = self.server_host_port
1154
1190
# The secondary server is the proxy
1155
1191
self.proxy_url = self.get_secondary_url()
1158
1193
def _testing_pycurl(self):
1159
1194
# TODO: This is duplicated for lots of the classes in this file
1160
1195
return (features.pycurl.available()
1161
1196
and self._transport == PyCurlTransport)
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())
1198
def assertProxied(self):
1199
t = self.get_readonly_transport()
1200
self.assertEqual('proxied contents of foo\n', t.get('foo').read())
1202
def assertNotProxied(self):
1203
t = self.get_readonly_transport()
1204
self.assertEqual('contents of foo\n', t.get('foo').read())
1187
1206
def test_http_proxy(self):
1188
self.proxied_in_env({'http_proxy': self.proxy_url})
1207
self.overrideEnv('http_proxy', self.proxy_url)
1208
self.assertProxied()
1190
1210
def test_HTTP_PROXY(self):
1191
1211
if self._testing_pycurl():
1194
1214
# about. Should we ?)
1195
1215
raise tests.TestNotApplicable(
1196
1216
'pycurl does not check HTTP_PROXY for security reasons')
1197
self.proxied_in_env({'HTTP_PROXY': self.proxy_url})
1217
self.overrideEnv('HTTP_PROXY', self.proxy_url)
1218
self.assertProxied()
1199
1220
def test_all_proxy(self):
1200
self.proxied_in_env({'all_proxy': self.proxy_url})
1221
self.overrideEnv('all_proxy', self.proxy_url)
1222
self.assertProxied()
1202
1224
def test_ALL_PROXY(self):
1203
self.proxied_in_env({'ALL_PROXY': self.proxy_url})
1225
self.overrideEnv('ALL_PROXY', self.proxy_url)
1226
self.assertProxied()
1205
1228
def test_http_proxy_with_no_proxy(self):
1206
self.not_proxied_in_env({'http_proxy': self.proxy_url,
1207
'no_proxy': self.no_proxy_host})
1229
self.overrideEnv('no_proxy', self.no_proxy_host)
1230
self.overrideEnv('http_proxy', self.proxy_url)
1231
self.assertNotProxied()
1209
1233
def test_HTTP_PROXY_with_NO_PROXY(self):
1210
1234
if self._testing_pycurl():
1211
1235
raise tests.TestNotApplicable(
1212
1236
'pycurl does not check HTTP_PROXY for security reasons')
1213
self.not_proxied_in_env({'HTTP_PROXY': self.proxy_url,
1214
'NO_PROXY': self.no_proxy_host})
1237
self.overrideEnv('NO_PROXY', self.no_proxy_host)
1238
self.overrideEnv('HTTP_PROXY', self.proxy_url)
1239
self.assertNotProxied()
1216
1241
def test_all_proxy_with_no_proxy(self):
1217
self.not_proxied_in_env({'all_proxy': self.proxy_url,
1218
'no_proxy': self.no_proxy_host})
1242
self.overrideEnv('no_proxy', self.no_proxy_host)
1243
self.overrideEnv('all_proxy', self.proxy_url)
1244
self.assertNotProxied()
1220
1246
def test_ALL_PROXY_with_NO_PROXY(self):
1221
self.not_proxied_in_env({'ALL_PROXY': self.proxy_url,
1222
'NO_PROXY': self.no_proxy_host})
1247
self.overrideEnv('NO_PROXY', self.no_proxy_host)
1248
self.overrideEnv('ALL_PROXY', self.proxy_url)
1249
self.assertNotProxied()
1224
1251
def test_http_proxy_without_scheme(self):
1252
self.overrideEnv('http_proxy', self.server_host_port)
1225
1253
if self._testing_pycurl():
1226
1254
# pycurl *ignores* invalid proxy env variables. If that ever change
1227
1255
# in the future, this test will fail indicating that pycurl do not
1228
1256
# ignore anymore such variables.
1229
self.not_proxied_in_env({'http_proxy': self.server_host_port})
1257
self.assertNotProxied()
1231
self.assertRaises(errors.InvalidURL,
1232
self.proxied_in_env,
1233
{'http_proxy': self.server_host_port})
1259
self.assertRaises(errors.InvalidURL, self.assertProxied)
1236
1262
class TestRanges(http_utils.TestCaseWithWebserver):
1237
1263
"""Test the Range header in GET methods."""
1265
scenarios = multiply_scenarios(
1266
vary_by_http_client_implementation(),
1267
vary_by_http_protocol_version(),
1239
1270
def setUp(self):
1240
1271
http_utils.TestCaseWithWebserver.setUp(self)
1241
1272
self.build_tree_contents([('a', '0123456789')],)
1610
1662
# Only one 'Authentication Required' error should occur
1611
1663
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)
1632
1665
def test_changing_nonce(self):
1633
1666
if self._auth_server not in (http_utils.HTTPDigestAuthServer,
1634
1667
http_utils.ProxyDigestAuthServer):
1650
1683
# initial 'who are you' and a second 'who are you' with the new nonce)
1651
1684
self.assertEqual(2, self.server.auth_required_errors)
1686
def test_user_from_auth_conf(self):
1687
if self._testing_pycurl():
1688
raise tests.TestNotApplicable(
1689
'pycurl does not support authentication.conf')
1692
self.server.add_user(user, password)
1693
_setup_authentication_config(
1695
port=self.server.port,
1698
t = self.get_user_transport(None, None)
1699
# Issue a request to the server to connect
1700
self.assertEqual('contents of a\n', t.get('a').read())
1701
# Only one 'Authentication Required' error should occur
1702
self.assertEqual(1, self.server.auth_required_errors)
1705
def _setup_authentication_config(**kwargs):
1706
conf = config.AuthenticationConfig()
1707
conf._get_config().update({'httptest': kwargs})
1712
class TestUrllib2AuthHandler(tests.TestCaseWithTransport):
1713
"""Unit tests for glue by which urllib2 asks us for authentication"""
1715
def test_get_user_password_without_port(self):
1716
"""We cope if urllib2 doesn't tell us the port.
1718
See https://bugs.launchpad.net/bzr/+bug/654684
1722
_setup_authentication_config(
1727
handler = _urllib2_wrappers.HTTPAuthHandler()
1728
got_pass = handler.get_user_password(dict(
1735
self.assertEquals((user, password), got_pass)
1655
1738
class TestProxyAuth(TestAuth):
1656
1739
"""Test proxy authentication schemes."""
1741
scenarios = multiply_scenarios(
1742
vary_by_http_client_implementation(),
1743
vary_by_http_protocol_version(),
1744
vary_by_http_proxy_auth_scheme(),
1658
1747
_auth_header = 'Proxy-authorization'
1659
1748
_password_prompt_prefix = 'Proxy '
1660
1749
_username_prompt_prefix = 'Proxy '
1662
1751
def setUp(self):
1663
1752
super(TestProxyAuth, self).setUp()
1665
self.addCleanup(self._restore_env)
1666
1753
# Override the contents to avoid false positives
1667
1754
self.build_tree_contents([('a', 'not proxied contents of a\n'),
1668
1755
('b', 'not proxied contents of b\n'),
1673
1760
def get_user_transport(self, user, password):
1674
self._install_env({'all_proxy': self.get_user_url(user, password)})
1761
self.overrideEnv('all_proxy', self.get_user_url(user, password))
1675
1762
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)
1685
1764
def test_empty_pass(self):
1686
1765
if self._testing_pycurl():