Skip to content

Commit

Permalink
Refactoring route testing
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanMcCormickJr committed Dec 2, 2024
1 parent c778ca1 commit c8096c6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 58 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ edition = "2021"

[dependencies]
rocket = { version = "0.5.1", features = ["tls",] }

[lib]
name = "bville_recycle"
path = "src/lib.rs"
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#![forbid(unsafe_code)]

#[macro_use]
extern crate rocket;

#[get("/")]
pub fn map_root() -> &'static str {
"Map of Bartlesville recycling options"
}

#[get("/map")]
pub fn map() -> &'static str {
"Map of Bartlesville recycling options"
}

pub fn rocket() -> rocket::Rocket<rocket::Build> {
rocket::build().mount("/", routes![map_root, map])
}
61 changes: 3 additions & 58 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,64 +1,9 @@
#![forbid(unsafe_code)]

#[macro_use] extern crate rocket;



#[get("/")]
fn map_root() -> &'static str {
"Map of Bartlesville recycling options"
}

#[get("/map")]
fn map() -> &'static str {
"Map of Bartlesville recycling options"
}
use bville_recycle::rocket as app_rocket;
use rocket::launch; // Rename the imported function

#[launch]
fn rocket() -> _ {
rocket::build()
.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
use rocket::local::blocking::Client; // Import the blocking client for testing
use rocket::http::Status; // Import HTTP status for response checks

#[test] // Tests the map_root function
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] // Tests the map function
fn test_map() {
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())
);
}
app_rocket()
}
27 changes: 27 additions & 0 deletions tests/routes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use bville_recycle::rocket;
use rocket::http::Status; // Import HTTP status for response checks
use rocket::local::blocking::Client; // Import the blocking client for testing // Import the rocket function from the library

#[test] // Tests the map_root function
fn test_map_root() {
let rocket = rocket();
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] // Tests the map function
fn test_map() {
let rocket = rocket();
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())
);
}

0 comments on commit c8096c6

Please sign in to comment.