Skip to content

Commit

Permalink
feat(file): rename BlockingFiles
Browse files Browse the repository at this point in the history
add test case
  • Loading branch information
tu6ge committed Apr 20, 2023
1 parent 423ba4d commit 25bb539
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 29 deletions.
2 changes: 1 addition & 1 deletion examples/delete_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use aliyun_oss_client::client::Client;

extern crate dotenv;

use aliyun_oss_client::file::BlockingFile;
use aliyun_oss_client::file::BlockingFiles;
use dotenv::dotenv;

fn main() {
Expand Down
4 changes: 2 additions & 2 deletions examples/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use aliyun_oss_client::{
config::get_url_resource,
file::{File, FileError, GetStd},
types::CanonicalizedResource,
BucketName, Client, EndPoint, KeyId, KeySecret,
BucketName, Client, EndPoint, Error as OssError, KeyId, KeySecret,
};
use reqwest::Url;

Expand Down Expand Up @@ -44,7 +44,7 @@ impl File<Client> for MyObject {
}

#[tokio::main]
async fn main() -> Result<(), FileError> {
async fn main() -> Result<(), OssError> {
for entry in fs::read_dir("examples")? {
let path = entry?.path();
let path = path.as_path();
Expand Down
2 changes: 1 addition & 1 deletion examples/object_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async fn main() {
println!(
"list: {:?}, token: {:?}",
object_list,
object_list.next_continuation_token()
object_list.next_continuation_token_str()
);

let stream = object_list.into_stream();
Expand Down
31 changes: 10 additions & 21 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,15 +702,15 @@ impl<Item: RefineObject<E> + Send + Sync, E: Error + Send + Sync> AlignBuilder
}

#[cfg(feature = "blocking")]
pub use blocking::Files as BlockingFile;
pub use blocking::Files as BlockingFiles;

use self::error_impl::FileErrorKind;

/// 同步的文件模块
#[cfg(feature = "blocking")]
pub mod blocking {

use super::{error_impl::FileErrorKind, FileError, GetStdWithPath};
use super::{error_impl::FileErrorKind, header_from_content_length, FileError, GetStdWithPath};
use crate::{
blocking::builder::RequestBuilder,
bucket::Bucket,
Expand Down Expand Up @@ -766,7 +766,7 @@ pub mod blocking {
/// # use dotenv::dotenv;
/// # dotenv().ok();
/// # let client = aliyun_oss_client::ClientRc::from_env().unwrap();
/// use crate::aliyun_oss_client::file::BlockingFile;
/// use crate::aliyun_oss_client::file::BlockingFiles;
///
/// fn sig_match(buf: &[u8]) -> bool {
/// return buf.len() >= 3 && buf[0] == 0x64 && buf[1] == 0x57 && buf[2] == 0x35;
Expand Down Expand Up @@ -823,12 +823,7 @@ pub mod blocking {

let content_length = content.len().to_string();
let headers = vec![
(
CONTENT_LENGTH,
HeaderValue::from_str(&content_length).map_err(|e| FileError {
kind: FileErrorKind::InvalidContentLength(e),
})?,
),
(CONTENT_LENGTH, header_from_content_length(&content_length)?),
(
CONTENT_TYPE,
content_type.parse().map_err(|e| FileError {
Expand All @@ -838,8 +833,7 @@ pub mod blocking {
];

let response = self
.builder_with_header(Method::PUT, url, canonicalized, headers)
.map_err(FileError::from)?
.builder_with_header(Method::PUT, url, canonicalized, headers)?
.body(content);

response.send_adjust_error().map_err(FileError::from)
Expand All @@ -860,12 +854,9 @@ pub mod blocking {
vec![("Range".parse().unwrap(), range.into().into())];

Ok(self
.builder_with_header(Method::GET, url, canonicalized, headers)
.map_err(FileError::from)?
.send_adjust_error()
.map_err(FileError::from)?
.text()
.map_err(FileError::from)?
.builder_with_header(Method::GET, url, canonicalized, headers)?
.send_adjust_error()?
.text()?
.into_bytes())
}

Expand All @@ -875,10 +866,8 @@ pub mod blocking {
kind: FileErrorKind::NotFoundCanonicalizedResource,
})?;

self.builder(Method::DELETE, url, canonicalized)
.map_err(FileError::from)?
.send_adjust_error()
.map_err(FileError::from)?;
self.builder(Method::DELETE, url, canonicalized)?
.send_adjust_error()?;

Ok(())
}
Expand Down
45 changes: 45 additions & 0 deletions src/file/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,48 @@ async fn test_delete_object_error() {
FileErrorKind::NotFoundCanonicalizedResource
));
}

#[cfg(feature = "blocking")]
mod blocking_files_trait {
use crate::{
file::{error_impl::FileErrorKind, BlockingFiles},
ClientRc,
};

#[test]
fn test_put_content_base_error() {
let client = ClientRc::test_init();
let err = client
.put_content_base("aa".into(), "image/jpg", "aaa/")
.unwrap_err();
assert!(matches!(
err.kind,
FileErrorKind::NotFoundCanonicalizedResource
));

let err = client
.put_content_base("aa".into(), "\n", "aaa")
.unwrap_err();
assert!(matches!(err.kind, FileErrorKind::InvalidContentType(_)));
}

#[test]
fn test_get_object_error() {
let client = ClientRc::test_init();
let err = client.get_object("aaa/", ..).unwrap_err();
assert!(matches!(
err.kind,
FileErrorKind::NotFoundCanonicalizedResource
));
}

#[test]
fn test_delete_object_error() {
let client = ClientRc::test_init();
let err = client.delete_object("aaa/").unwrap_err();
assert!(matches!(
err.kind,
FileErrorKind::NotFoundCanonicalizedResource
));
}
}
2 changes: 1 addition & 1 deletion src/tests/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,7 @@ async fn test_delete_object() {
#[test]
fn test_blocking_delete_object() {
use crate::client::ClientRc;
use crate::{blocking::builder::Middleware, file::BlockingFile};
use crate::{blocking::builder::Middleware, file::BlockingFiles};
use reqwest::blocking::{Request, Response};
use std::rc::Rc;

Expand Down
5 changes: 2 additions & 3 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,9 @@ mod test_async {
// }
}

#[cfg(all(feature = "blocking", not(tarpaulin)))]
#[cfg(feature = "blocking")]
mod test_blocking {

#[cfg(feature = "put_file")]
use aliyun_oss_client::file::BlockingFile;
use aliyun_oss_client::ClientRc;
use assert_matches::assert_matches;
use dotenv::dotenv;
Expand Down Expand Up @@ -197,6 +195,7 @@ mod test_blocking {
#[cfg(feature = "put_file")]
#[test]
fn test_put_and_delete_file() {
use aliyun_oss_client::file::BlockingFiles;
dotenv().ok();

let client = ClientRc::from_env().unwrap();
Expand Down

0 comments on commit 25bb539

Please sign in to comment.