Macro aws_smithy_mocks_experimental::mock_client

source ·
macro_rules! mock_client {
    ($aws_crate: ident, $rules: expr) => { ... };
    ($aws_crate: ident, $rule_mode: expr, $rules: expr) => { ... };
    ($aws_crate: ident, $rule_mode: expr, $rules: expr, $additional_configuration: expr) => { ... };
}
Expand description

mock_client! macro produces a Client configured with a number of Rules and appropriate test default configuration.

§Examples

Create a client that uses a mock failure and then a success:

use aws_sdk_s3::operation::get_object::{GetObjectOutput, GetObjectError};
use aws_sdk_s3::types::error::NoSuchKey;
use aws_sdk_s3::Client;
use aws_smithy_types::byte_stream::ByteStream;
use aws_smithy_mocks_experimental::{mock_client, mock, RuleMode};
let get_object_happy_path = mock!(Client::get_object)
  .match_requests(|req|req.bucket() == Some("test-bucket") && req.key() == Some("test-key"))
  .then_output(||GetObjectOutput::builder().body(ByteStream::from_static(b"12345-abcde")).build());
let get_object_error_path = mock!(Client::get_object)
  .then_error(||GetObjectError::NoSuchKey(NoSuchKey::builder().build()));
let client = mock_client!(aws_sdk_s3, RuleMode::Sequential, &[&get_object_error_path, &get_object_happy_path]);

Create a client but customize a specific setting:

use aws_sdk_s3::operation::get_object::GetObjectOutput;
use aws_sdk_s3::Client;
use aws_smithy_types::byte_stream::ByteStream;
use aws_smithy_mocks_experimental::{mock_client, mock, RuleMode};
let get_object_happy_path = mock!(Client::get_object)
  .match_requests(|req|req.bucket() == Some("test-bucket") && req.key() == Some("test-key"))
  .then_output(||GetObjectOutput::builder().body(ByteStream::from_static(b"12345-abcde")).build());
let client = mock_client!(
    aws_sdk_s3,
    RuleMode::Sequential,
    &[&get_object_happy_path],
    // Perhaps you need to force path style
    |client_builder|client_builder.force_path_style(true)
);