2018-09-20 04:14:30 +00:00
|
|
|
#![feature(proc_macro_non_items, proc_macro_gen, decl_macro)]
|
2018-04-15 02:53:36 +00:00
|
|
|
|
2018-06-28 15:55:15 +00:00
|
|
|
#[macro_use] extern crate rocket;
|
2018-04-15 02:53:36 +00:00
|
|
|
|
|
|
|
use rocket::request::FlashMessage;
|
|
|
|
use rocket::response::Flash;
|
|
|
|
|
|
|
|
const FLASH_MESSAGE: &str = "Hey! I'm a flash message. :)";
|
|
|
|
|
|
|
|
#[post("/")]
|
|
|
|
fn set() -> Flash<&'static str> {
|
|
|
|
Flash::success("This is the page.", FLASH_MESSAGE)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/unused")]
|
|
|
|
fn unused(flash: Option<FlashMessage>) -> Option<()> {
|
|
|
|
flash.map(|_| ())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/use")]
|
|
|
|
fn used(flash: Option<FlashMessage>) -> Option<String> {
|
|
|
|
flash.map(|flash| flash.msg().into())
|
|
|
|
}
|
|
|
|
|
|
|
|
mod flash_lazy_remove_tests {
|
|
|
|
use rocket::local::Client;
|
|
|
|
use rocket::http::Status;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test() {
|
|
|
|
use super::*;
|
|
|
|
let r = rocket::ignite().mount("/", routes![set, unused, used]);
|
|
|
|
let client = Client::new(r).unwrap();
|
|
|
|
|
|
|
|
// Ensure the cookie's not there at first.
|
|
|
|
let response = client.get("/unused").dispatch();
|
|
|
|
assert_eq!(response.status(), Status::NotFound);
|
|
|
|
|
|
|
|
// Set the flash cookie.
|
|
|
|
client.post("/").dispatch();
|
|
|
|
|
|
|
|
// Try once.
|
|
|
|
let response = client.get("/unused").dispatch();
|
|
|
|
assert_eq!(response.status(), Status::Ok);
|
|
|
|
|
|
|
|
// Try again; should still be there.
|
|
|
|
let response = client.get("/unused").dispatch();
|
|
|
|
assert_eq!(response.status(), Status::Ok);
|
|
|
|
|
|
|
|
// Now use it.
|
|
|
|
let mut response = client.get("/use").dispatch();
|
|
|
|
assert_eq!(response.body_string(), Some(FLASH_MESSAGE.into()));
|
|
|
|
|
|
|
|
// Now it should be gone.
|
|
|
|
let response = client.get("/unused").dispatch();
|
|
|
|
assert_eq!(response.status(), Status::NotFound);
|
|
|
|
|
|
|
|
// Still gone.
|
|
|
|
let response = client.get("/use").dispatch();
|
|
|
|
assert_eq!(response.status(), Status::NotFound);
|
|
|
|
}
|
|
|
|
}
|