5 releases
new 0.2.2 | Feb 12, 2025 |
---|---|
0.2.1 | Apr 2, 2024 |
0.2.0 | Mar 25, 2024 |
0.1.1 | Mar 12, 2024 |
0.1.0 | Jan 18, 2024 |
#2216 in Network programming
18,415 downloads per month
Used in 2 crates
710KB
14K
SLoC
aws-smithy-mocks
This package allows testing clients generated by smithy-rs (including all packages of the AWS Rust SDK) by using interceptors to return stub responses. This approach is quite useful for testing both happy-path and simple error scenarios and avoids the need for mocking the entire client or using traits.
As an example, consider this simple usage with S3:
#[tokio::test]
async fn test_s3() {
let s3_real_object = mock!(Client::get_object).then_output(|| {
GetObjectOutput::builder()
.body(ByteStream::from_static(b"test-test-test"))
.build()
});
let s3 = mock_client!(aws_sdk_s3, [&s3_real_object]);
let data = s3
.get_object()
.bucket("test-bucket")
.key("correct-key")
.send()
.await
.expect("success response")
.body
.collect()
.await
.expect("successful read")
.to_vec();
assert_eq!(data, b"test-test-test");
assert_eq!(s3_real_object.num_calls(), 1);
}
You can find more examples in the tests
folder of this crate.
Shortcomings of this approach
This approach is not well suited for testing precise error handling, especially when considering retries or interactions with HTTP responses—This approach hijacks the request response flow entirely and is not a faithful model in these cases.
If you need to test behavior around retries or connection management, you should use HTTP-connection based mocking instead.
This crate is part of the AWS SDK for Rust and the smithy-rs code generator.
Dependencies
~5–11MB
~106K SLoC