|
3 | 3 | * SPDX-License-Identifier: Apache-2.0 |
4 | 4 | */ |
5 | 5 |
|
| 6 | +//! This crate allows mocking of smithy clients. |
| 7 | +
|
6 | 8 | /* Automatically managed default lints */ |
7 | 9 | #![cfg_attr(docsrs, feature(doc_auto_cfg))] |
8 | 10 | /* End of automatically managed default lints */ |
@@ -94,23 +96,51 @@ macro_rules! mock { |
94 | 96 | /// .then_error(||GetObjectError::NoSuchKey(NoSuchKey::builder().build())); |
95 | 97 | /// let client = mock_client!(aws_sdk_s3, RuleMode::Sequential, &[&get_object_error_path, &get_object_happy_path]); |
96 | 98 | /// ``` |
| 99 | +/// |
| 100 | +/// **Create a client but customize a specific setting**: |
| 101 | +/// ```rust,ignore |
| 102 | +/// use aws_sdk_s3::operation::get_object::GetObjectOutput; |
| 103 | +/// use aws_sdk_s3::Client; |
| 104 | +/// use aws_smithy_types::byte_stream::ByteStream; |
| 105 | +/// use aws_smithy_mocks_experimental::{mock_client, mock, RuleMode}; |
| 106 | +/// let get_object_happy_path = mock!(Client::get_object) |
| 107 | +/// .match_requests(|req|req.bucket() == Some("test-bucket") && req.key() == Some("test-key")) |
| 108 | +/// .then_output(||GetObjectOutput::builder().body(ByteStream::from_static(b"12345-abcde")).build()); |
| 109 | +/// let client = mock_client!( |
| 110 | +/// aws_sdk_s3, |
| 111 | +/// RuleMode::Sequential, |
| 112 | +/// &[&get_object_happy_path], |
| 113 | +/// // Perhaps you need to force path style |
| 114 | +/// |client_builder|client_builder.force_path_style(true) |
| 115 | +/// ); |
| 116 | +/// ``` |
| 117 | +/// |
97 | 118 | #[macro_export] |
98 | 119 | macro_rules! mock_client { |
99 | 120 | ($aws_crate: ident, $rules: expr) => { |
100 | 121 | mock_client!($aws_crate, $crate::RuleMode::Sequential, $rules) |
101 | 122 | }; |
102 | 123 | ($aws_crate: ident, $rule_mode: expr, $rules: expr) => {{ |
| 124 | + mock_client!($aws_crate, $rule_mode, $rules, |conf| conf) |
| 125 | + }}; |
| 126 | + ($aws_crate: ident, $rule_mode: expr, $rules: expr, $additional_configuration: expr) => {{ |
103 | 127 | let mut mock_response_interceptor = |
104 | 128 | $crate::MockResponseInterceptor::new().rule_mode($rule_mode); |
105 | 129 | for rule in $rules { |
106 | 130 | mock_response_interceptor = mock_response_interceptor.with_rule(rule) |
107 | 131 | } |
| 132 | + // allow callers to avoid explicitly specifying the type |
| 133 | + fn coerce<T: Fn($aws_crate::config::Builder) -> $aws_crate::config::Builder>(f: T) -> T { |
| 134 | + f |
| 135 | + } |
108 | 136 | $aws_crate::client::Client::from_conf( |
109 | | - $aws_crate::config::Config::builder() |
110 | | - .with_test_defaults() |
111 | | - .region($aws_crate::config::Region::from_static("us-east-1")) |
112 | | - .interceptor(mock_response_interceptor) |
113 | | - .build(), |
| 137 | + coerce($additional_configuration)( |
| 138 | + $aws_crate::config::Config::builder() |
| 139 | + .with_test_defaults() |
| 140 | + .region($aws_crate::config::Region::from_static("us-east-1")) |
| 141 | + .interceptor(mock_response_interceptor), |
| 142 | + ) |
| 143 | + .build(), |
114 | 144 | ) |
115 | 145 | }}; |
116 | 146 | } |
|
0 commit comments