mirror of
https://github.com/rwf2/Rocket.git
synced 2024-12-29 05:42:35 +00:00
8d28e845d9
This is now consistent with the serialization of 'Flash' and better illustrates the purpose of the late 'name' property. Additionally, take 'Into<String>' instead of 'AsRef<str>' so we only allocate when necessary.
60 lines
1.6 KiB
Rust
60 lines
1.6 KiB
Rust
#[macro_use] extern crate rocket;
|
|
|
|
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(|f| f.message().into())
|
|
}
|
|
|
|
mod flash_lazy_remove_tests {
|
|
use rocket::local::blocking::Client;
|
|
use rocket::http::Status;
|
|
|
|
#[test]
|
|
fn test() {
|
|
use super::*;
|
|
|
|
// Ensure the cookie's not there at first.
|
|
let client = Client::debug_with(routes![set, unused, used]).unwrap();
|
|
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 response = client.get("/use").dispatch();
|
|
assert_eq!(response.into_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);
|
|
}
|
|
}
|