82
85
('pycurl', dict(_transport=PyCurlTransport,
83
86
_server=http_server.HttpServer_PyCurl,
84
87
_url_protocol='http+pycurl',)))
85
return transport_scenarios
88
def vary_by_http_protocol_version():
89
"""Test on http/1.0 and 1.1"""
91
('HTTP/1.0', dict(_protocol_version='HTTP/1.0')),
92
('HTTP/1.1', dict(_protocol_version='HTTP/1.1')),
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 = [
126
('basic', dict(_auth_server=http_utils.ProxyBasicAuthServer)),
127
('digest', dict(_auth_server=http_utils.ProxyDigestAuthServer)),
129
dict(_auth_server=http_utils.ProxyBasicAndDigestAuthServer)),
96
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 = [
98
141
('basic', dict(_auth_server=http_utils.HTTPBasicAuthServer)),
99
142
('digest', dict(_auth_server=http_utils.HTTPDigestAuthServer)),
101
dict(_auth_server=http_utils.HTTPBasicAndDigestAuthServer)),
103
# Add some attributes common to all scenarios
104
for scenario_id, scenario_dict in scenarios:
105
scenario_dict.update(_auth_header='Authorization',
106
_username_prompt_prefix='',
107
_password_prompt_prefix='')
111
def vary_by_http_proxy_auth_scheme():
113
('proxy-basic', dict(_auth_server=http_utils.ProxyBasicAuthServer)),
114
('proxy-digest', dict(_auth_server=http_utils.ProxyDigestAuthServer)),
115
('proxy-basicdigest',
116
dict(_auth_server=http_utils.ProxyBasicAndDigestAuthServer)),
118
# Add some attributes common to all scenarios
119
for scenario_id, scenario_dict in scenarios:
120
scenario_dict.update(_auth_header='Proxy-Authorization',
121
_username_prompt_prefix='Proxy ',
122
_password_prompt_prefix='Proxy ')
126
def vary_by_http_activity():
144
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((
127
155
activity_scenarios = [
128
156
('urllib,http', dict(_activity_server=ActivityHTTPServer,
129
_transport=_urllib.HttpTransport_urllib,)),
157
_transport=_urllib.HttpTransport_urllib,)),
131
159
if tests.HTTPSServerFeature.available():
132
160
activity_scenarios.append(
133
161
('urllib,https', dict(_activity_server=ActivityHTTPSServer,
134
_transport=_urllib.HttpTransport_urllib,)),)
162
_transport=_urllib.HttpTransport_urllib,)),)
135
163
if features.pycurl.available():
136
164
activity_scenarios.append(
137
165
('pycurl,http', dict(_activity_server=ActivityHTTPServer,
138
_transport=PyCurlTransport,)),)
166
_transport=PyCurlTransport,)),)
139
167
if tests.HTTPSServerFeature.available():
140
168
from bzrlib.tests import (
269
305
self.assertEqual('realm="Thou should not pass"', remainder)
272
class TestHTTPRangeParsing(tests.TestCase):
275
super(TestHTTPRangeParsing, self).setUp()
276
# We focus on range parsing here and ignore everything else
277
class RequestHandler(http_server.TestingHTTPRequestHandler):
278
def setup(self): pass
279
def handle(self): pass
280
def finish(self): pass
282
self.req_handler = RequestHandler(None, None, None)
284
def assertRanges(self, ranges, header, file_size):
285
self.assertEquals(ranges,
286
self.req_handler._parse_ranges(header, file_size))
288
def test_simple_range(self):
289
self.assertRanges([(0,2)], 'bytes=0-2', 12)
292
self.assertRanges([(8, 11)], 'bytes=-4', 12)
294
def test_tail_bigger_than_file(self):
295
self.assertRanges([(0, 11)], 'bytes=-99', 12)
297
def test_range_without_end(self):
298
self.assertRanges([(4, 11)], 'bytes=4-', 12)
300
def test_invalid_ranges(self):
301
self.assertRanges(None, 'bytes=12-22', 12)
302
self.assertRanges(None, 'bytes=1-3,12-22', 12)
303
self.assertRanges(None, 'bytes=-', 12)
306
308
class TestHTTPServer(tests.TestCase):
307
309
"""Test the HTTP servers implementations."""
399
401
class TestHttpTransportUrls(tests.TestCase):
400
402
"""Test the http urls."""
402
scenarios = vary_by_http_client_implementation()
404
404
def test_abs_url(self):
405
405
"""Construction of absolute http URLs"""
406
t = self._transport('http://example.com/bzr/bzr.dev/')
406
t = self._transport('http://bazaar-vcs.org/bzr/bzr.dev/')
407
407
eq = self.assertEqualDiff
408
eq(t.abspath('.'), 'http://example.com/bzr/bzr.dev')
409
eq(t.abspath('foo/bar'), 'http://example.com/bzr/bzr.dev/foo/bar')
410
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')
411
411
eq(t.abspath('.bzr/1//2/./3'),
412
'http://example.com/bzr/bzr.dev/.bzr/1/2/3')
412
'http://bazaar-vcs.org/bzr/bzr.dev/.bzr/1/2/3')
414
414
def test_invalid_http_urls(self):
415
415
"""Trap invalid construction of urls"""
416
self._transport('http://example.com/bzr/bzr.dev/')
416
t = self._transport('http://bazaar-vcs.org/bzr/bzr.dev/')
417
417
self.assertRaises(errors.InvalidURL,
419
'http://http://example.com/bzr/bzr.dev/')
419
'http://http://bazaar-vcs.org/bzr/bzr.dev/')
421
421
def test_http_root_urls(self):
422
422
"""Construction of URLs from server root"""
423
t = self._transport('http://example.com/')
423
t = self._transport('http://bzr.ozlabs.org/')
424
424
eq = self.assertEqualDiff
425
425
eq(t.abspath('.bzr/tree-version'),
426
'http://example.com/.bzr/tree-version')
426
'http://bzr.ozlabs.org/.bzr/tree-version')
428
428
def test_http_impl_urls(self):
429
429
"""There are servers which ask for particular clients to connect"""
1122
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)
1125
1111
def _proxied_request(self):
1126
1112
handler = _urllib2_wrappers.ProxyHandler()
1127
request = _urllib2_wrappers.Request('GET', 'http://baz/buzzle')
1113
request = _urllib2_wrappers.Request('GET','http://baz/buzzle')
1128
1114
handler.set_proxy(request, 'http')
1131
def assertEvaluateProxyBypass(self, expected, host, no_proxy):
1132
handler = _urllib2_wrappers.ProxyHandler()
1133
self.assertEquals(expected,
1134
handler.evaluate_proxy_bypass(host, no_proxy))
1136
1117
def test_empty_user(self):
1137
self.overrideEnv('http_proxy', 'http://bar.com')
1138
request = self._proxied_request()
1139
self.assertFalse(request.headers.has_key('Proxy-authorization'))
1141
def test_user_with_at(self):
1142
self.overrideEnv('http_proxy',
1143
'http://username@domain:password@proxy_host:1234')
1118
self._install_env({'http_proxy': 'http://bar.com'})
1144
1119
request = self._proxied_request()
1145
1120
self.assertFalse(request.headers.has_key('Proxy-authorization'))
1147
1122
def test_invalid_proxy(self):
1148
1123
"""A proxy env variable without scheme"""
1149
self.overrideEnv('http_proxy', 'host:1234')
1124
self._install_env({'http_proxy': 'host:1234'})
1150
1125
self.assertRaises(errors.InvalidURL, self._proxied_request)
1152
def test_evaluate_proxy_bypass_true(self):
1153
"""The host is not proxied"""
1154
self.assertEvaluateProxyBypass(True, 'example.com', 'example.com')
1155
self.assertEvaluateProxyBypass(True, 'bzr.example.com', '*example.com')
1157
def test_evaluate_proxy_bypass_false(self):
1158
"""The host is proxied"""
1159
self.assertEvaluateProxyBypass(False, 'bzr.example.com', None)
1161
def test_evaluate_proxy_bypass_unknown(self):
1162
"""The host is not explicitly proxied"""
1163
self.assertEvaluateProxyBypass(None, 'example.com', 'not.example.com')
1164
self.assertEvaluateProxyBypass(None, 'bzr.example.com', 'example.com')
1166
def test_evaluate_proxy_bypass_empty_entries(self):
1167
"""Ignore empty entries"""
1168
self.assertEvaluateProxyBypass(None, 'example.com', '')
1169
self.assertEvaluateProxyBypass(None, 'example.com', ',')
1170
self.assertEvaluateProxyBypass(None, 'example.com', 'foo,,bar')
1173
1128
class TestProxyHttpServer(http_utils.TestCaseWithTwoWebservers):
1174
1129
"""Tests proxy server.
1203
1153
self.no_proxy_host = self.server_host_port
1204
1154
# The secondary server is the proxy
1205
1155
self.proxy_url = self.get_secondary_url()
1207
1158
def _testing_pycurl(self):
1208
1159
# TODO: This is duplicated for lots of the classes in this file
1209
1160
return (features.pycurl.available()
1210
1161
and self._transport == PyCurlTransport)
1212
def assertProxied(self):
1213
t = self.get_readonly_transport()
1214
self.assertEqual('proxied contents of foo\n', t.get('foo').read())
1216
def assertNotProxied(self):
1217
t = self.get_readonly_transport()
1218
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())
1220
1187
def test_http_proxy(self):
1221
self.overrideEnv('http_proxy', self.proxy_url)
1222
self.assertProxied()
1188
self.proxied_in_env({'http_proxy': self.proxy_url})
1224
1190
def test_HTTP_PROXY(self):
1225
1191
if self._testing_pycurl():
1228
1194
# about. Should we ?)
1229
1195
raise tests.TestNotApplicable(
1230
1196
'pycurl does not check HTTP_PROXY for security reasons')
1231
self.overrideEnv('HTTP_PROXY', self.proxy_url)
1232
self.assertProxied()
1197
self.proxied_in_env({'HTTP_PROXY': self.proxy_url})
1234
1199
def test_all_proxy(self):
1235
self.overrideEnv('all_proxy', self.proxy_url)
1236
self.assertProxied()
1200
self.proxied_in_env({'all_proxy': self.proxy_url})
1238
1202
def test_ALL_PROXY(self):
1239
self.overrideEnv('ALL_PROXY', self.proxy_url)
1240
self.assertProxied()
1203
self.proxied_in_env({'ALL_PROXY': self.proxy_url})
1242
1205
def test_http_proxy_with_no_proxy(self):
1243
self.overrideEnv('no_proxy', self.no_proxy_host)
1244
self.overrideEnv('http_proxy', self.proxy_url)
1245
self.assertNotProxied()
1206
self.not_proxied_in_env({'http_proxy': self.proxy_url,
1207
'no_proxy': self.no_proxy_host})
1247
1209
def test_HTTP_PROXY_with_NO_PROXY(self):
1248
1210
if self._testing_pycurl():
1249
1211
raise tests.TestNotApplicable(
1250
1212
'pycurl does not check HTTP_PROXY for security reasons')
1251
self.overrideEnv('NO_PROXY', self.no_proxy_host)
1252
self.overrideEnv('HTTP_PROXY', self.proxy_url)
1253
self.assertNotProxied()
1213
self.not_proxied_in_env({'HTTP_PROXY': self.proxy_url,
1214
'NO_PROXY': self.no_proxy_host})
1255
1216
def test_all_proxy_with_no_proxy(self):
1256
self.overrideEnv('no_proxy', self.no_proxy_host)
1257
self.overrideEnv('all_proxy', self.proxy_url)
1258
self.assertNotProxied()
1217
self.not_proxied_in_env({'all_proxy': self.proxy_url,
1218
'no_proxy': self.no_proxy_host})
1260
1220
def test_ALL_PROXY_with_NO_PROXY(self):
1261
self.overrideEnv('NO_PROXY', self.no_proxy_host)
1262
self.overrideEnv('ALL_PROXY', self.proxy_url)
1263
self.assertNotProxied()
1221
self.not_proxied_in_env({'ALL_PROXY': self.proxy_url,
1222
'NO_PROXY': self.no_proxy_host})
1265
1224
def test_http_proxy_without_scheme(self):
1266
self.overrideEnv('http_proxy', self.server_host_port)
1267
1225
if self._testing_pycurl():
1268
1226
# pycurl *ignores* invalid proxy env variables. If that ever change
1269
1227
# in the future, this test will fail indicating that pycurl do not
1270
1228
# ignore anymore such variables.
1271
self.assertNotProxied()
1229
self.not_proxied_in_env({'http_proxy': self.server_host_port})
1273
self.assertRaises(errors.InvalidURL, self.assertProxied)
1231
self.assertRaises(errors.InvalidURL,
1232
self.proxied_in_env,
1233
{'http_proxy': self.server_host_port})
1276
1236
class TestRanges(http_utils.TestCaseWithWebserver):
1277
1237
"""Test the Range header in GET methods."""
1279
scenarios = multiply_scenarios(
1280
vary_by_http_client_implementation(),
1281
vary_by_http_protocol_version(),
1284
1239
def setUp(self):
1285
1240
http_utils.TestCaseWithWebserver.setUp(self)
1286
1241
self.build_tree_contents([('a', '0123456789')],)
1503
1443
self.get_a, self.old_transport, redirected)
1506
def _setup_authentication_config(**kwargs):
1507
conf = config.AuthenticationConfig()
1508
conf._get_config().update({'httptest': kwargs})
1512
class TestUrllib2AuthHandler(tests.TestCaseWithTransport):
1513
"""Unit tests for glue by which urllib2 asks us for authentication"""
1515
def test_get_user_password_without_port(self):
1516
"""We cope if urllib2 doesn't tell us the port.
1518
See https://bugs.launchpad.net/bzr/+bug/654684
1522
_setup_authentication_config(scheme='http', host='localhost',
1523
user=user, password=password)
1524
handler = _urllib2_wrappers.HTTPAuthHandler()
1525
got_pass = handler.get_user_password(dict(
1532
self.assertEquals((user, password), got_pass)
1535
1446
class TestAuth(http_utils.TestCaseWithWebserver):
1536
1447
"""Test authentication scheme"""
1538
scenarios = multiply_scenarios(
1539
vary_by_http_client_implementation(),
1540
vary_by_http_protocol_version(),
1541
vary_by_http_auth_scheme(),
1449
_auth_header = 'Authorization'
1450
_password_prompt_prefix = ''
1451
_username_prompt_prefix = ''
1544
1455
def setUp(self):
1545
1456
super(TestAuth, self).setUp()
1696
1610
# Only one 'Authentication Required' error should occur
1697
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)
1699
1632
def test_changing_nonce(self):
1700
1633
if self._auth_server not in (http_utils.HTTPDigestAuthServer,
1701
1634
http_utils.ProxyDigestAuthServer):
1717
1650
# initial 'who are you' and a second 'who are you' with the new nonce)
1718
1651
self.assertEqual(2, self.server.auth_required_errors)
1720
def test_user_from_auth_conf(self):
1721
if self._testing_pycurl():
1722
raise tests.TestNotApplicable(
1723
'pycurl does not support authentication.conf')
1726
self.server.add_user(user, password)
1727
_setup_authentication_config(scheme='http', port=self.server.port,
1728
user=user, password=password)
1729
t = self.get_user_transport(None, None)
1730
# Issue a request to the server to connect
1731
self.assertEqual('contents of a\n', t.get('a').read())
1732
# Only one 'Authentication Required' error should occur
1733
self.assertEqual(1, self.server.auth_required_errors)
1735
def test_no_credential_leaks_in_log(self):
1736
self.overrideAttr(debug, 'debug_flags', set(['http']))
1738
password = 'very-sensitive-password'
1739
self.server.add_user(user, password)
1740
t = self.get_user_transport(user, password)
1741
# Capture the debug calls to mutter
1744
lines = args[0] % args[1:]
1745
# Some calls output multiple lines, just split them now since we
1746
# care about a single one later.
1747
self.mutters.extend(lines.splitlines())
1748
self.overrideAttr(trace, 'mutter', mutter)
1749
# Issue a request to the server to connect
1750
self.assertEqual(True, t.has('a'))
1751
# Only one 'Authentication Required' error should occur
1752
self.assertEqual(1, self.server.auth_required_errors)
1753
# Since the authentification succeeded, there should be a corresponding
1755
sent_auth_headers = [line for line in self.mutters
1756
if line.startswith('> %s' % (self._auth_header,))]
1757
self.assertLength(1, sent_auth_headers)
1758
self.assertStartsWith(sent_auth_headers[0],
1759
'> %s: <masked>' % (self._auth_header,))
1762
1655
class TestProxyAuth(TestAuth):
1763
"""Test proxy authentication schemes.
1765
This inherits from TestAuth to tweak the setUp and filter some failing
1769
scenarios = multiply_scenarios(
1770
vary_by_http_client_implementation(),
1771
vary_by_http_protocol_version(),
1772
vary_by_http_proxy_auth_scheme(),
1656
"""Test proxy authentication schemes."""
1658
_auth_header = 'Proxy-authorization'
1659
_password_prompt_prefix = 'Proxy '
1660
_username_prompt_prefix = 'Proxy '
1775
1662
def setUp(self):
1776
1663
super(TestProxyAuth, self).setUp()
1665
self.addCleanup(self._restore_env)
1777
1666
# Override the contents to avoid false positives
1778
1667
self.build_tree_contents([('a', 'not proxied contents of a\n'),
1779
1668
('b', 'not proxied contents of b\n'),