mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-09-22 21:30:44 +02:00
[upload - http-extra] fix: download content to file when unsuccessful response (and test) #1750 (#1783)
* fix and test * Update Cargo.toml Co-authored-by: Fabian-Lars <fabianlars@fabianlars.de> * Update Cargo.toml Co-authored-by: Fabian-Lars <fabianlars@fabianlars.de> * add .change log * clippy fixes * print test error * fix tests --------- Co-authored-by: Fabian-Lars <fabianlars@fabianlars.de> Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
This commit is contained in:
co-authored by
Fabian-Lars
Lucas Nogueira
parent
7a37355e17
commit
1d9741b52b
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"upload": 'patch:bug'
|
||||||
|
---
|
||||||
|
|
||||||
|
fix download content to file when unsuccessful response
|
||||||
Generated
+54
-1
@@ -337,6 +337,16 @@ dependencies = [
|
|||||||
"zbus",
|
"zbus",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "assert-json-diff"
|
||||||
|
version = "2.0.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "47e4f2b81832e72834d7518d8487a0396a28cc408186a2e8854c0f98011faf12"
|
||||||
|
dependencies = [
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "async-broadcast"
|
name = "async-broadcast"
|
||||||
version = "0.7.1"
|
version = "0.7.1"
|
||||||
@@ -1088,6 +1098,16 @@ dependencies = [
|
|||||||
"winapi",
|
"winapi",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "colored"
|
||||||
|
version = "2.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8"
|
||||||
|
dependencies = [
|
||||||
|
"lazy_static",
|
||||||
|
"windows-sys 0.48.0",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "combine"
|
name = "combine"
|
||||||
version = "4.6.7"
|
version = "4.6.7"
|
||||||
@@ -1961,7 +1981,7 @@ version = "0.6.2"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d9f0c14694cbd524c8720dd69b0e3179344f04ebb5f90f2e4a440c6ea3b2f1ee"
|
checksum = "d9f0c14694cbd524c8720dd69b0e3179344f04ebb5f90f2e4a440c6ea3b2f1ee"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"colored",
|
"colored 1.9.4",
|
||||||
"log",
|
"log",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -2808,6 +2828,7 @@ dependencies = [
|
|||||||
"http",
|
"http",
|
||||||
"http-body",
|
"http-body",
|
||||||
"httparse",
|
"httparse",
|
||||||
|
"httpdate",
|
||||||
"itoa 1.0.11",
|
"itoa 1.0.11",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"smallvec",
|
"smallvec",
|
||||||
@@ -3623,6 +3644,30 @@ dependencies = [
|
|||||||
"windows-sys 0.52.0",
|
"windows-sys 0.52.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "mockito"
|
||||||
|
version = "1.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "09b34bd91b9e5c5b06338d392463e1318d683cf82ec3d3af4014609be6e2108d"
|
||||||
|
dependencies = [
|
||||||
|
"assert-json-diff",
|
||||||
|
"bytes",
|
||||||
|
"colored 2.1.0",
|
||||||
|
"futures-util",
|
||||||
|
"http",
|
||||||
|
"http-body",
|
||||||
|
"http-body-util",
|
||||||
|
"hyper",
|
||||||
|
"hyper-util",
|
||||||
|
"log",
|
||||||
|
"rand 0.8.5",
|
||||||
|
"regex",
|
||||||
|
"serde_json",
|
||||||
|
"serde_urlencoded",
|
||||||
|
"similar",
|
||||||
|
"tokio",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "muda"
|
name = "muda"
|
||||||
version = "0.14.1"
|
version = "0.14.1"
|
||||||
@@ -5614,6 +5659,12 @@ version = "0.1.4"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f27f6278552951f1f2b8cf9da965d10969b2efdea95a6ec47987ab46edfe263a"
|
checksum = "f27f6278552951f1f2b8cf9da965d10969b2efdea95a6ec47987ab46edfe263a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "similar"
|
||||||
|
version = "2.6.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1de1d4f81173b03af4c0cbed3c898f6bff5b870e4a7f5d6f4057d62a7a4b686e"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "single-instance-example"
|
name = "single-instance-example"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
@@ -6811,6 +6862,7 @@ version = "2.0.0-rc.1"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"futures-util",
|
"futures-util",
|
||||||
"log",
|
"log",
|
||||||
|
"mockito",
|
||||||
"read-progress-stream",
|
"read-progress-stream",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"serde",
|
"serde",
|
||||||
@@ -7113,6 +7165,7 @@ dependencies = [
|
|||||||
"bytes",
|
"bytes",
|
||||||
"libc",
|
"libc",
|
||||||
"mio 1.0.2",
|
"mio 1.0.2",
|
||||||
|
"parking_lot",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"signal-hook-registry",
|
"signal-hook-registry",
|
||||||
"socket2",
|
"socket2",
|
||||||
|
|||||||
@@ -35,3 +35,7 @@ read-progress-stream = "1.0.0"
|
|||||||
native-tls = ["reqwest/native-tls"]
|
native-tls = ["reqwest/native-tls"]
|
||||||
native-tls-vendored = ["reqwest/native-tls-vendored"]
|
native-tls-vendored = ["reqwest/native-tls-vendored"]
|
||||||
rustls-tls = ["reqwest/rustls-tls"]
|
rustls-tls = ["reqwest/rustls-tls"]
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
mockito = "1.5.0"
|
||||||
|
tokio = { version = "*", features = ["macros"] }
|
||||||
|
|||||||
@@ -70,13 +70,19 @@ async fn download(
|
|||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
|
|
||||||
let mut request = client.get(url);
|
let mut request = client.get(url);
|
||||||
// Loop trought the headers keys and values
|
// Loop through the headers keys and values
|
||||||
// and add them to the request object.
|
// and add them to the request object.
|
||||||
for (key, value) in headers {
|
for (key, value) in headers {
|
||||||
request = request.header(&key, value);
|
request = request.header(&key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
let response = request.send().await?;
|
let response = request.send().await?;
|
||||||
|
if !response.status().is_success() {
|
||||||
|
return Err(Error::HttpErrorCode(
|
||||||
|
response.status().as_u16(),
|
||||||
|
response.text().await.unwrap_or_default(),
|
||||||
|
));
|
||||||
|
}
|
||||||
let total = response.content_length().unwrap_or(0);
|
let total = response.content_length().unwrap_or(0);
|
||||||
|
|
||||||
let mut file = BufWriter::new(File::create(file_path).await?);
|
let mut file = BufWriter::new(File::create(file_path).await?);
|
||||||
@@ -112,7 +118,7 @@ async fn upload(
|
|||||||
.header(reqwest::header::CONTENT_LENGTH, file_len)
|
.header(reqwest::header::CONTENT_LENGTH, file_len)
|
||||||
.body(file_to_body(on_progress, file));
|
.body(file_to_body(on_progress, file));
|
||||||
|
|
||||||
// Loop trought the headers keys and values
|
// Loop through the headers keys and values
|
||||||
// and add them to the request object.
|
// and add them to the request object.
|
||||||
for (key, value) in headers {
|
for (key, value) in headers {
|
||||||
request = request.header(&key, value);
|
request = request.header(&key, value);
|
||||||
@@ -145,3 +151,64 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
|
|||||||
.invoke_handler(tauri::generate_handler![download, upload])
|
.invoke_handler(tauri::generate_handler![download, upload])
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use mockito::{self, Mock, Server, ServerGuard};
|
||||||
|
use tauri::ipc::InvokeResponseBody;
|
||||||
|
struct MockedServer {
|
||||||
|
_server: ServerGuard,
|
||||||
|
url: String,
|
||||||
|
mocked_endpoint: Mock,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn should_error_if_status_not_success() {
|
||||||
|
let mocked_server = spawn_server_mocked(400).await;
|
||||||
|
let result = download_file(&mocked_server.url).await;
|
||||||
|
mocked_server.mocked_endpoint.assert();
|
||||||
|
assert!(result.is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn should_download_file_successfully() {
|
||||||
|
let mocked_server = spawn_server_mocked(200).await;
|
||||||
|
let result = download_file(&mocked_server.url).await;
|
||||||
|
mocked_server.mocked_endpoint.assert();
|
||||||
|
assert!(
|
||||||
|
result.is_ok(),
|
||||||
|
"failed to download file: {}",
|
||||||
|
result.unwrap_err()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn download_file(url: &str) -> Result<()> {
|
||||||
|
let file_path = concat!(env!("CARGO_MANIFEST_DIR"), "/test/test.txt");
|
||||||
|
let headers = HashMap::new();
|
||||||
|
let sender: Channel<ProgressPayload> =
|
||||||
|
Channel::new(|msg: InvokeResponseBody| -> tauri::Result<()> {
|
||||||
|
let _ = msg;
|
||||||
|
Ok(())
|
||||||
|
});
|
||||||
|
download(url, file_path, headers, sender).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn spawn_server_mocked(return_status: usize) -> MockedServer {
|
||||||
|
let mut _server = Server::new_async().await;
|
||||||
|
let path = "/mock_test";
|
||||||
|
let mock = _server
|
||||||
|
.mock("GET", path)
|
||||||
|
.with_status(return_status)
|
||||||
|
.with_body("mocked response body")
|
||||||
|
.create_async()
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let url = _server.url() + path;
|
||||||
|
MockedServer {
|
||||||
|
_server,
|
||||||
|
url,
|
||||||
|
mocked_endpoint: mock,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
mocked response body
|
||||||
Reference in New Issue
Block a user