~bzr-pqm/bzr/bzr.dev

3990.3.2 by Andrew Bennetts
Fix the do_body NotImplementedError log spam.
1
# Copyright (C) 2009 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Tests for smart server request infrastructure (bzrlib.smart.request)."""
18
3990.3.3 by Andrew Bennetts
Add a test that unexpected request bodies trigger a SmartProtocolError from request implementations.
19
from bzrlib import errors
3990.3.2 by Andrew Bennetts
Fix the do_body NotImplementedError log spam.
20
from bzrlib.smart import request
21
from bzrlib.tests import TestCase
22
23
3990.3.3 by Andrew Bennetts
Add a test that unexpected request bodies trigger a SmartProtocolError from request implementations.
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
3990.3.2 by Andrew Bennetts
Fix the do_body NotImplementedError log spam.
31
class TestSmartRequest(TestCase):
32
33
    def test_request_class_without_do_body(self):
34
        """If a request has no body data, and the request's implementation does
35
        not override do_body, then no exception is raised.
36
        """
37
        # Create a SmartServerRequestHandler with a SmartServerRequest subclass
38
        # that does not implement do_body.
39
        handler = request.SmartServerRequestHandler(
40
            None, {'foo': NoBodyRequest}, '/')
41
        # Emulate a request with no body (i.e. just args).
42
        handler.args_received(('foo',))
43
        handler.end_received()
3990.3.3 by Andrew Bennetts
Add a test that unexpected request bodies trigger a SmartProtocolError from request implementations.
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)
3990.3.2 by Andrew Bennetts
Fix the do_body NotImplementedError log spam.
62