1 # -*- coding: utf-8 -*-
6 from .. import errors
as httperrors
7 from ...tests
.helpers
import TestCaseWithLogging
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
)
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
)
25 # and the function raises an unexpected error
28 raise AttributeError('some error')
30 # when the function is called
31 with self
.assertRaises(bottle
.HTTPError
):
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
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
)
42 # and the function raises an error that is considered by the
46 raise httperrors
.HttpMappedError(http_code
=404)
48 # when the function is called
49 with self
.assertRaises(bottle
.HTTPError
):
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
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
)
60 # and the function raises an error that is considered by the
62 exception
= bottle
.HTTPError()
68 # when the function is called
69 with self
.assertRaises(bottle
.HTTPError
) as context
:
71 # then the exception should bypass the error handler
72 self
.assertEqual(context
.exception
, exception
)
75 if __name__
== '__main__':