1
# Copyright 2009 Canonical Ltd.
3
# This file is part of lazr.restfulclient.
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
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.
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/>.
18
"""Tests for the error_for helper function."""
24
from lazr.restfulclient.errors import (
25
BadRequest, ClientError, Conflict, HTTPError, MethodNotAllowed, NotFound,
26
PreconditionFailed, ResponseError, ServerError, Unauthorized, error_for)
29
class DummyRequest(object):
30
"""Just enough of a request to fool error_for()."""
31
def __init__(self, status):
35
class TestErrorFor(unittest.TestCase):
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)
44
self.assertTrue(isinstance(error, expected_error))
45
self.assertEquals(content, error.content)
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)
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)
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)
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")
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")
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")
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")
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")
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")
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")
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)