Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/tests/unit/test_errorhandler.py
1566 views
1
# Copyright 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License"). You
4
# may not use this file except in compliance with the License. A copy of
5
# the License is located at
6
#
7
# http://aws.amazon.com/apache2.0/
8
#
9
# or in the "license" file accompanying this file. This file is
10
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
# ANY KIND, either express or implied. See the License for the specific
12
# language governing permissions and limitations under the License.
13
from awscli.testutils import mock, unittest
14
15
from awscli import errorhandler
16
17
18
class TestErrorHandler(unittest.TestCase):
19
20
def create_http_response(self, **kwargs):
21
response = mock.Mock()
22
for key, value in kwargs.items():
23
setattr(response, key, value)
24
return response
25
26
def test_error_handler_client_side(self):
27
response = {
28
'Error': {'Code': 'AccessDenied',
29
'HostId': 'foohost',
30
'Message': 'Access Denied',
31
'RequestId': 'requestid'},
32
'ResponseMetadata': {}}
33
handler = errorhandler.ErrorHandler()
34
http_response = self.create_http_response(status_code=403)
35
# We're manually using the try/except form because
36
# we want to catch the exception and assert that it has specific
37
# attributes on it.
38
operation = mock.Mock()
39
operation.name = 'OperationName'
40
try:
41
handler(http_response, response, operation)
42
except errorhandler.ClientError as e:
43
# First, the operation name should be in the error message.
44
self.assertIn('OperationName', str(e))
45
# We should state that this is a ClientError.
46
self.assertIn('client error', str(e))
47
# And these values should be available on the exception
48
# so clients can access this information programmatically.
49
self.assertEqual(e.error_code, 'AccessDenied')
50
self.assertEqual(e.error_message, 'Access Denied')
51
self.assertEqual(e.operation_name, 'OperationName')
52
except Exception as e:
53
self.fail("Unexpected error raised: %s" % e)
54
else:
55
self.fail("Expected errorhandler.ClientError to be raised "
56
"but no exception was raised.")
57
58
def test_error_handler_server_side(self):
59
response = {
60
'Error': {'Code': 'InternalError',
61
'HostId': 'foohost',
62
'Message': 'An internal error has occurred',
63
'RequestId': 'requestid'},
64
'ResponseMetadata': {}}
65
handler = errorhandler.ErrorHandler()
66
http_response = self.create_http_response(status_code=500)
67
# We're manually using the try/except form because
68
# we want to catch the exception and assert that it has specific
69
# attributes on it.
70
operation = mock.Mock()
71
operation.name = 'OperationName'
72
try:
73
handler(http_response, response, operation)
74
except errorhandler.ServerError as e:
75
# First, the operation name should be in the error message.
76
self.assertIn('OperationName', str(e))
77
# We should state that this is a ServerError.
78
self.assertIn('server error', str(e))
79
# And these values should be available on the exception
80
# so clients can access this information programmatically.
81
self.assertEqual(e.error_code, 'InternalError')
82
self.assertEqual(e.error_message, 'An internal error has occurred')
83
self.assertEqual(e.operation_name, 'OperationName')
84
except Exception as e:
85
self.fail("Unexpected error raised: %s" % e)
86
else:
87
self.fail("Expected errorhandler.ServerError to be raised "
88
"but no exception was raised.")
89
90
def test_no_exception_raised_on_200(self):
91
response = {
92
'CommonPrefixes': [],
93
'Contents': [],
94
}
95
handler = errorhandler.ErrorHandler()
96
http_response = self.create_http_response(status_code=200)
97
# We're manually using the try/except form because
98
# we want to catch the exception and assert that it has specific
99
# attributes on it.
100
operation = mock.Mock()
101
operation.name = 'OperationName'
102
try:
103
self.assertIsNone(handler(http_response, response, operation))
104
except errorhandler.BaseOperationError as e:
105
self.fail("Unexpected error raised: %s" % e)
106
107
108
if __name__ == '__main__':
109
unittest.main()
110
111