Skip to content

Commit

Permalink
test: add unitest for oss
Browse files Browse the repository at this point in the history
Signed-off-by: lzzzt <[email protected]>
  • Loading branch information
Lzzzzzt committed Aug 5, 2024
1 parent 6e760e8 commit 5e25d02
Show file tree
Hide file tree
Showing 8 changed files with 873 additions and 748 deletions.
807 changes: 107 additions & 700 deletions Cargo.lock

Large diffs are not rendered by default.

17 changes: 11 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ members = [
"dragonfly-client-init",
"dragonfly-client-storage",
"dragonfly-client-util",
"dragonfly-client-backend/examples/plugin"
"dragonfly-client-backend/examples/plugin",
]

[workspace.package]
Expand All @@ -31,7 +31,12 @@ dragonfly-client-util = { path = "dragonfly-client-util", version = "0.1.94" }
dragonfly-client-init = { path = "dragonfly-client-init", version = "0.1.94" }
thiserror = "1.0"
dragonfly-api = "2.0.141"
reqwest = { version = "0.12.4", features = ["stream", "native-tls", "default-tls", "rustls-tls"] }
reqwest = { version = "0.12.4", features = [
"stream",
"native-tls",
"default-tls",
"rustls-tls",
] }
rcgen = { version = "0.12.1", features = ["x509-parser"] }
hyper = { version = "1.4", features = ["full"] }
hyper-util = { version = "0.1.6", features = [
Expand Down Expand Up @@ -70,10 +75,10 @@ humantime = "2.1.0"
prost-wkt-types = "0.4"
chrono = { version = "0.4.35", features = ["serde", "clock"] }
openssl = { version = "0.10", features = ["vendored"] }
opendal = { version = "0.47.3", features = [
"services-s3",
opendal = { version = "0.48.0", features = [
"services-s3",
"services-azblob",
"services-gcs",
"services-gcs",
"services-oss",
"services-obs",
"services-cos",
Expand All @@ -83,7 +88,7 @@ anyhow = "1.0.86"
toml_edit = "0.22.14"
toml = "0.8.16"
base16ct = { version = "0.2", features = ["alloc"] }
bytesize = {version = "1.2.0", features = ["serde"]}
bytesize = { version = "1.2.0", features = ["serde"] }
bytesize-serde = "0.2.1"
percent-encoding = "2.3.1"

Expand Down
3 changes: 2 additions & 1 deletion dragonfly-client-backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ futures = "0.3.28"
libloading = "0.8.5"

[dev-dependencies]
httpmock = "0.7.0"
wiremock = "0.6.1"
serial_test = "3.1.0"
63 changes: 38 additions & 25 deletions dragonfly-client-backend/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,24 +163,30 @@ impl Default for HTTP {
#[cfg(test)]
mod tests {
use crate::{http, Backend, GetRequest, HeadRequest};
use httpmock::{Method, MockServer};
use reqwest::{header::HeaderMap, StatusCode};
use wiremock::{
matchers::{method, path},
Mock, ResponseTemplate,
};

#[tokio::test]
async fn should_get_head_response() {
let server = MockServer::start();
server.mock(|when, then| {
when.method(Method::GET).path("/head");
then.status(200)
.header("content-type", "text/html; charset=UTF-8")
.body("");
});
let server = wiremock::MockServer::start().await;

Mock::given(method("GET"))
.and(path("/head"))
.respond_with(
ResponseTemplate::new(200)
.insert_header("Content-Type", "text/html; charset=UTF-8"),
)
.mount(&server)
.await;

let http_backend = http::HTTP::new();
let resp = http_backend
.head(HeadRequest {
task_id: "test".to_string(),
url: server.url("/head"),
url: format!("{}/head", server.uri()),
http_header: Some(HeaderMap::new()),
timeout: std::time::Duration::from_secs(5),
client_certs: None,
Expand All @@ -194,19 +200,22 @@ mod tests {

#[tokio::test]
async fn should_return_error_response_when_head_notexists() {
let server = MockServer::start();
server.mock(|when, then| {
when.method(Method::GET).path("/head");
then.status(200)
.header("content-type", "text/html; charset=UTF-8")
.body("");
});
let server = wiremock::MockServer::start().await;

Mock::given(method("GET"))
.and(path("/head"))
.respond_with(
ResponseTemplate::new(200)
.insert_header("Content-Type", "text/html; charset=UTF-8"),
)
.mount(&server)
.await;

let http_backend = http::HTTP::new();
let resp = http_backend
.head(HeadRequest {
task_id: "test".to_string(),
url: server.url("/head"),
url: format!("{}/head", server.uri()),
http_header: None,
timeout: std::time::Duration::from_secs(5),
client_certs: None,
Expand All @@ -219,19 +228,23 @@ mod tests {

#[tokio::test]
async fn should_get_response() {
let server = MockServer::start();
server.mock(|when, then| {
when.method(Method::GET).path("/get");
then.status(200)
.header("content-type", "text/html; charset=UTF-8")
.body("OK");
});
let server = wiremock::MockServer::start().await;

Mock::given(method("GET"))
.and(path("/get"))
.respond_with(
ResponseTemplate::new(200)
.insert_header("Content-Type", "text/html; charset=UTF-8")
.set_body_string("OK"),
)
.mount(&server)
.await;

let http_backend = http::HTTP::new();
let mut resp = http_backend
.get(GetRequest {
piece_id: "test".to_string(),
url: server.url("/get"),
url: format!("{}/get", server.uri()),
range: None,
http_header: Some(HeaderMap::new()),
timeout: std::time::Duration::from_secs(5),
Expand Down
1 change: 1 addition & 0 deletions dragonfly-client-backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub struct HeadRequest {
}

// HeadResponse is the head response for backend.
#[derive(Debug)]
pub struct HeadResponse {
// success is the success of the response.
pub success: bool,
Expand Down
Loading

0 comments on commit 5e25d02

Please sign in to comment.