~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smart_request.py

  • Committer: Andrew Bennetts
  • Date: 2009-02-11 09:54:36 UTC
  • mto: This revision was merged to the branch mainline in revision 4002.
  • Revision ID: andrew.bennetts@canonical.com-20090211095436-96dwxqkzc79iybxy
Add a test that unexpected request bodies trigger a SmartProtocolError from request implementations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Tests for smart server request infrastructure (bzrlib.smart.request)."""
18
18
 
 
19
from bzrlib import errors
19
20
from bzrlib.smart import request
20
21
from bzrlib.tests import TestCase
21
22
 
22
23
 
 
24
class NoBodyRequest(request.SmartServerRequest):
 
25
    """A request that does not implement do_body."""
 
26
 
 
27
    def do(self):
 
28
        return request.SuccessfulSmartServerResponse(('ok',))
 
29
 
 
30
 
23
31
class TestSmartRequest(TestCase):
24
32
 
25
33
    def test_request_class_without_do_body(self):
28
36
        """
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."""
33
 
            def do(self):
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.
 
45
 
 
46
    def test_unexpected_body(self):
 
47
        """If a request implementation receives an unexpected body, it
 
48
        raises an error.
 
49
        """
 
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
 
59
        # accept_body.
 
60
        exc = self.assertRaises(errors.SmartProtocolError, handler.end_received)
 
61
        self.assertEquals('Request does not expect a body', exc.details)
41
62