From f230d43fd5a7836e263de9977e20fd715ac0502a Mon Sep 17 00:00:00 2001 From: Lori Holden Date: Mon, 16 Jan 2017 11:34:19 -0500 Subject: [PATCH] Add example for the contrib UUID type. --- Cargo.toml | 1 + examples/uuid/Cargo.toml | 18 +++++++++++++++ examples/uuid/src/main.rs | 46 ++++++++++++++++++++++++++++++++++++++ examples/uuid/src/tests.rs | 33 +++++++++++++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 examples/uuid/Cargo.toml create mode 100644 examples/uuid/src/main.rs create mode 100644 examples/uuid/src/tests.rs diff --git a/Cargo.toml b/Cargo.toml index 4b0ff3c4..cc099e2e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,4 +29,5 @@ members = [ "examples/raw_upload", "examples/pastebin", "examples/state", + "examples/uuid", ] diff --git a/examples/uuid/Cargo.toml b/examples/uuid/Cargo.toml new file mode 100644 index 00000000..d94f1d0b --- /dev/null +++ b/examples/uuid/Cargo.toml @@ -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"] } diff --git a/examples/uuid/src/main.rs b/examples/uuid/src/main.rs new file mode 100644 index 00000000..47a4832f --- /dev/null +++ b/examples/uuid/src/main.rs @@ -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 = { + 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/")] +fn people(id: UUID) -> Result { + // 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(); +} diff --git a/examples/uuid/src/tests.rs b/examples/uuid/src/tests.rs new file mode 100644 index 00000000..47d4011e --- /dev/null +++ b/examples/uuid/src/tests.rs @@ -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"); +}