|
| 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 tests import unittest |
| 14 | + |
| 15 | +import mock |
| 16 | +from awscli import errorhandler |
| 17 | + |
| 18 | + |
| 19 | +class TestErrorHandler(unittest.TestCase): |
| 20 | + |
| 21 | + def create_http_response(self, **kwargs): |
| 22 | + response = mock.Mock() |
| 23 | + for key, value in kwargs.items(): |
| 24 | + setattr(response, key, value) |
| 25 | + return response |
| 26 | + |
| 27 | + def test_error_handler_client_side(self): |
| 28 | + response = { |
| 29 | + 'CommonPrefixes': [], |
| 30 | + 'Contents': [], |
| 31 | + 'Errors': [{'Code': 'AccessDenied', |
| 32 | + 'HostId': 'foohost', |
| 33 | + 'Message': 'Access Denied', |
| 34 | + 'RequestId': 'requestid'}], |
| 35 | + 'ResponseMetadata': {}} |
| 36 | + handler = errorhandler.ErrorHandler() |
| 37 | + http_response = self.create_http_response(status_code=403) |
| 38 | + # We're manually using the try/except form because |
| 39 | + # we want to catch the exception and assert that it has specific |
| 40 | + # attributes on it. |
| 41 | + operation = mock.Mock() |
| 42 | + operation.name = 'OperationName' |
| 43 | + try: |
| 44 | + handler(http_response, response, operation) |
| 45 | + except errorhandler.ClientError as e: |
| 46 | + # First, the operation name should be in the error message. |
| 47 | + self.assertIn('OperationName', str(e)) |
| 48 | + # We should state that this is a ClientError. |
| 49 | + self.assertIn('client error', str(e)) |
| 50 | + # And these values should be available on the exception |
| 51 | + # so clients can access this information programmatically. |
| 52 | + self.assertEqual(e.error_code, 'AccessDenied') |
| 53 | + self.assertEqual(e.error_message, 'Access Denied') |
| 54 | + self.assertEqual(e.operation_name, 'OperationName') |
| 55 | + except Exception as e: |
| 56 | + self.fail("Unexpected error raised: %s" % e) |
| 57 | + else: |
| 58 | + self.fail("Expected errorhandler.ClientError to be raised " |
| 59 | + "but no exception was raised.") |
| 60 | + |
| 61 | + def test_no_exception_raised_on_200(self): |
| 62 | + response = { |
| 63 | + 'CommonPrefixes': [], |
| 64 | + 'Contents': [], |
| 65 | + } |
| 66 | + handler = errorhandler.ErrorHandler() |
| 67 | + http_response = self.create_http_response(status_code=200) |
| 68 | + # We're manually using the try/except form because |
| 69 | + # we want to catch the exception and assert that it has specific |
| 70 | + # attributes on it. |
| 71 | + operation = mock.Mock() |
| 72 | + operation.name = 'OperationName' |
| 73 | + try: |
| 74 | + self.assertIsNone(handler(http_response, response, operation)) |
| 75 | + except errorhandler.BaseOperationError as e: |
| 76 | + self.fail("Unexpected error raised: %s" % e) |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == '__main__': |
| 80 | + unittest.main() |
0 commit comments