This class will allow you to stub out requests so you don't have to hit an endpoint to write tests. Responses are returned first in, first out. If operations are called out of order, or are called with no remaining queued responses, an error will be raised. **Example:** ::
| 44 | |
| 45 | |
| 46 | class Stubber: |
| 47 | """ |
| 48 | This class will allow you to stub out requests so you don't have to hit |
| 49 | an endpoint to write tests. Responses are returned first in, first out. |
| 50 | If operations are called out of order, or are called with no remaining |
| 51 | queued responses, an error will be raised. |
| 52 | |
| 53 | **Example:** |
| 54 | :: |
| 55 | import datetime |
| 56 | import botocore.session |
| 57 | from botocore.stub import Stubber |
| 58 | |
| 59 | |
| 60 | s3 = botocore.session.get_session().create_client('s3') |
| 61 | stubber = Stubber(s3) |
| 62 | |
| 63 | response = { |
| 64 | 'IsTruncated': False, |
| 65 | 'Name': 'test-bucket', |
| 66 | 'MaxKeys': 1000, 'Prefix': '', |
| 67 | 'Contents': [{ |
| 68 | 'Key': 'test.txt', |
| 69 | 'ETag': '"abc123"', |
| 70 | 'StorageClass': 'STANDARD', |
| 71 | 'LastModified': datetime.datetime(2016, 1, 20, 22, 9), |
| 72 | 'Owner': {'ID': 'abc123', 'DisplayName': 'myname'}, |
| 73 | 'Size': 14814 |
| 74 | }], |
| 75 | 'EncodingType': 'url', |
| 76 | 'ResponseMetadata': { |
| 77 | 'RequestId': 'abc123', |
| 78 | 'HTTPStatusCode': 200, |
| 79 | 'HostId': 'abc123' |
| 80 | }, |
| 81 | 'Marker': '' |
| 82 | } |
| 83 | |
| 84 | expected_params = {'Bucket': 'test-bucket'} |
| 85 | |
| 86 | stubber.add_response('list_objects', response, expected_params) |
| 87 | stubber.activate() |
| 88 | |
| 89 | service_response = s3.list_objects(Bucket='test-bucket') |
| 90 | assert service_response == response |
| 91 | |
| 92 | |
| 93 | This class can also be called as a context manager, which will handle |
| 94 | activation / deactivation for you. |
| 95 | |
| 96 | **Example:** |
| 97 | :: |
| 98 | import datetime |
| 99 | import botocore.session |
| 100 | from botocore.stub import Stubber |
| 101 | |
| 102 | |
| 103 | s3 = botocore.session.get_session().create_client('s3') |
no outgoing calls