28
28
return request.SuccessfulSmartServerResponse(('ok',))
31
class DoErrorRequest(request.SmartServerRequest):
32
"""A request that raises an error from self.do()."""
35
raise errors.NoSuchFile('xyzzy')
38
class ChunkErrorRequest(request.SmartServerRequest):
39
"""A request that raises an error from self.do_chunk()."""
45
def do_chunk(self, bytes):
46
raise errors.NoSuchFile('xyzzy')
49
class EndErrorRequest(request.SmartServerRequest):
50
"""A request that raises an error from self.do_end()."""
56
def do_chunk(self, bytes):
61
raise errors.NoSuchFile('xyzzy')
31
64
class TestSmartRequest(TestCase):
33
66
def test_request_class_without_do_body(self):
43
76
handler.end_received()
44
77
# Request done, no exception was raised.
80
class TestSmartRequestHandlerErrorTranslation(TestCase):
81
"""Tests that SmartServerRequestHandler will translate exceptions raised by
82
a SmartServerRequest into FailedSmartServerResponses.
85
def assertNoResponse(self, handler):
86
self.assertEqual(None, handler.response)
88
def assertResponseIsTranslatedError(self, handler):
89
expected_translation = ('NoSuchFile', 'xyzzy')
91
request.FailedSmartServerResponse(expected_translation),
94
def test_error_translation_from_args_received(self):
95
handler = request.SmartServerRequestHandler(
96
None, {'foo': DoErrorRequest}, '/')
97
handler.args_received(('foo',))
98
self.assertResponseIsTranslatedError(handler)
100
def test_error_translation_from_chunk_received(self):
101
handler = request.SmartServerRequestHandler(
102
None, {'foo': ChunkErrorRequest}, '/')
103
handler.args_received(('foo',))
104
self.assertNoResponse(handler)
105
handler.accept_body('bytes')
106
self.assertResponseIsTranslatedError(handler)
108
def test_error_translation_from_end_received(self):
109
handler = request.SmartServerRequestHandler(
110
None, {'foo': EndErrorRequest}, '/')
111
handler.args_received(('foo',))
112
self.assertNoResponse(handler)
113
handler.end_received()
114
self.assertResponseIsTranslatedError(handler)
117
class TestRequestHanderErrorTranslation(TestCase):
118
"""Tests for bzrlib.smart.request._translate_error."""
120
def assertTranslationEqual(self, expected_tuple, error):
121
self.assertEqual(expected_tuple, request._translate_error(error))
123
def test_NoSuchFile(self):
124
self.assertTranslationEqual(
125
('NoSuchFile', 'path'), errors.NoSuchFile('path'))
127
def test_LockContention(self):
128
self.assertTranslationEqual(
129
('LockContention', 'lock', 'msg'),
130
errors.LockContention('lock', 'msg'))
132
def test_TokenMismatch(self):
133
self.assertTranslationEqual(
134
('TokenMismatch', 'some-token', 'actual-token'),
135
errors.TokenMismatch('some-token', 'actual-token'))