mirror of
https://github.com/rwf2/Rocket.git
synced 2025-01-14 21:49:08 +00:00
560f0977d3
* body_string_wait and body_bytes_wait are removed; use `.await` instead * `dispatch()` is now an async fn and must be .await-ed * Add `#[rocket::async_test]` macro, similar in purpose to `tokio::test` * Tests now use either `rocket::async_test(async { })` or `#[rocket::async_test]` in order to `.await` the futures returned from `dispatch()` and `body_{string,bytes}()` * Update 'test.sh' to reflect the tests that should be passing. Broken: * Cloned dispatch and mut_dispatch() with a live previous response now both fail, due to a (partial) check for mutable aliasing in LocalRequest. * Some tests are still failing and need example-specific changes.
63 lines
1.8 KiB
Rust
63 lines
1.8 KiB
Rust
#![feature(proc_macro_hygiene)]
|
|
|
|
#[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(|flash| flash.msg().into())
|
|
}
|
|
|
|
mod flash_lazy_remove_tests {
|
|
use rocket::local::Client;
|
|
use rocket::http::Status;
|
|
|
|
#[rocket::async_test]
|
|
async 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().await;
|
|
assert_eq!(response.status(), Status::NotFound);
|
|
|
|
// Set the flash cookie.
|
|
client.post("/").dispatch().await;
|
|
|
|
// Try once.
|
|
let response = client.get("/unused").dispatch().await;
|
|
assert_eq!(response.status(), Status::Ok);
|
|
|
|
// Try again; should still be there.
|
|
let response = client.get("/unused").dispatch().await;
|
|
assert_eq!(response.status(), Status::Ok);
|
|
|
|
// Now use it.
|
|
let mut response = client.get("/use").dispatch().await;
|
|
assert_eq!(response.body_string().await, Some(FLASH_MESSAGE.into()));
|
|
|
|
// Now it should be gone.
|
|
let response = client.get("/unused").dispatch().await;
|
|
assert_eq!(response.status(), Status::NotFound);
|
|
|
|
// Still gone.
|
|
let response = client.get("/use").dispatch().await;
|
|
assert_eq!(response.status(), Status::NotFound);
|
|
}
|
|
}
|