You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by su...@apache.org on 2023/04/26 07:04:12 UTC

[incubator-opendal] 01/01: test for write_with_cache_control

This is an automated email from the ASF dual-hosted git repository.

suyanhanx pushed a commit to branch test-for-override-cache-control
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git

commit aa6b6b649a80c399c7258f1e906b13b8e4187f30
Author: suyanhanx <su...@gmail.com>
AuthorDate: Wed Apr 26 15:03:48 2023 +0800

    test for write_with_cache_control
    
    Signed-off-by: suyanhanx <su...@gmail.com>
---
 core/src/services/s3/backend.rs |  1 +
 core/tests/behavior/write.rs    | 47 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 47 insertions(+), 1 deletion(-)

diff --git a/core/src/services/s3/backend.rs b/core/src/services/s3/backend.rs
index e0f2af72..44d8b5c6 100644
--- a/core/src/services/s3/backend.rs
+++ b/core/src/services/s3/backend.rs
@@ -924,6 +924,7 @@ impl Accessor for S3Backend {
                 read_with_override_content_disposition: true,
 
                 write: true,
+                write_with_cache_control: true,
                 write_without_content_length: true,
 
                 list: true,
diff --git a/core/tests/behavior/write.rs b/core/tests/behavior/write.rs
index 1b5a468c..41f82ff0 100644
--- a/core/tests/behavior/write.rs
+++ b/core/tests/behavior/write.rs
@@ -21,7 +21,7 @@ use futures::AsyncSeekExt;
 use futures::StreamExt;
 use log::debug;
 use log::warn;
-use opendal::ops::{OpRead, OpStat};
+use opendal::ops::{OpRead, OpStat, OpWrite};
 use opendal::EntryMode;
 use opendal::ErrorKind;
 use opendal::Operator;
@@ -75,6 +75,7 @@ macro_rules! behavior_write_tests {
                 test_write,
                 test_write_with_dir_path,
                 test_write_with_special_chars,
+                test_write_with_cache_control,
                 test_stat,
                 test_stat_dir,
                 test_stat_with_special_chars,
@@ -179,6 +180,50 @@ pub async fn test_write_with_special_chars(op: Operator) -> Result<()> {
     Ok(())
 }
 
+// Write a single file with cache control should succeed.
+pub async fn test_write_with_cache_control(op: Operator) -> Result<()> {
+    if !(op.info().can_presign() && op.info().capability().write_with_cache_control) {
+        return Ok(());
+    }
+
+    let path = uuid::Uuid::new_v4().to_string();
+    let (content, _) = gen_bytes();
+
+    let target_cache_control = "max-age=3600";
+
+    let mut op_write = OpWrite::default();
+    op_write = op_write.with_cache_control(target_cache_control);
+
+    op.write_with(&path, op_write, content.clone()).await?;
+
+    let signed_req = op
+        .presign_read(&path, Duration::from_secs(60))
+        .await
+        .expect("presign must succeed");
+
+    let client = reqwest::Client::new();
+    let mut req = client.request(
+        signed_req.method().clone(),
+        Url::from_str(&signed_req.uri().to_string()).expect("must be valid url"),
+    );
+    for (k, v) in signed_req.header() {
+        req = req.header(k, v);
+    }
+
+    let resp = req.send().await.expect("send must succeed");
+
+    assert_eq!(resp.status(), http::StatusCode::OK);
+    assert_eq!(
+        resp.headers().get(http::header::CACHE_CONTROL).unwrap(),
+        target_cache_control
+    );
+    assert_eq!(resp.bytes().await?, content);
+
+    op.delete(&path).await.expect("delete must succeed");
+
+    Ok(())
+}
+
 /// Stat existing file should return metadata
 pub async fn test_stat(op: Operator) -> Result<()> {
     let path = uuid::Uuid::new_v4().to_string();