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