feature 8029 change RO to python3. Using vim plugins
[osm/RO.git] / RO / osm_ro / http_tools / tests / test_errors.py
1 # -*- coding: utf-8 -*-
2 import unittest
3
4 import bottle
5
6 from .. import errors as httperrors
7 from ...tests.helpers import TestCaseWithLogging
8
9
10 class TestHttpErrors(TestCaseWithLogging):
11 def test_http_error_base(self):
12 # When an error code is passed as argument
13 ex = httperrors.HttpMappedError(http_code=1226324)
14 # then it should be set in the exception object
15 self.assertEqual(ex.http_code, 1226324)
16 # When an error code is not passed as argument
17 ex = httperrors.HttpMappedError()
18 # then the default error code (internal server error) should be used
19 self.assertEqual(ex.http_code, httperrors.Internal_Server_Error)
20
21 def test_error_handler_should_log_unexpected_errors(self):
22 # Given a error handler wraps a function
23 error_handler = httperrors.ErrorHandler(self.logger)
24
25 # and the function raises an unexpected error
26 @error_handler
27 def _throw():
28 raise AttributeError('some error')
29
30 # when the function is called
31 with self.assertRaises(bottle.HTTPError):
32 _throw()
33 logs = self.caplog.getvalue()
34 # then the exception should be contained by bottle
35 # and a proper message should be logged
36 assert "Unexpected exception:" in logs
37
38 def test_error_handler_should_log_http_based_errors(self):
39 # Given a error handler wraps a function
40 error_handler = httperrors.ErrorHandler(self.logger)
41
42 # and the function raises an error that is considered by the
43 # application
44 @error_handler
45 def _throw():
46 raise httperrors.HttpMappedError(http_code=404)
47
48 # when the function is called
49 with self.assertRaises(bottle.HTTPError):
50 _throw()
51 logs = self.caplog.getvalue()
52 # then the exception should be contained by bottle
53 # and a proper message should be logged
54 assert "_throw error 404" in logs
55
56 def test_error_handler_should_ignore_bottle_errors(self):
57 # Given a error handler wraps a function
58 error_handler = httperrors.ErrorHandler(self.logger)
59
60 # and the function raises an error that is considered by the
61 # application
62 exception = bottle.HTTPError()
63
64 @error_handler
65 def _throw():
66 raise exception
67
68 # when the function is called
69 with self.assertRaises(bottle.HTTPError) as context:
70 _throw()
71 # then the exception should bypass the error handler
72 self.assertEqual(context.exception, exception)
73
74
75 if __name__ == '__main__':
76 unittest.main()