Add example for the contrib UUID type.

This commit is contained in:
Lori Holden 2017-01-16 11:34:19 -05:00 committed by Sergio Benitez
parent 06a7317fd9
commit f230d43fd5
4 changed files with 98 additions and 0 deletions

View File

@ -29,4 +29,5 @@ members = [
"examples/raw_upload",
"examples/pastebin",
"examples/state",
"examples/uuid",
]

18
examples/uuid/Cargo.toml Normal file
View File

@ -0,0 +1,18 @@
[package]
name = "uuid"
version = "0.1.0"
workspace = "../../"
[dependencies]
rocket = { path = "../../lib" }
rocket_codegen = { path = "../../codegen" }
uuid = "^0.3"
lazy_static = "^0.2"
[dependencies.rocket_contrib]
default-features = false
path = "../../contrib"
features = ["uuid"]
[dev-dependencies]
rocket = { path = "../../lib", features = ["testing"] }

46
examples/uuid/src/main.rs Normal file
View File

@ -0,0 +1,46 @@
#![feature(plugin)]
#![plugin(rocket_codegen)]
#[macro_use]
extern crate lazy_static;
extern crate uuid;
extern crate rocket;
extern crate rocket_contrib;
use std::collections::HashMap;
use uuid::Uuid;
use rocket_contrib::UUID;
#[cfg(test)]
mod tests;
lazy_static! {
// A small people lookup table for the sake of this example. In a real
// application this could be a database lookup. Notice that we use the
// uuid::Uuid type here and not the rocket_contrib::UUID type.
static ref PEOPLE: HashMap<Uuid, &'static str> = {
let mut m = HashMap::new();
let lacy_id = Uuid::parse_str("7f205202-7ba1-4c39-b2fc-3e630722bf9f").unwrap();
let bob_id = Uuid::parse_str("4da34121-bc7d-4fc1-aee6-bf8de0795333").unwrap();
let george_id = Uuid::parse_str("ad962969-4e3d-4de7-ac4a-2d86d6d10839").unwrap();
m.insert(lacy_id, "Lacy");
m.insert(bob_id, "Bob");
m.insert(george_id, "George");
m
};
}
#[get("/people/<id>")]
fn people(id: UUID) -> Result<String, String> {
// Because UUID implements the Deref trait, we use Deref coercion to
// convert rocket_contrib::UUID to uuid::Uuid.
Ok(PEOPLE.get(&id)
.map(|person| format!("We found: {}", person))
.ok_or(format!("Person not found for UUID: {}", id))?)
}
fn main() {
rocket::ignite()
.mount("/", routes![people])
.launch();
}

View File

@ -0,0 +1,33 @@
use super::rocket;
use rocket::testing::MockRequest;
use rocket::http::Method::*;
use rocket::http::Status;
fn test(uri: &str, expected: &str) {
let rocket = rocket::ignite().mount("/", routes![super::people]);
let mut req = MockRequest::new(Get, uri);
let mut res = req.dispatch_with(&rocket);
assert_eq!(res.body().and_then(|b| b.into_string()), Some(expected.into()));
}
fn test_404(uri: &str) {
let rocket = rocket::ignite().mount("/", routes![super::people]);
let mut req = MockRequest::new(Get, uri);
let res = req.dispatch_with(&rocket);
assert_eq!(res.status(), Status::NotFound);
}
#[test]
fn test_people() {
test("/people/7f205202-7ba1-4c39-b2fc-3e630722bf9f", "We found: Lacy");
test("/people/4da34121-bc7d-4fc1-aee6-bf8de0795333", "We found: Bob");
test("/people/ad962969-4e3d-4de7-ac4a-2d86d6d10839", "We found: George");
test("/people/e18b3a5c-488f-4159-a240-2101e0da19fd", "Person not found for UUID: e18b3a5c-488f-4159-a240-2101e0da19fd");
test_404("/people/invalid_uuid");
}