1 1 | /*
|
2 2 | * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
3 3 | * SPDX-License-Identifier: Apache-2.0
|
4 4 | */
|
5 5 |
|
6 - | use aws_sdk_s3::{config::Region, error::DisplayErrorContext, Client, Config};
|
6 + | use aws_sdk_s3::{config::Region, config::retry::RetryConfig, error::DisplayErrorContext, Client, Config};
|
7 7 | use aws_smithy_http_client::test_util::dvr::ReplayingClient;
|
8 8 |
|
9 9 | #[tokio::test]
|
10 10 | async fn test_content_length_enforcement_is_not_applied_to_head_request() {
|
11 11 | let http_client =
|
12 12 | ReplayingClient::from_file("tests/data/content-length-enforcement/head-object.json")
|
13 13 | .expect("recorded HTTP communication exists");
|
14 14 | let config = Config::builder()
|
15 15 | .with_test_defaults()
|
16 16 | .http_client(http_client.clone())
|
17 17 | .region(Region::new("us-east-1"))
|
18 + | .retry_config(RetryConfig::disabled()) // Disable retries for replay test
|
18 19 | .build();
|
19 20 | let client = Client::from_conf(config);
|
20 21 | let _resp = client
|
21 22 | .head_object()
|
22 23 | .key("dontcare.json")
|
23 24 | .bucket("dontcare")
|
24 25 | .send()
|
25 26 | .await
|
26 27 | .expect("content length enforcement must not apply to HEAD requests");
|
27 28 |
|
28 29 | // The body returned will be empty, so we pass an empty string for `media_type` to
|
29 30 | // `validate_body_and_headers_except`. That way, it'll do a string equality check on the empty
|
30 31 | // strings.
|
31 32 | http_client.relaxed_validate("").await.unwrap();
|
32 33 | }
|
33 34 |
|
34 35 | #[tokio::test]
|
35 36 | async fn test_content_length_enforcement_get_request_short() {
|
36 37 | let http_client =
|
37 38 | ReplayingClient::from_file("tests/data/content-length-enforcement/get-object-short.json")
|
38 39 | .expect("recorded HTTP communication exists");
|
39 40 | let config = Config::builder()
|
40 41 | .with_test_defaults()
|
41 42 | .http_client(http_client.clone())
|
42 43 | .region(Region::new("us-east-1"))
|
44 + | .retry_config(RetryConfig::disabled()) // Disable retries for replay test
|
43 45 | .build();
|
44 46 | let client = Client::from_conf(config);
|
45 47 | // The file we're fetching is exactly 10,000 bytes long, but we've set the
|
46 48 | // response's content-length to 9,999 bytes. This should trigger the
|
47 49 | // content-length enforcement.
|
48 50 |
|
49 51 | // This will succeed.
|
50 52 | let output = client
|
51 53 | .get_object()
|
52 54 | .key("1000-lines.txt")
|
53 55 | .bucket("dontcare")
|
54 56 | .send()
|
55 57 | .await
|
56 58 | .unwrap();
|
57 59 |
|
58 60 | // This will fail with a content-length mismatch error.
|
59 61 | let content_length_err = output.body.collect().await.unwrap_err();
|
60 62 |
|
61 63 | http_client
|
62 64 | .relaxed_validate("application/text")
|
63 65 | .await
|
64 66 | .unwrap();
|
65 67 | assert_eq!(
|
66 68 | DisplayErrorContext(content_length_err).to_string(),
|
67 69 | "streaming error: Invalid Content-Length: Expected 9999 bytes but 10000 bytes were received (Error { kind: StreamingError(ContentLengthError { expected: 9999, received: 10000 }) })"
|
68 70 | );
|
69 71 | }
|
70 72 |
|
71 73 | #[tokio::test]
|
72 74 | async fn test_content_length_enforcement_get_request_long() {
|
73 75 | let http_client =
|
74 76 | ReplayingClient::from_file("tests/data/content-length-enforcement/get-object-long.json")
|
75 77 | .expect("recorded HTTP communication exists");
|
76 78 | let config = Config::builder()
|
77 79 | .with_test_defaults()
|
78 80 | .http_client(http_client.clone())
|
79 81 | .region(Region::new("us-east-1"))
|
82 + | .retry_config(RetryConfig::disabled()) // Disable retries for replay test
|
80 83 | .build();
|
81 84 | let client = Client::from_conf(config);
|
82 85 | // The file we're fetching is exactly 10,000 bytes long, but we've set the
|
83 86 | // response's content-length to 9,999 bytes. This should trigger the
|
84 87 | // content-length enforcement.
|
85 88 |
|
86 89 | // This will succeed.
|
87 90 | let output = client
|
88 91 | .get_object()
|
89 92 | .key("1000-lines.txt")
|