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