|
| 1 | +//! Storing and retrieving session data with a custom data type, in a type safe |
| 2 | +//! way, with the Gotham web framework. |
| 3 | +
|
| 4 | +extern crate gotham; |
| 5 | +#[macro_use] |
| 6 | +extern crate gotham_derive; |
| 7 | +extern crate hyper; |
| 8 | +extern crate mime; |
| 9 | +extern crate serde; |
| 10 | +#[macro_use] |
| 11 | +extern crate serde_derive; |
| 12 | +extern crate time; |
| 13 | + |
| 14 | +use hyper::{Response, StatusCode}; |
| 15 | + |
| 16 | +use gotham::http::response::create_response; |
| 17 | +use gotham::pipeline::new_pipeline; |
| 18 | +use gotham::pipeline::single::single_pipeline; |
| 19 | +use gotham::router::Router; |
| 20 | +use gotham::router::builder::*; |
| 21 | +use gotham::state::{FromState, State}; |
| 22 | +use gotham::middleware::session::{NewSessionMiddleware, SessionData}; |
| 23 | + |
| 24 | +// A custom type for storing data associated with the user's session. |
| 25 | +#[derive(Clone, Deserialize, Serialize, StateData)] |
| 26 | +struct VisitData { |
| 27 | + count: usize, |
| 28 | + last_visit: String, |
| 29 | +} |
| 30 | + |
| 31 | +/// Handler function for `GET` requests directed to `/` |
| 32 | +/// |
| 33 | +/// Each request made will update state about your recent visits, and report it back. |
| 34 | +fn get_handler(mut state: State) -> (State, Response) { |
| 35 | + let maybe_visit_data = { |
| 36 | + let visit_data: &Option<VisitData> = SessionData::<Option<VisitData>>::borrow_from(&state); |
| 37 | + visit_data.clone() |
| 38 | + }; |
| 39 | + |
| 40 | + let body = match &maybe_visit_data { |
| 41 | + &Some(ref visit_data) => format!( |
| 42 | + "You have visited this page {} time(s) before. Your last visit was {}.\n", |
| 43 | + visit_data.count, visit_data.last_visit, |
| 44 | + ), |
| 45 | + &None => "You have never visited this page before.\n".to_owned(), |
| 46 | + }; |
| 47 | + let res = { |
| 48 | + create_response( |
| 49 | + &state, |
| 50 | + StatusCode::Ok, |
| 51 | + Some((body.as_bytes().to_vec(), mime::TEXT_PLAIN)), |
| 52 | + ) |
| 53 | + }; |
| 54 | + { |
| 55 | + let visit_data: &mut Option<VisitData> = |
| 56 | + SessionData::<Option<VisitData>>::borrow_mut_from(&mut state); |
| 57 | + let old_count = maybe_visit_data.map(|v| v.count).unwrap_or(0); |
| 58 | + *visit_data = Some(VisitData { |
| 59 | + count: old_count + 1, |
| 60 | + last_visit: format!("{}", time::now().rfc3339()), |
| 61 | + }); |
| 62 | + } |
| 63 | + (state, res) |
| 64 | +} |
| 65 | + |
| 66 | +/// Create a `Router` |
| 67 | +fn router() -> Router { |
| 68 | + let middleware = NewSessionMiddleware::default() |
| 69 | + .with_session_type::<Option<VisitData>>() |
| 70 | + // By default, the cookies used are only sent over secure connections. For our test server, |
| 71 | + // we don't set up an HTTPS certificate, so we allow the cookies to be sent over insecure |
| 72 | + // connections. This should not be done in real applications. |
| 73 | + .insecure(); |
| 74 | + let (chain, pipelines) = single_pipeline(new_pipeline().add(middleware).build()); |
| 75 | + build_router(chain, pipelines, |route| { |
| 76 | + route.get("/").to(get_handler); |
| 77 | + }) |
| 78 | +} |
| 79 | + |
| 80 | +/// Start a server and use a `Router` to dispatch requests |
| 81 | +pub fn main() { |
| 82 | + let addr = "127.0.0.1:7878"; |
| 83 | + println!("Listening for requests at http://{}", addr); |
| 84 | + gotham::start(addr, router()) |
| 85 | +} |
| 86 | + |
| 87 | +#[cfg(test)] |
| 88 | +mod tests { |
| 89 | + use super::*; |
| 90 | + use gotham::test::TestServer; |
| 91 | + use hyper::header::{Cookie, SetCookie}; |
| 92 | + use std::borrow::Cow; |
| 93 | + |
| 94 | + #[test] |
| 95 | + fn cookie_is_set_and_updates_response() { |
| 96 | + let test_server = TestServer::new(router()).unwrap(); |
| 97 | + let response = test_server |
| 98 | + .client() |
| 99 | + .get("http://localhost/") |
| 100 | + .perform() |
| 101 | + .unwrap(); |
| 102 | + |
| 103 | + assert_eq!(response.status(), StatusCode::Ok); |
| 104 | + |
| 105 | + let set_cookie: Vec<String> = { |
| 106 | + let cookie_header = response.headers().get::<SetCookie>(); |
| 107 | + assert!(cookie_header.is_some()); |
| 108 | + cookie_header.unwrap().0.clone() |
| 109 | + }; |
| 110 | + assert!(set_cookie.len() == 1); |
| 111 | + |
| 112 | + let body = response.read_body().unwrap(); |
| 113 | + assert_eq!( |
| 114 | + &body[..], |
| 115 | + "You have never visited this page before.\n".as_bytes() |
| 116 | + ); |
| 117 | + |
| 118 | + let cookie = { |
| 119 | + let mut cookie = Cookie::new(); |
| 120 | + |
| 121 | + let only_cookie: String = set_cookie.get(0).unwrap().clone(); |
| 122 | + let cookie_components: Vec<_> = only_cookie.split(";").collect(); |
| 123 | + let cookie_str_parts: Vec<_> = cookie_components.get(0).unwrap().split("=").collect(); |
| 124 | + cookie.append( |
| 125 | + Cow::Owned(cookie_str_parts.get(0).unwrap().to_string()), |
| 126 | + Cow::Owned(cookie_str_parts.get(1).unwrap().to_string()), |
| 127 | + ); |
| 128 | + cookie |
| 129 | + }; |
| 130 | + |
| 131 | + let response = test_server |
| 132 | + .client() |
| 133 | + .get("http://localhost/") |
| 134 | + .with_header(cookie) |
| 135 | + .perform() |
| 136 | + .unwrap(); |
| 137 | + |
| 138 | + assert_eq!(response.status(), StatusCode::Ok); |
| 139 | + let body = response.read_body().unwrap(); |
| 140 | + let body_string = String::from_utf8(body).unwrap(); |
| 141 | + assert!( |
| 142 | + body_string |
| 143 | + .starts_with("You have visited this page 1 time(s) before. Your last visit was ",), |
| 144 | + "Wrong body: {}", |
| 145 | + body_string |
| 146 | + ); |
| 147 | + } |
| 148 | +} |
0 commit comments