Added first unit tests
parent
e498fb38c8
commit
28aab01e77
6
Makefile
6
Makefile
|
@ -23,4 +23,10 @@ image: Dockerfile
|
||||||
# Run
|
# Run
|
||||||
run:
|
run:
|
||||||
@ cargo run
|
@ cargo run
|
||||||
|
.PHONY: run
|
||||||
|
|
||||||
|
|
||||||
|
# Testing
|
||||||
|
test:
|
||||||
|
@ cargo test
|
||||||
|
.PHONY: test
|
||||||
|
|
|
@ -1 +1,16 @@
|
||||||
pub mod routes;
|
#[cfg(test)] mod tests;
|
||||||
|
|
||||||
|
#[get("/world")]
|
||||||
|
pub fn world() -> &'static str {
|
||||||
|
"Hello, world!"
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("/<name>")]
|
||||||
|
pub fn hello(name: String) -> String {
|
||||||
|
format!("Hello, {}", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("/world?<name>&<age>")]
|
||||||
|
pub fn name_age(name: String, age: u16) -> String {
|
||||||
|
format!("Hello, {} who is {} years old!", name, age)
|
||||||
|
}
|
||||||
|
|
|
@ -1,14 +0,0 @@
|
||||||
#[get("/world")]
|
|
||||||
pub fn world() -> &'static str {
|
|
||||||
"Hello, world!"
|
|
||||||
}
|
|
||||||
|
|
||||||
#[get("/<name>")]
|
|
||||||
pub fn hello(name: String) -> String {
|
|
||||||
format!("Hello, {}", name)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[get("/world?<name>&<age>")]
|
|
||||||
pub fn name_age(name: String, age: u16) -> String {
|
|
||||||
format!("Hello, {} who is {} years old!", name, age)
|
|
||||||
}
|
|
|
@ -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()));
|
||||||
|
}
|
|
@ -1 +0,0 @@
|
||||||
mod ivago.controller;
|
|
|
@ -6,8 +6,8 @@ mod hello;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
rocket::ignite().mount("/hello", routes![
|
rocket::ignite().mount("/hello", routes![
|
||||||
hello::routes::world,
|
hello::world,
|
||||||
hello::routes::hello,
|
hello::hello,
|
||||||
hello::routes::name_age
|
hello::name_age
|
||||||
]).launch();
|
]).launch();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue