mirror of https://github.com/rwf2/Rocket.git
Add Rust 2015 example.
This commit is contained in:
parent
d9f989a496
commit
da7e022f99
|
@ -41,4 +41,5 @@ members = [
|
||||||
"examples/tls",
|
"examples/tls",
|
||||||
"examples/fairings",
|
"examples/fairings",
|
||||||
"examples/hello_2018",
|
"examples/hello_2018",
|
||||||
|
"examples/hello_2015",
|
||||||
]
|
]
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
[package]
|
||||||
|
name = "hello_2015"
|
||||||
|
version = "0.0.0"
|
||||||
|
workspace = "../../"
|
||||||
|
edition = "2015"
|
||||||
|
publish = false
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
rocket = { path = "../../core/lib" }
|
|
@ -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();
|
||||||
|
}
|
|
@ -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.");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue