diff --git a/Makefile b/Makefile index 70d000e..79daa85 100644 --- a/Makefile +++ b/Makefile @@ -23,4 +23,10 @@ image: Dockerfile # Run run: @ cargo run +.PHONY: run + +# Testing +test: + @ cargo test +.PHONY: test diff --git a/src/hello/mod.rs b/src/hello/mod.rs index 6a664ab..547276a 100644 --- a/src/hello/mod.rs +++ b/src/hello/mod.rs @@ -1 +1,16 @@ -pub mod routes; +#[cfg(test)] mod tests; + +#[get("/world")] +pub fn world() -> &'static str { + "Hello, world!" +} + +#[get("/")] +pub fn hello(name: String) -> String { + format!("Hello, {}", name) +} + +#[get("/world?&")] +pub fn name_age(name: String, age: u16) -> String { + format!("Hello, {} who is {} years old!", name, age) +} diff --git a/src/hello/routes.rs b/src/hello/routes.rs deleted file mode 100644 index c7482e9..0000000 --- a/src/hello/routes.rs +++ /dev/null @@ -1,14 +0,0 @@ -#[get("/world")] -pub fn world() -> &'static str { - "Hello, world!" -} - -#[get("/")] -pub fn hello(name: String) -> String { - format!("Hello, {}", name) -} - -#[get("/world?&")] -pub fn name_age(name: String, age: u16) -> String { - format!("Hello, {} who is {} years old!", name, age) -} diff --git a/src/hello/tests.rs b/src/hello/tests.rs new file mode 100644 index 0000000..83b122d --- /dev/null +++ b/src/hello/tests.rs @@ -0,0 +1,24 @@ +use rocket::local::Client; +use rocket::http::Status; + +fn rocket() -> rocket::Rocket { + rocket::ignite().mount("/", routes![super::world, super::hello, super::name_age]) +} + +#[test] +fn test_world() { + let client = Client::new(rocket()).expect("valid rocket instance"); + let mut response = client.get("/world").dispatch(); + + assert_eq!(response.status(), Status::Ok); + assert_eq!(response.body_string(), Some("Hello, world!".into())); +} + +#[test] +fn test_hello() { + let client = Client::new(rocket()).expect("valid rocket instance"); + let mut response = client.get("/thisisaname").dispatch(); + + assert_eq!(response.status(), Status::Ok); + assert_eq!(response.body_string(), Some("Hello, thisisaname".into())); +} diff --git a/src/ivago/controller.rs b/src/ivago/controller.rs deleted file mode 100644 index 0b3b0a3..0000000 --- a/src/ivago/controller.rs +++ /dev/null @@ -1 +0,0 @@ -mod ivago.controller; diff --git a/src/ivago/routes.rs b/src/ivago/tests.rs similarity index 100% rename from src/ivago/routes.rs rename to src/ivago/tests.rs diff --git a/src/main.rs b/src/main.rs index 7f811c2..a4ce2c9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,8 +6,8 @@ mod hello; fn main() { rocket::ignite().mount("/hello", routes![ - hello::routes::world, - hello::routes::hello, - hello::routes::name_age + hello::world, + hello::hello, + hello::name_age ]).launch(); }