mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-05-13 12:44:45 +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:
@@ -35,3 +35,7 @@ read-progress-stream = "1.0.0"
|
||||
native-tls = ["reqwest/native-tls"]
|
||||
native-tls-vendored = ["reqwest/native-tls-vendored"]
|
||||
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 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.
|
||||
for (key, value) in headers {
|
||||
request = request.header(&key, value);
|
||||
}
|
||||
|
||||
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 mut file = BufWriter::new(File::create(file_path).await?);
|
||||
@@ -112,7 +118,7 @@ async fn upload(
|
||||
.header(reqwest::header::CONTENT_LENGTH, file_len)
|
||||
.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.
|
||||
for (key, value) in headers {
|
||||
request = request.header(&key, value);
|
||||
@@ -145,3 +151,64 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
|
||||
.invoke_handler(tauri::generate_handler![download, upload])
|
||||
.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