From 42027029a9111aa2774002aab488ed099c2d268e Mon Sep 17 00:00:00 2001 From: "Jonathan A. McCormick, Jr." <67705789+JonathanMcCormickJr@users.noreply.github.com> Date: Sun, 1 Dec 2024 19:02:11 -0600 Subject: [PATCH] Add testing zone within main.rs --- Cargo.lock | 81 +++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 +- requirements.md | 3 ++ src/main.rs | 46 +++++++++++++++++++++++++--- 4 files changed, 127 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 495f633..14f8c7f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -95,6 +95,12 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "base64" +version = "0.21.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" + [[package]] name = "binascii" version = "0.1.4" @@ -867,6 +873,21 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" +[[package]] +name = "ring" +version = "0.17.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d" +dependencies = [ + "cc", + "cfg-if", + "getrandom", + "libc", + "spin", + "untrusted", + "windows-sys 0.52.0", +] + [[package]] name = "rocket" version = "0.5.1" @@ -939,12 +960,15 @@ dependencies = [ "percent-encoding", "pin-project-lite", "ref-cast", + "rustls", + "rustls-pemfile", "serde", "smallvec", "stable-pattern", "state", "time", "tokio", + "tokio-rustls", "uncased", ] @@ -967,6 +991,37 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "rustls" +version = "0.21.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f56a14d1f48b391359b22f731fd4bd7e43c97f3c50eee276f3aa09c94784d3e" +dependencies = [ + "log", + "ring", + "rustls-webpki", + "sct", +] + +[[package]] +name = "rustls-pemfile" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c74cae0a4cf6ccbbf5f359f08efdf8ee7e1dc532573bf0db71968cb56b1448c" +dependencies = [ + "base64", +] + +[[package]] +name = "rustls-webpki" +version = "0.101.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b6275d1ee7a1cd780b64aca7726599a1dbc893b1e64144529e55c3c2f745765" +dependencies = [ + "ring", + "untrusted", +] + [[package]] name = "rustversion" version = "1.0.18" @@ -991,6 +1046,16 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "sct" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da046153aa2352493d6cb7da4b6e5c0c057d8a1d0a9aa8560baffdd945acd414" +dependencies = [ + "ring", + "untrusted", +] + [[package]] name = "serde" version = "1.0.215" @@ -1198,6 +1263,16 @@ dependencies = [ "syn", ] +[[package]] +name = "tokio-rustls" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c28327cf380ac148141087fbfb9de9d7bd4e84ab5d2c28fbc911d753de8a7081" +dependencies = [ + "rustls", + "tokio", +] + [[package]] name = "tokio-stream" version = "0.1.16" @@ -1360,6 +1435,12 @@ version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + [[package]] name = "valuable" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 0e3ddf4..bfd9e1f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,4 +4,4 @@ version = "0.1.0" edition = "2021" [dependencies] -rocket = "0.5.1" +rocket = { version = "0.5.1", features = ["tls",] } diff --git a/requirements.md b/requirements.md index f19e390..e37ae13 100644 --- a/requirements.md +++ b/requirements.md @@ -17,3 +17,6 @@ All authentication-related data, and all non-public user profile data, must be p ## Database This app must use MariaDB with its TDE feature. +## Testing + +This project aspires to align with both test-driven development (TDD) as well as the "Rugged" software principles: diff --git a/src/main.rs b/src/main.rs index cf24156..12adea2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,9 @@ -// main.rs - - - #![forbid(unsafe_code)] #[macro_use] extern crate rocket; +use rocket::local::blocking::Client; // Import the blocking client for testing +use rocket::http::Status; // Import HTTP status for response checks #[get("/")] fn map_root() -> &'static str { @@ -23,3 +21,43 @@ fn rocket() -> _ { .mount("/", routes![map_root, map]) } +/* + _____ _ _ _____ +|_ _|__ ___| |_(_)_ __ __ _ |__ /___ _ __ ___ + | |/ _ \/ __| __| | '_ \ / _` | / // _ \| '_ \ / _ \ + | | __/\__ \ |_| | | | | (_| | / /| (_) | | | | __/ + |_|\___||___/\__|_|_| |_|\__, | /____\___/|_| |_|\___| + |___/ +↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ +*/ + +#[cfg(test)] // Ensure this block is only included during testing +mod tests { + use super::*; // Import functions and routes from the main module + + #[test] + fn test_map_root() { + let rocket = rocket::build() + .mount("/", routes![map_root]); + let client = Client::tracked(rocket).expect("valid rocket instance"); + let response = client.get("/").dispatch(); + assert_eq!(response.status(), Status::Ok); + assert_eq!( + response.into_string(), + Some("Map of Bartlesville recycling options".into()) + ); + } + + #[test] + fn test_map_route() { + let rocket = rocket::build() + .mount("/", routes![map]); + let client = Client::tracked(rocket).expect("valid rocket instance"); + let response = client.get("/map").dispatch(); + assert_eq!(response.status(), Status::Ok); + assert_eq!( + response.into_string(), + Some("Map of Bartlesville recycling options".into()) + ); + } +}