Skip to content

Commit

Permalink
Add modeled error
Browse files Browse the repository at this point in the history
  • Loading branch information
rcoh committed Dec 12, 2023
1 parent af034c0 commit fe8e389
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
10 changes: 10 additions & 0 deletions rust-runtime/aws-smithy-mocks-experimental/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ where
MockOutput::ModeledResponse(Arc::new(move || Ok(Output::erase(output())))),
)
}

/// If a rule matches, then return a specific error
pub fn then_error(self, output: impl Fn() -> E + Send + Sync + 'static) -> Rule {
Rule::new(
self.input_filter,
MockOutput::ModeledResponse(Arc::new(move || {
Err(OrchestratorError::operation(Error::erase(output())))
})),
)
}
}

#[derive(Clone)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@

use aws_sdk_s3::config::Region;
use aws_sdk_s3::operation::get_object::{GetObjectError, GetObjectOutput};
use aws_sdk_s3::operation::list_buckets::ListBucketsError;
use aws_sdk_s3::{Client, Config};
use aws_smithy_types::body::SdkBody;
use aws_smithy_types::byte_stream::ByteStream;
use aws_smithy_types::error::metadata::ProvideErrorMetadata;
use aws_smithy_types::error::ErrorMetadata;

use aws_smithy_mocks_experimental::{mock, MockResponseInterceptor};

Expand Down Expand Up @@ -44,9 +47,14 @@ async fn create_mock_s3_get_object() {
.build()
});

let modeled_error = mock!(Client::list_buckets).then_error(|| {
ListBucketsError::generic(ErrorMetadata::builder().code("InvalidAccessKey").build())
});

let get_object_mocks = MockResponseInterceptor::new()
.with_rule(&s3_404)
.with_rule(&s3_real_object);
.with_rule(&s3_real_object)
.with_rule(&modeled_error);

let s3 = aws_sdk_s3::Client::from_conf(
Config::builder()
Expand Down Expand Up @@ -83,4 +91,7 @@ async fn create_mock_s3_get_object() {
.to_vec();
assert_eq!(data, b"test-test-test");
assert_eq!(s3_real_object.num_calls(), 1);

let err = s3.list_buckets().send().await.expect_err("bad access key");
assert_eq!(err.code(), Some("InvalidAccessKey"));
}

0 comments on commit fe8e389

Please sign in to comment.