From da7e022f990e0b8e8201b0a359a43104686ff1a4 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Tue, 25 Jun 2019 14:06:11 -0400 Subject: [PATCH] Add Rust 2015 example. --- Cargo.toml | 1 + examples/hello_2015/Cargo.toml | 9 ++++++ examples/hello_2015/src/main.rs | 14 +++++++++ examples/hello_2015/src/tests.rs | 50 ++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 examples/hello_2015/Cargo.toml create mode 100644 examples/hello_2015/src/main.rs create mode 100644 examples/hello_2015/src/tests.rs diff --git a/Cargo.toml b/Cargo.toml index 9f7f11cb..d6b0e638 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,4 +41,5 @@ members = [ "examples/tls", "examples/fairings", "examples/hello_2018", + "examples/hello_2015", ] diff --git a/examples/hello_2015/Cargo.toml b/examples/hello_2015/Cargo.toml new file mode 100644 index 00000000..3c35bb00 --- /dev/null +++ b/examples/hello_2015/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "hello_2015" +version = "0.0.0" +workspace = "../../" +edition = "2015" +publish = false + +[dependencies] +rocket = { path = "../../core/lib" } diff --git a/examples/hello_2015/src/main.rs b/examples/hello_2015/src/main.rs new file mode 100644 index 00000000..0085e155 --- /dev/null +++ b/examples/hello_2015/src/main.rs @@ -0,0 +1,14 @@ +#![feature(proc_macro_hygiene, decl_macro)] + +#[macro_use] extern crate rocket; + +#[cfg(test)] mod tests; + +#[get("/")] +fn hello() -> &'static str { + "Hello, Rust 2015!" +} + +fn main() { + rocket::ignite().mount("/", routes![hello]).launch(); +} diff --git a/examples/hello_2015/src/tests.rs b/examples/hello_2015/src/tests.rs new file mode 100644 index 00000000..ab692957 --- /dev/null +++ b/examples/hello_2015/src/tests.rs @@ -0,0 +1,50 @@ +use rocket::{self, routes, local::Client}; + +#[test] +fn hello_world() { + let rocket = rocket::ignite().mount("/", routes![super::hello]); + let client = Client::new(rocket).unwrap(); + let mut response = client.get("/").dispatch(); + assert_eq!(response.body_string(), Some("Hello, Rust 2015!".into())); +} + +// Tests unrelated to the example. +mod scoped_uri_tests { + use rocket::{get, routes}; + + mod inner { + use rocket::uri; + + #[rocket::get("/")] + pub fn hello() -> String { + format!("Hello! Try {}.", uri!(super::hello_name: "Rust 2015")) + } + } + + #[get("/")] + fn hello_name(name: String) -> String { + format!("Hello, {}! This is {}.", name, rocket::uri!(hello_name: &name)) + } + + fn rocket() -> rocket::Rocket { + rocket::ignite() + .mount("/", routes![hello_name]) + .mount("/", rocket::routes![inner::hello]) + } + + use rocket::local::Client; + + #[test] + fn test_inner_hello() { + let client = Client::new(rocket()).unwrap(); + let mut response = client.get("/").dispatch(); + assert_eq!(response.body_string(), Some("Hello! Try /Rust%202015.".into())); + } + + #[test] + fn test_hello_name() { + let client = Client::new(rocket()).unwrap(); + let mut response = client.get("/Rust%202015").dispatch(); + assert_eq!(response.body_string().unwrap(), "Hello, Rust 2015! This is /Rust%202015."); + } +}