1146
1145
def test_hello(self):
1147
cmd = request.HelloRequest(None)
1146
cmd = _mod_request.HelloRequest(None, '/')
1148
1147
response = cmd.execute()
1149
1148
self.assertEqual(('ok', '2'), response.args)
1150
1149
self.assertEqual(None, response.body)
1156
1155
wt.add('hello')
1157
1156
rev_id = wt.commit('add hello')
1159
cmd = request.GetBundleRequest(self.get_transport())
1158
cmd = _mod_request.GetBundleRequest(self.get_transport(), '/')
1160
1159
response = cmd.execute('.', rev_id)
1161
1160
bundle = serializer.read_bundle(StringIO(response.body))
1162
1161
self.assertEqual((), response.args)
1172
1171
def build_handler(self, transport):
1173
1172
"""Returns a handler for the commands in protocol version one."""
1174
return request.SmartServerRequestHandler(transport,
1175
request.request_handlers)
1173
return _mod_request.SmartServerRequestHandler(
1174
transport, _mod_request.request_handlers, '/')
1177
1176
def test_construct_request_handler(self):
1178
1177
"""Constructing a request handler should be easy and set defaults."""
1179
handler = request.SmartServerRequestHandler(None, None)
1178
handler = _mod_request.SmartServerRequestHandler(None, commands=None,
1179
root_client_path='/')
1180
1180
self.assertFalse(handler.finished_reading)
1182
1182
def test_hello(self):
1188
1188
def test_disable_vfs_handler_classes_via_environment(self):
1189
1189
# VFS handler classes will raise an error from "execute" if
1190
1190
# BZR_NO_SMART_VFS is set.
1191
handler = vfs.HasRequest(None)
1191
handler = vfs.HasRequest(None, '/')
1192
1192
# set environment variable after construction to make sure it's
1194
1194
# Note that we can safely clobber BZR_NO_SMART_VFS here, because setUp
1253
1253
handler.accept_body('100,1')
1254
1254
handler.end_of_body()
1255
1255
self.assertTrue(handler.finished_reading)
1256
self.assertEqual(('ShortReadvError', 'a-file', '100', '1', '0'),
1256
self.assertEqual(('ShortReadvError', './a-file', '100', '1', '0'),
1257
1257
handler.response.args)
1258
1258
self.assertEqual(None, handler.response.body)
1344
1344
self.to_server)
1345
1345
self.client_protocol = self.client_protocol_class(self.client_medium)
1346
1346
self.smart_server = InstrumentedServerProtocol(self.server_to_client)
1347
self.smart_server_request = request.SmartServerRequestHandler(
1348
None, request.request_handlers)
1347
self.smart_server_request = _mod_request.SmartServerRequestHandler(
1348
None, _mod_request.request_handlers, root_client_path='/')
1350
1350
def assertOffsetSerialisation(self, expected_offsets, expected_serialised,
1361
1361
# XXX: '_deserialise_offsets' should be a method of the
1362
1362
# SmartServerRequestProtocol in future.
1363
readv_cmd = vfs.ReadvRequest(None)
1363
readv_cmd = vfs.ReadvRequest(None, '/')
1364
1364
offsets = readv_cmd._deserialise_offsets(expected_serialised)
1365
1365
self.assertEqual(expected_offsets, offsets)
1366
1366
serialised = client._serialise_offsets(offsets)
1375
1375
def do_body(cmd, body_bytes):
1376
1376
self.end_received = True
1377
1377
self.assertEqual('abcdefg', body_bytes)
1378
return request.SuccessfulSmartServerResponse(('ok', ))
1378
return _mod_request.SuccessfulSmartServerResponse(('ok', ))
1379
1379
smart_protocol.request._command = FakeCommand()
1380
1380
# Call accept_bytes to make sure that internal state like _body_decoder
1381
1381
# is initialised. This test should probably be given a clearer
1540
1540
None, lambda x: None)
1541
1541
self.assertEqual(1, smart_protocol.next_read_size())
1542
1542
smart_protocol._send_response(
1543
request.SuccessfulSmartServerResponse(('x',)))
1543
_mod_request.SuccessfulSmartServerResponse(('x',)))
1544
1544
self.assertEqual(0, smart_protocol.next_read_size())
1546
1546
def test__send_response_errors_with_base_response(self):
1548
1548
smart_protocol = protocol.SmartServerRequestProtocolOne(
1549
1549
None, lambda x: None)
1550
1550
self.assertRaises(AttributeError, smart_protocol._send_response,
1551
request.SmartServerResponse(('x',)))
1551
_mod_request.SmartServerResponse(('x',)))
1553
1553
def test_query_version(self):
1554
1554
"""query_version on a SmartClientProtocolOne should return a number.
1773
1773
def test_body_stream_error_serialistion(self):
1774
1774
stream = ['first chunk',
1775
request.FailedSmartServerResponse(
1775
_mod_request.FailedSmartServerResponse(
1776
1776
('FailureName', 'failure arg'))]
1777
1777
expected_bytes = (
1778
1778
'chunked\n' + 'b\nfirst chunk' +
1864
1864
None, lambda x: None)
1865
1865
self.assertEqual(1, smart_protocol.next_read_size())
1866
1866
smart_protocol._send_response(
1867
request.SuccessfulSmartServerResponse(('x',)))
1867
_mod_request.SuccessfulSmartServerResponse(('x',)))
1868
1868
self.assertEqual(0, smart_protocol.next_read_size())
1870
1870
def test__send_response_with_body_stream_sets_finished_reading(self):
1872
1872
None, lambda x: None)
1873
1873
self.assertEqual(1, smart_protocol.next_read_size())
1874
1874
smart_protocol._send_response(
1875
request.SuccessfulSmartServerResponse(('x',), body_stream=[]))
1875
_mod_request.SuccessfulSmartServerResponse(('x',), body_stream=[]))
1876
1876
self.assertEqual(0, smart_protocol.next_read_size())
1878
1878
def test__send_response_errors_with_base_response(self):
1880
1880
smart_protocol = protocol.SmartServerRequestProtocolTwo(
1881
1881
None, lambda x: None)
1882
1882
self.assertRaises(AttributeError, smart_protocol._send_response,
1883
request.SmartServerResponse(('x',)))
1883
_mod_request.SmartServerResponse(('x',)))
1885
1885
def test__send_response_includes_failure_marker(self):
1886
1886
"""FailedSmartServerResponse have 'failed\n' after the version."""
1888
1888
smart_protocol = protocol.SmartServerRequestProtocolTwo(
1889
1889
None, out_stream.write)
1890
1890
smart_protocol._send_response(
1891
request.FailedSmartServerResponse(('x',)))
1891
_mod_request.FailedSmartServerResponse(('x',)))
1892
1892
self.assertEqual(protocol.RESPONSE_VERSION_TWO + 'failed\nx\n',
1893
1893
out_stream.getvalue())
1898
1898
smart_protocol = protocol.SmartServerRequestProtocolTwo(
1899
1899
None, out_stream.write)
1900
1900
smart_protocol._send_response(
1901
request.SuccessfulSmartServerResponse(('x',)))
1901
_mod_request.SuccessfulSmartServerResponse(('x',)))
1902
1902
self.assertEqual(protocol.RESPONSE_VERSION_TWO + 'success\nx\n',
1903
1903
out_stream.getvalue())
2081
2081
smart_protocol.read_response_tuple(True)
2082
2082
expected_chunks = [
2084
request.FailedSmartServerResponse(('error arg1', 'arg2'))]
2084
_mod_request.FailedSmartServerResponse(('error arg1', 'arg2'))]
2085
2085
stream = smart_protocol.read_streamed_body()
2086
2086
self.assertEqual(expected_chunks, list(stream))
2106
2106
input = StringIO("\n")
2107
2107
output = StringIO()
2108
2108
client_medium = medium.SmartSimplePipesClientMedium(input, output)
2109
smart_client = client._SmartClient(client_medium)
2109
smart_client = client._SmartClient(client_medium, 'ignored base')
2110
2110
self.assertRaises(TypeError,
2111
2111
smart_client.call_with_body_bytes, method, args, body)
2112
2112
self.assertEqual("", output.getvalue())
2342
2342
decoder.accept_bytes(chunk_one + error_signal + error_chunks + finish)
2343
2343
self.assertTrue(decoder.finished_reading)
2344
2344
self.assertEqual('first chunk', decoder.read_next_chunk())
2345
expected_failure = request.FailedSmartServerResponse(
2345
expected_failure = _mod_request.FailedSmartServerResponse(
2346
2346
('part1', 'part2'))
2347
2347
self.assertEqual(expected_failure, decoder.read_next_chunk())
2358
2358
class TestSuccessfulSmartServerResponse(tests.TestCase):
2360
2360
def test_construct_no_body(self):
2361
response = request.SuccessfulSmartServerResponse(('foo', 'bar'))
2361
response = _mod_request.SuccessfulSmartServerResponse(('foo', 'bar'))
2362
2362
self.assertEqual(('foo', 'bar'), response.args)
2363
2363
self.assertEqual(None, response.body)
2365
2365
def test_construct_with_body(self):
2366
response = request.SuccessfulSmartServerResponse(
2367
('foo', 'bar'), 'bytes')
2366
response = _mod_request.SuccessfulSmartServerResponse(('foo', 'bar'),
2368
2368
self.assertEqual(('foo', 'bar'), response.args)
2369
2369
self.assertEqual('bytes', response.body)
2370
2370
# repr(response) doesn't trigger exceptions.
2373
2373
def test_construct_with_body_stream(self):
2374
2374
bytes_iterable = ['abc']
2375
response = request.SuccessfulSmartServerResponse(
2375
response = _mod_request.SuccessfulSmartServerResponse(
2376
2376
('foo', 'bar'), body_stream=bytes_iterable)
2377
2377
self.assertEqual(('foo', 'bar'), response.args)
2378
2378
self.assertEqual(bytes_iterable, response.body_stream)
2381
2381
"""'body' and 'body_stream' are mutually exclusive."""
2382
2382
self.assertRaises(
2383
2383
errors.BzrError,
2384
request.SuccessfulSmartServerResponse, (), 'body', ['stream'])
2384
_mod_request.SuccessfulSmartServerResponse, (), 'body', ['stream'])
2386
2386
def test_is_successful(self):
2387
2387
"""is_successful should return True for SuccessfulSmartServerResponse."""
2388
response = request.SuccessfulSmartServerResponse(('error',))
2388
response = _mod_request.SuccessfulSmartServerResponse(('error',))
2389
2389
self.assertEqual(True, response.is_successful())
2392
2392
class TestFailedSmartServerResponse(tests.TestCase):
2394
2394
def test_construct(self):
2395
response = request.FailedSmartServerResponse(('foo', 'bar'))
2395
response = _mod_request.FailedSmartServerResponse(('foo', 'bar'))
2396
2396
self.assertEqual(('foo', 'bar'), response.args)
2397
2397
self.assertEqual(None, response.body)
2398
response = request.FailedSmartServerResponse(('foo', 'bar'), 'bytes')
2398
response = _mod_request.FailedSmartServerResponse(('foo', 'bar'), 'bytes')
2399
2399
self.assertEqual(('foo', 'bar'), response.args)
2400
2400
self.assertEqual('bytes', response.body)
2401
2401
# repr(response) doesn't trigger exceptions.
2404
2404
def test_is_successful(self):
2405
2405
"""is_successful should return False for FailedSmartServerResponse."""
2406
response = request.FailedSmartServerResponse(('error',))
2406
response = _mod_request.FailedSmartServerResponse(('error',))
2407
2407
self.assertEqual(False, response.is_successful())