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

Download history 4062/week @ 2024-10-24 5887/week @ 2024-10-31 6788/week @ 2024-11-07 6475/week @ 2024-11-14 6689/week @ 2024-11-21 3418/week @ 2024-11-28 4118/week @ 2024-12-05 6111/week @ 2024-12-12 2432/week @ 2024-12-19 472/week @ 2024-12-26 3209/week @ 2025-01-02 4519/week @ 2025-01-09 4981/week @ 2025-01-16 3687/week @ 2025-01-23 3886/week @ 2025-01-30 5149/week @ 2025-02-06

18,415 downloads per month
Used in 2 crates

Apache-2.0

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