268
270
def test_empty_header(self):
269
271
scheme, remainder = self.parse_header('')
270
self.assertEqual('', scheme)
272
self.assertEquals('', scheme)
271
273
self.assertIs(None, remainder)
273
275
def test_negotiate_header(self):
274
276
scheme, remainder = self.parse_header('Negotiate')
275
self.assertEqual('negotiate', scheme)
277
self.assertEquals('negotiate', scheme)
276
278
self.assertIs(None, remainder)
278
280
def test_basic_header(self):
279
281
scheme, remainder = self.parse_header(
280
282
'Basic realm="Thou should not pass"')
281
self.assertEqual('basic', scheme)
282
self.assertEqual('realm="Thou should not pass"', remainder)
283
self.assertEquals('basic', scheme)
284
self.assertEquals('realm="Thou should not pass"', remainder)
284
286
def test_basic_extract_realm(self):
285
287
scheme, remainder = self.parse_header(
287
289
_urllib2_wrappers.BasicAuthHandler)
288
290
match, realm = self.auth_handler.extract_realm(remainder)
289
291
self.assertTrue(match is not None)
290
self.assertEqual('Thou should not pass', realm)
292
self.assertEquals('Thou should not pass', realm)
292
294
def test_digest_header(self):
293
295
scheme, remainder = self.parse_header(
294
296
'Digest realm="Thou should not pass"')
295
self.assertEqual('digest', scheme)
296
self.assertEqual('realm="Thou should not pass"', remainder)
297
self.assertEquals('digest', scheme)
298
self.assertEquals('realm="Thou should not pass"', remainder)
299
301
class TestHTTPServer(tests.TestCase):
307
309
server = http_server.HttpServer(BogusRequestHandler)
309
self.assertRaises(httplib.UnknownProtocol, server.start_server)
311
self.assertRaises(httplib.UnknownProtocol, server.setUp)
312
314
self.fail('HTTP Server creation did not raise UnknownProtocol')
314
316
def test_force_invalid_protocol(self):
315
317
server = http_server.HttpServer(protocol_version='HTTP/0.1')
317
self.assertRaises(httplib.UnknownProtocol, server.start_server)
319
self.assertRaises(httplib.UnknownProtocol, server.setUp)
320
322
self.fail('HTTP Server creation did not raise UnknownProtocol')
322
324
def test_server_start_and_stop(self):
323
325
server = http_server.HttpServer()
324
server.start_server()
326
328
self.assertTrue(server._http_running)
329
331
self.assertFalse(server._http_running)
331
333
def test_create_http_server_one_zero(self):
388
393
def test_url_parsing(self):
389
394
f = FakeManager()
390
395
url = http.extract_auth('http://example.com', f)
391
self.assertEqual('http://example.com', url)
392
self.assertEqual(0, len(f.credentials))
396
self.assertEquals('http://example.com', url)
397
self.assertEquals(0, len(f.credentials))
393
398
url = http.extract_auth(
394
399
'http://user:pass@www.bazaar-vcs.org/bzr/bzr.dev', f)
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'],
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'],
401
406
class TestHttpTransportUrls(tests.TestCase):
448
453
https by supplying a fake version_info that do not
451
self.requireFeature(features.pycurl)
452
# Import the module locally now that we now it's available.
453
pycurl = features.pycurl.module
459
raise tests.TestSkipped('pycurl not present')
455
461
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
457
483
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')
479
486
class TestHTTPConnections(http_utils.TestCaseWithWebserver):
763
768
self.assertEqual(None, server.host)
764
769
self.assertEqual(None, server.port)
766
def test_setUp_and_stop(self):
771
def test_setUp_and_tearDown(self):
767
772
server = RecordingServer(expect_body_tail=None)
768
server.start_server()
770
775
self.assertNotEqual(None, server.host)
771
776
self.assertNotEqual(None, server.port)
774
779
self.assertEqual(None, server.host)
775
780
self.assertEqual(None, server.port)
1388
1383
def setUp(self):
1389
if (features.pycurl.available()
1390
and self._transport == PyCurlTransport):
1384
if pycurl_present and self._transport == PyCurlTransport:
1391
1385
raise tests.TestNotApplicable(
1392
1386
"pycurl doesn't redirect silently annymore")
1393
1387
super(TestHTTPSilentRedirections, self).setUp()
1394
install_redirected_request(self)
1388
self.setup_redirected_request()
1389
self.addCleanup(self.cleanup_redirected_request)
1395
1390
self.build_tree_contents([('a','a'),
1397
1392
('1/a', 'redirected once'),
1416
1418
t = self.old_transport
1418
1420
req = RedirectedRequest('GET', t.abspath('a'))
1421
req.follow_redirections = True
1419
1422
new_prefix = 'http://%s:%s' % (self.new_server.host,
1420
1423
self.new_server.port)
1421
1424
self.old_server.redirections = \
1422
1425
[('(.*)', r'%s/1\1' % (new_prefix), 301),]
1423
self.assertEqual('redirected once',t._perform(req).read())
1426
self.assertEquals('redirected once',t._perform(req).read())
1425
1428
def test_five_redirections(self):
1426
1429
t = self.old_transport
1428
1431
req = RedirectedRequest('GET', t.abspath('a'))
1432
req.follow_redirections = True
1429
1433
old_prefix = 'http://%s:%s' % (self.old_server.host,
1430
1434
self.old_server.port)
1431
1435
new_prefix = 'http://%s:%s' % (self.new_server.host,
1468
1472
dir, file = urlutils.split(exception.target)
1469
1473
return self._transport(dir)
1471
self.assertEqual('0123456789',
1472
transport.do_catching_redirections(
1475
self.assertEquals('0123456789',
1476
transport.do_catching_redirections(
1473
1477
self.get_a, self.old_transport, redirected).read())
1474
self.assertEqual(1, self.redirections)
1478
self.assertEquals(1, self.redirections)
1476
1480
def test_redirection_loop(self):
2147
2149
def tearDown(self):
2148
2150
self._transport._report_activity = self.orig_report_activity
2149
self.server.stop_server()
2151
self.server.tearDown()
2150
2152
tests.TestCase.tearDown(self)
2152
2154
def assertActivitiesMatch(self):
2153
2155
# 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())