Rocket

A web framework for Rust. https://rocket.rs (mirror)
git clone git://git.thc420.xyz/Rocket
Log | Files | Refs | README

commit da7e022f990e0b8e8201b0a359a43104686ff1a4
parent d9f989a49616658878dfb3e9f37d52608e134e2a
Author: Sergio Benitez <sb@sergio.bz>
Date:   Tue, 25 Jun 2019 14:06:11 -0400

Add Rust 2015 example.

Diffstat:
MCargo.toml | 1+
Aexamples/hello_2015/Cargo.toml | 9+++++++++
Aexamples/hello_2015/src/main.rs | 14++++++++++++++
Aexamples/hello_2015/src/tests.rs | 50++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 74 insertions(+), 0 deletions(-)

diff --git 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 @@ -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 @@ -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 @@ -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("/<name>")] + 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."); + } +}