Skip to content

Commit 2326d94

Browse files
fxsalazarFabianLarslucasfernog
authored andcommitted
[upload - http-extra] fix: download content to file when unsuccessful response (and test) tauri-apps#1750 (tauri-apps#1783)
* fix and test * Update Cargo.toml Co-authored-by: Fabian-Lars <[email protected]> * Update Cargo.toml Co-authored-by: Fabian-Lars <[email protected]> * add .change log * clippy fixes * print test error * fix tests --------- Co-authored-by: Fabian-Lars <[email protected]> Co-authored-by: Lucas Nogueira <[email protected]>
1 parent 74ccaac commit 2326d94

File tree

5 files changed

+133
-3
lines changed

5 files changed

+133
-3
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"upload": 'patch:bug'
3+
---
4+
5+
fix download content to file when unsuccessful response

Cargo.lock

Lines changed: 54 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/upload/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,7 @@ read-progress-stream = "1.0.0"
3535
native-tls = ["reqwest/native-tls"]
3636
native-tls-vendored = ["reqwest/native-tls-vendored"]
3737
rustls-tls = ["reqwest/rustls-tls"]
38+
39+
[dev-dependencies]
40+
mockito = "1.5.0"
41+
tokio = { version = "*", features = ["macros"] }

plugins/upload/src/lib.rs

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,19 @@ async fn download(
7070
let client = reqwest::Client::new();
7171

7272
let mut request = client.get(url);
73-
// Loop trought the headers keys and values
73+
// Loop through the headers keys and values
7474
// and add them to the request object.
7575
for (key, value) in headers {
7676
request = request.header(&key, value);
7777
}
7878

7979
let response = request.send().await?;
80+
if !response.status().is_success() {
81+
return Err(Error::HttpErrorCode(
82+
response.status().as_u16(),
83+
response.text().await.unwrap_or_default(),
84+
));
85+
}
8086
let total = response.content_length().unwrap_or(0);
8187

8288
let mut file = BufWriter::new(File::create(file_path).await?);
@@ -112,7 +118,7 @@ async fn upload(
112118
.header(reqwest::header::CONTENT_LENGTH, file_len)
113119
.body(file_to_body(on_progress, file));
114120

115-
// Loop trought the headers keys and values
121+
// Loop through the headers keys and values
116122
// and add them to the request object.
117123
for (key, value) in headers {
118124
request = request.header(&key, value);
@@ -145,3 +151,64 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
145151
.invoke_handler(tauri::generate_handler![download, upload])
146152
.build()
147153
}
154+
155+
#[cfg(test)]
156+
mod tests {
157+
use super::*;
158+
use mockito::{self, Mock, Server, ServerGuard};
159+
use tauri::ipc::InvokeResponseBody;
160+
struct MockedServer {
161+
_server: ServerGuard,
162+
url: String,
163+
mocked_endpoint: Mock,
164+
}
165+
166+
#[tokio::test]
167+
async fn should_error_if_status_not_success() {
168+
let mocked_server = spawn_server_mocked(400).await;
169+
let result = download_file(&mocked_server.url).await;
170+
mocked_server.mocked_endpoint.assert();
171+
assert!(result.is_err());
172+
}
173+
174+
#[tokio::test]
175+
async fn should_download_file_successfully() {
176+
let mocked_server = spawn_server_mocked(200).await;
177+
let result = download_file(&mocked_server.url).await;
178+
mocked_server.mocked_endpoint.assert();
179+
assert!(
180+
result.is_ok(),
181+
"failed to download file: {}",
182+
result.unwrap_err()
183+
);
184+
}
185+
186+
async fn download_file(url: &str) -> Result<()> {
187+
let file_path = concat!(env!("CARGO_MANIFEST_DIR"), "/test/test.txt");
188+
let headers = HashMap::new();
189+
let sender: Channel<ProgressPayload> =
190+
Channel::new(|msg: InvokeResponseBody| -> tauri::Result<()> {
191+
let _ = msg;
192+
Ok(())
193+
});
194+
download(url, file_path, headers, sender).await
195+
}
196+
197+
async fn spawn_server_mocked(return_status: usize) -> MockedServer {
198+
let mut _server = Server::new_async().await;
199+
let path = "/mock_test";
200+
let mock = _server
201+
.mock("GET", path)
202+
.with_status(return_status)
203+
.with_body("mocked response body")
204+
.create_async()
205+
.await;
206+
207+
let url = _server.url() + path;
208+
MockedServer {
209+
_server,
210+
url,
211+
mocked_endpoint: mock,
212+
}
213+
}
214+
}

plugins/upload/test/test.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mocked response body

0 commit comments

Comments
 (0)