83
82
_server=http_server.HttpServer_urllib,
84
83
_qualified_prefix='http+urllib',)),
85
if features.pycurl.available():
87
86
transport_scenarios.append(
88
87
('pycurl', dict(_transport=PyCurlTransport,
89
88
_server=http_server.HttpServer_PyCurl,
90
89
_qualified_prefix='http+pycurl',)))
91
adapter.scenarios = transport_scenarios
92
tests.adapt_tests(t_tests, adapter, result)
90
tests.multiply_tests(t_tests, transport_scenarios, result)
94
# multiplied by one for each protocol version
92
# each implementation tested with each HTTP version
95
93
tp_tests, remaining_tests = tests.split_suite_by_condition(
96
94
remaining_tests, tests.condition_isinstance((
97
95
SmartHTTPTunnellingTest,
109
107
('HTTP/1.0', dict(_protocol_version='HTTP/1.0')),
110
108
('HTTP/1.1', dict(_protocol_version='HTTP/1.1')),
112
tp_scenarios = tests.multiply_scenarios(adapter.scenarios,
110
tp_scenarios = tests.multiply_scenarios(transport_scenarios,
113
111
protocol_scenarios)
114
adapter.scenarios = tp_scenarios
115
tests.adapt_tests(tp_tests, adapter, result)
117
# multiplied by one for each authentication scheme
112
tests.multiply_tests(tp_tests, tp_scenarios, result)
114
# proxy auth: each auth scheme on all http versions on all implementations.
115
tppa_tests, remaining_tests = tests.split_suite_by_condition(
116
remaining_tests, tests.condition_isinstance((
119
proxy_auth_scheme_scenarios = [
120
('basic', dict(_auth_server=http_utils.ProxyBasicAuthServer)),
121
('digest', dict(_auth_server=http_utils.ProxyDigestAuthServer)),
123
dict(_auth_server=http_utils.ProxyBasicAndDigestAuthServer)),
125
tppa_scenarios = tests.multiply_scenarios(tp_scenarios,
126
proxy_auth_scheme_scenarios)
127
tests.multiply_tests(tppa_tests, tppa_scenarios, result)
129
# auth: each auth scheme on all http versions on all implementations.
118
130
tpa_tests, remaining_tests = tests.split_suite_by_condition(
119
131
remaining_tests, tests.condition_isinstance((
122
134
auth_scheme_scenarios = [
123
('basic', dict(_auth_scheme='basic')),
124
('digest', dict(_auth_scheme='digest')),
135
('basic', dict(_auth_server=http_utils.HTTPBasicAuthServer)),
136
('digest', dict(_auth_server=http_utils.HTTPDigestAuthServer)),
138
dict(_auth_server=http_utils.HTTPBasicAndDigestAuthServer)),
126
adapter.scenarios = tests.multiply_scenarios(adapter.scenarios,
127
auth_scheme_scenarios)
128
tests.adapt_tests(tpa_tests, adapter, result)
140
tpa_scenarios = tests.multiply_scenarios(tp_scenarios,
141
auth_scheme_scenarios)
142
tests.multiply_tests(tpa_tests, tpa_scenarios, result)
144
# activity: on all http[s] versions on all implementations
130
145
tpact_tests, remaining_tests = tests.split_suite_by_condition(
131
146
remaining_tests, tests.condition_isinstance((
134
149
activity_scenarios = [
135
('http', dict(_activity_server=ActivityHTTPServer)),
150
('urllib,http', dict(_activity_server=ActivityHTTPServer,
151
_transport=_urllib.HttpTransport_urllib,)),
137
153
if tests.HTTPSServerFeature.available():
138
154
activity_scenarios.append(
139
('https', dict(_activity_server=ActivityHTTPSServer,)))
140
adapter.scenarios = tests.multiply_scenarios(tp_scenarios,
142
tests.adapt_tests(tpact_tests, adapter, result)
155
('urllib,https', dict(_activity_server=ActivityHTTPSServer,
156
_transport=_urllib.HttpTransport_urllib,)),)
157
if features.pycurl.available():
158
activity_scenarios.append(
159
('pycurl,http', dict(_activity_server=ActivityHTTPServer,
160
_transport=PyCurlTransport,)),)
161
if tests.HTTPSServerFeature.available():
162
from bzrlib.tests import (
165
# FIXME: Until we have a better way to handle self-signed
166
# certificates (like allowing them in a test specific
167
# authentication.conf for example), we need some specialized pycurl
168
# transport for tests.
169
class HTTPS_pycurl_transport(PyCurlTransport):
171
def __init__(self, base, _from_transport=None):
172
super(HTTPS_pycurl_transport, self).__init__(
173
base, _from_transport)
174
self.cabundle = str(ssl_certs.build_path('ca.crt'))
176
activity_scenarios.append(
177
('pycurl,https', dict(_activity_server=ActivityHTTPSServer,
178
_transport=HTTPS_pycurl_transport,)),)
180
tpact_scenarios = tests.multiply_scenarios(activity_scenarios,
182
tests.multiply_tests(tpact_tests, tpact_scenarios, result)
144
184
# No parametrization for the remaining tests
145
185
result.addTests(remaining_tests)
260
class TestAuthHeader(tests.TestCase):
262
def parse_header(self, header, auth_handler_class=None):
263
if auth_handler_class is None:
264
auth_handler_class = _urllib2_wrappers.AbstractAuthHandler
265
self.auth_handler = auth_handler_class()
266
return self.auth_handler._parse_auth_header(header)
268
def test_empty_header(self):
269
scheme, remainder = self.parse_header('')
270
self.assertEqual('', scheme)
271
self.assertIs(None, remainder)
273
def test_negotiate_header(self):
274
scheme, remainder = self.parse_header('Negotiate')
275
self.assertEqual('negotiate', scheme)
276
self.assertIs(None, remainder)
278
def test_basic_header(self):
279
scheme, remainder = self.parse_header(
280
'Basic realm="Thou should not pass"')
281
self.assertEqual('basic', scheme)
282
self.assertEqual('realm="Thou should not pass"', remainder)
284
def test_basic_extract_realm(self):
285
scheme, remainder = self.parse_header(
286
'Basic realm="Thou should not pass"',
287
_urllib2_wrappers.BasicAuthHandler)
288
match, realm = self.auth_handler.extract_realm(remainder)
289
self.assertTrue(match is not None)
290
self.assertEqual('Thou should not pass', realm)
292
def test_digest_header(self):
293
scheme, remainder = self.parse_header(
294
'Digest realm="Thou should not pass"')
295
self.assertEqual('digest', scheme)
296
self.assertEqual('realm="Thou should not pass"', remainder)
216
299
class TestHTTPServer(tests.TestCase):
217
300
"""Test the HTTP servers implementations."""
310
388
def test_url_parsing(self):
311
389
f = FakeManager()
312
390
url = http.extract_auth('http://example.com', f)
313
self.assertEquals('http://example.com', url)
314
self.assertEquals(0, len(f.credentials))
391
self.assertEqual('http://example.com', url)
392
self.assertEqual(0, len(f.credentials))
315
393
url = http.extract_auth(
316
'http://user:pass@www.bazaar-vcs.org/bzr/bzr.dev', f)
317
self.assertEquals('http://www.bazaar-vcs.org/bzr/bzr.dev', url)
318
self.assertEquals(1, len(f.credentials))
319
self.assertEquals([None, 'www.bazaar-vcs.org', 'user', 'pass'],
394
'http://user:pass@example.com/bzr/bzr.dev', f)
395
self.assertEqual('http://example.com/bzr/bzr.dev', url)
396
self.assertEqual(1, len(f.credentials))
397
self.assertEqual([None, 'example.com', 'user', 'pass'],
323
401
class TestHttpTransportUrls(tests.TestCase):
370
448
https by supplying a fake version_info that do not
376
raise tests.TestSkipped('pycurl not present')
451
self.requireFeature(features.pycurl)
452
# Import the module locally now that we now it's available.
453
pycurl = features.pycurl.module
378
version_info_orig = pycurl.version_info
380
# Now that we have pycurl imported, we can fake its version_info
381
# This was taken from a windows pycurl without SSL
383
pycurl.version_info = lambda : (2,
391
('ftp', 'gopher', 'telnet',
392
'dict', 'ldap', 'http', 'file'),
396
self.assertRaises(errors.DependencyNotPresent, self._transport,
397
'https://launchpad.net')
399
# Restore the right function
400
pycurl.version_info = version_info_orig
455
self.overrideAttr(pycurl, 'version_info',
456
# Fake the pycurl version_info This was taken from
457
# a windows pycurl without SSL (thanks to bialix)
466
('ftp', 'gopher', 'telnet',
467
'dict', 'ldap', 'http', 'file'),
471
self.assertRaises(errors.DependencyNotPresent, self._transport,
472
'https://launchpad.net')
403
475
class TestHTTPConnections(http_utils.TestCaseWithWebserver):
544
617
# for details) make no distinction between a closed
545
618
# socket and badly formatted status line, so we can't
546
619
# just test for ConnectionError, we have to test
547
# InvalidHttpResponse too.
548
self.assertRaises((errors.ConnectionError, errors.InvalidHttpResponse),
620
# InvalidHttpResponse too. And pycurl may raise ConnectionReset
621
# instead of ConnectionError too.
622
self.assertRaises(( errors.ConnectionError, errors.ConnectionReset,
623
errors.InvalidHttpResponse),
549
624
t.has, 'foo/bar')
551
626
def test_http_get(self):
552
627
server = self.get_readonly_server()
553
628
t = self._transport(server.get_url())
554
self.assertRaises((errors.ConnectionError, errors.InvalidHttpResponse),
629
self.assertRaises((errors.ConnectionError, errors.ConnectionReset,
630
errors.InvalidHttpResponse),
555
631
t.get, 'foo/bar')
683
759
self.assertEqual(None, server.host)
684
760
self.assertEqual(None, server.port)
686
def test_setUp_and_tearDown(self):
762
def test_setUp_and_stop(self):
687
763
server = RecordingServer(expect_body_tail=None)
764
server.start_server()
690
766
self.assertNotEqual(None, server.host)
691
767
self.assertNotEqual(None, server.port)
694
770
self.assertEqual(None, server.host)
695
771
self.assertEqual(None, server.port)
697
773
def test_send_receive_bytes(self):
698
server = RecordingServer(expect_body_tail='c')
700
self.addCleanup(server.tearDown)
774
server = RecordingServer(expect_body_tail='c', scheme='http')
775
self.start_server(server)
701
776
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
702
777
sock.connect((server.host, server.port))
703
778
sock.sendall('abc')
1484
1553
# initial 'who are you' and 'this is not you, who are you')
1485
1554
self.assertEqual(2, self.server.auth_required_errors)
1556
def test_prompt_for_username(self):
1557
if self._testing_pycurl():
1558
raise tests.TestNotApplicable(
1559
'pycurl cannot prompt, it handles auth by embedding'
1560
' user:pass in urls only')
1562
self.server.add_user('joe', 'foo')
1563
t = self.get_user_transport(None, None)
1564
stdout = tests.StringIOWrapper()
1565
stderr = tests.StringIOWrapper()
1566
ui.ui_factory = tests.TestUIFactory(stdin='joe\nfoo\n',
1567
stdout=stdout, stderr=stderr)
1568
self.assertEqual('contents of a\n',t.get('a').read())
1569
# stdin should be empty
1570
self.assertEqual('', ui.ui_factory.stdin.readline())
1572
expected_prompt = self._expected_username_prompt(t._unqualified_scheme)
1573
self.assertEqual(expected_prompt, stderr.read(len(expected_prompt)))
1574
self.assertEqual('', stdout.getvalue())
1575
self._check_password_prompt(t._unqualified_scheme, 'joe',
1487
1578
def test_prompt_for_password(self):
1488
1579
if self._testing_pycurl():
1489
1580
raise tests.TestNotApplicable(
1493
1584
self.server.add_user('joe', 'foo')
1494
1585
t = self.get_user_transport('joe', None)
1495
1586
stdout = tests.StringIOWrapper()
1496
ui.ui_factory = tests.TestUIFactory(stdin='foo\n', stdout=stdout)
1497
self.assertEqual('contents of a\n',t.get('a').read())
1587
stderr = tests.StringIOWrapper()
1588
ui.ui_factory = tests.TestUIFactory(stdin='foo\n',
1589
stdout=stdout, stderr=stderr)
1590
self.assertEqual('contents of a\n', t.get('a').read())
1498
1591
# stdin should be empty
1499
1592
self.assertEqual('', ui.ui_factory.stdin.readline())
1500
1593
self._check_password_prompt(t._unqualified_scheme, 'joe',
1595
self.assertEqual('', stdout.getvalue())
1502
1596
# And we shouldn't prompt again for a different request
1503
1597
# against the same transport.
1504
1598
self.assertEqual('contents of b\n',t.get('b').read())
2000
2090
code, f = t._post('abc def end-of-body\n')
2001
2091
self.assertEqual('lalala whatever as long as itsssss\n', f.read())
2002
2092
self.assertActivitiesMatch()
2095
class TestActivity(tests.TestCase, TestActivityMixin):
2098
tests.TestCase.setUp(self)
2099
self.server = self._activity_server(self._protocol_version)
2100
self.server.start_server()
2101
self.activities = {}
2102
def report_activity(t, bytes, direction):
2103
count = self.activities.get(direction, 0)
2105
self.activities[direction] = count
2107
# We override at class level because constructors may propagate the
2108
# bound method and render instance overriding ineffective (an
2109
# alternative would be to define a specific ui factory instead...)
2110
self.orig_report_activity = self._transport._report_activity
2111
self._transport._report_activity = report_activity
2114
self._transport._report_activity = self.orig_report_activity
2115
self.server.stop_server()
2116
tests.TestCase.tearDown(self)
2119
class TestNoReportActivity(tests.TestCase, TestActivityMixin):
2122
tests.TestCase.setUp(self)
2123
# Unlike TestActivity, we are really testing ReportingFileSocket and
2124
# ReportingSocket, so we don't need all the parametrization. Since
2125
# ReportingFileSocket and ReportingSocket are wrappers, it's easier to
2126
# test them through their use by the transport than directly (that's a
2127
# bit less clean but far more simpler and effective).
2128
self.server = ActivityHTTPServer('HTTP/1.1')
2129
self._transport=_urllib.HttpTransport_urllib
2131
self.server.start_server()
2133
# We override at class level because constructors may propagate the
2134
# bound method and render instance overriding ineffective (an
2135
# alternative would be to define a specific ui factory instead...)
2136
self.orig_report_activity = self._transport._report_activity
2137
self._transport._report_activity = None
2140
self._transport._report_activity = self.orig_report_activity
2141
self.server.stop_server()
2142
tests.TestCase.tearDown(self)
2144
def assertActivitiesMatch(self):
2145
# Nothing to check here
2149
class TestAuthOnRedirected(http_utils.TestCaseWithRedirectedWebserver):
2150
"""Test authentication on the redirected http server."""
2152
_auth_header = 'Authorization'
2153
_password_prompt_prefix = ''
2154
_username_prompt_prefix = ''
2155
_auth_server = http_utils.HTTPBasicAuthServer
2156
_transport = _urllib.HttpTransport_urllib
2158
def create_transport_readonly_server(self):
2159
return self._auth_server()
2161
def create_transport_secondary_server(self):
2162
"""Create the secondary server redirecting to the primary server"""
2163
new = self.get_readonly_server()
2165
redirecting = http_utils.HTTPServerRedirecting()
2166
redirecting.redirect_to(new.host, new.port)
2170
super(TestAuthOnRedirected, self).setUp()
2171
self.build_tree_contents([('a','a'),
2173
('1/a', 'redirected once'),
2175
new_prefix = 'http://%s:%s' % (self.new_server.host,
2176
self.new_server.port)
2177
self.old_server.redirections = [
2178
('(.*)', r'%s/1\1' % (new_prefix), 301),]
2179
self.old_transport = self._transport(self.old_server.get_url())
2180
self.new_server.add_user('joe', 'foo')
2182
def get_a(self, transport):
2183
return transport.get('a')
2185
def test_auth_on_redirected_via_do_catching_redirections(self):
2186
self.redirections = 0
2188
def redirected(transport, exception, redirection_notice):
2189
self.redirections += 1
2190
dir, file = urlutils.split(exception.target)
2191
return self._transport(dir)
2193
stdout = tests.StringIOWrapper()
2194
stderr = tests.StringIOWrapper()
2195
ui.ui_factory = tests.TestUIFactory(stdin='joe\nfoo\n',
2196
stdout=stdout, stderr=stderr)
2197
self.assertEqual('redirected once',
2198
transport.do_catching_redirections(
2199
self.get_a, self.old_transport, redirected).read())
2200
self.assertEqual(1, self.redirections)
2201
# stdin should be empty
2202
self.assertEqual('', ui.ui_factory.stdin.readline())
2203
# stdout should be empty, stderr will contains the prompts
2204
self.assertEqual('', stdout.getvalue())
2206
def test_auth_on_redirected_via_following_redirections(self):
2207
self.new_server.add_user('joe', 'foo')
2208
stdout = tests.StringIOWrapper()
2209
stderr = tests.StringIOWrapper()
2210
ui.ui_factory = tests.TestUIFactory(stdin='joe\nfoo\n',
2211
stdout=stdout, stderr=stderr)
2212
t = self.old_transport
2213
req = RedirectedRequest('GET', t.abspath('a'))
2214
new_prefix = 'http://%s:%s' % (self.new_server.host,
2215
self.new_server.port)
2216
self.old_server.redirections = [
2217
('(.*)', r'%s/1\1' % (new_prefix), 301),]
2218
self.assertEqual('redirected once',t._perform(req).read())
2219
# stdin should be empty
2220
self.assertEqual('', ui.ui_factory.stdin.readline())
2221
# stdout should be empty, stderr will contains the prompts
2222
self.assertEqual('', stdout.getvalue())