Skip to content

Commit

Permalink
test(core): test for write_with_content_type (#2140)
Browse files Browse the repository at this point in the history
* add test for write_with_content_type

Signed-off-by: suyanhanx <suyanhanx@gmail.com>

* enable for other cloud services

Signed-off-by: suyanhanx <suyanhanx@gmail.com>

---------

Signed-off-by: suyanhanx <suyanhanx@gmail.com>
  • Loading branch information
suyanhanx authored Apr 27, 2023
1 parent 4f2fc2a commit 3c3463b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
1 change: 1 addition & 0 deletions core/src/services/gcs/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ impl Accessor for GcsBackend {
read_can_next: true,

write: true,
write_with_content_type: true,
write_without_content_length: true,

list: true,
Expand Down
1 change: 1 addition & 0 deletions core/src/services/obs/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ impl Accessor for ObsBackend {
read_can_next: true,

write: true,
write_with_content_type: true,
write_with_cache_control: true,

list: true,
Expand Down
1 change: 1 addition & 0 deletions core/src/services/oss/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ impl Accessor for OssBackend {

write: true,
write_with_cache_control: true,
write_with_content_type: true,
write_without_content_length: true,

list: true,
Expand Down
1 change: 1 addition & 0 deletions core/src/services/s3/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,7 @@ impl Accessor for S3Backend {

write: true,
write_with_cache_control: true,
write_with_content_type: true,
write_without_content_length: true,

list: true,
Expand Down
56 changes: 44 additions & 12 deletions core/tests/behavior/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ use futures::AsyncSeekExt;
use futures::StreamExt;
use log::debug;
use log::warn;
use opendal::ops::{OpRead, OpStat, OpWrite};
use opendal::ops::OpRead;
use opendal::ops::OpStat;
use opendal::ops::OpWrite;
use opendal::EntryMode;
use opendal::ErrorKind;
use opendal::Operator;
Expand Down Expand Up @@ -76,6 +78,7 @@ macro_rules! behavior_write_tests {
test_write_with_dir_path,
test_write_with_special_chars,
test_write_with_cache_control,
test_write_with_content_type,
test_stat,
test_stat_dir,
test_stat_with_special_chars,
Expand Down Expand Up @@ -180,7 +183,7 @@ pub async fn test_write_with_special_chars(op: Operator) -> Result<()> {
Ok(())
}

// Write a single file with cache control should succeed.
/// Write a single file with cache control should succeed.
pub async fn test_write_with_cache_control(op: Operator) -> Result<()> {
if !op.info().capability().write_with_cache_control {
return Ok(());
Expand Down Expand Up @@ -208,6 +211,35 @@ pub async fn test_write_with_cache_control(op: Operator) -> Result<()> {
Ok(())
}

/// Write a single file with content type should succeed.
pub async fn test_write_with_content_type(op: Operator) -> Result<()> {
if !op.info().capability().write_with_content_type {
return Ok(());
}

let path = uuid::Uuid::new_v4().to_string();
let (content, size) = gen_bytes();

let target_content_type = "application/json";

let mut op_write = OpWrite::default();
op_write = op_write.with_content_type(target_content_type);

op.write_with(&path, op_write, content).await?;

let meta = op.stat(&path).await.expect("stat must succeed");
assert_eq!(meta.mode(), EntryMode::FILE);
assert_eq!(
meta.content_type().expect("content type must exist"),
target_content_type
);
assert_eq!(meta.content_length(), size as u64);

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();
Expand Down Expand Up @@ -537,7 +569,7 @@ pub async fn test_read_not_exist(op: Operator) -> Result<()> {
Ok(())
}

// Read with if_match should match, else get a ConditionNotMatch error.
/// Read with if_match should match, else get a ConditionNotMatch error.
pub async fn test_read_with_if_match(op: Operator) -> Result<()> {
if !op.info().capability().read_with_if_match {
return Ok(());
Expand Down Expand Up @@ -722,7 +754,7 @@ pub async fn test_read_with_special_chars(op: Operator) -> Result<()> {
Ok(())
}

// Read file with override_content_disposition should succeed.
/// Read file with override_content_disposition should succeed.
pub async fn test_read_with_override_content_disposition(op: Operator) -> Result<()> {
if !(op
.info()
Expand Down Expand Up @@ -775,7 +807,7 @@ pub async fn test_read_with_override_content_disposition(op: Operator) -> Result
Ok(())
}

// Delete existing file should succeed.
/// Delete existing file should succeed.
pub async fn test_writer_abort(op: Operator) -> Result<()> {
let path = uuid::Uuid::new_v4().to_string();
let (content, _) = gen_bytes();
Expand Down Expand Up @@ -803,7 +835,7 @@ pub async fn test_writer_abort(op: Operator) -> Result<()> {
Ok(())
}

// Delete existing file should succeed.
/// Delete existing file should succeed.
pub async fn test_delete(op: Operator) -> Result<()> {
let path = uuid::Uuid::new_v4().to_string();
let (content, _) = gen_bytes();
Expand All @@ -818,7 +850,7 @@ pub async fn test_delete(op: Operator) -> Result<()> {
Ok(())
}

// Delete empty dir should succeed.
/// Delete empty dir should succeed.
pub async fn test_delete_empty_dir(op: Operator) -> Result<()> {
let path = format!("{}/", uuid::Uuid::new_v4());

Expand All @@ -829,7 +861,7 @@ pub async fn test_delete_empty_dir(op: Operator) -> Result<()> {
Ok(())
}

// Delete file with special chars should succeed.
/// Delete file with special chars should succeed.
pub async fn test_delete_with_special_chars(op: Operator) -> Result<()> {
let path = format!("{} !@#$%^&()_+-=;',.txt", uuid::Uuid::new_v4());
debug!("Generate a random file: {}", &path);
Expand All @@ -845,7 +877,7 @@ pub async fn test_delete_with_special_chars(op: Operator) -> Result<()> {
Ok(())
}

// Delete not existing file should also succeed.
/// Delete not existing file should also succeed.
pub async fn test_delete_not_existing(op: Operator) -> Result<()> {
let path = uuid::Uuid::new_v4().to_string();

Expand All @@ -854,7 +886,7 @@ pub async fn test_delete_not_existing(op: Operator) -> Result<()> {
Ok(())
}

// Delete via stream.
/// Delete via stream.
pub async fn test_delete_stream(op: Operator) -> Result<()> {
let dir = uuid::Uuid::new_v4().to_string();
op.create_dir(&format!("{dir}/"))
Expand All @@ -881,7 +913,7 @@ pub async fn test_delete_stream(op: Operator) -> Result<()> {
Ok(())
}

// Append data into writer
/// Append data into writer
pub async fn test_writer_write(op: Operator) -> Result<()> {
let path = uuid::Uuid::new_v4().to_string();
let size = 5 * 1024 * 1024; // write file with 5 MiB
Expand Down Expand Up @@ -920,7 +952,7 @@ pub async fn test_writer_write(op: Operator) -> Result<()> {
Ok(())
}

// copy data from reader to writer
/// Copy data from reader to writer
pub async fn test_writer_futures_copy(op: Operator) -> Result<()> {
let path = uuid::Uuid::new_v4().to_string();
let (content, size): (Vec<u8>, usize) =
Expand Down

0 comments on commit 3c3463b

Please sign in to comment.