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