2019-08-20 23:53:00 +00:00
|
|
|
#![feature(proc_macro_hygiene)]
|
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")]
|
2019-06-13 01:48:02 +00:00
|
|
|
fn unused(flash: Option<FlashMessage<'_, '_>>) -> Option<()> {
|
2018-04-15 02:53:36 +00:00
|
|
|
flash.map(|_| ())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/use")]
|
2019-06-13 01:48:02 +00:00
|
|
|
fn used(flash: Option<FlashMessage<'_, '_>>) -> Option<String> {
|
2018-04-15 02:53:36 +00:00
|
|
|
flash.map(|flash| flash.msg().into())
|
|
|
|
}
|
|
|
|
|
|
|
|
mod flash_lazy_remove_tests {
|
|
|
|
use rocket::local::Client;
|
|
|
|
use rocket::http::Status;
|
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
#[rocket::async_test]
|
|
|
|
async fn test() {
|
2018-04-15 02:53:36 +00:00
|
|
|
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.
|
2019-08-24 17:27:10 +00:00
|
|
|
let response = client.get("/unused").dispatch().await;
|
2018-04-15 02:53:36 +00:00
|
|
|
assert_eq!(response.status(), Status::NotFound);
|
|
|
|
|
|
|
|
// Set the flash cookie.
|
2019-08-24 17:27:10 +00:00
|
|
|
client.post("/").dispatch().await;
|
2018-04-15 02:53:36 +00:00
|
|
|
|
|
|
|
// Try once.
|
2019-08-24 17:27:10 +00:00
|
|
|
let response = client.get("/unused").dispatch().await;
|
2018-04-15 02:53:36 +00:00
|
|
|
assert_eq!(response.status(), Status::Ok);
|
|
|
|
|
|
|
|
// Try again; should still be there.
|
2019-08-24 17:27:10 +00:00
|
|
|
let response = client.get("/unused").dispatch().await;
|
2018-04-15 02:53:36 +00:00
|
|
|
assert_eq!(response.status(), Status::Ok);
|
|
|
|
|
|
|
|
// Now use it.
|
2019-08-24 17:27:10 +00:00
|
|
|
let mut response = client.get("/use").dispatch().await;
|
|
|
|
assert_eq!(response.body_string().await, Some(FLASH_MESSAGE.into()));
|
2018-04-15 02:53:36 +00:00
|
|
|
|
|
|
|
// Now it should be gone.
|
2019-08-24 17:27:10 +00:00
|
|
|
let response = client.get("/unused").dispatch().await;
|
2018-04-15 02:53:36 +00:00
|
|
|
assert_eq!(response.status(), Status::NotFound);
|
|
|
|
|
|
|
|
// Still gone.
|
2019-08-24 17:27:10 +00:00
|
|
|
let response = client.get("/use").dispatch().await;
|
2018-04-15 02:53:36 +00:00
|
|
|
assert_eq!(response.status(), Status::NotFound);
|
|
|
|
}
|
|
|
|
}
|