2325
class HTTPTunnellingSmokeTest(tests.TestCaseWithTransport):
2325
class HTTPTunnellingSmokeTest(tests.TestCase):
2327
2327
def setUp(self):
2328
2328
super(HTTPTunnellingSmokeTest, self).setUp()
2329
2329
# We use the VFS layer as part of HTTP tunnelling tests.
2330
2330
self._captureVar('BZR_NO_SMART_VFS', None)
2332
def _test_bulk_data(self, url_protocol):
2333
# We should be able to send and receive bulk data in a single message.
2334
# The 'readv' command in the smart protocol both sends and receives bulk
2335
# data, so we use that.
2336
self.build_tree(['data-file'])
2337
self.transport_readonly_server = HTTPServerWithSmarts
2339
http_transport = self.get_readonly_transport()
2340
medium = http_transport.get_smart_medium()
2341
# Since we provide the medium, the url below will be mostly ignored
2342
# during the test, as long as the path is '/'.
2343
remote_transport = remote.RemoteTransport('bzr://fake_host/',
2346
[(0, "c")], list(remote_transport.readv("data-file", [(0,1)])))
2348
def test_bulk_data_pycurl(self):
2350
self._test_bulk_data('http+pycurl')
2351
except errors.UnsupportedProtocol, e:
2352
raise tests.TestSkipped(str(e))
2354
def test_bulk_data_urllib(self):
2355
self._test_bulk_data('http+urllib')
2357
2332
def test_smart_http_medium_request_accept_bytes(self):
2358
2333
medium = FakeHTTPMedium()
2359
2334
request = SmartClientHTTPMediumRequest(medium)
2363
2338
request.finished_writing()
2364
2339
self.assertEqual('abcdef', medium.written_request)
2366
def _test_http_send_smart_request(self, url_protocol):
2367
http_server = HTTPServerWithSmarts()
2368
http_server._url_protocol = url_protocol
2369
http_server.setUp(self.get_vfs_only_server())
2370
self.addCleanup(http_server.tearDown)
2372
post_body = 'hello\n'
2373
expected_reply_body = 'ok\x012\n'
2375
http_transport = get_transport(http_server.get_url())
2376
medium = http_transport.get_smart_medium()
2377
response = medium.send_http_smart_request(post_body)
2378
reply_body = response.read()
2379
self.assertEqual(expected_reply_body, reply_body)
2381
def test_http_send_smart_request_pycurl(self):
2383
self._test_http_send_smart_request('http+pycurl')
2384
except errors.UnsupportedProtocol, e:
2385
raise tests.TestSkipped(str(e))
2387
def test_http_send_smart_request_urllib(self):
2388
self._test_http_send_smart_request('http+urllib')
2390
def test_http_server_with_smarts(self):
2391
self.transport_readonly_server = HTTPServerWithSmarts
2393
post_body = 'hello\n'
2394
expected_reply_body = 'ok\x012\n'
2396
smart_server_url = self.get_readonly_url('.bzr/smart')
2397
reply = urllib2.urlopen(smart_server_url, post_body).read()
2399
self.assertEqual(expected_reply_body, reply)
2401
def test_smart_http_server_post_request_handler(self):
2402
self.transport_readonly_server = HTTPServerWithSmarts
2403
httpd = self.get_readonly_server()._get_httpd()
2405
socket = SampleSocket(
2406
'POST /.bzr/smart HTTP/1.0\r\n'
2407
# HTTP/1.0 posts must have a Content-Length.
2408
'Content-Length: 6\r\n'
2411
# Beware: the ('localhost', 80) below is the
2412
# client_address parameter, but we don't have one because
2413
# we have defined a socket which is not bound to an
2414
# address. The test framework never uses this client
2415
# address, so far...
2416
request_handler = SmartRequestHandler(socket, ('localhost', 80), httpd)
2417
response = socket.writefile.getvalue()
2418
self.assertStartsWith(response, 'HTTP/1.0 200 ')
2419
# This includes the end of the HTTP headers, and all the body.
2420
expected_end_of_response = '\r\n\r\nok\x012\n'
2421
self.assertEndsWith(response, expected_end_of_response)
2424
class SampleSocket(object):
2425
"""A socket-like object for use in testing the HTTP request handler."""
2427
def __init__(self, socket_read_content):
2428
"""Constructs a sample socket.
2430
:param socket_read_content: a byte sequence
2432
# Use plain python StringIO so we can monkey-patch the close method to
2433
# not discard the contents.
2434
from StringIO import StringIO
2435
self.readfile = StringIO(socket_read_content)
2436
self.writefile = StringIO()
2437
self.writefile.close = lambda: None
2439
def makefile(self, mode='r', bufsize=None):
2441
return self.readfile
2443
return self.writefile
2446
2342
class RemoteHTTPTransportTestCase(tests.TestCase):