1 # -*- coding: utf-8 -*-
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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
20 from .. import errors
as httperrors
21 from ...tests
.helpers
import TestCaseWithLogging
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
)
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
)
39 # and the function raises an unexpected error
42 raise AttributeError('some error')
44 # when the function is called
45 with self
.assertRaises(bottle
.HTTPError
):
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
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
)
56 # and the function raises an error that is considered by the
60 raise httperrors
.HttpMappedError(http_code
=404)
62 # when the function is called
63 with self
.assertRaises(bottle
.HTTPError
):
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
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
)
74 # and the function raises an error that is considered by the
76 exception
= bottle
.HTTPError()
82 # when the function is called
83 with self
.assertRaises(bottle
.HTTPError
) as context
:
85 # then the exception should bypass the error handler
86 self
.assertEqual(context
.exception
, exception
)
89 if __name__
== '__main__':