77
78
transport_scenarios = [
78
79
('urllib', dict(_transport=_urllib.HttpTransport_urllib,
79
80
_server=http_server.HttpServer_urllib,
80
_qualified_prefix='http+urllib',)),
81
_url_protocol='http+urllib',)),
82
83
if features.pycurl.available():
83
84
transport_scenarios.append(
84
85
('pycurl', dict(_transport=PyCurlTransport,
85
86
_server=http_server.HttpServer_PyCurl,
86
_qualified_prefix='http+pycurl',)))
87
_url_protocol='http+pycurl',)))
87
88
tests.multiply_tests(t_tests, transport_scenarios, result)
89
90
protocol_scenarios = [
228
229
self._sock.bind(('127.0.0.1', 0))
229
230
self.host, self.port = self._sock.getsockname()
230
231
self._ready = threading.Event()
231
self._thread = threading.Thread(target=self._accept_read_and_reply)
232
self._thread.setDaemon(True)
232
self._thread = test_server.ThreadWithException(
233
event=self._ready, target=self._accept_read_and_reply)
233
234
self._thread.start()
235
if 'threads' in tests.selftest_debug_flags:
236
sys.stderr.write('Thread started: %s\n' % (self._thread.ident,))
236
239
def _accept_read_and_reply(self):
237
240
self._sock.listen(1)
238
241
self._ready.set()
239
self._sock.settimeout(5)
241
conn, address = self._sock.accept()
242
# On win32, the accepted connection will be non-blocking to start
243
# with because we're using settimeout.
244
conn.setblocking(True)
242
conn, address = self._sock.accept()
243
if self._expect_body_tail is not None:
245
244
while not self.received_bytes.endswith(self._expect_body_tail):
246
245
self.received_bytes += conn.recv(4096)
247
246
conn.sendall('HTTP/1.1 200 OK\r\n')
248
except socket.timeout:
249
# Make sure the client isn't stuck waiting for us to e.g. accept.
250
248
self._sock.close()
251
249
except socket.error:
252
250
# The client may have already closed the socket.
255
253
def stop_server(self):
255
# Issue a fake connection to wake up the server and allow it to
257
fake_conn = osutils.connect_socket((self.host, self.port))
258
259
except socket.error:
259
260
# We might have already closed it. We don't care.
265
if 'threads' in tests.selftest_debug_flags:
266
sys.stderr.write('Thread joined: %s\n' % (self._thread.ident,))
265
269
class TestAuthHeader(tests.TestCase):
310
314
protocol_version = 'HTTP/0.1'
312
server = http_server.HttpServer(BogusRequestHandler)
314
self.assertRaises(httplib.UnknownProtocol, server.start_server)
317
self.fail('HTTP Server creation did not raise UnknownProtocol')
316
self.assertRaises(httplib.UnknownProtocol,
317
http_server.HttpServer, BogusRequestHandler)
319
319
def test_force_invalid_protocol(self):
320
server = http_server.HttpServer(protocol_version='HTTP/0.1')
322
self.assertRaises(httplib.UnknownProtocol, server.start_server)
325
self.fail('HTTP Server creation did not raise UnknownProtocol')
320
self.assertRaises(httplib.UnknownProtocol,
321
http_server.HttpServer, protocol_version='HTTP/0.1')
327
323
def test_server_start_and_stop(self):
328
324
server = http_server.HttpServer()
325
self.addCleanup(server.stop_server)
329
326
server.start_server()
331
self.assertTrue(server._http_running)
334
self.assertFalse(server._http_running)
327
self.assertTrue(server.server is not None)
328
self.assertTrue(server.server.serving is not None)
329
self.assertTrue(server.server.serving)
336
331
def test_create_http_server_one_zero(self):
337
332
class RequestHandlerOneZero(http_server.TestingHTTPRequestHandler):
341
336
server = http_server.HttpServer(RequestHandlerOneZero)
342
337
self.start_server(server)
343
self.assertIsInstance(server._httpd, http_server.TestingHTTPServer)
338
self.assertIsInstance(server.server, http_server.TestingHTTPServer)
345
340
def test_create_http_server_one_one(self):
346
341
class RequestHandlerOneOne(http_server.TestingHTTPRequestHandler):
350
345
server = http_server.HttpServer(RequestHandlerOneOne)
351
346
self.start_server(server)
352
self.assertIsInstance(server._httpd,
347
self.assertIsInstance(server.server,
353
348
http_server.TestingThreadingHTTPServer)
355
350
def test_create_http_server_force_one_one(self):
360
355
server = http_server.HttpServer(RequestHandlerOneZero,
361
356
protocol_version='HTTP/1.1')
362
357
self.start_server(server)
363
self.assertIsInstance(server._httpd,
358
self.assertIsInstance(server.server,
364
359
http_server.TestingThreadingHTTPServer)
366
361
def test_create_http_server_force_one_zero(self):
371
366
server = http_server.HttpServer(RequestHandlerOneOne,
372
367
protocol_version='HTTP/1.0')
373
368
self.start_server(server)
374
self.assertIsInstance(server._httpd,
369
self.assertIsInstance(server.server,
375
370
http_server.TestingHTTPServer)
488
483
def test_http_has(self):
489
484
server = self.get_readonly_server()
490
t = self._transport(server.get_url())
485
t = self.get_readonly_transport()
491
486
self.assertEqual(t.has('foo/bar'), True)
492
487
self.assertEqual(len(server.logs), 1)
493
488
self.assertContainsRe(server.logs[0],
496
491
def test_http_has_not_found(self):
497
492
server = self.get_readonly_server()
498
t = self._transport(server.get_url())
493
t = self.get_readonly_transport()
499
494
self.assertEqual(t.has('not-found'), False)
500
495
self.assertContainsRe(server.logs[1],
501
496
r'"HEAD /not-found HTTP/1.." 404 - "-" "bzr/')
503
498
def test_http_get(self):
504
499
server = self.get_readonly_server()
505
t = self._transport(server.get_url())
500
t = self.get_readonly_transport()
506
501
fp = t.get('foo/bar')
507
502
self.assertEqualDiff(
531
526
"""Test registrations of various http implementations"""
533
528
def test_http_registered(self):
534
t = transport.get_transport('%s://foo.com/' % self._qualified_prefix)
529
t = transport.get_transport('%s://foo.com/' % self._url_protocol)
535
530
self.assertIsInstance(t, transport.Transport)
536
531
self.assertIsInstance(t, self._transport)
541
536
def test_post_body_is_received(self):
542
537
server = RecordingServer(expect_body_tail='end-of-body',
543
scheme=self._qualified_prefix)
538
scheme=self._url_protocol)
544
539
self.start_server(server)
545
540
url = server.get_url()
546
http_transport = self._transport(url)
541
# FIXME: needs a cleanup -- vila 20100611
542
http_transport = transport.get_transport(url)
547
543
code, response = http_transport._post('abc def end-of-body')
549
545
server.received_bytes.startswith('POST /.bzr/smart HTTP/1.'))
593
589
_req_handler_class = http_server.TestingHTTPRequestHandler
595
591
def create_transport_readonly_server(self):
596
return http_server.HttpServer(self._req_handler_class,
597
protocol_version=self._protocol_version)
592
server = http_server.HttpServer(self._req_handler_class,
593
protocol_version=self._protocol_version)
594
server._url_protocol = self._url_protocol
599
597
def _testing_pycurl(self):
600
598
# TODO: This is duplicated for lots of the classes in this file
605
603
class WallRequestHandler(http_server.TestingHTTPRequestHandler):
606
604
"""Whatever request comes in, close the connection"""
608
def handle_one_request(self):
606
def _handle_one_request(self):
609
607
"""Handle a single HTTP request, by abruptly closing the connection"""
610
608
self.close_connection = 1
616
614
_req_handler_class = WallRequestHandler
618
616
def test_http_has(self):
619
server = self.get_readonly_server()
620
t = self._transport(server.get_url())
617
t = self.get_readonly_transport()
621
618
# Unfortunately httplib (see HTTPResponse._read_status
622
619
# for details) make no distinction between a closed
623
620
# socket and badly formatted status line, so we can't
629
626
t.has, 'foo/bar')
631
628
def test_http_get(self):
632
server = self.get_readonly_server()
633
t = self._transport(server.get_url())
629
t = self.get_readonly_transport()
634
630
self.assertRaises((errors.ConnectionError, errors.ConnectionReset,
635
631
errors.InvalidHttpResponse),
636
632
t.get, 'foo/bar')
653
649
_req_handler_class = BadStatusRequestHandler
655
651
def test_http_has(self):
656
server = self.get_readonly_server()
657
t = self._transport(server.get_url())
652
t = self.get_readonly_transport()
658
653
self.assertRaises(errors.InvalidHttpResponse, t.has, 'foo/bar')
660
655
def test_http_get(self):
661
server = self.get_readonly_server()
662
t = self._transport(server.get_url())
656
t = self.get_readonly_transport()
663
657
self.assertRaises(errors.InvalidHttpResponse, t.get, 'foo/bar')
670
664
"""Fakes handling a single HTTP request, returns a bad status"""
671
665
ignored = http_server.TestingHTTPRequestHandler.parse_request(self)
672
666
self.wfile.write("Invalid status line\r\n")
667
# If we don't close the connection pycurl will hang. Since this is a
668
# stress test we don't *have* to respect the protocol, but we don't
669
# have to sabotage it too much either.
670
self.close_connection = True
682
680
_req_handler_class = InvalidStatusRequestHandler
684
def test_http_has(self):
685
if self._testing_pycurl() and self._protocol_version == 'HTTP/1.1':
686
raise tests.KnownFailure(
687
'pycurl hangs if the server send back garbage')
688
super(TestInvalidStatusServer, self).test_http_has()
690
def test_http_get(self):
691
if self._testing_pycurl() and self._protocol_version == 'HTTP/1.1':
692
raise tests.KnownFailure(
693
'pycurl hangs if the server send back garbage')
694
super(TestInvalidStatusServer, self).test_http_get()
697
683
class BadProtocolRequestHandler(http_server.TestingHTTPRequestHandler):
698
684
"""Whatever request comes in, returns a bad protocol version"""
720
706
super(TestBadProtocolServer, self).setUp()
722
708
def test_http_has(self):
723
server = self.get_readonly_server()
724
t = self._transport(server.get_url())
709
t = self.get_readonly_transport()
725
710
self.assertRaises(errors.InvalidHttpResponse, t.has, 'foo/bar')
727
712
def test_http_get(self):
728
server = self.get_readonly_server()
729
t = self._transport(server.get_url())
713
t = self.get_readonly_transport()
730
714
self.assertRaises(errors.InvalidHttpResponse, t.get, 'foo/bar')
746
730
_req_handler_class = ForbiddenRequestHandler
748
732
def test_http_has(self):
749
server = self.get_readonly_server()
750
t = self._transport(server.get_url())
733
t = self.get_readonly_transport()
751
734
self.assertRaises(errors.TransportError, t.has, 'foo/bar')
753
736
def test_http_get(self):
754
server = self.get_readonly_server()
755
t = self._transport(server.get_url())
737
t = self.get_readonly_transport()
756
738
self.assertRaises(errors.TransportError, t.get, 'foo/bar')
797
779
self.build_tree_contents([('a', '0123456789')],)
799
781
def test_readv(self):
800
server = self.get_readonly_server()
801
t = self._transport(server.get_url())
782
t = self.get_readonly_transport()
802
783
l = list(t.readv('a', ((0, 1), (1, 1), (3, 2), (9, 1))))
803
784
self.assertEqual(l[0], (0, '0'))
804
785
self.assertEqual(l[1], (1, '1'))
806
787
self.assertEqual(l[3], (9, '9'))
808
789
def test_readv_out_of_order(self):
809
server = self.get_readonly_server()
810
t = self._transport(server.get_url())
790
t = self.get_readonly_transport()
811
791
l = list(t.readv('a', ((1, 1), (9, 1), (0, 1), (3, 2))))
812
792
self.assertEqual(l[0], (1, '1'))
813
793
self.assertEqual(l[1], (9, '9'))
815
795
self.assertEqual(l[3], (3, '34'))
817
797
def test_readv_invalid_ranges(self):
818
server = self.get_readonly_server()
819
t = self._transport(server.get_url())
798
t = self.get_readonly_transport()
821
800
# This is intentionally reading off the end of the file
822
801
# since we are sure that it cannot get there
831
810
def test_readv_multiple_get_requests(self):
832
811
server = self.get_readonly_server()
833
t = self._transport(server.get_url())
812
t = self.get_readonly_transport()
834
813
# force transport to issue multiple requests
835
814
t._max_readv_combine = 1
836
815
t._max_get_ranges = 1
845
824
def test_readv_get_max_size(self):
846
825
server = self.get_readonly_server()
847
t = self._transport(server.get_url())
826
t = self.get_readonly_transport()
848
827
# force transport to issue multiple requests by limiting the number of
849
828
# bytes by request. Note that this apply to coalesced offsets only, a
850
829
# single range will keep its size even if bigger than the limit.
860
839
def test_complete_readv_leave_pipe_clean(self):
861
840
server = self.get_readonly_server()
862
t = self._transport(server.get_url())
841
t = self.get_readonly_transport()
863
842
# force transport to issue multiple requests
864
843
t._get_max_size = 2
865
844
l = list(t.readv('a', ((0, 1), (1, 1), (2, 4), (6, 4))))
871
850
def test_incomplete_readv_leave_pipe_clean(self):
872
851
server = self.get_readonly_server()
873
t = self._transport(server.get_url())
852
t = self.get_readonly_transport()
874
853
# force transport to issue multiple requests
875
854
t._get_max_size = 2
876
855
# Don't collapse readv results into a list so that we leave unread
1027
1006
def test_readv_with_short_reads(self):
1028
1007
server = self.get_readonly_server()
1029
t = self._transport(server.get_url())
1008
t = self.get_readonly_transport()
1030
1009
# Force separate ranges for each offset
1031
1010
t._bytes_to_read_before_seek = 0
1032
1011
ireadv = iter(t.readv('a', ((0, 1), (2, 1), (4, 2), (9, 1))))
1083
1062
return LimitedRangeHTTPServer(range_limit=self.range_limit,
1084
1063
protocol_version=self._protocol_version)
1086
def get_transport(self):
1087
return self._transport(self.get_readonly_server().get_url())
1089
1065
def setUp(self):
1090
1066
http_utils.TestCaseWithWebserver.setUp(self)
1091
1067
# We need to manipulate ranges that correspond to real chunks in the
1095
1071
self.build_tree_contents([('a', content)],)
1097
1073
def test_few_ranges(self):
1098
t = self.get_transport()
1074
t = self.get_readonly_transport()
1099
1075
l = list(t.readv('a', ((0, 4), (1024, 4), )))
1100
1076
self.assertEqual(l[0], (0, '0000'))
1101
1077
self.assertEqual(l[1], (1024, '0001'))
1102
1078
self.assertEqual(1, self.get_readonly_server().GET_request_nb)
1104
1080
def test_more_ranges(self):
1105
t = self.get_transport()
1081
t = self.get_readonly_transport()
1106
1082
l = list(t.readv('a', ((0, 4), (1024, 4), (4096, 4), (8192, 4))))
1107
1083
self.assertEqual(l[0], (0, '0000'))
1108
1084
self.assertEqual(l[1], (1024, '0001'))
1161
1137
# FIXME: We don't have an https server available, so we don't
1162
# test https connections.
1138
# test https connections. --vila toolongago
1164
1140
def setUp(self):
1165
1141
super(TestProxyHttpServer, self).setUp()
1142
self.transport_secondary_server = http_utils.ProxyServer
1166
1143
self.build_tree_contents([('foo', 'contents of foo\n'),
1167
1144
('foo-proxied', 'proxied contents of foo\n')])
1168
1145
# Let's setup some attributes for tests
1169
self.server = self.get_readonly_server()
1170
self.proxy_address = '%s:%d' % (self.server.host, self.server.port)
1146
server = self.get_readonly_server()
1147
self.server_host_port = '%s:%d' % (server.host, server.port)
1171
1148
if self._testing_pycurl():
1172
1149
# Oh my ! pycurl does not check for the port as part of
1173
1150
# no_proxy :-( So we just test the host part
1174
self.no_proxy_host = self.server.host
1151
self.no_proxy_host = server.host
1176
self.no_proxy_host = self.proxy_address
1153
self.no_proxy_host = self.server_host_port
1177
1154
# The secondary server is the proxy
1178
self.proxy = self.get_secondary_server()
1179
self.proxy_url = self.proxy.get_url()
1155
self.proxy_url = self.get_secondary_url()
1180
1156
self._old_env = {}
1182
1158
def _testing_pycurl(self):
1184
1160
return (features.pycurl.available()
1185
1161
and self._transport == PyCurlTransport)
1187
def create_transport_secondary_server(self):
1188
"""Creates an http server that will serve files with
1189
'-proxied' appended to their names.
1191
return http_utils.ProxyServer(protocol_version=self._protocol_version)
1193
1163
def _install_env(self, env):
1194
1164
for name, value in env.iteritems():
1195
1165
self._old_env[name] = osutils.set_or_unset_env(name, value)
1201
1171
def proxied_in_env(self, env):
1202
1172
self._install_env(env)
1203
url = self.server.get_url()
1204
t = self._transport(url)
1173
t = self.get_readonly_transport()
1206
1175
self.assertEqual('proxied contents of foo\n', t.get('foo').read())
1210
1179
def not_proxied_in_env(self, env):
1211
1180
self._install_env(env)
1212
url = self.server.get_url()
1213
t = self._transport(url)
1181
t = self.get_readonly_transport()
1215
1183
self.assertEqual('contents of foo\n', t.get('foo').read())
1258
1226
# pycurl *ignores* invalid proxy env variables. If that ever change
1259
1227
# in the future, this test will fail indicating that pycurl do not
1260
1228
# ignore anymore such variables.
1261
self.not_proxied_in_env({'http_proxy': self.proxy_address})
1229
self.not_proxied_in_env({'http_proxy': self.server_host_port})
1263
1231
self.assertRaises(errors.InvalidURL,
1264
1232
self.proxied_in_env,
1265
{'http_proxy': self.proxy_address})
1233
{'http_proxy': self.server_host_port})
1268
1236
class TestRanges(http_utils.TestCaseWithWebserver):
1271
1239
def setUp(self):
1272
1240
http_utils.TestCaseWithWebserver.setUp(self)
1273
1241
self.build_tree_contents([('a', '0123456789')],)
1274
server = self.get_readonly_server()
1275
self.transport = self._transport(server.get_url())
1277
1243
def create_transport_readonly_server(self):
1278
1244
return http_server.HttpServer(protocol_version=self._protocol_version)
1280
1246
def _file_contents(self, relpath, ranges):
1247
t = self.get_readonly_transport()
1281
1248
offsets = [ (start, end - start + 1) for start, end in ranges]
1282
coalesce = self.transport._coalesce_offsets
1249
coalesce = t._coalesce_offsets
1283
1250
coalesced = list(coalesce(offsets, limit=0, fudge_factor=0))
1284
code, data = self.transport._get(relpath, coalesced)
1251
code, data = t._get(relpath, coalesced)
1285
1252
self.assertTrue(code in (200, 206),'_get returns: %d' % code)
1286
1253
for start, end in ranges:
1287
1254
data.seek(start)
1288
1255
yield data.read(end - start + 1)
1290
1257
def _file_tail(self, relpath, tail_amount):
1291
code, data = self.transport._get(relpath, [], tail_amount)
1258
t = self.get_readonly_transport()
1259
code, data = t._get(relpath, [], tail_amount)
1292
1260
self.assertTrue(code in (200, 206),'_get returns: %d' % code)
1293
1261
data.seek(-tail_amount, 2)
1294
1262
return data.read(tail_amount)
1313
1281
class TestHTTPRedirections(http_utils.TestCaseWithRedirectedWebserver):
1314
1282
"""Test redirection between http servers."""
1316
def create_transport_secondary_server(self):
1317
"""Create the secondary server redirecting to the primary server"""
1318
new = self.get_readonly_server()
1320
redirecting = http_utils.HTTPServerRedirecting(
1321
protocol_version=self._protocol_version)
1322
redirecting.redirect_to(new.host, new.port)
1325
1284
def setUp(self):
1326
1285
super(TestHTTPRedirections, self).setUp()
1327
1286
self.build_tree_contents([('a', '0123456789'),
1329
1288
'# Bazaar revision bundle v0.9\n#\n')
1331
# The requests to the old server will be redirected to the new server
1332
self.old_transport = self._transport(self.old_server.get_url())
1334
1291
def test_redirected(self):
1335
self.assertRaises(errors.RedirectRequested, self.old_transport.get, 'a')
1336
t = self._transport(self.new_server.get_url())
1337
self.assertEqual('0123456789', t.get('a').read())
1292
self.assertRaises(errors.RedirectRequested,
1293
self.get_old_transport().get, 'a')
1294
self.assertEqual('0123456789', self.get_new_transport().get('a').read())
1340
1297
class RedirectedRequest(_urllib2_wrappers.Request):
1357
1314
test.overrideAttr(_urllib2_wrappers, 'Request', RedirectedRequest)
1317
def cleanup_http_redirection_connections(test):
1318
# Some sockets are opened but never seen by _urllib, so we trap them at
1319
# the _urllib2_wrappers level to be able to clean them up.
1320
def socket_disconnect(sock):
1322
sock.shutdown(socket.SHUT_RDWR)
1324
except socket.error:
1326
def connect(connection):
1327
test.http_connect_orig(connection)
1328
test.addCleanup(socket_disconnect, connection.sock)
1329
test.http_connect_orig = test.overrideAttr(
1330
_urllib2_wrappers.HTTPConnection, 'connect', connect)
1331
def connect(connection):
1332
test.https_connect_orig(connection)
1333
test.addCleanup(socket_disconnect, connection.sock)
1334
test.https_connect_orig = test.overrideAttr(
1335
_urllib2_wrappers.HTTPSConnection, 'connect', connect)
1360
1338
class TestHTTPSilentRedirections(http_utils.TestCaseWithRedirectedWebserver):
1361
1339
"""Test redirections.
1375
1353
if (features.pycurl.available()
1376
1354
and self._transport == PyCurlTransport):
1377
1355
raise tests.TestNotApplicable(
1378
"pycurl doesn't redirect silently annymore")
1356
"pycurl doesn't redirect silently anymore")
1379
1357
super(TestHTTPSilentRedirections, self).setUp()
1380
1358
install_redirected_request(self)
1359
cleanup_http_redirection_connections(self)
1381
1360
self.build_tree_contents([('a','a'),
1383
1362
('1/a', 'redirected once'),
1391
1370
('5/a', 'redirected 5 times'),
1394
self.old_transport = self._transport(self.old_server.get_url())
1396
def create_transport_secondary_server(self):
1397
"""Create the secondary server, redirections are defined in the tests"""
1398
return http_utils.HTTPServerRedirecting(
1399
protocol_version=self._protocol_version)
1401
1373
def test_one_redirection(self):
1402
t = self.old_transport
1404
req = RedirectedRequest('GET', t.abspath('a'))
1374
t = self.get_old_transport()
1375
req = RedirectedRequest('GET', t._remote_path('a'))
1405
1376
new_prefix = 'http://%s:%s' % (self.new_server.host,
1406
1377
self.new_server.port)
1407
1378
self.old_server.redirections = \
1408
1379
[('(.*)', r'%s/1\1' % (new_prefix), 301),]
1409
self.assertEqual('redirected once',t._perform(req).read())
1380
self.assertEqual('redirected once', t._perform(req).read())
1411
1382
def test_five_redirections(self):
1412
t = self.old_transport
1414
req = RedirectedRequest('GET', t.abspath('a'))
1383
t = self.get_old_transport()
1384
req = RedirectedRequest('GET', t._remote_path('a'))
1415
1385
old_prefix = 'http://%s:%s' % (self.old_server.host,
1416
1386
self.old_server.port)
1417
1387
new_prefix = 'http://%s:%s' % (self.new_server.host,
1423
1393
('/4(.*)', r'%s/5\1' % (new_prefix), 301),
1424
1394
('(/[^/]+)', r'%s/1\1' % (old_prefix), 301),
1426
self.assertEqual('redirected 5 times',t._perform(req).read())
1396
self.assertEqual('redirected 5 times', t._perform(req).read())
1429
1399
class TestDoCatchRedirections(http_utils.TestCaseWithRedirectedWebserver):
1432
1402
def setUp(self):
1433
1403
super(TestDoCatchRedirections, self).setUp()
1434
1404
self.build_tree_contents([('a', '0123456789'),],)
1436
self.old_transport = self._transport(self.old_server.get_url())
1438
def get_a(self, transport):
1439
return transport.get('a')
1405
cleanup_http_redirection_connections(self)
1407
self.old_transport = self.get_old_transport()
1441
1412
def test_no_redirection(self):
1442
t = self._transport(self.new_server.get_url())
1413
t = self.get_new_transport()
1444
1415
# We use None for redirected so that we fail if redirected
1445
1416
self.assertEqual('0123456789',
1449
1420
def test_one_redirection(self):
1450
1421
self.redirections = 0
1452
def redirected(transport, exception, redirection_notice):
1423
def redirected(t, exception, redirection_notice):
1453
1424
self.redirections += 1
1454
dir, file = urlutils.split(exception.target)
1455
return self._transport(dir)
1425
redirected_t = t._redirected_to(exception.source, exception.target)
1457
1428
self.assertEqual('0123456789',
1458
1429
transport.do_catching_redirections(
1488
1459
('b', 'contents of b\n'),])
1490
1461
def create_transport_readonly_server(self):
1491
return self._auth_server(protocol_version=self._protocol_version)
1462
server = self._auth_server(protocol_version=self._protocol_version)
1463
server._url_protocol = self._url_protocol
1493
1466
def _testing_pycurl(self):
1494
1467
# TODO: This is duplicated for lots of the classes in this file
1509
1482
def get_user_transport(self, user, password):
1510
return self._transport(self.get_user_url(user, password))
1483
t = transport.get_transport(self.get_user_url(user, password))
1512
1486
def test_no_user(self):
1513
1487
self.server.add_user('joe', 'foo')
1699
1673
def get_user_transport(self, user, password):
1700
1674
self._install_env({'all_proxy': self.get_user_url(user, password)})
1701
return self._transport(self.server.get_url())
1675
return TestAuth.get_user_transport(self, user, password)
1703
1677
def _install_env(self, env):
1704
1678
for name, value in env.iteritems():
1731
1705
self.readfile = StringIO(socket_read_content)
1732
1706
self.writefile = StringIO()
1733
1707
self.writefile.close = lambda: None
1708
self.close = lambda: None
1735
1710
def makefile(self, mode='r', bufsize=None):
1736
1711
if 'r' in mode:
1746
1721
# We use the VFS layer as part of HTTP tunnelling tests.
1747
1722
self._captureVar('BZR_NO_SMART_VFS', None)
1748
1723
self.transport_readonly_server = http_utils.HTTPServerWithSmarts
1724
self.http_server = self.get_readonly_server()
1750
1726
def create_transport_readonly_server(self):
1751
return http_utils.HTTPServerWithSmarts(
1727
server = http_utils.HTTPServerWithSmarts(
1752
1728
protocol_version=self._protocol_version)
1729
server._url_protocol = self._url_protocol
1754
1732
def test_open_bzrdir(self):
1755
1733
branch = self.make_branch('relpath')
1756
http_server = self.get_readonly_server()
1757
url = http_server.get_url() + 'relpath'
1734
url = self.http_server.get_url() + 'relpath'
1758
1735
bd = bzrdir.BzrDir.open(url)
1736
self.addCleanup(bd.transport.disconnect)
1759
1737
self.assertIsInstance(bd, _mod_remote.RemoteBzrDir)
1761
1739
def test_bulk_data(self):
1763
1741
# The 'readv' command in the smart protocol both sends and receives
1764
1742
# bulk data, so we use that.
1765
1743
self.build_tree(['data-file'])
1766
http_server = self.get_readonly_server()
1767
http_transport = self._transport(http_server.get_url())
1744
http_transport = transport.get_transport(self.http_server.get_url())
1768
1745
medium = http_transport.get_smart_medium()
1769
1746
# Since we provide the medium, the url below will be mostly ignored
1770
1747
# during the test, as long as the path is '/'.
1778
1755
post_body = 'hello\n'
1779
1756
expected_reply_body = 'ok\x012\n'
1781
http_server = self.get_readonly_server()
1782
http_transport = self._transport(http_server.get_url())
1758
http_transport = transport.get_transport(self.http_server.get_url())
1783
1759
medium = http_transport.get_smart_medium()
1784
1760
response = medium.send_http_smart_request(post_body)
1785
1761
reply_body = response.read()
1786
1762
self.assertEqual(expected_reply_body, reply_body)
1788
1764
def test_smart_http_server_post_request_handler(self):
1789
httpd = self.get_readonly_server()._get_httpd()
1765
httpd = self.http_server.server
1791
1767
socket = SampleSocket(
1792
1768
'POST /.bzr/smart %s \r\n' % self._protocol_version
1825
1801
def test_probe_smart_server(self):
1826
1802
"""Test error handling against server refusing smart requests."""
1827
server = self.get_readonly_server()
1828
t = self._transport(server.get_url())
1803
t = self.get_readonly_transport()
1829
1804
# No need to build a valid smart request here, the server will not even
1830
1805
# try to interpret it.
1831
1806
self.assertRaises(errors.SmartProtocolError,
1832
1807
t.get_smart_medium().send_http_smart_request,
1835
1811
class Test_redirected_to(tests.TestCase):
1837
1813
def test_redirected_to_subdir(self):
1893
def handle_one_request(self):
1869
def _handle_one_request(self):
1894
1870
tcs = self.server.test_case_server
1895
1871
requestline = self.rfile.readline()
1896
1872
headers = self.MessageClass(self.rfile, 0)
1964
1940
self.addCleanup(self.server.stop_server)
1966
1942
def get_transport(self):
1967
return self._transport(self.server.get_url())
1943
t = self._transport(self.server.get_url())
1944
# FIXME: Needs cleanup -- vila 20100611
1969
1947
def assertActivitiesMatch(self):
1970
1948
self.assertEqual(self.server.bytes_read,
2076
2054
t = self.get_transport()
2077
2055
# We must send a single line of body bytes, see
2078
# PredefinedRequestHandler.handle_one_request
2056
# PredefinedRequestHandler._handle_one_request
2079
2057
code, f = t._post('abc def end-of-body\n')
2080
2058
self.assertEqual('lalala whatever as long as itsssss\n', f.read())
2081
2059
self.assertActivitiesMatch()
2115
2093
_auth_server = http_utils.HTTPBasicAuthServer
2116
2094
_transport = _urllib.HttpTransport_urllib
2118
def create_transport_readonly_server(self):
2119
return self._auth_server(protocol_version=self._protocol_version)
2121
def create_transport_secondary_server(self):
2122
"""Create the secondary server redirecting to the primary server"""
2123
new = self.get_readonly_server()
2125
redirecting = http_utils.HTTPServerRedirecting(
2126
protocol_version=self._protocol_version)
2127
redirecting.redirect_to(new.host, new.port)
2130
2096
def setUp(self):
2131
2097
super(TestAuthOnRedirected, self).setUp()
2132
2098
self.build_tree_contents([('a','a'),
2137
2103
self.new_server.port)
2138
2104
self.old_server.redirections = [
2139
2105
('(.*)', r'%s/1\1' % (new_prefix), 301),]
2140
self.old_transport = self._transport(self.old_server.get_url())
2106
self.old_transport = self.get_old_transport()
2141
2107
self.new_server.add_user('joe', 'foo')
2143
def get_a(self, transport):
2144
return transport.get('a')
2108
cleanup_http_redirection_connections(self)
2110
def create_transport_readonly_server(self):
2111
server = self._auth_server(protocol_version=self._protocol_version)
2112
server._url_protocol = self._url_protocol
2146
2118
def test_auth_on_redirected_via_do_catching_redirections(self):
2147
2119
self.redirections = 0
2149
def redirected(transport, exception, redirection_notice):
2121
def redirected(t, exception, redirection_notice):
2150
2122
self.redirections += 1
2151
dir, file = urlutils.split(exception.target)
2152
return self._transport(dir)
2123
redirected_t = t._redirected_to(exception.source, exception.target)
2124
self.addCleanup(redirected_t.disconnect)
2154
2127
stdout = tests.StringIOWrapper()
2155
2128
stderr = tests.StringIOWrapper()
2176
2149
self.new_server.port)
2177
2150
self.old_server.redirections = [
2178
2151
('(.*)', r'%s/1\1' % (new_prefix), 301),]
2179
self.assertEqual('redirected once',t._perform(req).read())
2152
self.assertEqual('redirected once', t._perform(req).read())
2180
2153
# stdin should be empty
2181
2154
self.assertEqual('', ui.ui_factory.stdin.readline())
2182
2155
# stdout should be empty, stderr will contains the prompts