270
268
def test_empty_header(self):
271
269
scheme, remainder = self.parse_header('')
272
self.assertEquals('', scheme)
270
self.assertEqual('', scheme)
273
271
self.assertIs(None, remainder)
275
273
def test_negotiate_header(self):
276
274
scheme, remainder = self.parse_header('Negotiate')
277
self.assertEquals('negotiate', scheme)
275
self.assertEqual('negotiate', scheme)
278
276
self.assertIs(None, remainder)
280
278
def test_basic_header(self):
281
279
scheme, remainder = self.parse_header(
282
280
'Basic realm="Thou should not pass"')
283
self.assertEquals('basic', scheme)
284
self.assertEquals('realm="Thou should not pass"', remainder)
281
self.assertEqual('basic', scheme)
282
self.assertEqual('realm="Thou should not pass"', remainder)
286
284
def test_basic_extract_realm(self):
287
285
scheme, remainder = self.parse_header(
393
388
def test_url_parsing(self):
394
389
f = FakeManager()
395
390
url = http.extract_auth('http://example.com', f)
396
self.assertEquals('http://example.com', url)
397
self.assertEquals(0, len(f.credentials))
391
self.assertEqual('http://example.com', url)
392
self.assertEqual(0, len(f.credentials))
398
393
url = http.extract_auth(
399
394
'http://user:pass@www.bazaar-vcs.org/bzr/bzr.dev', f)
400
self.assertEquals('http://www.bazaar-vcs.org/bzr/bzr.dev', url)
401
self.assertEquals(1, len(f.credentials))
402
self.assertEquals([None, 'www.bazaar-vcs.org', 'user', 'pass'],
395
self.assertEqual('http://www.bazaar-vcs.org/bzr/bzr.dev', url)
396
self.assertEqual(1, len(f.credentials))
397
self.assertEqual([None, 'www.bazaar-vcs.org', 'user', 'pass'],
406
401
class TestHttpTransportUrls(tests.TestCase):
453
448
https by supplying a fake version_info that do not
459
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
461
455
version_info_orig = pycurl.version_info
463
# Now that we have pycurl imported, we can fake its version_info
464
# This was taken from a windows pycurl without SSL
466
pycurl.version_info = lambda : (2,
474
('ftp', 'gopher', 'telnet',
475
'dict', 'ldap', 'http', 'file'),
479
self.assertRaises(errors.DependencyNotPresent, self._transport,
480
'https://launchpad.net')
482
# Restore the right function
483
457
pycurl.version_info = version_info_orig
458
self.addCleanup(restore)
460
# Fake the pycurl version_info This was taken from a windows pycurl
461
# without SSL (thanks to bialix)
462
pycurl.version_info = lambda : (2,
470
('ftp', 'gopher', 'telnet',
471
'dict', 'ldap', 'http', 'file'),
475
self.assertRaises(errors.DependencyNotPresent, self._transport,
476
'https://launchpad.net')
486
479
class TestHTTPConnections(http_utils.TestCaseWithWebserver):
2100
2098
code, f = t._post('abc def end-of-body\n')
2101
2099
self.assertEqual('lalala whatever as long as itsssss\n', f.read())
2102
2100
self.assertActivitiesMatch()
2103
class TestActivity(tests.TestCase, TestActivityMixin):
2106
tests.TestCase.setUp(self)
2107
self.server = self._activity_server(self._protocol_version)
2108
self.server.start_server()
2109
self.activities = {}
2110
def report_activity(t, bytes, direction):
2111
count = self.activities.get(direction, 0)
2113
self.activities[direction] = count
2115
# We override at class level because constructors may propagate the
2116
# bound method and render instance overriding ineffective (an
2117
# alternative would be to define a specific ui factory instead...)
2118
self.orig_report_activity = self._transport._report_activity
2119
self._transport._report_activity = report_activity
2122
self._transport._report_activity = self.orig_report_activity
2123
self.server.stop_server()
2124
tests.TestCase.tearDown(self)
2127
class TestNoReportActivity(tests.TestCase, TestActivityMixin):
2130
tests.TestCase.setUp(self)
2131
# Unlike TestActivity, we are really testing ReportingFileSocket and
2132
# ReportingSocket, so we don't need all the parametrization. Since
2133
# ReportingFileSocket and ReportingSocket are wrappers, it's easier to
2134
# test them through their use by the transport than directly (that's a
2135
# bit less clean but far more simpler and effective).
2136
self.server = ActivityHTTPServer('HTTP/1.1')
2137
self._transport=_urllib.HttpTransport_urllib
2139
self.server.start_server()
2141
# We override at class level because constructors may propagate the
2142
# bound method and render instance overriding ineffective (an
2143
# alternative would be to define a specific ui factory instead...)
2144
self.orig_report_activity = self._transport._report_activity
2145
self._transport._report_activity = None
2148
self._transport._report_activity = self.orig_report_activity
2149
self.server.stop_server()
2150
tests.TestCase.tearDown(self)
2152
def assertActivitiesMatch(self):
2153
# Nothing to check here
2157
class TestAuthOnRedirected(http_utils.TestCaseWithRedirectedWebserver):
2158
"""Test authentication on the redirected http server."""
2160
_auth_header = 'Authorization'
2161
_password_prompt_prefix = ''
2162
_username_prompt_prefix = ''
2163
_auth_server = http_utils.HTTPBasicAuthServer
2164
_transport = _urllib.HttpTransport_urllib
2166
def create_transport_readonly_server(self):
2167
return self._auth_server()
2169
def create_transport_secondary_server(self):
2170
"""Create the secondary server redirecting to the primary server"""
2171
new = self.get_readonly_server()
2173
redirecting = http_utils.HTTPServerRedirecting()
2174
redirecting.redirect_to(new.host, new.port)
2178
super(TestAuthOnRedirected, self).setUp()
2179
self.build_tree_contents([('a','a'),
2181
('1/a', 'redirected once'),
2183
new_prefix = 'http://%s:%s' % (self.new_server.host,
2184
self.new_server.port)
2185
self.old_server.redirections = [
2186
('(.*)', r'%s/1\1' % (new_prefix), 301),]
2187
self.old_transport = self._transport(self.old_server.get_url())
2188
self.new_server.add_user('joe', 'foo')
2190
def get_a(self, transport):
2191
return transport.get('a')
2193
def test_auth_on_redirected_via_do_catching_redirections(self):
2194
self.redirections = 0
2196
def redirected(transport, exception, redirection_notice):
2197
self.redirections += 1
2198
dir, file = urlutils.split(exception.target)
2199
return self._transport(dir)
2201
stdout = tests.StringIOWrapper()
2202
stderr = tests.StringIOWrapper()
2203
ui.ui_factory = tests.TestUIFactory(stdin='joe\nfoo\n',
2204
stdout=stdout, stderr=stderr)
2205
self.assertEqual('redirected once',
2206
transport.do_catching_redirections(
2207
self.get_a, self.old_transport, redirected).read())
2208
self.assertEqual(1, self.redirections)
2209
# stdin should be empty
2210
self.assertEqual('', ui.ui_factory.stdin.readline())
2211
# stdout should be empty, stderr will contains the prompts
2212
self.assertEqual('', stdout.getvalue())
2214
def test_auth_on_redirected_via_following_redirections(self):
2215
self.new_server.add_user('joe', 'foo')
2216
stdout = tests.StringIOWrapper()
2217
stderr = tests.StringIOWrapper()
2218
ui.ui_factory = tests.TestUIFactory(stdin='joe\nfoo\n',
2219
stdout=stdout, stderr=stderr)
2220
t = self.old_transport
2221
req = RedirectedRequest('GET', t.abspath('a'))
2222
new_prefix = 'http://%s:%s' % (self.new_server.host,
2223
self.new_server.port)
2224
self.old_server.redirections = [
2225
('(.*)', r'%s/1\1' % (new_prefix), 301),]
2226
self.assertEqual('redirected once',t._perform(req).read())
2227
# stdin should be empty
2228
self.assertEqual('', ui.ui_factory.stdin.readline())
2229
# stdout should be empty, stderr will contains the prompts
2230
self.assertEqual('', stdout.getvalue())