~ubuntu-branches/debian/experimental/lazr.restfulclient/experimental

« back to all changes in this revision

Viewing changes to src/lazr/restfulclient/tests/test_error.py

  • Committer: Bazaar Package Importer
  • Author(s): Luca Falavigna
  • Date: 2010-07-24 15:54:08 UTC
  • mfrom: (1.2.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20100724155408-utbdh42cijnipvva
Tags: 0.9.21-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2009 Canonical Ltd.
 
2
 
 
3
# This file is part of lazr.restfulclient.
 
4
#
 
5
# lazr.restfulclient is free software: you can redistribute it and/or
 
6
# modify it under the terms of the GNU Lesser General Public License
 
7
# as published by the Free Software Foundation, version 3 of the
 
8
# License.
 
9
#
 
10
# lazr.restfulclient is distributed in the hope that it will be
 
11
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 
12
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
13
# Lesser General Public License for more details.
 
14
#
 
15
# You should have received a copy of the GNU Lesser General Public License
 
16
# along with lazr.restfulclient. If not, see <http://www.gnu.org/licenses/>.
 
17
 
 
18
"""Tests for the error_for helper function."""
 
19
 
 
20
__metaclass__ = type
 
21
 
 
22
import unittest
 
23
 
 
24
from lazr.restfulclient.errors import (
 
25
    BadRequest, ClientError, Conflict, HTTPError, MethodNotAllowed, NotFound,
 
26
    PreconditionFailed, ResponseError, ServerError, Unauthorized, error_for)
 
27
 
 
28
 
 
29
class DummyRequest(object):
 
30
    """Just enough of a request to fool error_for()."""
 
31
    def __init__(self, status):
 
32
        self.status = status
 
33
 
 
34
 
 
35
class TestErrorFor(unittest.TestCase):
 
36
 
 
37
    def error_for_status(self, status, expected_error, content=''):
 
38
        """Make sure error_for returns the right HTTPError subclass."""
 
39
        request = DummyRequest(status)
 
40
        error = error_for(request, content)
 
41
        if expected_error is None:
 
42
            self.assertEquals(error, None)
 
43
        else:
 
44
            self.assertTrue(isinstance(error, expected_error))
 
45
            self.assertEquals(content, error.content)
 
46
 
 
47
    def test_no_error_for_2xx(self):
 
48
        """Make sure a 2xx response code yields no error."""
 
49
        for status in (200, 201, 209, 299):
 
50
            self.error_for_status(status, None)
 
51
 
 
52
    def test_no_error_for_2xx(self):
 
53
        """Make sure a 2xx response code yields no error."""
 
54
        for status in (200, 201, 209, 299):
 
55
            self.error_for_status(status, None)
 
56
 
 
57
    def test_no_error_for_3xx(self):
 
58
        """Make sure a 3xx response code yields no error."""
 
59
        for status in (301, 302, 303, 304, 399):
 
60
            self.error_for_status(status, None)
 
61
 
 
62
    def test_error_for_400(self):
 
63
        """Make sure a 400 response code yields ResponseError."""
 
64
        self.error_for_status(400, ResponseError, "error message")
 
65
 
 
66
    def test_error_for_401(self):
 
67
        """Make sure a 401 response code yields Unauthorized."""
 
68
        self.error_for_status(401, Unauthorized, "error message")
 
69
 
 
70
    def test_error_for_404(self):
 
71
        """Make sure a 404 response code yields Not Found."""
 
72
        self.error_for_status(404, NotFound, "error message")
 
73
 
 
74
    def test_error_for_404(self):
 
75
        """Make sure a 405 response code yields MethodNotAllowed."""
 
76
        self.error_for_status(405, MethodNotAllowed, "error message")
 
77
 
 
78
    def test_error_for_409(self):
 
79
        """Make sure a 409 response code yields Conflict."""
 
80
        self.error_for_status(409, Conflict, "error message")
 
81
 
 
82
    def test_error_for_412(self):
 
83
        """Make sure a 412 response code yields PreconditionFailed."""
 
84
        self.error_for_status(412, PreconditionFailed, "error message")
 
85
 
 
86
    def test_error_for_4xx(self):
 
87
        """Make sure an unrexognized 4xx response code yields ClientError."""
 
88
        self.error_for_status(499, ClientError, "error message")
 
89
 
 
90
    def test_no_error_for_5xx(self):
 
91
        """Make sure a 5xx response codes yields ServerError."""
 
92
        for status in (500, 502, 503, 599):
 
93
            self.error_for_status(status, ServerError)