Add Rust 2015 example.

This commit is contained in:
Sergio Benitez 2019-06-25 14:06:11 -04:00
parent d9f989a496
commit da7e022f99
4 changed files with 74 additions and 0 deletions

View File

@ -41,4 +41,5 @@ members = [
"examples/tls", "examples/tls",
"examples/fairings", "examples/fairings",
"examples/hello_2018", "examples/hello_2018",
"examples/hello_2015",
] ]

View File

@ -0,0 +1,9 @@
[package]
name = "hello_2015"
version = "0.0.0"
workspace = "../../"
edition = "2015"
publish = false
[dependencies]
rocket = { path = "../../core/lib" }

View File

@ -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();
}

View File

@ -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.");
}
}