93
def vary_by_http_proxy_auth_scheme():
95
('basic', dict(_auth_server=http_utils.ProxyBasicAuthServer)),
96
('digest', dict(_auth_server=http_utils.ProxyDigestAuthServer)),
98
dict(_auth_server=http_utils.ProxyBasicAndDigestAuthServer)),
102
96
def vary_by_http_auth_scheme():
104
98
('basic', dict(_auth_server=http_utils.HTTPBasicAuthServer)),
105
99
('digest', dict(_auth_server=http_utils.HTTPDigestAuthServer)),
107
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 ')
111
126
def vary_by_http_activity():
178
193
self._sock.bind(('127.0.0.1', 0))
179
194
self.host, self.port = self._sock.getsockname()
180
195
self._ready = threading.Event()
181
self._thread = test_server.ThreadWithException(
182
event=self._ready, target=self._accept_read_and_reply)
196
self._thread = test_server.TestThread(
197
sync_event=self._ready, target=self._accept_read_and_reply)
183
198
self._thread.start()
184
199
if 'threads' in tests.selftest_debug_flags:
185
200
sys.stderr.write('Thread started: %s\n' % (self._thread.ident,))
254
269
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)
257
306
class TestHTTPServer(tests.TestCase):
258
307
"""Test the HTTP servers implementations."""
1076
1125
def _proxied_request(self):
1077
1126
handler = _urllib2_wrappers.ProxyHandler()
1078
request = _urllib2_wrappers.Request('GET','http://baz/buzzle')
1127
request = _urllib2_wrappers.Request('GET', 'http://baz/buzzle')
1079
1128
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))
1082
1136
def test_empty_user(self):
1083
1137
self.overrideEnv('http_proxy', 'http://bar.com')
1084
1138
request = self._proxied_request()
1085
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')
1144
request = self._proxied_request()
1145
self.assertFalse(request.headers.has_key('Proxy-authorization'))
1087
1147
def test_invalid_proxy(self):
1088
1148
"""A proxy env variable without scheme"""
1089
1149
self.overrideEnv('http_proxy', 'host:1234')
1090
1150
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')
1093
1173
class TestProxyHttpServer(http_utils.TestCaseWithTwoWebservers):
1094
1174
"""Tests proxy server.
1423
1503
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)
1426
1535
class TestAuth(http_utils.TestCaseWithWebserver):
1427
1536
"""Test authentication scheme"""
1584
1687
ui.ui_factory = tests.TestUIFactory(stdin=stdin_content,
1585
1688
stderr=tests.StringIOWrapper())
1586
1689
# Create a minimal config file with the right password
1587
_setup_authentication_config(
1589
port=self.server.port,
1690
_setup_authentication_config(scheme='http', port=self.server.port,
1691
user=user, password=password)
1592
1692
# Issue a request to the server to connect
1593
1693
self.assertEqual('contents of a\n',t.get('a').read())
1594
1694
# stdin should have been left untouched
1625
1725
password = 'foo'
1626
1726
self.server.add_user(user, password)
1627
_setup_authentication_config(
1629
port=self.server.port,
1727
_setup_authentication_config(scheme='http', port=self.server.port,
1728
user=user, password=password)
1632
1729
t = self.get_user_transport(None, None)
1633
1730
# Issue a request to the server to connect
1634
1731
self.assertEqual('contents of a\n', t.get('a').read())
1635
1732
# Only one 'Authentication Required' error should occur
1636
1733
self.assertEqual(1, self.server.auth_required_errors)
1639
def _setup_authentication_config(**kwargs):
1640
conf = config.AuthenticationConfig()
1641
conf._get_config().update({'httptest': kwargs})
1646
class TestUrllib2AuthHandler(tests.TestCaseWithTransport):
1647
"""Unit tests for glue by which urllib2 asks us for authentication"""
1649
def test_get_user_password_without_port(self):
1650
"""We cope if urllib2 doesn't tell us the port.
1652
See https://bugs.launchpad.net/bzr/+bug/654684
1735
def test_no_credential_leaks_in_log(self):
1736
self.overrideAttr(debug, 'debug_flags', set(['http']))
1656
_setup_authentication_config(
1661
handler = _urllib2_wrappers.HTTPAuthHandler()
1662
got_pass = handler.get_user_password(dict(
1669
self.assertEquals((user, password), got_pass)
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,))
1672
1762
class TestProxyAuth(TestAuth):
1673
"""Test proxy authentication schemes."""
1763
"""Test proxy authentication schemes.
1765
This inherits from TestAuth to tweak the setUp and filter some failing
1675
1769
scenarios = multiply_scenarios(
1676
1770
vary_by_http_client_implementation(),
1947
2037
tests.TestCase.setUp(self)
1948
2038
self.server = self._activity_server(self._protocol_version)
1949
2039
self.server.start_server()
1950
self.activities = {}
2040
_activities = {} # Don't close over self and create a cycle
1951
2041
def report_activity(t, bytes, direction):
1952
count = self.activities.get(direction, 0)
2042
count = _activities.get(direction, 0)
1954
self.activities[direction] = count
2044
_activities[direction] = count
2045
self.activities = _activities
1956
2047
# We override at class level because constructors may propagate the
1957
2048
# bound method and render instance overriding ineffective (an