`configure_router()` recibe Routes, que no monta ninguna ruta sin su Access: `route()`, `public_route()` y `admin_route()`, que toma el permiso de la página del panel de la que cuelga. Una ruta del panel montada de otra forma impide arrancar. `configure_middleware()` recibe Layers y `configure_static_files()` recibe StaticFiles, siempre públicos.
118 lines
4.1 KiB
Rust
118 lines
4.1 KiB
Rust
use pagetop::prelude::*;
|
|
|
|
use web::http::{StatusCode, header};
|
|
|
|
struct FlashExtension;
|
|
|
|
#[async_trait]
|
|
impl Extension for FlashExtension {
|
|
fn configure_router(&self, routes: Routes) -> Routes {
|
|
routes
|
|
.public_route("/save", web::post(save))
|
|
.public_route(
|
|
"/again",
|
|
web::get(|| async { Redirect::see_other("/done") }),
|
|
)
|
|
.public_route("/done", web::get(done))
|
|
.public_route("/file", web::get(|| async { "plain text" }))
|
|
}
|
|
}
|
|
|
|
async fn save(request: HttpRequest) -> Response {
|
|
let cx = Context::new(request);
|
|
Redirect::see_other("/done")
|
|
.with_message(&cx, MessageLevel::Info, Lc::n("Saved <b>today</b>"))
|
|
.with_message(&cx, MessageLevel::Error, Lc::n("One; problem"))
|
|
}
|
|
|
|
async fn done(request: HttpRequest) -> Result<Markup, ErrorPage> {
|
|
Page::new(request)
|
|
.with_child(Html::with(|_| html! { p { "Done" } }))
|
|
.render()
|
|
.await
|
|
}
|
|
|
|
async fn send(request: web::test::TestRequest) -> Response {
|
|
let app = web::test::init_router(Application::prepare(&FlashExtension).await.test());
|
|
web::test::send_request(&app, request.to_request()).await
|
|
}
|
|
|
|
fn get(uri: &str, cookie: Option<&str>) -> web::test::TestRequest {
|
|
let request = web::test::TestRequest::get().uri(uri);
|
|
match cookie {
|
|
Some(cookie) => request.header("cookie", cookie),
|
|
None => request,
|
|
}
|
|
}
|
|
|
|
fn set_cookie(response: &Response) -> Option<&str> {
|
|
response
|
|
.headers()
|
|
.get(header::SET_COOKIE)
|
|
.and_then(|value| value.to_str().ok())
|
|
}
|
|
|
|
// `name=value` of the flash cookie set by `response`.
|
|
fn flash_cookie(response: &Response) -> String {
|
|
let cookie = set_cookie(response).expect("the response sets the flash cookie");
|
|
assert!(cookie.starts_with("pagetop_flash="), "{cookie}");
|
|
cookie.split(';').next().unwrap().to_owned()
|
|
}
|
|
|
|
#[pagetop::test]
|
|
async fn messages_are_shown_on_the_next_page_and_then_cleared() {
|
|
let saved = send(web::test::TestRequest::post().uri("/save")).await;
|
|
assert_eq!(saved.status(), StatusCode::SEE_OTHER);
|
|
assert!(set_cookie(&saved).unwrap().contains("Max-Age=60"));
|
|
let cookie = flash_cookie(&saved);
|
|
|
|
let page = send(get("/done", Some(&cookie))).await;
|
|
assert_eq!(
|
|
set_cookie(&page),
|
|
Some("pagetop_flash=; Path=/; HttpOnly; SameSite=Lax; Max-Age=0")
|
|
);
|
|
let body = web::test::read_body_text(page).await;
|
|
// Shown once, as plain text, before the content.
|
|
let info = body
|
|
.find("Saved <b>today</b>")
|
|
.expect("info message");
|
|
assert!(body.contains("message message-error"));
|
|
assert!(body.contains("One; problem"));
|
|
assert!(info < body.find("Done").unwrap());
|
|
assert_eq!(body.matches("message-info").count(), 1);
|
|
|
|
// Without the cookie there is nothing to show or to clear.
|
|
let page = send(get("/done", None)).await;
|
|
assert_eq!(set_cookie(&page), None);
|
|
assert!(
|
|
!web::test::read_body_text(page)
|
|
.await
|
|
.contains("message-info")
|
|
);
|
|
}
|
|
|
|
#[pagetop::test]
|
|
async fn messages_wait_for_a_page() {
|
|
let cookie = flash_cookie(&send(web::test::TestRequest::post().uri("/save")).await);
|
|
// Another redirect, or a response that is not a page, keeps the cookie untouched.
|
|
assert_eq!(set_cookie(&send(get("/again", Some(&cookie))).await), None);
|
|
assert_eq!(set_cookie(&send(get("/file", Some(&cookie))).await), None);
|
|
// An error page shows them.
|
|
let missing = send(get("/missing", Some(&cookie))).await;
|
|
assert_eq!(missing.status(), StatusCode::NOT_FOUND);
|
|
assert!(set_cookie(&missing).unwrap().contains("Max-Age=0"));
|
|
assert!(
|
|
web::test::read_body_text(missing)
|
|
.await
|
|
.contains("One; problem")
|
|
);
|
|
}
|
|
|
|
#[pagetop::test]
|
|
async fn forged_cookies_only_show_escaped_text() {
|
|
let cookie = "pagetop_flash=w%3Cscript%3Ealert(1)%3C%2Fscript%3E|xignored";
|
|
let body = web::test::read_body_text(send(get("/done", Some(cookie))).await).await;
|
|
assert!(body.contains("<script>alert(1)</script>"));
|
|
assert!(!body.contains("<script>alert"));
|
|
assert!(!body.contains("ignored"));
|
|
}
|