From 02dfb0302fb1c8e5fa08b922f742bbf382c7a8bb Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Sun, 15 Sep 2024 13:16:10 -0300 Subject: [PATCH] fix(tests): use hyper instead of tiny-http to serve updates --- Cargo.lock | 36 +------- core/tests/app-updater/Cargo.toml | 4 +- core/tests/app-updater/tests/update.rs | 114 +++++++++++++++---------- 3 files changed, 73 insertions(+), 81 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 660a8116ea0a..cb7e6cff4f09 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -101,12 +101,14 @@ checksum = "f538837af36e6f6a9be0faa67f9a314f8119e4e4b5867c6ab40ed60360142519" name = "app-updater-tests" version = "0.1.0" dependencies = [ + "hyper", "serde", "serde_json", "serial_test", "tauri", "time", - "tiny_http", + "tokio", + "tokio-util", ] [[package]] @@ -129,12 +131,6 @@ dependencies = [ "x11rb", ] -[[package]] -name = "ascii" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d92bec98840b8f03a5ff5413de5293bfcd8bf96467cf5452609f939ec6f5de16" - [[package]] name = "assert-json-diff" version = "2.0.2" @@ -588,12 +584,6 @@ dependencies = [ "windows-targets 0.52.5", ] -[[package]] -name = "chunked_transfer" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e4de3bc4ea267985becf712dc6d9eed8b04c953b3fcfb339ebc87acd9804901" - [[package]] name = "cipher" version = "0.4.4" @@ -4396,26 +4386,6 @@ dependencies = [ "libc", "num_threads", "serde", - "time-macros", -] - -[[package]] -name = "time-macros" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42657b1a6f4d817cda8e7a0ace261fe0cc946cf3a80314390b22cc61ae080792" - -[[package]] -name = "tiny_http" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0d6ef4e10d23c1efb862eecad25c5054429a71958b4eeef85eb5e7170b477ca" -dependencies = [ - "ascii", - "chunked_transfer", - "log", - "time", - "url", ] [[package]] diff --git a/core/tests/app-updater/Cargo.toml b/core/tests/app-updater/Cargo.toml index 299d49ce52ea..f8e3f25c4552 100644 --- a/core/tests/app-updater/Cargo.toml +++ b/core/tests/app-updater/Cargo.toml @@ -7,6 +7,8 @@ edition = "2021" serde = { version = "1", features = ["derive"] } serde_json = "1" tauri = { path = "../../tauri", features = ["updater"] } -tiny_http = "0.11" +hyper = { version = "0.14", features = ["server", "stream"] } +tokio-util = { version = "0.7", features = ["codec"] } +tokio = { version = "1", features = ["fs"] } time = { version = "0.3", features = ["formatting"] } serial_test = "0.8" diff --git a/core/tests/app-updater/tests/update.rs b/core/tests/app-updater/tests/update.rs index 669196618f91..fd2123f919d5 100644 --- a/core/tests/app-updater/tests/update.rs +++ b/core/tests/app-updater/tests/update.rs @@ -12,7 +12,12 @@ use std::{ sync::Arc, }; +use hyper::{ + service::{make_service_fn, service_fn}, + Body, Method, Request, Response, StatusCode, +}; use serde::Serialize; +use tokio_util::codec::{BytesCodec, FramedRead}; const UPDATER_PRIVATE_KEY: &str = "dW50cnVzdGVkIGNvbW1lbnQ6IHJzaWduIGVuY3J5cHRlZCBzZWNyZXQga2V5ClJXUlRZMEl5dkpDN09RZm5GeVAzc2RuYlNzWVVJelJRQnNIV2JUcGVXZUplWXZXYXpqUUFBQkFBQUFBQUFBQUFBQUlBQUFBQTZrN2RnWGh5dURxSzZiL1ZQSDdNcktiaHRxczQwMXdQelRHbjRNcGVlY1BLMTBxR2dpa3I3dDE1UTVDRDE4MXR4WlQwa1BQaXdxKy9UU2J2QmVSNXhOQWFDeG1GSVllbUNpTGJQRkhhTnROR3I5RmdUZi90OGtvaGhJS1ZTcjdZU0NyYzhQWlQ5cGM9Cg=="; // const UPDATER_PUBLIC_KEY: &str = "dW50cnVzdGVkIGNvbW1lbnQ6IG1pbmlzaWduIHB1YmxpYyBrZXk6IEZFOUJFNDg1NTU4NUZDQUQKUldTdC9JVlZoZVNiL2tVVG1hSFRETjRIZXE0a0F6d3dSY2ViYzdrSFh2MjBGWm1jM0NoWVFqM1YK"; @@ -415,53 +420,68 @@ fn update_app_flow) -> (PathBuf, TauriVersion)>(build_app_ let target = target.clone(); - // create the updater server - let server = - Arc::new(tiny_http::Server::http("localhost:3007").expect("failed to start updater server")); - - let server_ = server.clone(); - std::thread::spawn(move || { - for request in server_.incoming_requests() { - match request.url() { - "/" => { - let mut platforms = HashMap::new(); - - platforms.insert( - target.clone(), - PlatformUpdate { - signature: signature.clone(), - url: "http://localhost:3007/download", - with_elevated_task: false, - }, - ); - let body = serde_json::to_vec(&Update { - version: UPDATE_APP_VERSION, - date: time::OffsetDateTime::now_utc() - .format(&time::format_description::well_known::Rfc3339) - .unwrap(), - platforms, - }) - .unwrap(); - let len = body.len(); - let response = tiny_http::Response::new( - tiny_http::StatusCode(200), - Vec::new(), - std::io::Cursor::new(body), - Some(len), - None, - ); - let _ = request.respond(response); - } - "/download" => { - let _ = request.respond(tiny_http::Response::from_file( - File::open(&updater_path).unwrap_or_else(|_| { - panic!("failed to open updater bundle {}", updater_path.display()) - }), - )); - } - _ => (), + let (tx, rx) = tokio::sync::oneshot::channel::<()>(); + + let runtime = tokio::runtime::Runtime::new().unwrap(); + + runtime.spawn(async move { + // create the updater server + let addr = "127.0.0.1:3007".parse().unwrap(); + + let make_service = make_service_fn(move |_| { + let updater_path = updater_path.clone(); + let signature = signature.clone(); + let target = target.clone(); + async move { + Ok::<_, hyper::Error>(service_fn(move |req| { + let updater_path = updater_path.clone(); + let signature = signature.clone(); + let target = target.clone(); + async move { + match (req.method(), req.uri().path()) { + (&Method::GET, "/") => { + let mut platforms = HashMap::new(); + + platforms.insert( + target.clone(), + PlatformUpdate { + signature: signature.clone(), + url: "http://localhost:3007/download", + with_elevated_task: false, + }, + ); + let body = serde_json::to_vec(&Update { + version: UPDATE_APP_VERSION, + date: time::OffsetDateTime::now_utc() + .format(&time::format_description::well_known::Rfc3339) + .unwrap(), + platforms, + }) + .unwrap(); + + Ok(Response::new(hyper::Body::from(body))) + } + (&Method::GET, "/download") => { + let file = tokio::fs::File::open(&updater_path).await.unwrap(); + let stream = FramedRead::new(file, BytesCodec::new()); + let body = Body::wrap_stream(stream); + return Ok(Response::new(body)); + } + _ => Response::builder() + .status(StatusCode::NOT_FOUND) + .body("Not Found".into()), + } + } + })) } - } + }); + let server = hyper::Server::bind(&addr).serve(make_service); + + let graceful = server.with_graceful_shutdown(async { + rx.await.ok(); + }); + + graceful.await.unwrap(); }); let config = Config { @@ -557,6 +577,6 @@ fn update_app_flow) -> (PathBuf, TauriVersion)>(build_app_ std::fs::remove_file(tauri_v1_fixture_dir.join("target/debug/app-updater.exe")).unwrap(); // graceful shutdown - server.unblock(); + tx.send(()).unwrap(); } }