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():
128
113
('urllib,http', dict(_activity_server=ActivityHTTPServer,
129
114
_transport=_urllib.HttpTransport_urllib,)),
131
if features.HTTPSServerFeature.available():
116
if tests.HTTPSServerFeature.available():
132
117
activity_scenarios.append(
133
118
('urllib,https', dict(_activity_server=ActivityHTTPSServer,
134
119
_transport=_urllib.HttpTransport_urllib,)),)
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."""
1049
999
self.assertEqual('single', t._range_hint)
1052
class TruncatedBeforeBoundaryRequestHandler(
1053
http_server.TestingHTTPRequestHandler):
1054
"""Truncation before a boundary, like in bug 198646"""
1056
_truncated_ranges = 1
1058
def get_multiple_ranges(self, file, file_size, ranges):
1059
self.send_response(206)
1060
self.send_header('Accept-Ranges', 'bytes')
1062
self.send_header('Content-Type',
1063
'multipart/byteranges; boundary=%s' % boundary)
1064
boundary_line = '--%s\r\n' % boundary
1065
# Calculate the Content-Length
1067
for (start, end) in ranges:
1068
content_length += len(boundary_line)
1069
content_length += self._header_line_length(
1070
'Content-type', 'application/octet-stream')
1071
content_length += self._header_line_length(
1072
'Content-Range', 'bytes %d-%d/%d' % (start, end, file_size))
1073
content_length += len('\r\n') # end headers
1074
content_length += end - start # + 1
1075
content_length += len(boundary_line)
1076
self.send_header('Content-length', content_length)
1079
# Send the multipart body
1081
for (start, end) in ranges:
1082
if cur + self._truncated_ranges >= len(ranges):
1083
# Abruptly ends the response and close the connection
1084
self.close_connection = 1
1086
self.wfile.write(boundary_line)
1087
self.send_header('Content-type', 'application/octet-stream')
1088
self.send_header('Content-Range', 'bytes %d-%d/%d'
1089
% (start, end, file_size))
1091
self.send_range_content(file, start, end - start + 1)
1094
self.wfile.write(boundary_line)
1097
class TestTruncatedBeforeBoundary(TestSpecificRequestHandler):
1098
"""Tests the case of bug 198646, disconnecting before a boundary."""
1100
_req_handler_class = TruncatedBeforeBoundaryRequestHandler
1103
super(TestTruncatedBeforeBoundary, self).setUp()
1104
self.build_tree_contents([('a', '0123456789')],)
1106
def test_readv_with_short_reads(self):
1107
server = self.get_readonly_server()
1108
t = self.get_readonly_transport()
1109
# Force separate ranges for each offset
1110
t._bytes_to_read_before_seek = 0
1111
ireadv = iter(t.readv('a', ((0, 1), (2, 1), (4, 2), (9, 1))))
1112
self.assertEqual((0, '0'), ireadv.next())
1113
self.assertEqual((2, '2'), ireadv.next())
1114
self.assertEqual((4, '45'), ireadv.next())
1115
self.assertEqual((9, '9'), ireadv.next())
1118
1002
class LimitedRangeRequestHandler(http_server.TestingHTTPRequestHandler):
1119
1003
"""Errors out when range specifiers exceed the limit"""
1192
1076
def _proxied_request(self):
1193
1077
handler = _urllib2_wrappers.ProxyHandler()
1194
request = _urllib2_wrappers.Request('GET', 'http://baz/buzzle')
1078
request = _urllib2_wrappers.Request('GET','http://baz/buzzle')
1195
1079
handler.set_proxy(request, 'http')
1198
def assertEvaluateProxyBypass(self, expected, host, no_proxy):
1199
handler = _urllib2_wrappers.ProxyHandler()
1200
self.assertEquals(expected,
1201
handler.evaluate_proxy_bypass(host, no_proxy))
1203
1082
def test_empty_user(self):
1204
1083
self.overrideEnv('http_proxy', 'http://bar.com')
1205
1084
request = self._proxied_request()
1206
1085
self.assertFalse(request.headers.has_key('Proxy-authorization'))
1208
def test_user_with_at(self):
1209
self.overrideEnv('http_proxy',
1210
'http://username@domain:password@proxy_host:1234')
1211
request = self._proxied_request()
1212
self.assertFalse(request.headers.has_key('Proxy-authorization'))
1214
1087
def test_invalid_proxy(self):
1215
1088
"""A proxy env variable without scheme"""
1216
1089
self.overrideEnv('http_proxy', 'host:1234')
1217
1090
self.assertRaises(errors.InvalidURL, self._proxied_request)
1219
def test_evaluate_proxy_bypass_true(self):
1220
"""The host is not proxied"""
1221
self.assertEvaluateProxyBypass(True, 'example.com', 'example.com')
1222
self.assertEvaluateProxyBypass(True, 'bzr.example.com', '*example.com')
1224
def test_evaluate_proxy_bypass_false(self):
1225
"""The host is proxied"""
1226
self.assertEvaluateProxyBypass(False, 'bzr.example.com', None)
1228
def test_evaluate_proxy_bypass_unknown(self):
1229
"""The host is not explicitly proxied"""
1230
self.assertEvaluateProxyBypass(None, 'example.com', 'not.example.com')
1231
self.assertEvaluateProxyBypass(None, 'bzr.example.com', 'example.com')
1233
def test_evaluate_proxy_bypass_empty_entries(self):
1234
"""Ignore empty entries"""
1235
self.assertEvaluateProxyBypass(None, 'example.com', '')
1236
self.assertEvaluateProxyBypass(None, 'example.com', ',')
1237
self.assertEvaluateProxyBypass(None, 'example.com', 'foo,,bar')
1240
1093
class TestProxyHttpServer(http_utils.TestCaseWithTwoWebservers):
1241
1094
"""Tests proxy server.
1570
1423
self.get_a, self.old_transport, redirected)
1573
def _setup_authentication_config(**kwargs):
1574
conf = config.AuthenticationConfig()
1575
conf._get_config().update({'httptest': kwargs})
1579
class TestUrllib2AuthHandler(tests.TestCaseWithTransport):
1580
"""Unit tests for glue by which urllib2 asks us for authentication"""
1582
def test_get_user_password_without_port(self):
1583
"""We cope if urllib2 doesn't tell us the port.
1585
See https://bugs.launchpad.net/bzr/+bug/654684
1589
_setup_authentication_config(scheme='http', host='localhost',
1590
user=user, password=password)
1591
handler = _urllib2_wrappers.HTTPAuthHandler()
1592
got_pass = handler.get_user_password(dict(
1599
self.assertEquals((user, password), got_pass)
1602
1426
class TestAuth(http_utils.TestCaseWithWebserver):
1603
1427
"""Test authentication scheme"""
1755
1584
ui.ui_factory = tests.TestUIFactory(stdin=stdin_content,
1756
1585
stderr=tests.StringIOWrapper())
1757
1586
# Create a minimal config file with the right password
1758
_setup_authentication_config(scheme='http', port=self.server.port,
1759
user=user, password=password)
1587
_setup_authentication_config(
1589
port=self.server.port,
1760
1592
# Issue a request to the server to connect
1761
1593
self.assertEqual('contents of a\n',t.get('a').read())
1762
1594
# stdin should have been left untouched
1793
1625
password = 'foo'
1794
1626
self.server.add_user(user, password)
1795
_setup_authentication_config(scheme='http', port=self.server.port,
1796
user=user, password=password)
1627
_setup_authentication_config(
1629
port=self.server.port,
1797
1632
t = self.get_user_transport(None, None)
1798
1633
# Issue a request to the server to connect
1799
1634
self.assertEqual('contents of a\n', t.get('a').read())
1800
1635
# Only one 'Authentication Required' error should occur
1801
1636
self.assertEqual(1, self.server.auth_required_errors)
1803
def test_no_credential_leaks_in_log(self):
1804
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
1806
password = 'very-sensitive-password'
1807
self.server.add_user(user, password)
1808
t = self.get_user_transport(user, password)
1809
# Capture the debug calls to mutter
1812
lines = args[0] % args[1:]
1813
# Some calls output multiple lines, just split them now since we
1814
# care about a single one later.
1815
self.mutters.extend(lines.splitlines())
1816
self.overrideAttr(trace, 'mutter', mutter)
1817
# Issue a request to the server to connect
1818
self.assertEqual(True, t.has('a'))
1819
# Only one 'Authentication Required' error should occur
1820
self.assertEqual(1, self.server.auth_required_errors)
1821
# Since the authentification succeeded, there should be a corresponding
1823
sent_auth_headers = [line for line in self.mutters
1824
if line.startswith('> %s' % (self._auth_header,))]
1825
self.assertLength(1, sent_auth_headers)
1826
self.assertStartsWith(sent_auth_headers[0],
1827
'> %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)
1830
1672
class TestProxyAuth(TestAuth):
1831
"""Test proxy authentication schemes.
1833
This inherits from TestAuth to tweak the setUp and filter some failing
1673
"""Test proxy authentication schemes."""
1837
1675
scenarios = multiply_scenarios(
1838
1676
vary_by_http_client_implementation(),
1917
1759
# The 'readv' command in the smart protocol both sends and receives
1918
1760
# bulk data, so we use that.
1919
1761
self.build_tree(['data-file'])
1920
http_transport = transport.get_transport_from_url(
1921
self.http_server.get_url())
1762
http_transport = transport.get_transport(self.http_server.get_url())
1922
1763
medium = http_transport.get_smart_medium()
1923
1764
# Since we provide the medium, the url below will be mostly ignored
1924
1765
# during the test, as long as the path is '/'.
2014
1853
r = t._redirected_to('http://www.example.com/foo',
2015
1854
'http://foo.example.com/foo/subdir')
2016
1855
self.assertIsInstance(r, type(t))
2017
self.assertEquals('http://foo.example.com/foo/subdir/',
2020
1857
def test_redirected_to_same_host_sibling_protocol(self):
2021
1858
t = self._transport('http://www.example.com/foo')
2022
1859
r = t._redirected_to('http://www.example.com/foo',
2023
1860
'https://www.example.com/foo')
2024
1861
self.assertIsInstance(r, type(t))
2025
self.assertEquals('https://www.example.com/foo/',
2028
1863
def test_redirected_to_same_host_different_protocol(self):
2029
1864
t = self._transport('http://www.example.com/foo')
2030
1865
r = t._redirected_to('http://www.example.com/foo',
2031
1866
'ftp://www.example.com/foo')
2032
1867
self.assertNotEquals(type(r), type(t))
2033
self.assertEquals('ftp://www.example.com/foo/', r.external_url())
2035
def test_redirected_to_same_host_specific_implementation(self):
2036
t = self._transport('http://www.example.com/foo')
2037
r = t._redirected_to('http://www.example.com/foo',
2038
'https+urllib://www.example.com/foo')
2039
self.assertEquals('https://www.example.com/foo/', r.external_url())
2041
1869
def test_redirected_to_different_host_same_user(self):
2042
1870
t = self._transport('http://joe@www.example.com/foo')
2043
1871
r = t._redirected_to('http://www.example.com/foo',
2044
1872
'https://foo.example.com/foo')
2045
1873
self.assertIsInstance(r, type(t))
2046
self.assertEqual(t._parsed_url.user, r._parsed_url.user)
2047
self.assertEquals('https://joe@foo.example.com/foo/', r.external_url())
1874
self.assertEqual(t._user, r._user)
2050
1877
class PredefinedRequestHandler(http_server.TestingHTTPRequestHandler):
2120
1947
tests.TestCase.setUp(self)
2121
1948
self.server = self._activity_server(self._protocol_version)
2122
1949
self.server.start_server()
2123
_activities = {} # Don't close over self and create a cycle
1950
self.activities = {}
2124
1951
def report_activity(t, bytes, direction):
2125
count = _activities.get(direction, 0)
1952
count = self.activities.get(direction, 0)
2127
_activities[direction] = count
2128
self.activities = _activities
1954
self.activities[direction] = count
2130
1956
# We override at class level because constructors may propagate the
2131
1957
# bound method and render instance overriding ineffective (an