forked from rwf2/Rocket
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ef65a8
commit c815911
Showing
14 changed files
with
239 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,4 +28,5 @@ members = [ | |
"examples/hello_alt_methods", | ||
"examples/raw_upload", | ||
"examples/pastebin", | ||
"examples/state", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "state" | ||
version = "0.0.1" | ||
workspace = "../../" | ||
|
||
[dependencies] | ||
rocket = { path = "../../lib" } | ||
rocket_codegen = { path = "../../codegen" } | ||
|
||
[dev-dependencies] | ||
rocket = { path = "../../lib", features = ["testing"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#![feature(plugin)] | ||
#![plugin(rocket_codegen)] | ||
|
||
extern crate rocket; | ||
|
||
#[cfg(test)] mod tests; | ||
|
||
use std::sync::atomic::{AtomicUsize, Ordering}; | ||
|
||
use rocket::State; | ||
use rocket::response::content; | ||
|
||
struct HitCount(AtomicUsize); | ||
|
||
#[get("/")] | ||
fn index(hit_count: State<HitCount>) -> content::HTML<String> { | ||
hit_count.0.fetch_add(1, Ordering::Relaxed); | ||
let msg = "Your visit has been recorded!"; | ||
let count = format!("Visits: {}", count(hit_count)); | ||
content::HTML(format!("{}<br /><br />{}", msg, count)) | ||
} | ||
|
||
#[get("/count")] | ||
fn count(hit_count: State<HitCount>) -> String { | ||
hit_count.0.load(Ordering::Relaxed).to_string() | ||
} | ||
|
||
fn rocket() -> rocket::Rocket { | ||
rocket::ignite() | ||
.mount("/", routes![index, count]) | ||
.manage(HitCount(AtomicUsize::new(0))) | ||
} | ||
|
||
fn main() { | ||
rocket().launch(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use rocket::Rocket; | ||
use rocket::testing::MockRequest; | ||
use rocket::http::Method::*; | ||
use rocket::http::Status; | ||
|
||
fn register_hit(rocket: &Rocket) { | ||
let mut req = MockRequest::new(Get, "/"); | ||
let response = req.dispatch_with(&rocket); | ||
assert_eq!(response.status(), Status::Ok); | ||
} | ||
|
||
fn get_count(rocket: &Rocket) -> usize { | ||
let mut req = MockRequest::new(Get, "/count"); | ||
let mut response = req.dispatch_with(&rocket); | ||
let body_string = response.body().and_then(|b| b.into_string()).unwrap(); | ||
body_string.parse().unwrap() | ||
} | ||
|
||
#[test] | ||
fn test_count() { | ||
let rocket = super::rocket(); | ||
|
||
// Count should start at 0. | ||
assert_eq!(get_count(&rocket), 0); | ||
|
||
for _ in 0..99 { register_hit(&rocket); } | ||
assert_eq!(get_count(&rocket), 99); | ||
|
||
register_hit(&rocket); | ||
assert_eq!(get_count(&rocket), 100); | ||
} | ||
|
||
// Cargo runs each test in parallel on different threads. We use all of these | ||
// tests below to show (and assert) that state is managed per-Rocket instance. | ||
#[test] fn test_count_parallel() { test_count() } | ||
#[test] fn test_count_parallel_2() { test_count() } | ||
#[test] fn test_count_parallel_3() { test_count() } | ||
#[test] fn test_count_parallel_4() { test_count() } | ||
#[test] fn test_count_parallel_5() { test_count() } | ||
#[test] fn test_count_parallel_6() { test_count() } | ||
#[test] fn test_count_parallel_7() { test_count() } | ||
#[test] fn test_count_parallel_8() { test_count() } | ||
#[test] fn test_count_parallel_9() { test_count() } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use std::ops::Deref; | ||
|
||
use request::{self, FromRequest, Request}; | ||
use outcome::Outcome; | ||
use http::Status; | ||
|
||
// TODO: Doc. | ||
#[derive(Debug, PartialEq, Eq)] | ||
pub struct State<'r, T: Send + Sync + 'static>(&'r T); | ||
|
||
impl<'r, T: Send + Sync + 'static> State<'r, T> { | ||
/// Retrieve a borrow to the underyling value. | ||
/// | ||
/// Using this method is typically unnecessary as `State` implements `Deref` | ||
/// with a `Target` of `T`. This means Rocket will automatically coerce a | ||
/// `State<T>` to an `&T` when the types call for it. | ||
pub fn inner(&self) -> &'r T { | ||
self.0 | ||
} | ||
} | ||
|
||
// TODO: Doc. | ||
impl<'a, 'r, T: Send + Sync + 'static> FromRequest<'a, 'r> for State<'r, T> { | ||
type Error = (); | ||
|
||
fn from_request(req: &'a Request<'r>) -> request::Outcome<State<'r, T>, ()> { | ||
if let Some(state) = req.get_state() { | ||
match state.try_get::<T>() { | ||
Some(state) => Outcome::Success(State(state)), | ||
None => { | ||
error_!("Attempted to retrieve unmanaged state!"); | ||
Outcome::Failure((Status::InternalServerError, ())) | ||
} | ||
} | ||
} else { | ||
error_!("Internal Rocket error: managed state is unset!"); | ||
error_!("Please report this error in the Rocket GitHub issue tracker."); | ||
Outcome::Failure((Status::InternalServerError, ())) | ||
} | ||
} | ||
} | ||
|
||
impl<'r, T: Send + Sync + 'static> Deref for State<'r, T> { | ||
type Target = T; | ||
|
||
fn deref(&self) -> &T { | ||
self.0 | ||
} | ||
} |
Oops, something went wrong.