17
17
"""Tests for smart server request infrastructure (bzrlib.smart.request)."""
19
from bzrlib import errors
19
20
from bzrlib.smart import request
20
21
from bzrlib.tests import TestCase
24
class NoBodyRequest(request.SmartServerRequest):
25
"""A request that does not implement do_body."""
28
return request.SuccessfulSmartServerResponse(('ok',))
23
31
class TestSmartRequest(TestCase):
25
33
def test_request_class_without_do_body(self):
29
37
# Create a SmartServerRequestHandler with a SmartServerRequest subclass
30
38
# that does not implement do_body.
31
class NoBodyRequest(request.SmartServerRequest):
32
"""A request that does not implement do_body."""
34
return request.SuccessfulSmartServerResponse(('ok',))
35
39
handler = request.SmartServerRequestHandler(
36
40
None, {'foo': NoBodyRequest}, '/')
37
41
# Emulate a request with no body (i.e. just args).
38
42
handler.args_received(('foo',))
39
43
handler.end_received()
40
# no exception was raised.
44
# Request done, no exception was raised.
46
def test_unexpected_body(self):
47
"""If a request implementation receives an unexpected body, it
50
# Create a SmartServerRequestHandler with a SmartServerRequest subclass
51
# that does not implement do_body.
52
handler = request.SmartServerRequestHandler(
53
None, {'foo': NoBodyRequest}, '/')
54
# Emulate a request with a body
55
handler.args_received(('foo',))
56
handler.accept_body('some body bytes')
57
# Note that the exception currently occurs at the end of the request.
58
# In principle it would also be ok for it to happen earlier, during
60
exc = self.assertRaises(errors.SmartProtocolError, handler.end_received)
61
self.assertEquals('Request does not expect a body', exc.details)