Adding cover to tox.ini default envs
[osm/RO.git] / RO / osm_ro / http_tools / tests / test_errors.py
1 # -*- coding: utf-8 -*-
2 ##
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14 ##
15
16 import unittest
17
18 import bottle
19
20 from .. import errors as httperrors
21 from ...tests.helpers import TestCaseWithLogging
22
23
24 class TestHttpErrors(TestCaseWithLogging):
25 def test_http_error_base(self):
26 # When an error code is passed as argument
27 ex = httperrors.HttpMappedError(http_code=1226324)
28 # then it should be set in the exception object
29 self.assertEqual(ex.http_code, 1226324)
30 # When an error code is not passed as argument
31 ex = httperrors.HttpMappedError()
32 # then the default error code (internal server error) should be used
33 self.assertEqual(ex.http_code, httperrors.Internal_Server_Error)
34
35 def test_error_handler_should_log_unexpected_errors(self):
36 # Given a error handler wraps a function
37 error_handler = httperrors.ErrorHandler(self.logger)
38
39 # and the function raises an unexpected error
40 @error_handler
41 def _throw():
42 raise AttributeError('some error')
43
44 # when the function is called
45 with self.assertRaises(bottle.HTTPError):
46 _throw()
47 logs = self.caplog.getvalue()
48 # then the exception should be contained by bottle
49 # and a proper message should be logged
50 assert "Unexpected exception:" in logs
51
52 def test_error_handler_should_log_http_based_errors(self):
53 # Given a error handler wraps a function
54 error_handler = httperrors.ErrorHandler(self.logger)
55
56 # and the function raises an error that is considered by the
57 # application
58 @error_handler
59 def _throw():
60 raise httperrors.HttpMappedError(http_code=404)
61
62 # when the function is called
63 with self.assertRaises(bottle.HTTPError):
64 _throw()
65 logs = self.caplog.getvalue()
66 # then the exception should be contained by bottle
67 # and a proper message should be logged
68 assert "_throw error 404" in logs
69
70 def test_error_handler_should_ignore_bottle_errors(self):
71 # Given a error handler wraps a function
72 error_handler = httperrors.ErrorHandler(self.logger)
73
74 # and the function raises an error that is considered by the
75 # application
76 exception = bottle.HTTPError()
77
78 @error_handler
79 def _throw():
80 raise exception
81
82 # when the function is called
83 with self.assertRaises(bottle.HTTPError) as context:
84 _throw()
85 # then the exception should bypass the error handler
86 self.assertEqual(context.exception, exception)
87
88
89 if __name__ == '__main__':
90 unittest.main()